Skip to content

Commit

Permalink
@nocommit test
Browse files Browse the repository at this point in the history
Differential Revision: D58222146
  • Loading branch information
dannysu authored and facebook-github-bot committed Jun 6, 2024
1 parent 88e1ec4 commit c691d90
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
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
63 changes: 63 additions & 0 deletions lib/VM/JSLib/TypedArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,61 @@ 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]].
double len = self->getLength();

// 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.
double k = 0;
MutableHandle<> kHandle{runtime};
MutableHandle<> fromValueHandle{runtime};

auto marker = gcScope.createMarker();
// 6. Repeat, while k < len,
while (k < len) {
gcScope.flushToMarker(marker);

// 6a. Let from be ! ToString(𝔽(length - k - 1)).
double from = len - k - 1;

// 6c. Let fromValue be ? Get(O, from).
fromValueHandle =
JSObject::getOwnIndexed(createPseudoHandle(self.get()), runtime, from);

// 6d. Perform ! Set(A, Pk, fromValue, true).
if (LLVM_UNLIKELY(
A->setOwnIndexed(A, runtime, k, fromValueHandle) ==
ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}

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

return A.getHermesValue();
}

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

Expand Down Expand Up @@ -2044,6 +2099,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

0 comments on commit c691d90

Please sign in to comment.