From a60a67ea880bb4b5de849ee8f7676a475f60c85f Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Tue, 20 Oct 2020 17:38:52 -0400 Subject: [PATCH] Align detached buffer semantics with web reality, R3 --- .../every/callbackfn-detachbuffer.js | 17 +++---- .../filter/callbackfn-detachbuffer.js | 14 +++-- .../find/predicate-may-detach-buffer.js | 51 +++++++++---------- .../findIndex/predicate-may-detach-buffer.js | 44 +++++++--------- .../forEach/callbackfn-detachbuffer.js | 11 ++-- .../prototype/map/callbackfn-detachbuffer.js | 15 +++--- .../reduce/callbackfn-detachbuffer.js | 16 +++--- .../reduceRight/callbackfn-detachbuffer.js | 16 +++--- .../prototype/some/callbackfn-detachbuffer.js | 14 +++-- 9 files changed, 87 insertions(+), 111 deletions(-) diff --git a/test/built-ins/TypedArray/prototype/every/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/every/callbackfn-detachbuffer.js index 8032d06bf3..4357585aaf 100644 --- a/test/built-ins/TypedArray/prototype/every/callbackfn-detachbuffer.js +++ b/test/built-ins/TypedArray/prototype/every/callbackfn-detachbuffer.js @@ -15,7 +15,7 @@ info: | 22.1.3.5 Array.prototype.every ( callbackfn [ , thisArg ] ) ... - 6. Repeat, while k < len + 5. Repeat, while k < len ... c. If kPresent is true, then i. Let kValue be ? Get(O, Pk). @@ -29,16 +29,13 @@ testWithTypedArrayConstructors(function(TA) { var loops = 0; var sample = new TA(2); - assert.throws(TypeError, function() { - sample.every(function() { - if (loops === 1) { - throw new Test262Error("callbackfn called twice"); - } + sample.every(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - loops++; - return true; - }); + } + loops++; + return true; }); - assert.sameValue(loops, 1); + assert.sameValue(loops, 2); }); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js index 1e2251472f..92eff63ff3 100644 --- a/test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js @@ -21,15 +21,13 @@ testWithTypedArrayConstructors(function(TA) { var loops = 0; var sample = new TA(2); - assert.throws(TypeError, function() { - sample.filter(function() { - if (loops === 1) { - throw new Test262Error("callbackfn called twice"); - } + sample.filter(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - loops++; - }); + } + loops++; + return true; }); - assert.sameValue(loops, 1); + assert.sameValue(loops, 2); }); diff --git a/test/built-ins/TypedArray/prototype/find/predicate-may-detach-buffer.js b/test/built-ins/TypedArray/prototype/find/predicate-may-detach-buffer.js index fbac50d14c..e997087a16 100644 --- a/test/built-ins/TypedArray/prototype/find/predicate-may-detach-buffer.js +++ b/test/built-ins/TypedArray/prototype/find/predicate-may-detach-buffer.js @@ -5,7 +5,7 @@ esid: sec-%typedarray%.prototype.find description: > Predicate may detach the buffer info: | - 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) + %TypedArray%.prototype.find (predicate [ , thisArg ] ) %TypedArray%.prototype.find is a distinct function that implements the same algorithm as Array.prototype.find as defined in 22.1.3.8 @@ -17,42 +17,41 @@ info: | possibility that calls to predicate may cause the this value to become detached. - ... - 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + Array.prototype.find ( predicate[ , thisArg ] ) + + Let O be ? ToObject(this value). + Let len be ? LengthOfArrayLike(O). + If IsCallable(predicate) is false, throw a TypeError exception. + Let k be 0. + Repeat, while k < len, + Let Pk be ! ToString(𝔽(k)). + Let kValue be ? Get(O, Pk). + Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + If testResult is true, return kValue. + Set k to k + 1. + Return undefined. + + IntegerIndexedElementGet ( O, index ) - ... - 4. If thisArg was supplied, let T be thisArg; else let T be undefined. - 5. Let k be 0. - 6. Repeat, while k < len ... - b. Let kValue be ? Get(O, Pk). - c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). - ... + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. - 9.4.5.8 IntegerIndexedElementGet ( O, index ) - - ... - 3. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. - 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. - ... includes: [testTypedArray.js, detachArrayBuffer.js] features: [TypedArray] ---*/ testWithTypedArrayConstructors(function(TA) { - var sample = new TA(2); var loops = 0; - var completion = false; + var sample = new TA(2); - assert.throws(TypeError, function() { - sample.find(function() { - loops++; + sample.find(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - completion = true; - }); - }, "throws a TypeError getting a value from the detached buffer"); + } + loops++; + }); - assert.sameValue(loops, 1, "predicate is called once"); - assert(completion, "abrupt completion does not come from DETACHBUFFER"); + assert.sameValue(loops, 2); }); diff --git a/test/built-ins/TypedArray/prototype/findIndex/predicate-may-detach-buffer.js b/test/built-ins/TypedArray/prototype/findIndex/predicate-may-detach-buffer.js index 6124bc3fbb..3e81207009 100644 --- a/test/built-ins/TypedArray/prototype/findIndex/predicate-may-detach-buffer.js +++ b/test/built-ins/TypedArray/prototype/findIndex/predicate-may-detach-buffer.js @@ -7,28 +7,26 @@ description: > info: | 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) - %TypedArray%.prototype.findIndex is a distinct function that implements the - same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that - the this object's [[ArrayLength]] internal slot is accessed in place of - performing a [[Get]] of "length". + %TypedArray%.prototype.findIndex is a distinct function that implements the + same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that + the this object's [[ArrayLength]] internal slot is accessed in place of + performing a [[Get]] of "length". - ... + ... 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) - ... - 6. Repeat, while k < len + Repeat, while k < len, + Let Pk be ! ToString(𝔽(k)). + Let kValue be ? Get(O, Pk). + Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). ... - b. Let kValue be ? Get(O, Pk). - c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). - ... - 9.4.5.8 IntegerIndexedElementGet ( O, index ) + IntegerIndexedElementGet ( O, index ) + + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. - ... - 3. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. - 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. - ... includes: [testTypedArray.js, detachArrayBuffer.js] features: [TypedArray] ---*/ @@ -36,16 +34,12 @@ features: [TypedArray] testWithTypedArrayConstructors(function(TA) { var sample = new TA(2); var loops = 0; - var completion = false; - assert.throws(TypeError, function() { - sample.findIndex(function() { - loops++; + sample.findIndex(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - completion = true; - }); - }, "throws a TypeError getting a value from the detached buffer"); - - assert.sameValue(loops, 1, "predicated is called once"); - assert(completion, "abrupt completion does not come from DETACHBUFFER"); + } + loops++; + }); + assert.sameValue(loops, 2, "predicate is called once"); }); diff --git a/test/built-ins/TypedArray/prototype/forEach/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/forEach/callbackfn-detachbuffer.js index 6540255f74..8c2fac6b6b 100644 --- a/test/built-ins/TypedArray/prototype/forEach/callbackfn-detachbuffer.js +++ b/test/built-ins/TypedArray/prototype/forEach/callbackfn-detachbuffer.js @@ -29,14 +29,11 @@ testWithTypedArrayConstructors(function(TA) { var loops = 0; var sample = new TA(2); - assert.throws(TypeError, function() { - sample.forEach(function() { - if (loops === 1) { - throw new Test262Error("callbackfn called twice"); - } + sample.forEach(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - loops++; - }); + } + loops++; }); assert.sameValue(loops, 1); diff --git a/test/built-ins/TypedArray/prototype/map/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/map/callbackfn-detachbuffer.js index a183289a05..cc36b154e6 100644 --- a/test/built-ins/TypedArray/prototype/map/callbackfn-detachbuffer.js +++ b/test/built-ins/TypedArray/prototype/map/callbackfn-detachbuffer.js @@ -21,16 +21,13 @@ testWithTypedArrayConstructors(function(TA) { var loops = 0; var sample = new TA(2); - assert.throws(TypeError, function() { - sample.map(function() { - if (loops === 1) { - throw new Test262Error("callbackfn called twice"); - } + sample.map(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - loops++; - return 0; - }); + } + loops++; + return true; }); - assert.sameValue(loops, 1, "callbackfn called only once"); + assert.sameValue(loops, 2); }); diff --git a/test/built-ins/TypedArray/prototype/reduce/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/reduce/callbackfn-detachbuffer.js index 773b4b4c2d..1d3c8f783e 100644 --- a/test/built-ins/TypedArray/prototype/reduce/callbackfn-detachbuffer.js +++ b/test/built-ins/TypedArray/prototype/reduce/callbackfn-detachbuffer.js @@ -30,15 +30,13 @@ testWithTypedArrayConstructors(function(TA) { var loops = 0; var sample = new TA(2); - assert.throws(TypeError, function() { - sample.reduce(function() { - if (loops === 1) { - throw new Test262Error("callbackfn called twice"); - } + sample.reduce(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - loops++; - }, 0); - }); + } + loops++; + return true; + }, 0); - assert.sameValue(loops, 1, "callbackfn called only once"); + assert.sameValue(loops, 2); }); diff --git a/test/built-ins/TypedArray/prototype/reduceRight/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/reduceRight/callbackfn-detachbuffer.js index 7731c1e962..498c5a5a3d 100644 --- a/test/built-ins/TypedArray/prototype/reduceRight/callbackfn-detachbuffer.js +++ b/test/built-ins/TypedArray/prototype/reduceRight/callbackfn-detachbuffer.js @@ -30,15 +30,13 @@ testWithTypedArrayConstructors(function(TA) { var loops = 0; var sample = new TA(2); - assert.throws(TypeError, function() { - sample.reduceRight(function() { - if (loops === 1) { - throw new Test262Error("callbackfn called twice"); - } + sample.reduceRight(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - loops++; - }, 0); - }); + } + loops++; + return true; + }, 0); - assert.sameValue(loops, 1, "callbackfn called only once"); + assert.sameValue(loops, 2); }); diff --git a/test/built-ins/TypedArray/prototype/some/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/some/callbackfn-detachbuffer.js index 495e2bc91a..1bc4bd56ba 100644 --- a/test/built-ins/TypedArray/prototype/some/callbackfn-detachbuffer.js +++ b/test/built-ins/TypedArray/prototype/some/callbackfn-detachbuffer.js @@ -29,15 +29,13 @@ testWithTypedArrayConstructors(function(TA) { var loops = 0; var sample = new TA(2); - assert.throws(TypeError, function() { - sample.some(function() { - if (loops === 1) { - throw new Test262Error("callbackfn called twice"); - } + sample.some(function() { + if (loops === 0) { $DETACHBUFFER(sample.buffer); - loops++; - }); + } + loops++; + return false; }); - assert.sameValue(loops, 1); + assert.sameValue(loops, 2); });