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 TypedArray.prototype.toReversed #1366

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

robik
Copy link
Contributor

@robik robik commented Mar 29, 2024

Summary

This PR implements ES2023 TypedArray.prototype.toReversed method. This is a follow-up of PR #1286 which added with method on standard Array prototype.

See also:

I am creating separate PRs for each method in case there are any suggestions or change requests from your side, so that PRs are more independent.

Test Plan

Code is annotated with algorithm steps from EcmaScript specification for easier verification and maintenance.
I also added tests to verify that methods work as intended. There might be some more edge cases that might be covered based on your experience.

$ echo "new Int8Array([0, 1, 2, 3]).toReversed()" | ./bin/hermes
# >> Int8Array [ 3, 2, 1, 0 ]

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Mar 29, 2024
@facebook-github-bot
Copy link
Contributor

@dannysu has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@dannysu dannysu left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! A minor comment on something caught by our internal linter.

lib/VM/JSLib/TypedArray.cpp Outdated Show resolved Hide resolved
@robik robik force-pushed the typed-array-es14-to-reversed branch from 4e9bb1b to 76da167 Compare May 23, 2024 10:56
@facebook-github-bot
Copy link
Contributor

@robik has updated the pull request. You must reimport the pull request before landing.


// 5. Let k be 0.
double k = 0;
MutableHandle<> kHandle{runtime};
Copy link
Contributor

Choose a reason for hiding this comment

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

The unused variable is still here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh damn, sorry, Must have re-added it on rebase I think

@facebook-github-bot
Copy link
Contributor

@dannysu has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

lib/VM/JSLib/TypedArray.cpp Outdated Show resolved Hide resolved
@robik robik force-pushed the typed-array-es14-to-reversed branch from 76da167 to a766b77 Compare June 18, 2024 11:59
@facebook-github-bot
Copy link
Contributor

@robik has updated the pull request. You must reimport the pull request before landing.

JSArrayBuffer::size_type from = len - k - 1;

// 6d. Perform ! Set(A, Pk, fromValue, true).
JSArrayBuffer::copyDataBlockBytes(runtime, aBuffer, k * byteWidth, srcBuffer, from * byteWidth, byteWidth);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you get the underlying data pointer at above lines and use memcpy here directly? And you can further change k and from to be index in the underlying data array, so you don't need to do multiplications here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

WDYT about creating a static utility function for copying elements in JSTypedArray (where byte width is known) rather than copying bytes?

Copy link
Contributor

Choose a reason for hiding this comment

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

Then you have to specialize it for every valid width. Probably not necessary at the moment. It can be done later if we do need to dig more performance. For now, memcpy on the underlying data_ array is good enough.

@lavenzg
Copy link
Contributor

lavenzg commented Jun 18, 2024

For the other PRs, we should eliminate setOwnIndexed or setToCopyOfTypedArray as well, since we know exactly the the underlying data array and the source/target array buffer have the same type.

@robik
Copy link
Contributor Author

robik commented Jun 19, 2024

Sure thing! I just wanted to make sure that the current approach is correct first. If it is, I can apply it to other methods :)

@facebook-github-bot
Copy link
Contributor

@robik has updated the pull request. You must reimport the pull request before landing.

@robik robik force-pushed the typed-array-es14-to-reversed branch from cca70ae to 165fb0b Compare June 28, 2024 14:56
@facebook-github-bot
Copy link
Contributor

@robik has updated the pull request. You must reimport the pull request before landing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants