Skip to content

Commit 5168db1

Browse files
authored
Handle TypedArray detach during iteration (#209)
Per spec: detaching the TA mid-iteration is allowed. TypedArray.prototype.sort should not throw an exception when that happens and now no longer does.
1 parent 8baafc4 commit 5168db1

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

quickjs.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49591,8 +49591,13 @@ static int js_TA_cmp_generic(const void *a, const void *b, void *opaque) {
4959149591
uint32_t a_idx, b_idx;
4959249592
JSValue argv[2];
4959349593
JSValue res;
49594+
JSObject *p;
4959449595
int cmp;
4959549596

49597+
p = JS_VALUE_GET_OBJ(psc->arr);
49598+
if (typed_array_is_detached(ctx, p))
49599+
return 0;
49600+
4959649601
cmp = 0;
4959749602
if (!psc->exception) {
4959849603
a_idx = *(uint32_t *)a;
@@ -49622,9 +49627,6 @@ static int js_TA_cmp_generic(const void *a, const void *b, void *opaque) {
4962249627
/* make sort stable: compare array offsets */
4962349628
cmp = (a_idx > b_idx) - (a_idx < b_idx);
4962449629
}
49625-
if (validate_typed_array(ctx, psc->arr) < 0) {
49626-
psc->exception = 1;
49627-
}
4962849630
done:
4962949631
JS_FreeValue(ctx, (JSValue)argv[0]);
4963049632
JS_FreeValue(ctx, (JSValue)argv[1]);
@@ -49719,6 +49721,9 @@ static JSValue js_typed_array_sort(JSContext *ctx, JSValue this_val,
4971949721
js_TA_cmp_generic, &tsc);
4972049722
if (tsc.exception)
4972149723
goto fail;
49724+
// per spec: typed array can be detached mid-iteration
49725+
if (typed_array_is_detached(ctx, p))
49726+
goto done;
4972249727
array_tmp = js_malloc(ctx, len * elt_size);
4972349728
if (!array_tmp) {
4972449729
fail:
@@ -49755,6 +49760,7 @@ static JSValue js_typed_array_sort(JSContext *ctx, JSValue this_val,
4975549760
abort();
4975649761
}
4975749762
js_free(ctx, array_tmp);
49763+
done:
4975849764
js_free(ctx, array_idx);
4975949765
} else {
4976049766
rqsort(array_ptr, len, elt_size, cmpfun, &tsc);

test262_errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ test262/test/built-ins/AsyncGeneratorPrototype/return/return-suspendedYield-brok
33
test262/test/built-ins/AsyncGeneratorPrototype/return/return-suspendedYield-broken-promise-try-catch.js:39: strict mode: TypeError: $DONE() not called
44
test262/test/built-ins/RegExp/lookahead-quantifier-match-groups.js:27: Test262Error: Expected [a, abc] and [a, undefined] to have the same contents. ? quantifier
55
test262/test/built-ins/RegExp/lookahead-quantifier-match-groups.js:27: strict mode: Test262Error: Expected [a, abc] and [a, undefined] to have the same contents. ? quantifier
6-
test262/test/built-ins/TypedArray/prototype/sort/sort-tonumber.js:30: TypeError: ArrayBuffer is detached (Testing with Float64Array.)
7-
test262/test/built-ins/TypedArray/prototype/sort/sort-tonumber.js:30: strict mode: TypeError: ArrayBuffer is detached (Testing with Float64Array.)
86
test262/test/built-ins/TypedArrayConstructors/internals/DefineOwnProperty/BigInt/detached-buffer.js:46: Test262Error: (Testing with BigInt64Array.)
97
test262/test/built-ins/TypedArrayConstructors/internals/DefineOwnProperty/BigInt/detached-buffer.js:46: strict mode: Test262Error: (Testing with BigInt64Array.)
108
test262/test/built-ins/TypedArrayConstructors/internals/DefineOwnProperty/BigInt/tonumber-value-detached-buffer.js:40: Test262Error: Reflect.defineProperty(ta, 0, {value: {valueOf() {$DETACHBUFFER(ta.buffer); return 42n;}}}) must return true Expected SameValue(«false», «true») to be true (Testing with BigInt64Array.)

0 commit comments

Comments
 (0)