-
-
Notifications
You must be signed in to change notification settings - Fork 406
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.at() #1613
Conversation
Thank you for your contribution! |
Hi! I updated the branch with fixes to some errors I was able to get cargo check gave me. This fix compiled and successfully ran a test.js file on my computer for the implementation. |
Test262 conformance changes:
Fixed tests (22):
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some recommendations to improve the code. Good job!
boa/src/builtins/array/mod.rs
Outdated
//2. let len be ? LengthOfArrayLike(O) | ||
let len = obj.length_of_array_like(context)? as i64; | ||
//3. let relativeIndex be ? ToIntegerOrInfinity(index) | ||
let relative_index = args.get(0).unwrap().to_integer_or_infinity(context)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let relative_index = args.get(0).unwrap().to_integer_or_infinity(context)?; | |
let relative_index = args.get_or_undefined(0).to_integer_or_infinity(context)?; |
This won't panic if at
is called with no arguments, like so: [1, 2, 3, 4].at()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed this literally like 15-20 minutes ago! Completely unaware that get_or_undefined existed! Definitely making this update.
boa/src/builtins/array/mod.rs
Outdated
//handle most likely impossible case of | ||
//IntegerOrInfinity::NegativeInfinity || IntegerOrInfinity::PositiveInfinity | ||
//by setting k to len which will return undefined below | ||
_ => len, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of relying on the condition below, maybe it would be better if we early returned here:
_ => len, | |
_ => return Ok(JsValue::undefined()), |
Makes it easier to identify that only finite numbers are valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely makes more sense to identify only finite numbers being valid
boa/src/builtins/array/mod.rs
Outdated
/// When at method takes desired integer as index and returns the value at given | ||
/// index. Negative integer counts backwards. If index is invalid, the at method | ||
/// returns undefined. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you clarify this comment? You could instead take the explanation of the function on MDN and paste it here, that should be enough to explain what this does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took the MDN description. I thought that one was the best too, but I was a bit hesitant to just grab it.
Added all the feedback above. Let me know if there's any more changes! The match guard one was kind of cool not gonna lie. Thanks for all the help and feedback! 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Just a small change that you can ignore if you consider it less readable :)
boa/src/builtins/array/mod.rs
Outdated
IntegerOrInfinity::Integer(i) if i >= 0 && i < len => i, | ||
//5. Else, let k be len + relativeIndex | ||
//integer should be negative, so abs() and check if less than or equal to length of array | ||
IntegerOrInfinity::Integer(i) if i.abs() <= len && i != len => len + i, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IntegerOrInfinity::Integer(i) if i.abs() <= len && i != len => len + i, | |
IntegerOrInfinity::Integer(i) if i < 0 && i.abs() <= len => len + i, |
I think this describes the check a lot better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally agree! pushed the change to the second arm of the match. Again, thanks for all the feedback!
We usually wait for two approved reviews until a PR is merged into master, so you'll just have to wait a bit of time and your contribution should be merged soon 😄 |
Hi!
This Pull Request closes #1596.
I implemented the Array.prototype.at() using the link to the ECMAScript feature provided on the issue. I also added to the method call for at() to the chain.
ECMAScript had Array.prototype.at() just in front of the Array.prototype.concat() method, so I placed the function for at prior to that method to follow the doc order.
Just as a note: this is my first pull request. So if there is anything wrong or something I missed, feel free to let me know!