Skip to content
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
9 changes: 4 additions & 5 deletions lib/VM/JSLib/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2691,19 +2691,18 @@ CallResult<HermesValue> arrayPrototypeSplice(void *, Runtime &runtime) {
gcScope.flushToMarker(gcMarker);
}

// Use i here to refer to (k-1) in the spec, and reindex the loop.
double i = len - 1;
// (len - actualDeleteCount + itemCount) >= 0 so k never underflow
uint64_t k = len;

// Delete the remaining elements from the right that we didn't copy into.
while (i > (len - actualDeleteCount + itemCount - 1)) {
lv.i = HermesValue::encodeTrustedNumberValue(i);
while (k-- > len - actualDeleteCount + itemCount) {
lv.i = HermesValue::encodeTrustedNumberValue(k);
if (LLVM_UNLIKELY(
JSObject::deleteComputed(
lv.O, runtime, lv.i, PropOpFlags().plusThrowOnError()) ==
ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
i -= 1;
gcScope.flushToMarker(gcMarker);
}
} else if (itemCount > actualDeleteCount) {
Expand Down
24 changes: 24 additions & 0 deletions test/hermes/regress-array-splice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: %hermes -O %s | %FileCheck --match-full-lines %s

print("splice");
// CHECK: splice

let removed = [];
let arr = new Proxy([], {
deleteProperty(target, p) {
removed.push(p);
return Reflect.deleteProperty(target, p);
},
});

arr.push('a', 'b', 'c');
arr.splice(0);
print(removed);
// CHECK-NEXT: 2,1,0