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.with #1365

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.with 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]).with(0, 5)" | ./bin/hermes
# >> Int8Array [ 5 ]

@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
@robik
Copy link
Contributor Author

robik commented Mar 29, 2024

Also, as an future improvement area, is it possible to determine if an array is an rvalue (as in a temporary value, which reference to was not captured yet)?

I was thinking of implementing a basic copy-ellision. If there's a way right now, I might create a follow-up PR that implements it, e.g: checks if the target array is an rvalue, and if so, changes it in-place for some non mutating methods so that we do not allocate new array on every call.

This would be beneficial for cases like below:

const finalArray = [0, 0, 2]
  .with(1, 1)
  .toReversed();

@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.

@tmikov
Copy link
Contributor

tmikov commented Mar 29, 2024

@robik, high level optimizations like that are definitely something we are planning for in Static Hermes, but unfortunately they are almost impossible to do ahead of time in a regular untyped JS.

In regular JS, when we encounter x.with(1, 1).toReversed(), we don't know whether x is an Array, what with() does, whether it has been overridden by a polyfill, etc.

Things change dramatically in Static Hermes:

let x : any[];
...
x.with(1,1).toReversed()

Copy link
Contributor

@avp avp left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, it looks mostly good. I've left a couple comments regarding pointer safety that will have to be fixed before we can land it.

lib/VM/JSLib/TypedArray.cpp Outdated Show resolved Hide resolved
lib/VM/JSLib/TypedArray.cpp Outdated Show resolved Hide resolved
@robik robik force-pushed the typed-array-es14-with branch from fa46507 to 6911c97 Compare May 23, 2024 10:54
@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-with branch from 6911c97 to ec06f42 Compare June 28, 2024 09:18
@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-with branch from ec06f42 to 2586bbb 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.

@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.

4 participants