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.at() #1613

Merged
merged 18 commits into from
Oct 3, 2021

Conversation

nekevss
Copy link
Member

@nekevss nekevss commented Oct 2, 2021

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!

@jedel1043 jedel1043 changed the title Implementation of Array.prototype.at() #1596 Implement Array.prototype.at() Oct 2, 2021
@jedel1043
Copy link
Member

jedel1043 commented Oct 2, 2021

Thank you for your contribution!
The code currently doesn't compile, so I'd advise you to run cargo check and cargo clippy to fix your compilation errors :)
And in general, before committing anything you should always run those two commands to ensure it at least compiles, then you can check if the tests pass or fail in the CI we have implemented for PRs

@jedel1043 jedel1043 added this to the v0.14.0 milestone Oct 2, 2021
@jedel1043 jedel1043 added builtins PRs and Issues related to builtins/intrinsics enhancement New feature or request labels Oct 2, 2021
@nekevss
Copy link
Member Author

nekevss commented Oct 2, 2021

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.

@jedel1043 jedel1043 added the Hacktoberfest Hacktoberfest 2021 - https://hacktoberfest.digitalocean.com label Oct 2, 2021
@jedel1043
Copy link
Member

Test262 conformance changes:

Test result master count PR count difference
Total 80,930 80,930 0
Passed 33,313 33,335 +22
Ignored 15,898 15,898 0
Failed 31,719 31,697 -22
Panics 0 0 0
Conformance 41.16% 41.19% +0.03%
Fixed tests (22):
test/built-ins/Array/prototype/at/returns-item.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/returns-item.js (previously Failed)
test/built-ins/Array/prototype/at/returns-undefined-for-holes-in-sparse-arrays.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/returns-undefined-for-holes-in-sparse-arrays.js (previously Failed)
test/built-ins/Array/prototype/at/name.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/name.js (previously Failed)
test/built-ins/Array/prototype/at/returns-undefined-for-out-of-range-index.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/returns-undefined-for-out-of-range-index.js (previously Failed)
test/built-ins/Array/prototype/at/index-argument-tointeger.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/index-argument-tointeger.js (previously Failed)
test/built-ins/Array/prototype/at/returns-item-relative-index.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/returns-item-relative-index.js (previously Failed)
test/built-ins/Array/prototype/at/return-abrupt-from-this.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/return-abrupt-from-this.js (previously Failed)
test/built-ins/Array/prototype/at/index-non-numeric-argument-tointeger-invalid.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/index-non-numeric-argument-tointeger-invalid.js (previously Failed)
test/built-ins/Array/prototype/at/index-non-numeric-argument-tointeger.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/index-non-numeric-argument-tointeger.js (previously Failed)
test/built-ins/Array/prototype/at/length.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/length.js (previously Failed)
test/built-ins/Array/prototype/at/prop-desc.js [strict mode] (previously Failed)
test/built-ins/Array/prototype/at/prop-desc.js (previously Failed)

Copy link
Member

@jedel1043 jedel1043 left a 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!

//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)?;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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()

Copy link
Member Author

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.

//handle most likely impossible case of
//IntegerOrInfinity::NegativeInfinity || IntegerOrInfinity::PositiveInfinity
//by setting k to len which will return undefined below
_ => len,
Copy link
Member

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:

Suggested change
_ => len,
_ => return Ok(JsValue::undefined()),

Makes it easier to identify that only finite numbers are valid.

Copy link
Member Author

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

Comment on lines 493 to 495
/// 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.
Copy link
Member

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.

Copy link
Member Author

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.

@nekevss
Copy link
Member Author

nekevss commented Oct 2, 2021

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! 😄

Copy link
Member

@jedel1043 jedel1043 left a 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 :)

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,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Member Author

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!

@jedel1043
Copy link
Member

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 😄

@HalidOdat HalidOdat merged commit 94992a4 into boa-dev:master Oct 3, 2021
@nekevss nekevss deleted the impl-array-prototype-at branch October 5, 2021 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builtins PRs and Issues related to builtins/intrinsics enhancement New feature or request Hacktoberfest Hacktoberfest 2021 - https://hacktoberfest.digitalocean.com
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Array.prototype.at()
3 participants