Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Array.prototype.forEach #192

Closed
wants to merge 1 commit into from
Closed

Conversation

xSke
Copy link

@xSke xSke commented Oct 26, 2019

No description provided.

var sum = 0;
var indexSum = 0;
var listLengthSum = 0;
function callingCallback(item, index, list) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could test using this inside the callback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, come to think of it, I am not sure this has been implemented. However, you could add a TODO for when it is.

let this_arg = args
.get(1)
.cloned()
.unwrap_or_else(|| Gc::new(ValueData::Undefined));
Copy link
Contributor

@bojan88 bojan88 Oct 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can replace this line with .unwrap_or(&Gc::new(ValueData::Undefined));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clippy is going to complain that you are not using unwrap_or_else().

let this_arg = args
.get(1)
.cloned()
.unwrap_or_else(|| Gc::new(ValueData::Undefined));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the undefined() function, defined in src/lib/builtins/value.rs

let element = this.get_field(&i.to_string());
let arguments = vec![element.clone(), to_value(i), this.clone()];

interpreter.call(callback_arg, &this_arg, arguments)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback should not be called for undefined values. Also, can you please add tests for cases where array is modified in the callback? For ex. new elements are added to array, elements inserted in the middle, elements deleted.

MDN has a few examples on how this function should work in such cases here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not sure this is quite right, it does run for undefined values, just not values that have been deleted

@IovoslavIovchev
Copy link
Contributor

@xSke do you need any help on this?

@jasonwilliams
Copy link
Member

Replace by jasonwilliams#268
Thanks @xSke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants