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

Add support for ES2023 Array non-mutating methods #1286

Closed
wants to merge 3 commits into from

Conversation

robik
Copy link
Contributor

@robik robik commented Jan 28, 2024

Summary

Overview

This PR implements partial support for new Array.prototype non-mutating methods introduced in ES2023. These are:

Array.prototype.toSorted() is implemented in separate PR #1298 as per request.

Other Array methods introduced in ES14 seem to already be implemented (findLast, findLastIndex).

Implementation for TypedArray methods are not included in this PR and will be provided in another one.

Motivation

Aforementioned methods see support in all major browsers for quite some time 1234. Adding support brings hermes closer to full ES2023 coverage and will improve interoperability with browsers. It also should provide slight performance gains across all applications upon adoption.

obraz

Tasks

  • Implementation
  • Add tests
  • Format changes
  • Add documentation somewhere?

Implementation/review notes

This is my first contribution to this project. While I have some experience in C++ and have spent some time studying hermes codebase, there might be some slight misusages of your APIs (most notably handles and memory management) and breaking code-style. If so, please let me know and I will try fix them promptly 🙂

Most of the code was inspired/taken from already existing mutating functions. I also tried to apply optimisations that I've noticed in some of the methods (most notably fast path for array element accessing).

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.

Quick test:

$ echo "[1,2,3,4,5].toReversed()" | ./bin/hermes
# >> [ 5, 4, 3, 2, 1, [length]: 5 ]

$ echo "[ 1, 2, 3, 4 ].toSpliced(2, 1, '3a', '3b', '3c')" | ./bin/hermes
# >> [ 1, 2, "3a", "3b", "3c", 4, [length]: 6 ]

$ echo "[1,0,3].with(1, 2)" | ./bin/hermes
# >> [ 1, 2, 3, [length]: 3 ]

Fixes #1078

Footnotes

  1. https://caniuse.com/?search=toReversed

  2. https://caniuse.com/?search=toSorted

  3. https://caniuse.com/?search=toSpliced

  4. https://caniuse.com/?search=array.with

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

ljharb commented Jan 28, 2024

(fwiw after ES6, only the year should be used; so instead of ES14, it should be ES2023)

@robik robik changed the title Add support for ES14 Array non-mutating methods Add support for ES2023 Array non-mutating methods Jan 28, 2024
@robik robik force-pushed the add-es14-array-methods branch from cc14ad0 to 32af68f Compare January 29, 2024 21:20
@facebook-github-bot
Copy link
Contributor

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

@robik robik force-pushed the add-es14-array-methods branch from 32af68f to 641e4cb Compare February 1, 2024 10: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

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

@tmikov
Copy link
Contributor

tmikov commented Feb 5, 2024

@robik thank you very much for this PR. We are reviewing it - it looks generally good, we might have some minor comments.

@tmikov
Copy link
Contributor

tmikov commented Feb 6, 2024

@robik there are some issues in toSorted(), caused partially by Hermes being non-spec compliant. Could you split this into two PRs - one containing everything but toSorted(), which we can probably land faster, and then toSorted(), which might require more time to sort out (pun intended).

@robik robik force-pushed the add-es14-array-methods branch from 641e4cb to d2d3bc4 Compare February 6, 2024 11:08
@facebook-github-bot
Copy link
Contributor

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

1 similar comment
@facebook-github-bot
Copy link
Contributor

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

@robik
Copy link
Contributor Author

robik commented Feb 6, 2024

@robik thank you very much for this PR. We are reviewing it - it looks generally good, we might have some minor comments.

Thanks! I'm ready for the review 🫡

@robik there are some issues in toSorted(), caused partially by Hermes being non-spec compliant. Could you split this into two PRs - one containing everything but toSorted(), which we can probably land faster, and then toSorted(), which might require more time to sort out (pun intended).

Sure! I've moved toSorted() implementation to new PR #1298

@robik robik force-pushed the add-es14-array-methods branch from 1acb0c6 to fbc68c3 Compare February 6, 2024 11:29
@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

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

@tmikov
Copy link
Contributor

tmikov commented Feb 8, 2024

@robik thanks a lot! This looks good, we are landing it.

@facebook-github-bot
Copy link
Contributor

@tmikov merged this pull request in b30f0e4.

facebook-github-bot pushed a commit that referenced this pull request Feb 23, 2024
Summary:
Original Author: [email protected]
Original Git: b30f0e4
Original Reviewed By: avp
Original Revision: D53206784

### Overview

This PR implements partial support for new `Array.prototype` non-mutating methods introduced in ES2023. These are:

 - [`Array.prototype.toReversed()`](https://262.ecma-international.org/14.0/#sec-array.prototype.toreversed)
 - [`Array.prototype.toSpliced(start, skipCount, ...items)`](https://262.ecma-international.org/14.0/#sec-array.prototype.tospliced)
 - [`Array.prototype.with(index, value)`](https://262.ecma-international.org/14.0/#sec-array.prototype.with)

`Array.prototype.toSorted()` is implemented in separate PR #1298 as per request.

Other Array methods introduced in ES14 seem to already be implemented (`findLast`, `findLastIndex`).

Implementation for `TypedArray` methods are not included in this PR and will be provided in another one.

### Motivation

Aforementioned methods see support in all major browsers for quite some time [^toReversed][^toSorted][^toSpliced][^with]. Adding support brings hermes closer to full ES2023 coverage and will improve interoperability with browsers. It also should provide slight performance gains across all applications upon adoption.

<img width="1438" alt="obraz" src="https://github.com/facebook/hermes/assets/681837/e6f405b7-0645-4d27-8ca7-54f2c0aaf5d6">

[^toReversed]: https://caniuse.com/?search=toReversed
[^toSorted]: https://caniuse.com/?search=toSorted
[^toSpliced]: https://caniuse.com/?search=toSpliced
[^with]: https://caniuse.com/?search=array.with

- [x] Implementation
- [x] Add tests
- [x] Format changes
- [ ] Add documentation somewhere?

### Implementation/review notes

This is my first contribution to this project. While I have some experience in C++ and have spent some time studying hermes codebase, there might be some slight misusages of your APIs (most notably handles and memory management) and breaking code-style. If so, please let me know and I will try fix them promptly 🙂

Most of the code was inspired/taken from already existing mutating functions. I also tried to apply optimisations that I've noticed in some of the methods (most notably fast path for array element accessing).

Pull Request resolved: #1286

Pulled By: tmikov

Reviewed By: avp

Differential Revision: D54092069

fbshipit-source-id: cc4b58cf9d832538ff22df954e6655075a709aa3
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. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support Array.prototype.with
4 participants