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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/hermes/VM/NativeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ NATIVE_FUNCTION(typedArrayPrototypeSort)
NATIVE_FUNCTION(typedArrayPrototypeSubarray)
NATIVE_FUNCTION(typedArrayPrototypeSymbolToStringTag)
NATIVE_FUNCTION(typedArrayPrototypeToLocaleString)
NATIVE_FUNCTION(typedArrayPrototypeToReversed)
NATIVE_FUNCTION(unescape)
NATIVE_FUNCTION(weakMapConstructor)
NATIVE_FUNCTION(weakMapPrototypeDelete)
Expand Down
55 changes: 55 additions & 0 deletions lib/VM/JSLib/TypedArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,53 @@ typedArrayPrototypeToLocaleString(void *, Runtime &runtime, NativeArgs args) {
return HermesValue::encodeStringValue(*builder->getStringPrimitive());
}

/// ES14.0 23.2.3.32
CallResult<HermesValue>
typedArrayPrototypeToReversed(void *, Runtime &runtime, NativeArgs args) {
GCScope gcScope{runtime};

// 2. Perform ? ValidateTypedArray(O).
if (JSTypedArrayBase::validateTypedArray(runtime, args.getThisHandle()) ==
ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}

// 1. Let O be this value
auto self = args.vmcastThis<JSTypedArrayBase>();

// 3. Let len be O.[[ArrayLength]].
auto len = self->getLength();
auto byteLength = self->getByteLength();
auto byteWidth = self->getByteWidth();

// 4. Let A be ? TypedArrayCreateSameType(O, « 𝔽(len) »).
auto aRes = JSTypedArrayBase::allocateSpecies(runtime, self, len);
if (LLVM_UNLIKELY(aRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
auto A = aRes.getValue();

// 5. Let k be 0.
JSArrayBuffer::size_type k = 0;

auto dstBlock = A->getBuffer(runtime)->getDataBlock(runtime);
auto srcBlock = self->getBuffer(runtime)->getDataBlock(runtime);

// 6. Repeat, while k < len,
while (k < byteLength) {
// 6a. Let from be ! ToString(𝔽(length - k - 1)).
size_t from = byteLength - k - byteWidth;

// 6d. Perform ! Set(A, Pk, fromValue, true).
memcpy(dstBlock + k, srcBlock + from, byteWidth);

// 6e. Set k to k + 1.
k += byteWidth;
}

return A.getHermesValue();
}

Handle<JSObject> createTypedArrayBaseConstructor(Runtime &runtime) {
auto proto = Handle<JSObject>::vmcast(&runtime.typedArrayBasePrototype);

Expand Down Expand Up @@ -2044,6 +2091,14 @@ Handle<JSObject> createTypedArrayBaseConstructor(Runtime &runtime) {
typedArrayPrototypeToLocaleString,
0);

defineMethod(
runtime,
proto,
Predefined::getSymbolID(Predefined::toReversed),
nullptr,
typedArrayPrototypeToReversed,
0);

// TypedArrayBase.xxx
defineMethod(
runtime,
Expand Down
10 changes: 10 additions & 0 deletions test/hermes/TypedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,16 @@ cons.forEach(function(TypedArray) {
});
/// @}

/// @name TypedArray.prototype.toReversed
/// @{
cons.forEach(function(TypedArray) {
var arr = new TypedArray([ 0, 1, 2, 3 ]);

assert.arrayEqual(arr.toReversed(), [ 3, 2, 1, 0 ]);
assert.arrayEqual(new TypedArray([]).toReversed(), []);
});
/// @}

/// @name Exception cases
/// @{

Expand Down
Loading