Skip to content

Commit 8baafc4

Browse files
authored
Don't throw OOB exception for detached typed array (#208)
`a[42] = 1` where a is a detached typed array should not throw but `Object.defineProperty()` still should. Add a check and a flag that distinguishes between the two cases.
1 parent b478329 commit 8baafc4

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

quickjs.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -8429,6 +8429,9 @@ int JS_SetPropertyInternal2(JSContext *ctx, JSValue this_obj,
84298429
JS_FreeValue(ctx, val);
84308430
if (JS_IsException(val))
84318431
return -1;
8432+
if (typed_array_is_detached(ctx, p1))
8433+
if (!(flags & JS_PROP_DEFINE_PROPERTY))
8434+
return FALSE; // per spec: no OOB exception
84328435
return JS_ThrowTypeErrorOrFalse(ctx, flags, "out-of-bound numeric index");
84338436
}
84348437
}
@@ -8687,6 +8690,9 @@ static int JS_SetPropertyValue(JSContext *ctx, JSValue this_obj,
86878690
return -1;
86888691
if (unlikely(idx >= (uint32_t)p->u.array.count)) {
86898692
ta_out_of_bound:
8693+
if (typed_array_is_detached(ctx, p))
8694+
if (!(flags & JS_PROP_DEFINE_PROPERTY))
8695+
return FALSE; // per spec: no OOB exception
86908696
return JS_ThrowTypeErrorOrFalse(ctx, flags, "out-of-bound numeric index");
86918697
}
86928698
p->u.array.u.double_ptr[idx] = d;
@@ -34488,7 +34494,8 @@ static __exception int JS_DefinePropertyDesc(JSContext *ctx, JSValue obj,
3448834494
return -1;
3448934495

3449034496
ret = JS_DefineProperty(ctx, obj, prop,
34491-
d.value, d.getter, d.setter, d.flags | flags);
34497+
d.value, d.getter, d.setter,
34498+
d.flags | flags | JS_PROP_DEFINE_PROPERTY);
3449234499
js_free_desc(ctx, &d);
3449334500
return ret;
3449434501
}

quickjs.h

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
246246

247247
#define JS_PROP_NO_ADD (1 << 16) /* internal use */
248248
#define JS_PROP_NO_EXOTIC (1 << 17) /* internal use */
249+
#define JS_PROP_DEFINE_PROPERTY (1 << 18) /* internal use */
249250

250251
#define JS_DEFAULT_STACK_SIZE (256 * 1024)
251252

test262_errors.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ test262/test/built-ins/TypedArrayConstructors/internals/DefineOwnProperty/detach
1313
test262/test/built-ins/TypedArrayConstructors/internals/DefineOwnProperty/detached-buffer.js:47: strict mode: Test262Error: (Testing with Float64Array.)
1414
test262/test/built-ins/TypedArrayConstructors/internals/DefineOwnProperty/tonumber-value-detached-buffer.js:42: Test262Error: Reflect.defineProperty(ta, 0, {value: {valueOf() {$DETACHBUFFER(ta.buffer); return 42;}}} ) must return true Expected SameValue(«false», «true») to be true (Testing with Float64Array.)
1515
test262/test/built-ins/TypedArrayConstructors/internals/DefineOwnProperty/tonumber-value-detached-buffer.js:42: strict mode: Test262Error: Reflect.defineProperty(ta, 0, {value: {valueOf() {$DETACHBUFFER(ta.buffer); return 42;}}} ) must return true Expected SameValue(«false», «true») to be true (Testing with Float64Array.)
16-
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/detached-buffer-realm.js:37: strict mode: TypeError: out-of-bound numeric index (Testing with BigInt64Array.)
1716
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/detached-buffer.js:34: TypeError: cannot convert bigint to number (Testing with BigInt64Array.)
18-
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/detached-buffer.js:32: strict mode: TypeError: out-of-bound numeric index (Testing with BigInt64Array.)
17+
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/detached-buffer.js:34: strict mode: TypeError: cannot convert bigint to number (Testing with BigInt64Array.)
1918
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-minus-zero.js:20: Test262Error: Reflect.set("new TA([42n])", "-0", 1n) must return true Expected SameValue(«false», «true») to be true (Testing with BigInt64Array.)
2019
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-minus-zero.js:20: strict mode: Test262Error: Reflect.set("new TA([42n])", "-0", 1n) must return true Expected SameValue(«false», «true») to be true (Testing with BigInt64Array.)
2120
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-not-integer.js:21: Test262Error: Reflect.set("new TA([42n])", "1.1", 1n) must return true Expected SameValue(«false», «true») to be true (Testing with BigInt64Array.)
@@ -24,8 +23,6 @@ test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-out-of
2423
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-out-of-bounds.js:27: strict mode: Test262Error: Reflect.set("new TA([42n])", "-1", 1n) must return false Expected SameValue(«false», «true») to be true (Testing with BigInt64Array.)
2524
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/tonumber-value-detached-buffer.js:24: Test262Error: Expected SameValue(«false», «true») to be true (Testing with BigInt64Array.)
2625
test262/test/built-ins/TypedArrayConstructors/internals/Set/BigInt/tonumber-value-detached-buffer.js:24: strict mode: Test262Error: Expected SameValue(«false», «true») to be true (Testing with BigInt64Array.)
27-
test262/test/built-ins/TypedArrayConstructors/internals/Set/detached-buffer-realm.js:37: strict mode: TypeError: out-of-bound numeric index (Testing with Float64Array.)
28-
test262/test/built-ins/TypedArrayConstructors/internals/Set/detached-buffer.js:32: strict mode: TypeError: out-of-bound numeric index (Testing with Float64Array.)
2926
test262/test/built-ins/TypedArrayConstructors/internals/Set/key-is-minus-zero.js:22: Test262Error: Reflect.set(sample, "-0", 1) must return true Expected SameValue(«false», «true») to be true (Testing with Float64Array.)
3027
test262/test/built-ins/TypedArrayConstructors/internals/Set/key-is-minus-zero.js:22: strict mode: Test262Error: Reflect.set(sample, "-0", 1) must return true Expected SameValue(«false», «true») to be true (Testing with Float64Array.)
3128
test262/test/built-ins/TypedArrayConstructors/internals/Set/key-is-not-integer.js:22: Test262Error: Reflect.set(sample, "1.1", 1) must return true Expected SameValue(«false», «true») to be true (Testing with Float64Array.)

0 commit comments

Comments
 (0)