diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/test262/built-ins/TypedArray/prototype/find | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
156 files changed, 7142 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/detached-buffer.js new file mode 100644 index 0000000000..51b7d60af4 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/detached-buffer.js @@ -0,0 +1,34 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Throws a TypeError if this has a detached buffer +info: | + 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.find(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..05b01d3167 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/get-length-ignores-length-prop.js @@ -0,0 +1,56 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + [[Get]] of "length" uses [[ArrayLength]] +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithBigIntTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42n]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.find(function() { return true; }), + 42n + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-changes-value.js new file mode 100644 index 0000000000..64667d84a6 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-changes-value.js @@ -0,0 +1,80 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Change values during predicate call +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [compareArray.js, testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var arr = [1n, 2n, 3n]; + var sample; + var result; + + sample = new TA(3); + sample.find(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0n, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.find(function(val, i) { + if ( i === 0 ) { + sample[2] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, 7n, "value found"); + + sample = new TA(arr); + result = sample.find(function(val, i) { + if ( i === 0 ) { + sample[2] = 7n; + } + return val === 3n; + }); + assert.sameValue(result, undefined, "value not found"); + + sample = new TA(arr); + result = sample.find(function(val, i) { + if ( i > 0 ) { + sample[0] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, undefined, "value not found - changed after call"); + + sample = new TA(arr); + result = sample.find(function() { + sample[0] = 7n; + return true; + }); + assert.sameValue(result, 1n, "find() returns previous found value"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-parameters.js new file mode 100644 index 0000000000..ab3bb965ba --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-parameters.js @@ -0,0 +1,64 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 2n, 62n]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.find(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 39n, "results[0][0] === 39, value"); + assert.sameValue(result[1], 0, "results[0][1] === 0, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3 arguments"); + + result = results[1]; + assert.sameValue(result[0], 2n, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3 arguments"); + + result = results[2]; + assert.sameValue(result[0], 62n, "results[2][0] === 62, value"); + assert.sameValue(result[1], 2, "results[2][1] === 2, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3 arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..cbfc687e78 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-this-non-strict.js @@ -0,0 +1,61 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Verify predicate this on non-strict mode +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [noStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +var T = this; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.find(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.find(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.find(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..9c5c07eba5 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-call-this-strict-strict.js @@ -0,0 +1,56 @@ +'use strict'; +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Verify predicate this on strict mode +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [onlyStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.find(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.find(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..149b44b285 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-is-not-callable-throws.js @@ -0,0 +1,68 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Throws a TypeError exception if predicate is not callable. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 3. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + + assert.throws(TypeError, function() { + sample.find({}); + }, "object"); + + assert.throws(TypeError, function() { + sample.find(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.find(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.find(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.find(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.find(1); + }, "number"); + + assert.throws(TypeError, function() { + sample.find(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.find([]); + }, "array"); + + assert.throws(TypeError, function() { + sample.find(/./); + }, "regexp"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..0cb8181ca1 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-may-detach-buffer.js @@ -0,0 +1,59 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Predicate may detach the buffer +info: | + %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 + + ... + + However, such optimization must not introduce any observable changes in the + specified behaviour of the algorithm and must take into account the + possibility that calls to predicate may cause the this value to become + detached. + + + 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 ) + + ... + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. + +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var loops = 0; + var sample = new TA(2); + + sample.find(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + + assert.sameValue(loops, 2); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..7c863b77c2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/predicate-not-called-on-empty-array.js @@ -0,0 +1,51 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Predicate is not called on empty instances +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var result = sample.find(function() { + called = true; + return true; + }); + + assert.sameValue( + called, + false, + "empty instance does not call predicate" + ); + assert.sameValue( + result, + undefined, + "find returns undefined when predicate is not called" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..406a6618f9 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-abrupt-from-predicate-call.js @@ -0,0 +1,42 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Return abrupt from predicate call. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + var predicate = function() { + throw new Test262Error(); + }; + + assert.throws(Test262Error, function() { + sample.find(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..bd9d8b9de7 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testBigIntTypedArray.js] +features: [ArrayBuffer, BigInt, TypedArray, arrow-function, resizable-arraybuffer] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.find, + 'function', + 'implements TypedArray.prototype.find' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithBigIntTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.find(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.find(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the find operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.find(() => {}); + throw new Test262Error('find completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-found-value-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-found-value-predicate-result-is-true.js new file mode 100644 index 0000000000..a5c977152a --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-found-value-predicate-result-is-true.js @@ -0,0 +1,67 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Return found value if predicate return a boolean true value. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + d. If testResult is true, return kValue. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 2n, 62n]); + var called, result; + + called = 0; + result = sample.find(function() { + called++; + return true; + }); + assert.sameValue(result, 39n, "returned true on sample[0]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.find(function(val) { + called++; + return val === 62n; + }); + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 62n, "returned true on sample[3]"); + + result = sample.find(function() { return "string"; }); + assert.sameValue(result, 39n, "ToBoolean(string)"); + + result = sample.find(function() { return {}; }); + assert.sameValue(result, 39n, "ToBoolean(object)"); + + result = sample.find(function() { return Symbol(""); }); + assert.sameValue(result, 39n, "ToBoolean(symbol)"); + + result = sample.find(function() { return 1; }); + assert.sameValue(result, 39n, "ToBoolean(number)"); + + result = sample.find(function() { return -1; }); + assert.sameValue(result, 39n, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-undefined-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-undefined-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..43714b5e4a --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/return-undefined-if-predicate-returns-false-value.js @@ -0,0 +1,62 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Return undefined if predicate always returns a boolean false value. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... + 7. Return undefined. +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(3); + var called = 0; + + var result = sample.find(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, undefined); + + result = sample.find(function() { return ""; }); + assert.sameValue(result, undefined, "ToBoolean(empty string)"); + + result = sample.find(function() { return undefined; }); + assert.sameValue(result, undefined, "ToBoolean(undefined)"); + + result = sample.find(function() { return null; }); + assert.sameValue(result, undefined, "ToBoolean(null)"); + + result = sample.find(function() { return 0; }); + assert.sameValue(result, undefined, "ToBoolean(0)"); + + result = sample.find(function() { return -0; }); + assert.sameValue(result, undefined, "ToBoolean(-0)"); + + result = sample.find(function() { return NaN; }); + assert.sameValue(result, undefined, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/shell.js new file mode 100644 index 0000000000..90ee9c114d --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/BigInt/shell.js @@ -0,0 +1,42 @@ +// GENERATED, DO NOT EDIT +// file: testBigIntTypedArray.js +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: | + Collection of functions used to assert the correctness of BigInt TypedArray objects. +defines: + - TypedArray + - testWithBigIntTypedArrayConstructors +---*/ + +/** + * The %TypedArray% intrinsic constructor function. + */ +var TypedArray = Object.getPrototypeOf(Int8Array); + +/** + * Calls the provided function for every typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithBigIntTypedArrayConstructors(f, selected) { + /** + * Array containing every BigInt typed array constructor. + */ + var constructors = selected || [ + BigInt64Array, + BigUint64Array + ]; + + for (var i = 0; i < constructors.length; ++i) { + var constructor = constructors[i]; + try { + f(constructor); + } catch (e) { + e.message += " (Testing with " + constructor.name + ".)"; + throw e; + } + } +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/callbackfn-resize.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/callbackfn-resize.js new file mode 100644 index 0000000000..3165cc78d0 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/callbackfn-resize.js @@ -0,0 +1,76 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Instance buffer can be resized during iteration +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, resizable-arraybuffer] +---*/ + +// If the host chooses to throw as allowed by the specification, the observed +// behavior will be identical to the case where `ArrayBuffer.prototype.resize` +// has not been implemented. The following assertion prevents this test from +// passing in runtimes which have not implemented the method. +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +testWithTypedArrayConstructors(function(TA) { + var BPE = TA.BYTES_PER_ELEMENT; + var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4}); + var sample = new TA(buffer); + var finalElement, expectedElements, expectedIndices, expectedArrays; + var elements, indices, arrays, result; + + elements = []; + indices = []; + arrays = []; + result = sample.find(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(2 * BPE); + finalElement = undefined; + expectedElements = [0, 0]; + expectedIndices = [0, 1]; + expectedArrays = [sample, sample]; + } catch (_) { + finalElement = 0; + expectedElements = [0, 0, 0]; + expectedIndices = [0, 1, 2]; + expectedArrays = [sample, sample, sample]; + } + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)'); + assert.compareArray(indices, [0, 1, 2], 'indices (shrink)'); + assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)'); + assert.sameValue(result, undefined, 'result (shrink)'); + + elements = []; + indices = []; + arrays = []; + result = sample.find(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(4 * BPE); + } catch (_) {} + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, expectedElements, 'elements (grow)'); + assert.compareArray(indices, expectedIndices, 'indices (grow)'); + assert.compareArray(arrays, expectedArrays, 'arrays (grow)'); + assert.sameValue(result, undefined, 'result (grow)'); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/detached-buffer.js new file mode 100644 index 0000000000..5de4a56041 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/detached-buffer.js @@ -0,0 +1,34 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Throws a TypeError if this has a detached buffer +info: | + 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.find(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..ff41a27af9 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/get-length-ignores-length-prop.js @@ -0,0 +1,56 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + [[Get]] of "length" uses [[ArrayLength]] +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.find(function() { return true; }), + 42 + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/invoked-as-func.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/invoked-as-func.js new file mode 100644 index 0000000000..e63cc49e07 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/invoked-as-func.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Throws a TypeError exception when invoked as a function +info: | + 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var find = TypedArray.prototype.find; + +assert.sameValue(typeof find, 'function'); + +assert.throws(TypeError, function() { + find(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/invoked-as-method.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/invoked-as-method.js new file mode 100644 index 0000000000..a4d8b598ba --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/invoked-as-method.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Requires a [[TypedArrayName]] internal slot. +info: | + 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +assert.sameValue(typeof TypedArrayPrototype.find, 'function'); + +assert.throws(TypeError, function() { + TypedArrayPrototype.find(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/length.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/length.js new file mode 100644 index 0000000000..1583fe26d2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/length.js @@ -0,0 +1,32 @@ +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.find +description: > + %TypedArray%.prototype.find.length is 1. +info: | + %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray] +---*/ + +assert.sameValue(TypedArray.prototype.find.length, 1); + +verifyNotEnumerable(TypedArray.prototype.find, "length"); +verifyNotWritable(TypedArray.prototype.find, "length"); +verifyConfigurable(TypedArray.prototype.find, "length"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/name.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/name.js new file mode 100644 index 0000000000..1d1f09f6b8 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/name.js @@ -0,0 +1,29 @@ +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.find +description: > + %TypedArray%.prototype.find.name is "find". +info: | + %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray] +---*/ + +assert.sameValue(TypedArray.prototype.find.name, "find"); + +verifyNotEnumerable(TypedArray.prototype.find, "name"); +verifyNotWritable(TypedArray.prototype.find, "name"); +verifyConfigurable(TypedArray.prototype.find, "name"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/not-a-constructor.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/not-a-constructor.js new file mode 100644 index 0000000000..c7453c01f2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/not-a-constructor.js @@ -0,0 +1,35 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + TypedArray.prototype.find does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js, testTypedArray.js] +features: [Reflect.construct, arrow-function, TypedArray] +---*/ + +assert.sameValue( + isConstructor(TypedArray.prototype.find), + false, + 'isConstructor(TypedArray.prototype.find) must return false' +); + +assert.throws(TypeError, () => { + let u8 = new Uint8Array(1); new u8.find(() => {}); +}, '`let u8 = new Uint8Array(1); new u8.find(() => {})` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-changes-value.js new file mode 100644 index 0000000000..7dcaccf39b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-changes-value.js @@ -0,0 +1,80 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Change values during predicate call +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [compareArray.js, testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var arr = [1, 2, 3]; + var sample; + var result; + + sample = new TA(3); + sample.find(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.find(function(val, i) { + if ( i === 0 ) { + sample[2] = 7; + } + return val === 7; + }); + assert.sameValue(result, 7, "value found"); + + sample = new TA(arr); + result = sample.find(function(val, i) { + if ( i === 0 ) { + sample[2] = 7; + } + return val === 3; + }); + assert.sameValue(result, undefined, "value not found"); + + sample = new TA(arr); + result = sample.find(function(val, i) { + if ( i > 0 ) { + sample[0] = 7; + } + return val === 7; + }); + assert.sameValue(result, undefined, "value not found - changed after call"); + + sample = new TA(arr); + result = sample.find(function() { + sample[0] = 7; + return true; + }); + assert.sameValue(result, 1, "find() returns previous found value"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-parameters.js new file mode 100644 index 0000000000..3df875dc37 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-parameters.js @@ -0,0 +1,64 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 2, 62]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.find(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 39, "results[0][0] === 39, value"); + assert.sameValue(result[1], 0, "results[0][1] === 0, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3 arguments"); + + result = results[1]; + assert.sameValue(result[0], 2, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3 arguments"); + + result = results[2]; + assert.sameValue(result[0], 62, "results[2][0] === 62, value"); + assert.sameValue(result[1], 2, "results[2][1] === 2, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3 arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..b96f0ee394 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-this-non-strict.js @@ -0,0 +1,61 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Verify predicate this on non-strict mode +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [noStrict] +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var T = this; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.find(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.find(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.find(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..207b73208c --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-call-this-strict-strict.js @@ -0,0 +1,56 @@ +'use strict'; +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Verify predicate this on strict mode +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [onlyStrict] +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.find(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.find(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..40f7c959dd --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-is-not-callable-throws.js @@ -0,0 +1,68 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Throws a TypeError exception if predicate is not callable. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 3. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + + assert.throws(TypeError, function() { + sample.find({}); + }, "object"); + + assert.throws(TypeError, function() { + sample.find(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.find(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.find(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.find(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.find(1); + }, "number"); + + assert.throws(TypeError, function() { + sample.find(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.find([]); + }, "array"); + + assert.throws(TypeError, function() { + sample.find(/./); + }, "regexp"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..ca16ed882d --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-may-detach-buffer.js @@ -0,0 +1,59 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Predicate may detach the buffer +info: | + %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 + + ... + + However, such optimization must not introduce any observable changes in the + specified behaviour of the algorithm and must take into account the + possibility that calls to predicate may cause the this value to become + detached. + + + 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(F(k)). + Let kValue be ? Get(O, Pk). + Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, F(k), O »)). + If testResult is true, return kValue. + Set k to k + 1. + Return undefined. + + IntegerIndexedElementGet ( O, index ) + + ... + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. + +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var loops = 0; + var sample = new TA(2); + + sample.find(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + + assert.sameValue(loops, 2); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..7ea189e4b1 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/predicate-not-called-on-empty-array.js @@ -0,0 +1,51 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Predicate is not called on empty instances +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var result = sample.find(function() { + called = true; + return true; + }); + + assert.sameValue( + called, + false, + "empty instance does not call predicate" + ); + assert.sameValue( + result, + undefined, + "find returns undefined when predicate is not called" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/prop-desc.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/prop-desc.js new file mode 100644 index 0000000000..ed0168a1cd --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/prop-desc.js @@ -0,0 +1,21 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + "find" property of TypedArrayPrototype +info: | + ES6 section 17: Every other data property described in clauses 18 through 26 + and in Annex B.2 has the attributes { [[Writable]]: true, + [[Enumerable]]: false, [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +verifyNotEnumerable(TypedArrayPrototype, 'find'); +verifyWritable(TypedArrayPrototype, 'find'); +verifyConfigurable(TypedArrayPrototype, 'find'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..2215239c21 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-abrupt-from-predicate-call.js @@ -0,0 +1,42 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Return abrupt from predicate call. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + var predicate = function() { + throw new Test262Error(); + }; + + assert.throws(Test262Error, function() { + sample.find(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..73c4118313 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testTypedArray.js] +features: [TypedArray, resizable-arraybuffer] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.find, + 'function', + 'implements TypedArray.prototype.find' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.find(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.find(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the find operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.find(() => {}); + throw new Test262Error('find completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-found-value-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-found-value-predicate-result-is-true.js new file mode 100644 index 0000000000..f5cfd69ff7 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-found-value-predicate-result-is-true.js @@ -0,0 +1,67 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Return found value if predicate return a boolean true value. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + d. If testResult is true, return kValue. + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 2, 62]); + var called, result; + + called = 0; + result = sample.find(function() { + called++; + return true; + }); + assert.sameValue(result, 39, "returned true on sample[0]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.find(function(val) { + called++; + return val === 62; + }); + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 62, "returned true on sample[3]"); + + result = sample.find(function() { return "string"; }); + assert.sameValue(result, 39, "ToBoolean(string)"); + + result = sample.find(function() { return {}; }); + assert.sameValue(result, 39, "ToBoolean(object)"); + + result = sample.find(function() { return Symbol(""); }); + assert.sameValue(result, 39, "ToBoolean(symbol)"); + + result = sample.find(function() { return 1; }); + assert.sameValue(result, 39, "ToBoolean(number)"); + + result = sample.find(function() { return -1; }); + assert.sameValue(result, 39, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-undefined-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-undefined-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..bbfa0928e9 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/return-undefined-if-predicate-returns-false-value.js @@ -0,0 +1,62 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Return undefined if predicate always returns a boolean false value. +info: | + 22.2.3.10 %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 except that the this + object's [[ArrayLength]] internal slot is accessed in place of performing a + [[Get]] of "length". The implementation of the algorithm may be optimized with + the knowledge that the this value is an object that has a fixed length and + whose integer indexed properties are not sparse. + + ... + + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... + 7. Return undefined. +includes: [testTypedArray.js] +features: [Symbol, TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(3); + var called = 0; + + var result = sample.find(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, undefined); + + result = sample.find(function() { return ""; }); + assert.sameValue(result, undefined, "ToBoolean(empty string)"); + + result = sample.find(function() { return undefined; }); + assert.sameValue(result, undefined, "ToBoolean(undefined)"); + + result = sample.find(function() { return null; }); + assert.sameValue(result, undefined, "ToBoolean(null)"); + + result = sample.find(function() { return 0; }); + assert.sameValue(result, undefined, "ToBoolean(0)"); + + result = sample.find(function() { return -0; }); + assert.sameValue(result, undefined, "ToBoolean(-0)"); + + result = sample.find(function() { return NaN; }); + assert.sameValue(result, undefined, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/shell.js new file mode 100644 index 0000000000..eda1477282 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/shell.js @@ -0,0 +1,24 @@ +// GENERATED, DO NOT EDIT +// file: isConstructor.js +// Copyright (C) 2017 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: | + Test if a given function is a constructor function. +defines: [isConstructor] +features: [Reflect.construct] +---*/ + +function isConstructor(f) { + if (typeof f !== "function") { + throw new Test262Error("isConstructor invoked with a non-function value"); + } + + try { + Reflect.construct(function(){}, [], f); + } catch (e) { + return false; + } + return true; +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/this-is-not-object.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/this-is-not-object.js new file mode 100644 index 0000000000..9a6479ee49 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/this-is-not-object.js @@ -0,0 +1,53 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: Throws a TypeError exception when `this` is not Object +info: | + 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray] +---*/ + +var find = TypedArray.prototype.find; +var predicate = function() {}; + +assert.throws(TypeError, function() { + find.call(undefined, predicate); +}, "this is undefined"); + +assert.throws(TypeError, function() { + find.call(null, predicate); +}, "this is null"); + +assert.throws(TypeError, function() { + find.call(42, predicate); +}, "this is 42"); + +assert.throws(TypeError, function() { + find.call("1", predicate); +}, "this is a string"); + +assert.throws(TypeError, function() { + find.call(true, predicate); +}, "this is true"); + +assert.throws(TypeError, function() { + find.call(false, predicate); +}, "this is false"); + +var s = Symbol("s"); +assert.throws(TypeError, function() { + find.call(s, predicate); +}, "this is a Symbol"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/find/this-is-not-typedarray-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/find/this-is-not-typedarray-instance.js new file mode 100644 index 0000000000..cdec6e01ad --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/find/this-is-not-typedarray-instance.js @@ -0,0 +1,45 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.find +description: > + Throws a TypeError exception when `this` is not a TypedArray instance +info: | + 22.2.3.10 %TypedArray%.prototype.find (predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var find = TypedArray.prototype.find; +var predicate = function() {}; + +assert.throws(TypeError, function() { + find.call({}, predicate); +}, "this is an Object"); + +assert.throws(TypeError, function() { + find.call([], predicate); +}, "this is an Array"); + +var ab = new ArrayBuffer(8); +assert.throws(TypeError, function() { + find.call(ab, predicate); +}, "this is an ArrayBuffer instance"); + +var dv = new DataView(new ArrayBuffer(8), 0, 1); +assert.throws(TypeError, function() { + find.call(dv, predicate); +}, "this is a DataView instance"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/detached-buffer.js new file mode 100644 index 0000000000..adc2846924 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/detached-buffer.js @@ -0,0 +1,34 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Throws a TypeError if this has a detached buffer +info: | + 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.findIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..a6ac97105e --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/get-length-ignores-length-prop.js @@ -0,0 +1,54 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + [[Get]] of "length" uses [[ArrayLength]] +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithBigIntTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42n]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.findIndex(function() { return true; }), + 0 + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-changes-value.js new file mode 100644 index 0000000000..d7ac7f2cd5 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-changes-value.js @@ -0,0 +1,69 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Change values during predicate call +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [compareArray.js, testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var arr = [10n, 20n, 30n]; + var sample; + var result; + + sample = new TA(3); + sample.findIndex(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0n, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.findIndex(function(val, i) { + if ( i === 0 ) { + sample[2] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, 2, "value found"); + + sample = new TA(arr); + result = sample.findIndex(function(val, i) { + if ( i === 0 ) { + sample[2] = 7n; + } + return val === 30n; + }); + assert.sameValue(result, -1, "value not found"); + + sample = new TA(arr); + result = sample.findIndex(function(val, i) { + if ( i > 0 ) { + sample[0] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, -1, "value not found - changed after call"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-parameters.js new file mode 100644 index 0000000000..5d29ca105b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-parameters.js @@ -0,0 +1,62 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 2n, 62n]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.findIndex(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 39n, "results[0][0] === 39, value"); + assert.sameValue(result[1], 0, "results[0][1] === 0, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3, arguments"); + + result = results[1]; + assert.sameValue(result[0], 2n, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3, arguments"); + + result = results[2]; + assert.sameValue(result[0], 62n, "results[2][0] === 62, value"); + assert.sameValue(result[1], 2, "results[2][1] === 2, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3, arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..4a9a19e8fa --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-this-non-strict.js @@ -0,0 +1,59 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Verify predicate this on non-strict mode +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [noStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +var T = this; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findIndex(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.findIndex(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.findIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..1712b2c26f --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-call-this-strict-strict.js @@ -0,0 +1,54 @@ +'use strict'; +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [onlyStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findIndex(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.findIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..7bdbbc4f19 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-is-not-callable-throws.js @@ -0,0 +1,66 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Throws a TypeError exception if predicate is not callable. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 3. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + assert.throws(TypeError, function() { + sample.findIndex({}); + }, "{}"); + + assert.throws(TypeError, function() { + sample.findIndex(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.findIndex(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.findIndex(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.findIndex(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.findIndex(1); + }, "1"); + + assert.throws(TypeError, function() { + sample.findIndex(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.findIndex([]); + }, "[]"); + + assert.throws(TypeError, function() { + sample.findIndex(/./); + }, "/./"); +}); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..345efae749 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-may-detach-buffer.js @@ -0,0 +1,50 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// Copyright (C) 2021 Apple Inc. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate may detach the buffer +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + 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 ) + + ... + 3. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(2); + var loops = 0; + + sample.findIndex(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + assert.sameValue(loops, 2, "predicate is called once"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..fad5dad300 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/predicate-not-called-on-empty-array.js @@ -0,0 +1,50 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate is not called on an empty instance +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... + 7. Return -1. +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var predicate = function() { + called = true; + return true; + }; + + var result = sample.findIndex(predicate); + + assert.sameValue( + called, false, + "does not call predicate" + ); + assert.sameValue( + result, -1, + "returns -1 on an empty instance" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..aebfdbac18 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-abrupt-from-predicate-call.js @@ -0,0 +1,40 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Return abrupt from predicate call. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + assert.throws(Test262Error, function() { + sample.findIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..bbcc356805 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testBigIntTypedArray.js] +features: [ArrayBuffer, BigInt, TypedArray, arrow-function, resizable-arraybuffer] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.findIndex, + 'function', + 'implements TypedArray.prototype.findIndex' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithBigIntTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.findIndex(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.findIndex(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the findIndex operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.findIndex(() => {}); + throw new Test262Error('findIndex completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-index-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-index-predicate-result-is-true.js new file mode 100644 index 0000000000..605b95ed9c --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-index-predicate-result-is-true.js @@ -0,0 +1,67 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Return index if predicate return a boolean true value. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + d. If testResult is true, return k. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 3n, 9n]); + var called = 0; + + var result = sample.findIndex(function() { + called++; + return true; + }); + + assert.sameValue(result, 0, "returned true on sample[0]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.findIndex(function(val) { + called++; + return val === 9n; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 2, "returned true on sample[3]"); + + result = sample.findIndex(function() { return "string"; }); + assert.sameValue(result, 0, "ToBoolean(string)"); + + result = sample.findIndex(function() { return {}; }); + assert.sameValue(result, 0, "ToBoolean(object)"); + + result = sample.findIndex(function() { return Symbol(""); }); + assert.sameValue(result, 0, "ToBoolean(symbol)"); + + result = sample.findIndex(function() { return 1; }); + assert.sameValue(result, 0, "ToBoolean(number)"); + + result = sample.findIndex(function() { return -1; }); + assert.sameValue(result, 0, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-negative-one-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-negative-one-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..3cb8bcd20a --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/return-negative-one-if-predicate-returns-false-value.js @@ -0,0 +1,60 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Return -1 if predicate always returns a boolean false value. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... + 7. Return -1. +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([1n, 2n, 3n]); + var called = 0; + + var result = sample.findIndex(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, -1, "result is -1 when predicate returns are false"); + + result = sample.findIndex(function() { return ""; }); + assert.sameValue(result, -1, "ToBoolean(string)"); + + result = sample.findIndex(function() { return undefined; }); + assert.sameValue(result, -1, "ToBoolean(undefined)"); + + result = sample.findIndex(function() { return null; }); + assert.sameValue(result, -1, "ToBoolean(null)"); + + result = sample.findIndex(function() { return 0; }); + assert.sameValue(result, -1, "ToBoolean(0)"); + + result = sample.findIndex(function() { return -0; }); + assert.sameValue(result, -1, "ToBoolean(-0)"); + + result = sample.findIndex(function() { return NaN; }); + assert.sameValue(result, -1, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/shell.js new file mode 100644 index 0000000000..90ee9c114d --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/BigInt/shell.js @@ -0,0 +1,42 @@ +// GENERATED, DO NOT EDIT +// file: testBigIntTypedArray.js +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: | + Collection of functions used to assert the correctness of BigInt TypedArray objects. +defines: + - TypedArray + - testWithBigIntTypedArrayConstructors +---*/ + +/** + * The %TypedArray% intrinsic constructor function. + */ +var TypedArray = Object.getPrototypeOf(Int8Array); + +/** + * Calls the provided function for every typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithBigIntTypedArrayConstructors(f, selected) { + /** + * Array containing every BigInt typed array constructor. + */ + var constructors = selected || [ + BigInt64Array, + BigUint64Array + ]; + + for (var i = 0; i < constructors.length; ++i) { + var constructor = constructors[i]; + try { + f(constructor); + } catch (e) { + e.message += " (Testing with " + constructor.name + ".)"; + throw e; + } + } +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/callbackfn-resize.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/callbackfn-resize.js new file mode 100644 index 0000000000..1986131e11 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/callbackfn-resize.js @@ -0,0 +1,76 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Instance buffer can be resized during iteration +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, resizable-arraybuffer] +---*/ + +// If the host chooses to throw as allowed by the specification, the observed +// behavior will be identical to the case where `ArrayBuffer.prototype.resize` +// has not been implemented. The following assertion prevents this test from +// passing in runtimes which have not implemented the method. +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +testWithTypedArrayConstructors(function(TA) { + var BPE = TA.BYTES_PER_ELEMENT; + var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4}); + var sample = new TA(buffer); + var finalElement, expectedElements, expectedIndices, expectedArrays; + var elements, indices, arrays, result; + + elements = []; + indices = []; + arrays = []; + result = sample.findIndex(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(2 * BPE); + finalElement = undefined; + expectedElements = [0, 0]; + expectedIndices = [0, 1]; + expectedArrays = [sample, sample]; + } catch (_) { + finalElement = 0; + expectedElements = [0, 0, 0]; + expectedIndices = [0, 1, 2]; + expectedArrays = [sample, sample, sample]; + } + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)'); + assert.compareArray(indices, [0, 1, 2], 'indices (shrink)'); + assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)'); + assert.sameValue(result, -1, 'result (shrink)'); + + elements = []; + indices = []; + arrays = []; + result = sample.findIndex(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(4 * BPE); + } catch (_) {} + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, expectedElements, 'elements (grow)'); + assert.compareArray(indices, expectedIndices, 'indices (grow)'); + assert.compareArray(arrays, expectedArrays, 'arrays (grow)'); + assert.sameValue(result, -1, 'result (grow)'); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/detached-buffer.js new file mode 100644 index 0000000000..b936c3dc74 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/detached-buffer.js @@ -0,0 +1,34 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Throws a TypeError if this has a detached buffer +info: | + 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.findIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..19f41d6844 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/get-length-ignores-length-prop.js @@ -0,0 +1,54 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + [[Get]] of "length" uses [[ArrayLength]] +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 2. Let len be ? ToLength(? Get(O, "length")). + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.findIndex(function() { return true; }), + 0 + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/invoked-as-func.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/invoked-as-func.js new file mode 100644 index 0000000000..4d1c3dbcaf --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/invoked-as-func.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Throws a TypeError exception when invoked as a function +info: | + 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var findIndex = TypedArray.prototype.findIndex; + +assert.sameValue(typeof findIndex, 'function'); + +assert.throws(TypeError, function() { + findIndex(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/invoked-as-method.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/invoked-as-method.js new file mode 100644 index 0000000000..e787cd2dfc --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/invoked-as-method.js @@ -0,0 +1,31 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Requires a [[TypedArrayName]] internal slot. +info: | + 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +assert.sameValue(typeof TypedArrayPrototype.findIndex, 'function'); + +assert.throws(TypeError, function() { + TypedArrayPrototype.findIndex(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/length.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/length.js new file mode 100644 index 0000000000..47fd253eef --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/length.js @@ -0,0 +1,32 @@ +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + %TypedArray%.prototype.findIndex.length is 1. +info: | + %TypedArray%.prototype.findIndex (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray] +---*/ + +assert.sameValue(TypedArray.prototype.findIndex.length, 1); + +verifyNotEnumerable(TypedArray.prototype.findIndex, "length"); +verifyNotWritable(TypedArray.prototype.findIndex, "length"); +verifyConfigurable(TypedArray.prototype.findIndex, "length"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/name.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/name.js new file mode 100644 index 0000000000..efe006ee61 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/name.js @@ -0,0 +1,29 @@ +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + %TypedArray%.prototype.findIndex.name is "findIndex". +info: | + %TypedArray%.prototype.findIndex (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray] +---*/ + +assert.sameValue(TypedArray.prototype.findIndex.name, "findIndex"); + +verifyNotEnumerable(TypedArray.prototype.findIndex, "name"); +verifyNotWritable(TypedArray.prototype.findIndex, "name"); +verifyConfigurable(TypedArray.prototype.findIndex, "name"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/not-a-constructor.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/not-a-constructor.js new file mode 100644 index 0000000000..c41db60aef --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/not-a-constructor.js @@ -0,0 +1,35 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + TypedArray.prototype.findIndex does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js, testTypedArray.js] +features: [Reflect.construct, arrow-function, TypedArray] +---*/ + +assert.sameValue( + isConstructor(TypedArray.prototype.findIndex), + false, + 'isConstructor(TypedArray.prototype.findIndex) must return false' +); + +assert.throws(TypeError, () => { + let u8 = new Uint8Array(1); new u8.findIndex(() => {}); +}, '`let u8 = new Uint8Array(1); new u8.findIndex(() => {})` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-changes-value.js new file mode 100644 index 0000000000..8ee1fcccae --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-changes-value.js @@ -0,0 +1,69 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Change values during predicate call +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [compareArray.js, testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var arr = [10, 20, 30]; + var sample; + var result; + + sample = new TA(3); + sample.findIndex(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.findIndex(function(val, i) { + if ( i === 0 ) { + sample[2] = 7; + } + return val === 7; + }); + assert.sameValue(result, 2, "value found"); + + sample = new TA(arr); + result = sample.findIndex(function(val, i) { + if ( i === 0 ) { + sample[2] = 7; + } + return val === 30; + }); + assert.sameValue(result, -1, "value not found"); + + sample = new TA(arr); + result = sample.findIndex(function(val, i) { + if ( i > 0 ) { + sample[0] = 7; + } + return val === 7; + }); + assert.sameValue(result, -1, "value not found - changed after call"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-parameters.js new file mode 100644 index 0000000000..86ec93c907 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-parameters.js @@ -0,0 +1,62 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 2, 62]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.findIndex(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 39, "results[0][0] === 39, value"); + assert.sameValue(result[1], 0, "results[0][1] === 0, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3, arguments"); + + result = results[1]; + assert.sameValue(result[0], 2, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3, arguments"); + + result = results[2]; + assert.sameValue(result[0], 62, "results[2][0] === 62, value"); + assert.sameValue(result[1], 2, "results[2][1] === 2, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3, arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..db41c2cae1 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-this-non-strict.js @@ -0,0 +1,59 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Verify predicate this on non-strict mode +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [noStrict] +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var T = this; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findIndex(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.findIndex(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.findIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..d13f4c3bcf --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-call-this-strict-strict.js @@ -0,0 +1,54 @@ +'use strict'; +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 4. If thisArg was supplied, let T be thisArg; else let T be undefined. + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +flags: [onlyStrict] +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findIndex(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.findIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..cc43dfd920 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-is-not-callable-throws.js @@ -0,0 +1,66 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Throws a TypeError exception if predicate is not callable. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 3. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + assert.throws(TypeError, function() { + sample.findIndex({}); + }, "{}"); + + assert.throws(TypeError, function() { + sample.findIndex(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.findIndex(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.findIndex(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.findIndex(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.findIndex(1); + }, "1"); + + assert.throws(TypeError, function() { + sample.findIndex(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.findIndex([]); + }, "[]"); + + assert.throws(TypeError, function() { + sample.findIndex(/./); + }, "/./"); +}); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..a86b1b06b0 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-may-detach-buffer.js @@ -0,0 +1,47 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate may detach the buffer +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + Repeat, while k < len, + Let Pk be ! ToString(F(k)). + Let kValue be ? Get(O, Pk). + Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, F(k), O »)). + ... + + IntegerIndexedElementGet ( O, index ) + + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. + +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(2); + var loops = 0; + + sample.findIndex(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + assert.sameValue(loops, 2, "predicate is called once"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..5ad0de0f26 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/predicate-not-called-on-empty-array.js @@ -0,0 +1,50 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Predicate is not called on an empty instance +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... + 7. Return -1. +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var predicate = function() { + called = true; + return true; + }; + + var result = sample.findIndex(predicate); + + assert.sameValue( + called, false, + "does not call predicate" + ); + assert.sameValue( + result, -1, + "returns -1 on an empty instance" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/prop-desc.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/prop-desc.js new file mode 100644 index 0000000000..26f01cd00a --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/prop-desc.js @@ -0,0 +1,21 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + "findIndex" property of TypedArrayPrototype +info: | + ES6 section 17: Every other data property described in clauses 18 through 26 + and in Annex B.2 has the attributes { [[Writable]]: true, + [[Enumerable]]: false, [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +verifyNotEnumerable(TypedArrayPrototype, 'findIndex'); +verifyWritable(TypedArrayPrototype, 'findIndex'); +verifyConfigurable(TypedArrayPrototype, 'findIndex'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..abb7c66056 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-abrupt-from-predicate-call.js @@ -0,0 +1,40 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Return abrupt from predicate call. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + assert.throws(Test262Error, function() { + sample.findIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..f8ca0ff7c9 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testTypedArray.js] +features: [TypedArray, resizable-arraybuffer] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.findIndex, + 'function', + 'implements TypedArray.prototype.findIndex' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.findIndex(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.findIndex(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the findIndex operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.findIndex(() => {}); + throw new Test262Error('findIndex completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-index-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-index-predicate-result-is-true.js new file mode 100644 index 0000000000..c0c0a8a07b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-index-predicate-result-is-true.js @@ -0,0 +1,67 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Return index if predicate return a boolean true value. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 5. Let k be 0. + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + d. If testResult is true, return k. + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 3, 9]); + var called = 0; + + var result = sample.findIndex(function() { + called++; + return true; + }); + + assert.sameValue(result, 0, "returned true on sample[0]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.findIndex(function(val) { + called++; + return val === 9; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 2, "returned true on sample[3]"); + + result = sample.findIndex(function() { return "string"; }); + assert.sameValue(result, 0, "ToBoolean(string)"); + + result = sample.findIndex(function() { return {}; }); + assert.sameValue(result, 0, "ToBoolean(object)"); + + result = sample.findIndex(function() { return Symbol(""); }); + assert.sameValue(result, 0, "ToBoolean(symbol)"); + + result = sample.findIndex(function() { return 1; }); + assert.sameValue(result, 0, "ToBoolean(number)"); + + result = sample.findIndex(function() { return -1; }); + assert.sameValue(result, 0, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-negative-one-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-negative-one-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..e17e63a44b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/return-negative-one-if-predicate-returns-false-value.js @@ -0,0 +1,60 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Return -1 if predicate always returns a boolean false value. +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". + + ... + + 22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] ) + + ... + 6. Repeat, while k < len + ... + c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)). + ... + 7. Return -1. +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([1, 2, 3]); + var called = 0; + + var result = sample.findIndex(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, -1, "result is -1 when predicate returns are false"); + + result = sample.findIndex(function() { return ""; }); + assert.sameValue(result, -1, "ToBoolean(string)"); + + result = sample.findIndex(function() { return undefined; }); + assert.sameValue(result, -1, "ToBoolean(undefined)"); + + result = sample.findIndex(function() { return null; }); + assert.sameValue(result, -1, "ToBoolean(null)"); + + result = sample.findIndex(function() { return 0; }); + assert.sameValue(result, -1, "ToBoolean(0)"); + + result = sample.findIndex(function() { return -0; }); + assert.sameValue(result, -1, "ToBoolean(-0)"); + + result = sample.findIndex(function() { return NaN; }); + assert.sameValue(result, -1, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/shell.js new file mode 100644 index 0000000000..eda1477282 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/shell.js @@ -0,0 +1,24 @@ +// GENERATED, DO NOT EDIT +// file: isConstructor.js +// Copyright (C) 2017 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: | + Test if a given function is a constructor function. +defines: [isConstructor] +features: [Reflect.construct] +---*/ + +function isConstructor(f) { + if (typeof f !== "function") { + throw new Test262Error("isConstructor invoked with a non-function value"); + } + + try { + Reflect.construct(function(){}, [], f); + } catch (e) { + return false; + } + return true; +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/this-is-not-object.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/this-is-not-object.js new file mode 100644 index 0000000000..5297a42273 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/this-is-not-object.js @@ -0,0 +1,53 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: Throws a TypeError exception when `this` is not Object +info: | + 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray] +---*/ + +var findIndex = TypedArray.prototype.findIndex; +var predicate = function() {}; + +assert.throws(TypeError, function() { + findIndex.call(undefined, predicate); +}, "this is undefined"); + +assert.throws(TypeError, function() { + findIndex.call(null, predicate); +}, "this is null"); + +assert.throws(TypeError, function() { + findIndex.call(42, predicate); +}, "this is 42"); + +assert.throws(TypeError, function() { + findIndex.call("1", predicate); +}, "this is a string"); + +assert.throws(TypeError, function() { + findIndex.call(true, predicate); +}, "this is true"); + +assert.throws(TypeError, function() { + findIndex.call(false, predicate); +}, "this is false"); + +var s = Symbol("s"); +assert.throws(TypeError, function() { + findIndex.call(s, predicate); +}, "this is a Symbol"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/this-is-not-typedarray-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/this-is-not-typedarray-instance.js new file mode 100644 index 0000000000..60b87b1561 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findIndex/this-is-not-typedarray-instance.js @@ -0,0 +1,45 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findindex +description: > + Throws a TypeError exception when `this` is not a TypedArray instance +info: | + 22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray] +---*/ + +var findIndex = TypedArray.prototype.findIndex; +var predicate = function() {}; + +assert.throws(TypeError, function() { + findIndex.call({}, predicate); +}, "this is an Object"); + +assert.throws(TypeError, function() { + findIndex.call([], predicate); +}, "this is an Array"); + +var ab = new ArrayBuffer(8); +assert.throws(TypeError, function() { + findIndex.call(ab, predicate); +}, "this is an ArrayBuffer instance"); + +var dv = new DataView(new ArrayBuffer(8), 0, 1); +assert.throws(TypeError, function() { + findIndex.call(dv, predicate); +}, "this is a DataView instance"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/detached-buffer.js new file mode 100644 index 0000000000..7e42ffaa8a --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/detached-buffer.js @@ -0,0 +1,32 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Throws a TypeError if this has a detached buffer +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 2. Perform ? ValidateTypedArray(O). + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.findLast(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..582951473b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/get-length-ignores-length-prop.js @@ -0,0 +1,45 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + [[Get]] of "length" uses [[ArrayLength]] +info: | + 22.2.3.10 %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + 3. Let len be O.[[ArrayLength]]. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithBigIntTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42n]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.findLast(function() { return true; }), + 42n + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-changes-value.js new file mode 100644 index 0000000000..ffbbdea13f --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-changes-value.js @@ -0,0 +1,68 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Change values during predicate call +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [compareArray.js, testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var arr = [1n, 2n, 3n]; + var sample; + var result; + + sample = new TA(3); + sample.findLast(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0n, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.findLast(function(val, i) { + if ( i === 2 ) { + sample[0] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, 7n, "value found"); + + sample = new TA(arr); + result = sample.findLast(function(val, i) { + if ( i === 2 ) { + sample[0] = 7n; + } + return val === 1n; + }); + assert.sameValue(result, undefined, "value not found"); + + sample = new TA(arr); + result = sample.findLast(function(val, i) { + if ( i < 2 ) { + sample[2] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, undefined, "value not found - changed after call"); + + sample = new TA(arr); + result = sample.findLast(function() { + sample[2] = 7n; + return true; + }); + assert.sameValue(result, 3n, "findLast() returns previous found value"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-parameters.js new file mode 100644 index 0000000000..9eeba6edfb --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-parameters.js @@ -0,0 +1,51 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 2n, 62n]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.findLast(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 62n, "results[0][0] === 62, value"); + assert.sameValue(result[1], 2, "results[0][1] === 2, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3 arguments"); + + result = results[1]; + assert.sameValue(result[0], 2n, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3 arguments"); + + result = results[2]; + assert.sameValue(result[0], 39n, "results[2][0] === 39, value"); + assert.sameValue(result[1], 0, "results[2][1] === 0, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3 arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..b0ddca30f0 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-this-non-strict.js @@ -0,0 +1,47 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Verify predicate this on non-strict mode +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [noStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +var T = this; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLast(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.findLast(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.findLast(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..a9fc757ba6 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-call-this-strict-strict.js @@ -0,0 +1,42 @@ +'use strict'; +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Verify predicate this on strict mode +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [onlyStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLast(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.findLast(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..cc2a8381ea --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-is-not-callable-throws.js @@ -0,0 +1,57 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Throws a TypeError exception if predicate is not callable. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + 4. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + + assert.throws(TypeError, function() { + sample.findLast({}); + }, "object"); + + assert.throws(TypeError, function() { + sample.findLast(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.findLast(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.findLast(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.findLast(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.findLast(1); + }, "number"); + + assert.throws(TypeError, function() { + sample.findLast(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.findLast([]); + }, "array"); + + assert.throws(TypeError, function() { + sample.findLast(/./); + }, "regexp"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..a48ee44eeb --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-may-detach-buffer.js @@ -0,0 +1,41 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Predicate may detach the buffer +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + + However, such optimization must not introduce any observable changes in the + specified behaviour of the algorithm and must take into account the + possibility that calls to predicate may cause the this value to become + detached. + + IntegerIndexedElementGet ( O, index ) + + ... + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. + +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var loops = 0; + var sample = new TA(2); + + sample.findLast(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + + assert.sameValue(loops, 2); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..61365a1b3c --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/predicate-not-called-on-empty-array.js @@ -0,0 +1,39 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Predicate is not called on empty instances +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var result = sample.findLast(function() { + called = true; + return true; + }); + + assert.sameValue( + called, + false, + "empty instance does not call predicate" + ); + assert.sameValue( + result, + undefined, + "findLast returns undefined when predicate is not called" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..775b8ff398 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-abrupt-from-predicate-call.js @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Return abrupt from predicate call. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + var predicate = function() { + throw new Test262Error(); + }; + + assert.throws(Test262Error, function() { + sample.findLast(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..f4075a8af8 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testBigIntTypedArray.js] +features: [ArrayBuffer, BigInt, TypedArray, array-find-from-last, arrow-function, resizable-arraybuffer] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.findLast, + 'function', + 'implements TypedArray.prototype.findLast' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithBigIntTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.findLast(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.findLast(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the findLast operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.findLast(() => {}); + throw new Test262Error('findLast completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-found-value-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-found-value-predicate-result-is-true.js new file mode 100644 index 0000000000..30513a47e9 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-found-value-predicate-result-is-true.js @@ -0,0 +1,55 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Return found value if predicate return a boolean true value. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + d. If testResult is true, return kValue. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 2n, 62n]); + var called, result; + + called = 0; + result = sample.findLast(function() { + called++; + return true; + }); + assert.sameValue(result, 62n, "returned true on sample[2]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.findLast(function(val) { + called++; + return val === 39n; + }); + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 39n, "returned true on sample[0]"); + + result = sample.findLast(function() { return "string"; }); + assert.sameValue(result, 62n, "ToBoolean(string)"); + + result = sample.findLast(function() { return {}; }); + assert.sameValue(result, 62n, "ToBoolean(object)"); + + result = sample.findLast(function() { return Symbol(""); }); + assert.sameValue(result, 62n, "ToBoolean(symbol)"); + + result = sample.findLast(function() { return 1; }); + assert.sameValue(result, 62n, "ToBoolean(number)"); + + result = sample.findLast(function() { return -1; }); + assert.sameValue(result, 62n, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-undefined-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-undefined-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..239455b309 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/return-undefined-if-predicate-returns-false-value.js @@ -0,0 +1,51 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Return undefined if predicate always returns a boolean false value. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + 6. 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + 7. Return undefined. +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(3); + var called = 0; + + var result = sample.findLast(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, undefined); + + result = sample.findLast(function() { return ""; }); + assert.sameValue(result, undefined, "ToBoolean(empty string)"); + + result = sample.findLast(function() { return undefined; }); + assert.sameValue(result, undefined, "ToBoolean(undefined)"); + + result = sample.findLast(function() { return null; }); + assert.sameValue(result, undefined, "ToBoolean(null)"); + + result = sample.findLast(function() { return 0; }); + assert.sameValue(result, undefined, "ToBoolean(0)"); + + result = sample.findLast(function() { return -0; }); + assert.sameValue(result, undefined, "ToBoolean(-0)"); + + result = sample.findLast(function() { return NaN; }); + assert.sameValue(result, undefined, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/shell.js new file mode 100644 index 0000000000..90ee9c114d --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/BigInt/shell.js @@ -0,0 +1,42 @@ +// GENERATED, DO NOT EDIT +// file: testBigIntTypedArray.js +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: | + Collection of functions used to assert the correctness of BigInt TypedArray objects. +defines: + - TypedArray + - testWithBigIntTypedArrayConstructors +---*/ + +/** + * The %TypedArray% intrinsic constructor function. + */ +var TypedArray = Object.getPrototypeOf(Int8Array); + +/** + * Calls the provided function for every typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithBigIntTypedArrayConstructors(f, selected) { + /** + * Array containing every BigInt typed array constructor. + */ + var constructors = selected || [ + BigInt64Array, + BigUint64Array + ]; + + for (var i = 0; i < constructors.length; ++i) { + var constructor = constructors[i]; + try { + f(constructor); + } catch (e) { + e.message += " (Testing with " + constructor.name + ".)"; + throw e; + } + } +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/callbackfn-resize.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/callbackfn-resize.js new file mode 100644 index 0000000000..9205a424fb --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/callbackfn-resize.js @@ -0,0 +1,76 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Instance buffer can be resized during iteration +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, resizable-arraybuffer] +---*/ + +// If the host chooses to throw as allowed by the specification, the observed +// behavior will be identical to the case where `ArrayBuffer.prototype.resize` +// has not been implemented. The following assertion prevents this test from +// passing in runtimes which have not implemented the method. +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +testWithTypedArrayConstructors(function(TA) { + var BPE = TA.BYTES_PER_ELEMENT; + var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4}); + var sample = new TA(buffer); + var secondElement, expectedElements, expectedIndices, expectedArrays; + var elements, indices, arrays, result; + + elements = []; + indices = []; + arrays = []; + result = sample.findLast(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(BPE); + secondElement = undefined; + expectedElements = [0]; + expectedIndices = [0]; + expectedArrays = [sample]; + } catch (_) { + secondElement = 0; + expectedElements = [0, 0, 0]; + expectedIndices = [2, 1, 0]; + expectedArrays = [sample, sample, sample]; + } + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, [0, secondElement, 0], 'elements (shrink)'); + assert.compareArray(indices, [2, 1, 0], 'indices (shrink)'); + assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)'); + assert.sameValue(result, undefined, 'result (shrink)'); + + elements = []; + indices = []; + arrays = []; + result = sample.findLast(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(4 * BPE); + } catch (_) {} + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, expectedElements, 'elements (grow)'); + assert.compareArray(indices, expectedIndices, 'indices (grow)'); + assert.compareArray(arrays, expectedArrays, 'arrays (grow)'); + assert.sameValue(result, undefined, 'result (grow)'); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/detached-buffer.js new file mode 100644 index 0000000000..4aa8408d8b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/detached-buffer.js @@ -0,0 +1,32 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Throws a TypeError if this has a detached buffer +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 2. Perform ? ValidateTypedArray(O). + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray, array-find-from-last] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.findLast(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..2914416949 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/get-length-ignores-length-prop.js @@ -0,0 +1,44 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + [[Get]] of "length" uses [[ArrayLength]] +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + ... + 3. Let len be O.[[ArrayLength]]. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.findLast(function() { return true; }), + 42 + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/invoked-as-func.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/invoked-as-func.js new file mode 100644 index 0000000000..ccfb329e0b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/invoked-as-func.js @@ -0,0 +1,29 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Throws a TypeError exception when invoked as a function +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 2. Perform ? ValidateTypedArray(O). + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var findLast = TypedArray.prototype.findLast; + +assert.sameValue(typeof findLast, 'function'); + +assert.throws(TypeError, function() { + findLast(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/invoked-as-method.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/invoked-as-method.js new file mode 100644 index 0000000000..cb1bc32592 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/invoked-as-method.js @@ -0,0 +1,29 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Requires a [[TypedArrayName]] internal slot. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 2. Perform ? ValidateTypedArray(O). + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +assert.sameValue(typeof TypedArrayPrototype.findLast, 'function'); + +assert.throws(TypeError, function() { + TypedArrayPrototype.findLast(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/length.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/length.js new file mode 100644 index 0000000000..0513865dd8 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/length.js @@ -0,0 +1,34 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + %TypedArray%.prototype.findLast.length is 1. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +assert.sameValue(TypedArray.prototype.findLast.length, 1); + +verifyProperty(TypedArray.prototype.findLast, "length", { + enumerable: false, + writable: false, + configurable: true +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/name.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/name.js new file mode 100644 index 0000000000..4ed9497836 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/name.js @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + %TypedArray%.prototype.findLast.name is "findLast". +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +assert.sameValue(TypedArray.prototype.findLast.name, "findLast"); + +verifyProperty(TypedArray.prototype.findLast, "name", { + enumerable: false, + writable: false, + configurable: true +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/not-a-constructor.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/not-a-constructor.js new file mode 100644 index 0000000000..0aa9fd189c --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/not-a-constructor.js @@ -0,0 +1,35 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + TypedArray.prototype.findLast does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js, testTypedArray.js] +features: [Reflect.construct, arrow-function, TypedArray, array-find-from-last] +---*/ + +assert.sameValue( + isConstructor(TypedArray.prototype.findLast), + false, + 'isConstructor(TypedArray.prototype.findLast) must return false' +); + +assert.throws(TypeError, () => { + let u8 = new Uint8Array(1); new u8.findLast(() => {}); +}, '`let u8 = new Uint8Array(1); new u8.findLast(() => {})` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-changes-value.js new file mode 100644 index 0000000000..fa6d96cb83 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-changes-value.js @@ -0,0 +1,68 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Change values during predicate call +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + +includes: [compareArray.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var arr = [1, 2, 3]; + var sample; + var result; + + sample = new TA(3); + sample.findLast(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.findLast(function(val, i) { + if ( i === 2 ) { + sample[0] = 7; + } + return val === 7; + }); + assert.sameValue(result, 7, "value found"); + + sample = new TA(arr); + result = sample.findLast(function(val, i) { + if ( i === 2 ) { + sample[0] = 7; + } + return val === 1; + }); + assert.sameValue(result, undefined, "value not found"); + + sample = new TA(arr); + result = sample.findLast(function(val, i) { + if ( i < 2 ) { + sample[2] = 7; + } + return val === 7; + }); + assert.sameValue(result, undefined, "value not found - changed after call"); + + sample = new TA(arr); + result = sample.findLast(function() { + sample[2] = 7; + return true; + }); + assert.sameValue(result, 3, "findLast() returns previous found value"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-parameters.js new file mode 100644 index 0000000000..b31a93f459 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-parameters.js @@ -0,0 +1,51 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 2, 62]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.findLast(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 62, "results[0][0] === 62, value"); + assert.sameValue(result[1], 2, "results[0][1] === 2, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3 arguments"); + + result = results[1]; + assert.sameValue(result[0], 2, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3 arguments"); + + result = results[2]; + assert.sameValue(result[0], 39, "results[2][0] === 39, value"); + assert.sameValue(result[1], 0, "results[2][1] === 0, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3 arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..b35b25e9f2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-this-non-strict.js @@ -0,0 +1,47 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Verify predicate this on non-strict mode +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [noStrict] +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var T = this; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLast(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.findLast(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.findLast(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..6c9b6936bf --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-call-this-strict-strict.js @@ -0,0 +1,42 @@ +'use strict'; +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Verify predicate this on strict mode +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [onlyStrict] +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLast(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.findLast(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..4e46f36162 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-is-not-callable-throws.js @@ -0,0 +1,57 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Throws a TypeError exception if predicate is not callable. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + 4. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + + assert.throws(TypeError, function() { + sample.findLast({}); + }, "object"); + + assert.throws(TypeError, function() { + sample.findLast(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.findLast(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.findLast(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.findLast(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.findLast(1); + }, "number"); + + assert.throws(TypeError, function() { + sample.findLast(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.findLast([]); + }, "array"); + + assert.throws(TypeError, function() { + sample.findLast(/./); + }, "regexp"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..51cc7dad9a --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-may-detach-buffer.js @@ -0,0 +1,41 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Predicate may detach the buffer +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + + However, such optimization must not introduce any observable changes in the + specified behaviour of the algorithm and must take into account the + possibility that calls to predicate may cause the this value to become + detached. + + IntegerIndexedElementGet ( O, index ) + + ... + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. + +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var loops = 0; + var sample = new TA(2); + + sample.findLast(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + + assert.sameValue(loops, 2); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..ffe6e0a944 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/predicate-not-called-on-empty-array.js @@ -0,0 +1,39 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Predicate is not called on empty instances +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var result = sample.findLast(function() { + called = true; + return true; + }); + + assert.sameValue( + called, + false, + "empty instance does not call predicate" + ); + assert.sameValue( + result, + undefined, + "findLast returns undefined when predicate is not called" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/prop-desc.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/prop-desc.js new file mode 100644 index 0000000000..bc0ad32e75 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/prop-desc.js @@ -0,0 +1,23 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findLast +description: > + "findLast" property of TypedArrayPrototype +info: | + ES6 section 17: Every other data property described in clauses 18 through 26 + and in Annex B.2 has the attributes { [[Writable]]: true, + [[Enumerable]]: false, [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +verifyProperty(TypedArray.prototype, "findLast", { + enumerable: false, + writable: true, + configurable: true +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..b18ead0dc0 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-abrupt-from-predicate-call.js @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Return abrupt from predicate call. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + var predicate = function() { + throw new Test262Error(); + }; + + assert.throws(Test262Error, function() { + sample.findLast(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..f0e2cf11ea --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testTypedArray.js] +features: [TypedArray, resizable-arraybuffer, array-find-from-last] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.findLast, + 'function', + 'implements TypedArray.prototype.findLast' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.findLast(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.findLast(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the findLast operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.findLast(() => {}); + throw new Test262Error('findLast completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-found-value-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-found-value-predicate-result-is-true.js new file mode 100644 index 0000000000..842492b522 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-found-value-predicate-result-is-true.js @@ -0,0 +1,55 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Return found value if predicate return a boolean true value. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 6. Repeat, while k ≥ 0, + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + d. If testResult is true, return kValue. + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 2, 62]); + var called, result; + + called = 0; + result = sample.findLast(function() { + called++; + return true; + }); + assert.sameValue(result, 62, "returned true on sample[2]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.findLast(function(val) { + called++; + return val === 39; + }); + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 39, "returned true on sample[0]"); + + result = sample.findLast(function() { return "string"; }); + assert.sameValue(result, 62, "ToBoolean(string)"); + + result = sample.findLast(function() { return {}; }); + assert.sameValue(result, 62, "ToBoolean(object)"); + + result = sample.findLast(function() { return Symbol(""); }); + assert.sameValue(result, 62, "ToBoolean(symbol)"); + + result = sample.findLast(function() { return 1; }); + assert.sameValue(result, 62, "ToBoolean(number)"); + + result = sample.findLast(function() { return -1; }); + assert.sameValue(result, 62, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-undefined-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-undefined-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..313915e115 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/return-undefined-if-predicate-returns-false-value.js @@ -0,0 +1,51 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Return undefined if predicate always returns a boolean false value. +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + ... + 6. 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + 7. Return undefined. +includes: [testTypedArray.js] +features: [Symbol, TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(3); + var called = 0; + + var result = sample.findLast(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, undefined); + + result = sample.findLast(function() { return ""; }); + assert.sameValue(result, undefined, "ToBoolean(empty string)"); + + result = sample.findLast(function() { return undefined; }); + assert.sameValue(result, undefined, "ToBoolean(undefined)"); + + result = sample.findLast(function() { return null; }); + assert.sameValue(result, undefined, "ToBoolean(null)"); + + result = sample.findLast(function() { return 0; }); + assert.sameValue(result, undefined, "ToBoolean(0)"); + + result = sample.findLast(function() { return -0; }); + assert.sameValue(result, undefined, "ToBoolean(-0)"); + + result = sample.findLast(function() { return NaN; }); + assert.sameValue(result, undefined, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/shell.js new file mode 100644 index 0000000000..eda1477282 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/shell.js @@ -0,0 +1,24 @@ +// GENERATED, DO NOT EDIT +// file: isConstructor.js +// Copyright (C) 2017 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: | + Test if a given function is a constructor function. +defines: [isConstructor] +features: [Reflect.construct] +---*/ + +function isConstructor(f) { + if (typeof f !== "function") { + throw new Test262Error("isConstructor invoked with a non-function value"); + } + + try { + Reflect.construct(function(){}, [], f); + } catch (e) { + return false; + } + return true; +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/this-is-not-object.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/this-is-not-object.js new file mode 100644 index 0000000000..8e3cd5e0fb --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/this-is-not-object.js @@ -0,0 +1,51 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: Throws a TypeError exception when `this` is not Object +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 2. Perform ? ValidateTypedArray(O). + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray, array-find-from-last] +---*/ + +var findLast = TypedArray.prototype.findLast; +var predicate = function() {}; + +assert.throws(TypeError, function() { + findLast.call(undefined, predicate); +}, "this is undefined"); + +assert.throws(TypeError, function() { + findLast.call(null, predicate); +}, "this is null"); + +assert.throws(TypeError, function() { + findLast.call(42, predicate); +}, "this is 42"); + +assert.throws(TypeError, function() { + findLast.call("1", predicate); +}, "this is a string"); + +assert.throws(TypeError, function() { + findLast.call(true, predicate); +}, "this is true"); + +assert.throws(TypeError, function() { + findLast.call(false, predicate); +}, "this is false"); + +var s = Symbol("s"); +assert.throws(TypeError, function() { + findLast.call(s, predicate); +}, "this is a Symbol"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/this-is-not-typedarray-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/this-is-not-typedarray-instance.js new file mode 100644 index 0000000000..d86c6593a8 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLast/this-is-not-typedarray-instance.js @@ -0,0 +1,43 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlast +description: > + Throws a TypeError exception when `this` is not a TypedArray instance +info: | + %TypedArray%.prototype.findLast (predicate [ , thisArg ] ) + + 2. Perform ? ValidateTypedArray(O). + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var findlast = TypedArray.prototype.findlast; +var predicate = function() {}; + +assert.throws(TypeError, function() { + findlast.call({}, predicate); +}, "this is an Object"); + +assert.throws(TypeError, function() { + findlast.call([], predicate); +}, "this is an Array"); + +var ab = new ArrayBuffer(8); +assert.throws(TypeError, function() { + findlast.call(ab, predicate); +}, "this is an ArrayBuffer instance"); + +var dv = new DataView(new ArrayBuffer(8), 0, 1); +assert.throws(TypeError, function() { + findlast.call(dv, predicate); +}, "this is a DataView instance"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/detached-buffer.js new file mode 100644 index 0000000000..912a877c91 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/detached-buffer.js @@ -0,0 +1,34 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Throws a TypeError if this has a detached buffer +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 2. Perform ? ValidateTypedArray(O). + ... + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.findLastIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..6ebea5d93e --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/get-length-ignores-length-prop.js @@ -0,0 +1,45 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + [[Get]] of "length" uses [[ArrayLength]] +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 3. Let len be O.[[ArrayLength]]. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithBigIntTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42n]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.findLastIndex(function() { return true; }), + 0 + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-changes-value.js new file mode 100644 index 0000000000..a2e1ee53d2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-changes-value.js @@ -0,0 +1,60 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Change values during predicate call +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [compareArray.js, testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var arr = [10n, 20n, 30n]; + var sample; + var result; + + sample = new TA(3); + sample.findLastIndex(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0n, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.findLastIndex(function(val, i) { + if ( i === 2 ) { + sample[0] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, 0, "value found"); + + sample = new TA(arr); + result = sample.findLastIndex(function(val, i) { + if ( i === 2 ) { + sample[0] = 7n; + } + return val === 10n; + }); + assert.sameValue(result, -1, "value not found"); + + sample = new TA(arr); + result = sample.findLastIndex(function(val, i) { + if ( i < 2 ) { + sample[2] = 7n; + } + return val === 7n; + }); + assert.sameValue(result, -1, "value not found - changed after call"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-parameters.js new file mode 100644 index 0000000000..e589cb2732 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-parameters.js @@ -0,0 +1,52 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 2n, 62n]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.findLastIndex(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 62n, "results[0][0] === 62, value"); + assert.sameValue(result[1], 2, "results[0][1] === 2, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3, arguments"); + + result = results[1]; + assert.sameValue(result[0], 2n, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3, arguments"); + + result = results[2]; + assert.sameValue(result[0], 39n, "results[2][0] === 39, value"); + assert.sameValue(result[1], 0, "results[2][1] === 0, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3, arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..82c17225c6 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-this-non-strict.js @@ -0,0 +1,49 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Verify predicate this on non-strict mode +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [noStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +var T = this; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLastIndex(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.findLastIndex(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.findLastIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..d9219834fe --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-call-this-strict-strict.js @@ -0,0 +1,44 @@ +'use strict'; +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [onlyStrict] +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLastIndex(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.findLastIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..0ad12e72a1 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-is-not-callable-throws.js @@ -0,0 +1,57 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Throws a TypeError exception if predicate is not callable. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 4. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + assert.throws(TypeError, function() { + sample.findLastIndex({}); + }, "{}"); + + assert.throws(TypeError, function() { + sample.findLastIndex(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.findLastIndex(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.findLastIndex(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.findLastIndex(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.findLastIndex(1); + }, "1"); + + assert.throws(TypeError, function() { + sample.findLastIndex(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.findLastIndex([]); + }, "[]"); + + assert.throws(TypeError, function() { + sample.findLastIndex(/./); + }, "/./"); +}); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..b4f8e774a9 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-may-detach-buffer.js @@ -0,0 +1,39 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate may detach the buffer +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 6. Repeat, while k ≥ 0, + a. Let Pk be ! ToString(𝔽(k)). + b. Let kValue be ! Get(O, Pk). + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + + IntegerIndexedElementGet ( O, index ) + + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. + +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(2); + var loops = 0; + + sample.findLastIndex(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + assert.sameValue(loops, 2, "predicate is called once"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..63090c3b54 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/predicate-not-called-on-empty-array.js @@ -0,0 +1,41 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate is not called on an empty instance +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + 7. Return -1. +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var predicate = function() { + called = true; + return true; + }; + + var result = sample.findLastIndex(predicate); + + assert.sameValue( + called, false, + "does not call predicate" + ); + assert.sameValue( + result, -1, + "returns -1 on an empty instance" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..4f5b4d6f52 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-abrupt-from-predicate-call.js @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Return abrupt from predicate call. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + assert.throws(Test262Error, function() { + sample.findLastIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..354fd40931 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testBigIntTypedArray.js] +features: [ArrayBuffer, BigInt, TypedArray, array-find-from-last, arrow-function, resizable-arraybuffer] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.findLastIndex, + 'function', + 'implements TypedArray.prototype.findLastIndex' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithBigIntTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.findLastIndex(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.findLastIndex(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the findLastIndex operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.findLastIndex(() => {}); + throw new Test262Error('findLastIndex completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-index-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-index-predicate-result-is-true.js new file mode 100644 index 0000000000..28a0bff79e --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-index-predicate-result-is-true.js @@ -0,0 +1,58 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Return index if predicate return a boolean true value. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + d. If testResult is true, return 𝔽(k). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([39n, 3n, 9n]); + var called = 0; + + var result = sample.findLastIndex(function() { + called++; + return true; + }); + + assert.sameValue(result, 2, "returned true on sample[2]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.findLastIndex(function(val) { + called++; + return val === 39n; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 0, "returned true on sample[0]"); + + result = sample.findLastIndex(function() { return "string"; }); + assert.sameValue(result, 2, "ToBoolean(string)"); + + result = sample.findLastIndex(function() { return {}; }); + assert.sameValue(result, 2, "ToBoolean(object)"); + + result = sample.findLastIndex(function() { return Symbol(""); }); + assert.sameValue(result, 2, "ToBoolean(symbol)"); + + result = sample.findLastIndex(function() { return 1; }); + assert.sameValue(result, 2, "ToBoolean(number)"); + + result = sample.findLastIndex(function() { return -1; }); + assert.sameValue(result, 2, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-negative-one-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-negative-one-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..eb08ec96ef --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/return-negative-one-if-predicate-returns-false-value.js @@ -0,0 +1,52 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Return -1 if predicate always returns a boolean false value. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + 7. Return -1𝔽. +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray, array-find-from-last] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([1n, 2n, 3n]); + var called = 0; + + var result = sample.findLastIndex(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, -1, "result is -1 when predicate returns are false"); + + result = sample.findLastIndex(function() { return ""; }); + assert.sameValue(result, -1, "ToBoolean(string)"); + + result = sample.findLastIndex(function() { return undefined; }); + assert.sameValue(result, -1, "ToBoolean(undefined)"); + + result = sample.findLastIndex(function() { return null; }); + assert.sameValue(result, -1, "ToBoolean(null)"); + + result = sample.findLastIndex(function() { return 0; }); + assert.sameValue(result, -1, "ToBoolean(0)"); + + result = sample.findLastIndex(function() { return -0; }); + assert.sameValue(result, -1, "ToBoolean(-0)"); + + result = sample.findLastIndex(function() { return NaN; }); + assert.sameValue(result, -1, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/shell.js new file mode 100644 index 0000000000..90ee9c114d --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/BigInt/shell.js @@ -0,0 +1,42 @@ +// GENERATED, DO NOT EDIT +// file: testBigIntTypedArray.js +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: | + Collection of functions used to assert the correctness of BigInt TypedArray objects. +defines: + - TypedArray + - testWithBigIntTypedArrayConstructors +---*/ + +/** + * The %TypedArray% intrinsic constructor function. + */ +var TypedArray = Object.getPrototypeOf(Int8Array); + +/** + * Calls the provided function for every typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithBigIntTypedArrayConstructors(f, selected) { + /** + * Array containing every BigInt typed array constructor. + */ + var constructors = selected || [ + BigInt64Array, + BigUint64Array + ]; + + for (var i = 0; i < constructors.length; ++i) { + var constructor = constructors[i]; + try { + f(constructor); + } catch (e) { + e.message += " (Testing with " + constructor.name + ".)"; + throw e; + } + } +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/callbackfn-resize.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/callbackfn-resize.js new file mode 100644 index 0000000000..b79f2aa982 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/callbackfn-resize.js @@ -0,0 +1,76 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Instance buffer can be resized during iteration +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, resizable-arraybuffer] +---*/ + +// If the host chooses to throw as allowed by the specification, the observed +// behavior will be identical to the case where `ArrayBuffer.prototype.resize` +// has not been implemented. The following assertion prevents this test from +// passing in runtimes which have not implemented the method. +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +testWithTypedArrayConstructors(function(TA) { + var BPE = TA.BYTES_PER_ELEMENT; + var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4}); + var sample = new TA(buffer); + var secondElement, expectedElements, expectedIndices, expectedArrays; + var elements, indices, arrays, result; + + elements = []; + indices = []; + arrays = []; + result = sample.findLastIndex(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(BPE); + secondElement = undefined; + expectedElements = [0]; + expectedIndices = [0]; + expectedArrays = [sample]; + } catch (_) { + secondElement = 0; + expectedElements = [0, 0, 0]; + expectedIndices = [2, 1, 0]; + expectedArrays = [sample, sample, sample]; + } + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, [0, secondElement, 0], 'elements (shrink)'); + assert.compareArray(indices, [2, 1, 0], 'indices (shrink)'); + assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)'); + assert.sameValue(result, -1, 'result (shrink)'); + + elements = []; + indices = []; + arrays = []; + result = sample.findLastIndex(function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(4 * BPE); + } catch (_) {} + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, expectedElements, 'elements (grow)'); + assert.compareArray(indices, expectedIndices, 'indices (grow)'); + assert.compareArray(arrays, expectedArrays, 'arrays (grow)'); + assert.sameValue(result, -1, 'result (grow)'); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/detached-buffer.js new file mode 100644 index 0000000000..953b277879 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/detached-buffer.js @@ -0,0 +1,34 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Throws a TypeError if this has a detached buffer +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 2. Perform ? ValidateTypedArray(O). + ... + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray, array-find-from-last] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.findLastIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/get-length-ignores-length-prop.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/get-length-ignores-length-prop.js new file mode 100644 index 0000000000..389ec09afa --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/get-length-ignores-length-prop.js @@ -0,0 +1,45 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + [[Get]] of "length" uses [[ArrayLength]] +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 3. Let len be O.[[ArrayLength]]. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +Object.defineProperty(TypedArray.prototype, "length", { + get: function() { + throw new Test262Error(); + } +}); + +testWithTypedArrayConstructors(function(TA) { + Object.defineProperty(TA.prototype, "length", { + get: function() { + throw new Test262Error(); + } + }); + + var sample = new TA([42]); + + Object.defineProperty(sample, "length", { + get: function() { + throw new Test262Error(); + }, + configurable: true + }); + + assert.sameValue( + sample.findLastIndex(function() { return true; }), + 0 + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/invoked-as-func.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/invoked-as-func.js new file mode 100644 index 0000000000..bec6bb9b56 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/invoked-as-func.js @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Throws a TypeError exception when invoked as a function +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 2. Perform ? ValidateTypedArray(O). + ... + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var findLastIndex = TypedArray.prototype.findLastIndex; + +assert.sameValue(typeof findLastIndex, 'function'); + +assert.throws(TypeError, function() { + findLastIndex(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/invoked-as-method.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/invoked-as-method.js new file mode 100644 index 0000000000..89ee3f90f7 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/invoked-as-method.js @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Requires a [[TypedArrayName]] internal slot. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + Perform ? ValidateTypedArray(O). + ... + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +assert.sameValue(typeof TypedArrayPrototype.findLastIndex, 'function'); + +assert.throws(TypeError, function() { + TypedArrayPrototype.findLastIndex(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/length.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/length.js new file mode 100644 index 0000000000..9586c335a5 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/length.js @@ -0,0 +1,34 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + %TypedArray%.prototype.findLastIndex.length is 1. +info: | + %TypedArray%.prototype.findLastIndex (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +assert.sameValue(TypedArray.prototype.findLastIndex.length, 1); + +verifyProperty(TypedArray.prototype.findLastIndex, "length", { + enumerable: false, + writable: false, + configurable: true +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/name.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/name.js new file mode 100644 index 0000000000..87e4639963 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/name.js @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + %TypedArray%.prototype.findLastIndex.name is "findLastIndex". +info: | + %TypedArray%.prototype.findLastIndex (predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +assert.sameValue(TypedArray.prototype.findLastIndex.name, "findLastIndex"); + +verifyProperty(TypedArray.prototype.findLastIndex, "name", { + enumerable: false, + writable: false, + configurable: true +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/not-a-constructor.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/not-a-constructor.js new file mode 100644 index 0000000000..ec57906ae8 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/not-a-constructor.js @@ -0,0 +1,35 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + TypedArray.prototype.findLastIndex does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js, testTypedArray.js] +features: [Reflect.construct, arrow-function, TypedArray, array-find-from-last] +---*/ + +assert.sameValue( + isConstructor(TypedArray.prototype.findLastIndex), + false, + 'isConstructor(TypedArray.prototype.findLastIndex) must return false' +); + +assert.throws(TypeError, () => { + let u8 = new Uint8Array(1); new u8.findLastIndex(() => {}); +}, '`let u8 = new Uint8Array(1); new u8.findLastIndex(() => {})` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-changes-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-changes-value.js new file mode 100644 index 0000000000..d288354edd --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-changes-value.js @@ -0,0 +1,60 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Change values during predicate call +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [compareArray.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var arr = [10, 20, 30]; + var sample; + var result; + + sample = new TA(3); + sample.findLastIndex(function(val, i) { + sample[i] = arr[i]; + + assert.sameValue(val, 0, "value is not mapped to instance"); + }); + assert(compareArray(sample, arr), "values set during each predicate call"); + + sample = new TA(arr); + result = sample.findLastIndex(function(val, i) { + if ( i === 2 ) { + sample[0] = 7; + } + return val === 7; + }); + assert.sameValue(result, 0, "value found"); + + sample = new TA(arr); + result = sample.findLastIndex(function(val, i) { + if ( i === 2 ) { + sample[0] = 7; + } + return val === 10; + }); + assert.sameValue(result, -1, "value not found"); + + sample = new TA(arr); + result = sample.findLastIndex(function(val, i) { + if ( i < 2) { + sample[2] = 7; + } + return val === 7; + }); + assert.sameValue(result, -1, "value not found - changed after call"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-parameters.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-parameters.js new file mode 100644 index 0000000000..6fd62813ea --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-parameters.js @@ -0,0 +1,52 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 2, 62]); + var results = []; + var result; + + sample.foo = "bar"; // Ignores non integer index properties + + sample.findLastIndex(function() { + results.push(arguments); + }); + + assert.sameValue(results.length, 3, "predicate is called for each index"); + + result = results[0]; + assert.sameValue(result[0], 62, "results[0][0] === 62, value"); + assert.sameValue(result[1], 2, "results[0][1] === 2, index"); + assert.sameValue(result[2], sample, "results[0][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[0].length === 3, arguments"); + + result = results[1]; + assert.sameValue(result[0], 2, "results[1][0] === 2, value"); + assert.sameValue(result[1], 1, "results[1][1] === 1, index"); + assert.sameValue(result[2], sample, "results[1][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[1].length === 3, arguments"); + + result = results[2]; + assert.sameValue(result[0], 39, "results[2][0] === 39, value"); + assert.sameValue(result[1], 0, "results[2][1] === 0, index"); + assert.sameValue(result[2], sample, "results[2][2] === sample, instance"); + assert.sameValue(result.length, 3, "results[2].length === 3, arguments"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..3c82732709 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-this-non-strict.js @@ -0,0 +1,49 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Verify predicate this on non-strict mode +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [noStrict] +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var T = this; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLastIndex(function() { + result = this; + }); + + assert.sameValue(result, T, "without thisArg, predicate this is the global"); + + result = null; + sample.findLastIndex(function() { + result = this; + }, undefined); + + assert.sameValue(result, T, "predicate this is the global when thisArg is undefined"); + + var o = {}; + result = null; + sample.findLastIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..db22a84372 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-call-this-strict-strict.js @@ -0,0 +1,44 @@ +'use strict'; +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +flags: [onlyStrict] +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + var result; + + sample.findLastIndex(function() { + result = this; + }); + + assert.sameValue( + result, + undefined, + "without thisArg, predicate this is undefined" + ); + + var o = {}; + sample.findLastIndex(function() { + result = this; + }, o); + + assert.sameValue(result, o, "thisArg becomes the predicate this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..3785ecdac2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-is-not-callable-throws.js @@ -0,0 +1,57 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Throws a TypeError exception if predicate is not callable. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 4. If IsCallable(predicate) is false, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + assert.throws(TypeError, function() { + sample.findLastIndex({}); + }, "{}"); + + assert.throws(TypeError, function() { + sample.findLastIndex(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.findLastIndex(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.findLastIndex(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.findLastIndex(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.findLastIndex(1); + }, "1"); + + assert.throws(TypeError, function() { + sample.findLastIndex(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.findLastIndex([]); + }, "[]"); + + assert.throws(TypeError, function() { + sample.findLastIndex(/./); + }, "/./"); +}); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-may-detach-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-may-detach-buffer.js new file mode 100644 index 0000000000..9edb72ff2f --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-may-detach-buffer.js @@ -0,0 +1,39 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate may detach the buffer +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 6. Repeat, while k ≥ 0, + a. Let Pk be ! ToString(𝔽(k)). + b. Let kValue be ! Get(O, Pk). + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + + IntegerIndexedElementGet ( O, index ) + + Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot. + If IsDetachedBuffer(buffer) is true, return undefined. + +includes: [testTypedArray.js, detachArrayBuffer.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(2); + var loops = 0; + + sample.findLastIndex(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + }); + assert.sameValue(loops, 2, "predicate is called once"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..857a521897 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/predicate-not-called-on-empty-array.js @@ -0,0 +1,41 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Predicate is not called on an empty instance +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + 7. Return -1. +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(); + var called = false; + + var predicate = function() { + called = true; + return true; + }; + + var result = sample.findLastIndex(predicate); + + assert.sameValue( + called, false, + "does not call predicate" + ); + assert.sameValue( + result, -1, + "returns -1 on an empty instance" + ); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/prop-desc.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/prop-desc.js new file mode 100644 index 0000000000..344ab98b2f --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/prop-desc.js @@ -0,0 +1,23 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + "findLastIndex" property of TypedArrayPrototype +info: | + ES6 section 17: Every other data property described in clauses 18 through 26 + and in Annex B.2 has the attributes { [[Writable]]: true, + [[Enumerable]]: false, [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var TypedArrayPrototype = TypedArray.prototype; + +verifyProperty(TypedArray.prototype, "findLastIndex", { + enumerable: false, + writable: true, + configurable: true +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..e615464938 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-abrupt-from-predicate-call.js @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Return abrupt from predicate call. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(1); + assert.throws(Test262Error, function() { + sample.findLastIndex(predicate); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..affee74fba --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testTypedArray.js] +features: [TypedArray, resizable-arraybuffer, array-find-from-last] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.findLastIndex, + 'function', + 'implements TypedArray.prototype.findLastIndex' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.findLastIndex(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.findLastIndex(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the findLastIndex operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.findLastIndex(() => {}); + throw new Test262Error('findLastIndex completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-index-predicate-result-is-true.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-index-predicate-result-is-true.js new file mode 100644 index 0000000000..c81fb38cdf --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-index-predicate-result-is-true.js @@ -0,0 +1,58 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Return index if predicate return a boolean true value. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + d. If testResult is true, return 𝔽(k). + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([39, 3, 9]); + var called = 0; + + var result = sample.findLastIndex(function() { + called++; + return true; + }); + + assert.sameValue(result, 2, "returned true on sample[2]"); + assert.sameValue(called, 1, "predicate was called once"); + + called = 0; + result = sample.findLastIndex(function(val) { + called++; + return val === 39; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, 0, "returned true on sample[0]"); + + result = sample.findLastIndex(function() { return "string"; }); + assert.sameValue(result, 2, "ToBoolean(string)"); + + result = sample.findLastIndex(function() { return {}; }); + assert.sameValue(result, 2, "ToBoolean(object)"); + + result = sample.findLastIndex(function() { return Symbol(""); }); + assert.sameValue(result, 2, "ToBoolean(symbol)"); + + result = sample.findLastIndex(function() { return 1; }); + assert.sameValue(result, 2, "ToBoolean(number)"); + + result = sample.findLastIndex(function() { return -1; }); + assert.sameValue(result, 2, "ToBoolean(negative number)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-negative-one-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-negative-one-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..e535075e9b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/return-negative-one-if-predicate-returns-false-value.js @@ -0,0 +1,52 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Return -1 if predicate always returns a boolean false value. +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 5. Let k be len - 1. + 6. Repeat, while k ≥ 0 + ... + c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)). + ... + 7. Return -1𝔽. +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA([1, 2, 3]); + var called = 0; + + var result = sample.findLastIndex(function() { + called++; + return false; + }); + + assert.sameValue(called, 3, "predicate was called three times"); + assert.sameValue(result, -1, "result is -1 when predicate returns are false"); + + result = sample.findLastIndex(function() { return ""; }); + assert.sameValue(result, -1, "ToBoolean(string)"); + + result = sample.findLastIndex(function() { return undefined; }); + assert.sameValue(result, -1, "ToBoolean(undefined)"); + + result = sample.findLastIndex(function() { return null; }); + assert.sameValue(result, -1, "ToBoolean(null)"); + + result = sample.findLastIndex(function() { return 0; }); + assert.sameValue(result, -1, "ToBoolean(0)"); + + result = sample.findLastIndex(function() { return -0; }); + assert.sameValue(result, -1, "ToBoolean(-0)"); + + result = sample.findLastIndex(function() { return NaN; }); + assert.sameValue(result, -1, "ToBoolean(NaN)"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/shell.js new file mode 100644 index 0000000000..eda1477282 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/shell.js @@ -0,0 +1,24 @@ +// GENERATED, DO NOT EDIT +// file: isConstructor.js +// Copyright (C) 2017 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: | + Test if a given function is a constructor function. +defines: [isConstructor] +features: [Reflect.construct] +---*/ + +function isConstructor(f) { + if (typeof f !== "function") { + throw new Test262Error("isConstructor invoked with a non-function value"); + } + + try { + Reflect.construct(function(){}, [], f); + } catch (e) { + return false; + } + return true; +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/this-is-not-object.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/this-is-not-object.js new file mode 100644 index 0000000000..1dbfc3115f --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/this-is-not-object.js @@ -0,0 +1,53 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: Throws a TypeError exception when `this` is not Object +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 2. Perform ? ValidateTypedArray(O). + ... + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + ... +includes: [testTypedArray.js] +features: [Symbol, TypedArray, array-find-from-last] +---*/ + +var findLastIndex = TypedArray.prototype.findLastIndex; +var predicate = function() {}; + +assert.throws(TypeError, function() { + findLastIndex.call(undefined, predicate); +}, "this is undefined"); + +assert.throws(TypeError, function() { + findLastIndex.call(null, predicate); +}, "this is null"); + +assert.throws(TypeError, function() { + findLastIndex.call(42, predicate); +}, "this is 42"); + +assert.throws(TypeError, function() { + findLastIndex.call("1", predicate); +}, "this is a string"); + +assert.throws(TypeError, function() { + findLastIndex.call(true, predicate); +}, "this is true"); + +assert.throws(TypeError, function() { + findLastIndex.call(false, predicate); +}, "this is false"); + +var s = Symbol("s"); +assert.throws(TypeError, function() { + findLastIndex.call(s, predicate); +}, "this is a Symbol"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/this-is-not-typedarray-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/this-is-not-typedarray-instance.js new file mode 100644 index 0000000000..cc92b40bec --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/findLastIndex/this-is-not-typedarray-instance.js @@ -0,0 +1,45 @@ +// Copyright (C) 2021 Microsoft. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.findlastindex +description: > + Throws a TypeError exception when `this` is not a TypedArray instance +info: | + %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] ) + + ... + 2. Perform ? ValidateTypedArray(O). + ... + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + 1. If Type(O) is not Object, throw a TypeError exception. + 2. If O does not have a [[TypedArrayName]] internal slot, throw a TypeError + exception. + ... +includes: [testTypedArray.js] +features: [TypedArray, array-find-from-last] +---*/ + +var findLastIndex = TypedArray.prototype.findLastIndex; +var predicate = function() {}; + +assert.throws(TypeError, function() { + findLastIndex.call({}, predicate); +}, "this is an Object"); + +assert.throws(TypeError, function() { + findLastIndex.call([], predicate); +}, "this is an Array"); + +var ab = new ArrayBuffer(8); +assert.throws(TypeError, function() { + findLastIndex.call(ab, predicate); +}, "this is an ArrayBuffer instance"); + +var dv = new DataView(new ArrayBuffer(8), 0, 1); +assert.throws(TypeError, function() { + findLastIndex.call(dv, predicate); +}, "this is a DataView instance"); + +reportCompare(0, 0); |