From 07caa4a2df2de1baa72ed56e9329d3f2ea941a6d Mon Sep 17 00:00:00 2001 From: Mike Pennisi Date: Tue, 24 Aug 2021 21:01:27 -0400 Subject: [PATCH] Support normative change to Resizable ArrayBuffer A recent normative change to the Resizable ArrayBuffer modified the criteria for a TypedArray becoming "out of bounds." Following the change, TypedArrays which track the length of their underlying ArrayBuffer instance are no longer considered "out of bounds" when the ArrayBuffer is resized such that its size matches the TypedArray's offset exactly. https://github.com/tc39/proposal-resizablearraybuffer/pull/70 The majority of this patch's changes extend coverage to include cases for both "on boundary" and "out of bounds" without reflecting any new semantics. Two changes describe observable differences in the new version of the algorithm: - out-of-bounds-when-species-retrieved-different-type.js - out-of-bounds-when-species-retrieved-same-type.js --- .../byteLength/resizable-array-buffer-auto.js | 7 +++++++ .../byteOffset/resizable-array-buffer-auto.js | 13 +++++++++---- .../prototype/length/resizable-array-buffer-auto.js | 7 +++++++ ...-bounds-when-species-retrieved-different-type.js | 11 ++++++++++- ...ut-of-bounds-when-species-retrieved-same-type.js | 11 ++++++++++- .../HasProperty/resizable-array-buffer-auto.js | 7 +++++++ .../integer-indexes-resizable-array-buffer-auto.js | 9 +++++++++ 7 files changed, 59 insertions(+), 6 deletions(-) diff --git a/test/built-ins/TypedArray/prototype/byteLength/resizable-array-buffer-auto.js b/test/built-ins/TypedArray/prototype/byteLength/resizable-array-buffer-auto.js index d50b9cbe95..21ea9650d7 100644 --- a/test/built-ins/TypedArray/prototype/byteLength/resizable-array-buffer-auto.js +++ b/test/built-ins/TypedArray/prototype/byteLength/resizable-array-buffer-auto.js @@ -42,5 +42,12 @@ testWithTypedArrayConstructors(function(TA) { expected = 0; } catch (_) {} + assert.sameValue(array.byteLength, expected, "following shrink (on boundary)"); + + try { + ab.resize(0); + expected = 0; + } catch (_) {} + assert.sameValue(array.byteLength, expected, "following shrink (out of bounds)"); }); diff --git a/test/built-ins/TypedArray/prototype/byteOffset/resizable-array-buffer-auto.js b/test/built-ins/TypedArray/prototype/byteOffset/resizable-array-buffer-auto.js index 261328fb95..364e17ad4c 100644 --- a/test/built-ins/TypedArray/prototype/byteOffset/resizable-array-buffer-auto.js +++ b/test/built-ins/TypedArray/prototype/byteOffset/resizable-array-buffer-auto.js @@ -34,13 +34,18 @@ testWithTypedArrayConstructors(function(TA) { assert.sameValue(array.byteOffset, BPE, "following shrink (within bounds)"); - var expected; + var expected = BPE; try { ab.resize(BPE); expected = 0; - } catch (_) { - expected = BPE; - } + } catch (_) {} + + assert.sameValue(array.byteOffset, expected, "following shrink (on boundary)"); + + try { + ab.resize(0); + expected = 0; + } catch (_) {} assert.sameValue(array.byteOffset, expected, "following shrink (out of bounds)"); }); diff --git a/test/built-ins/TypedArray/prototype/length/resizable-array-buffer-auto.js b/test/built-ins/TypedArray/prototype/length/resizable-array-buffer-auto.js index 77e1263a5c..a894a5898f 100644 --- a/test/built-ins/TypedArray/prototype/length/resizable-array-buffer-auto.js +++ b/test/built-ins/TypedArray/prototype/length/resizable-array-buffer-auto.js @@ -42,5 +42,12 @@ testWithTypedArrayConstructors(function(TA) { expected = 0; } catch (_) {} + assert.sameValue(array.length, expected, "following shrink (on boundary)"); + + try { + ab.resize(0); + expected = 0; + } catch (_) {} + assert.sameValue(array.length, expected, "following shrink (out of bounds)"); }); diff --git a/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-different-type.js b/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-different-type.js index d6bcdac56a..0aba12fc80 100644 --- a/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-different-type.js +++ b/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-different-type.js @@ -57,13 +57,22 @@ testWithTypedArrayConstructors(function(TA) { assert(compareArray(new TargetCtor(source), expected), 'following shrink (within bounds)'); + onGetSpecies = function() { + try { + ab.resize(BPE); + expected = []; + } catch (_) {} + }; + + assert(compareArray(new TargetCtor(source), expected), 'following shrink (on boundary)'); + // `assert.throws` cannot be used in this case because the expected error // is derived only after the constructor is invoked. var expectedError; var actualError; onGetSpecies = function() { try { - ab.resize(BPE); + ab.resize(0); expectedError = TypeError; } catch (_) { expectedError = Test262Error; diff --git a/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-same-type.js b/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-same-type.js index 4e2b9f3ceb..c633ba0696 100644 --- a/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-same-type.js +++ b/test/built-ins/TypedArrayConstructors/ctors/typedarray-arg/out-of-bounds-when-species-retrieved-same-type.js @@ -57,13 +57,22 @@ testWithTypedArrayConstructors(function(TA) { assert(compareArray(new TA(source), expected), 'following shrink (within bounds)'); + onGetSpecies = function() { + try { + ab.resize(BPE); + expected = []; + } catch (_) {} + }; + + assert(compareArray(new TA(source), expected), 'following shrink (on boundary)'); + // `assert.throws` cannot be used in this case because the expected error // is derived only after the constructor is invoked. var expectedError; var actualError; onGetSpecies = function() { try { - ab.resize(BPE); + ab.resize(0); expectedError = TypeError; } catch (_) { expectedError = Test262Error; diff --git a/test/built-ins/TypedArrayConstructors/internals/HasProperty/resizable-array-buffer-auto.js b/test/built-ins/TypedArrayConstructors/internals/HasProperty/resizable-array-buffer-auto.js index 4d0200d273..0fbc6d5910 100644 --- a/test/built-ins/TypedArrayConstructors/internals/HasProperty/resizable-array-buffer-auto.js +++ b/test/built-ins/TypedArrayConstructors/internals/HasProperty/resizable-array-buffer-auto.js @@ -50,5 +50,12 @@ testWithTypedArrayConstructors(function(TA) { expected = "false,false,false,false,false"; } catch (_) {} + assert.sameValue(inspect(array), expected, "following shrink (on boundary)"); + + try { + ab.resize(0); + expected = "false,false,false,false,false"; + } catch (_) {} + assert.sameValue(inspect(array), expected, "following shrink (out of bounds)"); }); diff --git a/test/built-ins/TypedArrayConstructors/internals/OwnPropertyKeys/integer-indexes-resizable-array-buffer-auto.js b/test/built-ins/TypedArrayConstructors/internals/OwnPropertyKeys/integer-indexes-resizable-array-buffer-auto.js index fb4ff1222c..abb0836ea1 100644 --- a/test/built-ins/TypedArrayConstructors/internals/OwnPropertyKeys/integer-indexes-resizable-array-buffer-auto.js +++ b/test/built-ins/TypedArrayConstructors/internals/OwnPropertyKeys/integer-indexes-resizable-array-buffer-auto.js @@ -52,6 +52,15 @@ testWithTypedArrayConstructors(function(TA) { expected = ""; } catch (_) {} + assert.sameValue( + Reflect.ownKeys(array).join(","), expected, "following shrink (on boundary)" + ); + + try { + ab.resize(0); + expected = ""; + } catch (_) {} + assert.sameValue( Reflect.ownKeys(array).join(","), expected, "following shrink (out of bounds)" );