Skip to content

Commit

Permalink
Add support for ES2023 Array non-mutating methods (#1286) (#1286)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
neildhar authored and facebook-github-bot committed Feb 23, 2024
1 parent e1a177c commit e607157
Show file tree
Hide file tree
Showing 4 changed files with 531 additions and 16 deletions.
3 changes: 3 additions & 0 deletions include/hermes/VM/NativeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ NATIVE_FUNCTION(arrayPrototypeSlice)
NATIVE_FUNCTION(arrayPrototypeSome)
NATIVE_FUNCTION(arrayPrototypeUnshift)
NATIVE_FUNCTION(arrayPrototypeSplice)
NATIVE_FUNCTION(arrayPrototypeToReversed)
NATIVE_FUNCTION(arrayPrototypeToSpliced)
NATIVE_FUNCTION(arrayPrototypeWith)
NATIVE_FUNCTION(asyncFunctionConstructor)
NATIVE_FUNCTION(atob)

Expand Down
3 changes: 3 additions & 0 deletions include/hermes/VM/PredefinedStrings.def
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ STR(includes, "includes")
STR(subarray, "subarray")
STR(flat, "flat")
STR(flatMap, "flatMap")
STR(toReversed, "toReversed")
STR(toSpliced, "toSpliced")
STR(with, "with")

STR(ArrayBuffer, "ArrayBuffer")
STR(byteLength, "byteLength")
Expand Down
Loading

0 comments on commit e607157

Please sign in to comment.