diff --git a/test/built-ins/ArrayBuffer/prototype/resize/descriptor.js b/test/built-ins/ArrayBuffer/prototype/resize/descriptor.js new file mode 100644 index 0000000000..957359cec6 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/descriptor.js @@ -0,0 +1,22 @@ +// 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-arraybuffer.prototype.resize +description: > + ArrayBuffer.prototype.resize has default data property attributes. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 17 ECMAScript Standard Built-in Objects: + 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] +features: [resizable-arraybuffer] +---*/ + +verifyProperty(ArrayBuffer.prototype, 'resize', { + enumerable: false, + writable: true, + configurable: true +}); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/extensible.js b/test/built-ins/ArrayBuffer/prototype/resize/extensible.js new file mode 100644 index 0000000000..94c627c138 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/extensible.js @@ -0,0 +1,15 @@ +// 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-arraybuffer.prototype.resize +description: ArrayBuffer.prototype.resize is extensible. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 17 ECMAScript Standard Built-in Objects: + Unless specified otherwise, the [[Extensible]] internal slot + of a built-in object initially has the value true. +features: [resizable-arraybuffer] +---*/ + +assert(Object.isExtensible(ArrayBuffer.prototype.resize)); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/length.js b/test/built-ins/ArrayBuffer/prototype/resize/length.js new file mode 100644 index 0000000000..4305565034 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/length.js @@ -0,0 +1,30 @@ +// 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-arraybuffer.prototype.resize +description: > + ArrayBuffer.prototype.resize.length is 1. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 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] +features: [resizable-arraybuffer] +---*/ + +verifyProperty(ArrayBuffer.prototype.resize, 'length', { + value: 1, + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/name.js b/test/built-ins/ArrayBuffer/prototype/resize/name.js new file mode 100644 index 0000000000..418242cf53 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/name.js @@ -0,0 +1,27 @@ +// 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-arraybuffer.prototype.resize +description: > + ArrayBuffer.prototype.resize.name is "resize". +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 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 }. +features: [resizable-arraybuffer] +includes: [propertyHelper.js] +---*/ + +verifyProperty(ArrayBuffer.prototype.resize, 'name', { + value: 'resize', + enumerable: false, + wrtiable: false, + configurable: true +}); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/new-length-excessive.js b/test/built-ins/ArrayBuffer/prototype/resize/new-length-excessive.js new file mode 100644 index 0000000000..48d4640a2a --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/new-length-excessive.js @@ -0,0 +1,25 @@ +// 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-arraybuffer.prototype.resize +description: > + Throws a RangeError the newLength value is larger than the max byte length +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + [...] +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(4, {maxByteLength: 4}); + +assert.throws(RangeError, function() { + ab.resize(5); +}); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/new-length-negative.js b/test/built-ins/ArrayBuffer/prototype/resize/new-length-negative.js new file mode 100644 index 0000000000..edbfcc6354 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/new-length-negative.js @@ -0,0 +1,25 @@ +// 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-arraybuffer.prototype.resize +description: > + Throws a RangeError the newLength value is less than zero +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + [...] +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(4, {maxByteLength: 4}); + +assert.throws(RangeError, function() { + ab.resize(-1); +}); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/new-length-non-number.js b/test/built-ins/ArrayBuffer/prototype/resize/new-length-non-number.js new file mode 100644 index 0000000000..c20ec987ca --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/new-length-non-number.js @@ -0,0 +1,37 @@ +// 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-arraybuffer.prototype.resize +description: Throws a TypeError if provided length cannot be coerced to a number +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + [...] +features: [resizable-arraybuffer] +---*/ + +var log = []; +var newLength = { + toString: function() { + log.push('toString'); + return {}; + }, + valueOf: function() { + log.push('valueOf'); + return {}; + } +}; +var ab = new ArrayBuffer(0, {maxByteLength: 4}); + +assert.throws(TypeError, function() { + ab.resize(newLength); +}); + +assert.sameValue(log.length, 2); +assert.sameValue(log[0], 'valueOf'); +assert.sameValue(log[1], 'toString'); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/nonconstructor.js b/test/built-ins/ArrayBuffer/prototype/resize/nonconstructor.js new file mode 100644 index 0000000000..986699a001 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/nonconstructor.js @@ -0,0 +1,24 @@ +// 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-arraybuffer.prototype.resize +description: > + ArrayBuffer.prototype.resize is not a constructor function. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 17 ECMAScript Standard Built-in 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. +---*/ + +assert.sameValue( + Object.prototype.hasOwnProperty.call(ArrayBuffer.prototype.resize, 'prototype'), + false +); + +var arrayBuffer = new ArrayBuffer(8); +assert.throws(TypeError, function() { + new arrayBuffer.resize(); +}); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/resize-grow.js b/test/built-ins/ArrayBuffer/prototype/resize/resize-grow.js new file mode 100644 index 0000000000..06a1ee7b24 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/resize-grow.js @@ -0,0 +1,77 @@ +// 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-arraybuffer.prototype.resize +description: Behavior when attempting to grow a resizable array buffer +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength). + [...] + + HostResizeArrayBuffer ( buffer, newByteLength ) + + The implementation of HostResizeArrayBuffer must conform to the following + requirements: + + - The abstract operation does not detach buffer. + - The abstract operation may complete normally or abruptly. + - If the abstract operation completes normally with handled, + buffer.[[ArrayBufferByteLength]] is newByteLength. + - The return value is either handled or unhandled. +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(4, {maxByteLength: 5}); +var caught = false; +var result; + +// 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 ab.resize, 'function'); + +try { + result = ab.resize(5); +} catch (_) { + caught = true; +} + +try { + ab.slice(); +} catch (_) { + throw new Test262Error('The ArrayBuffer under test was detached'); +} + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation and conforms to the +// invarient regarding [[ArrayBufferByteLength]] +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method updates [[ArrayBufferByteLength]] +// +// The final two conditions are indistinguishable. +assert(caught || ab.byteLength === 5, 'byteLength'); + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation, and the `resize` +// method returns early +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method executes its final steps +// +// All three conditions have the same effect on the value of `result`. +assert.sameValue(result, undefined, 'normal completion value'); + +// The contents of the ArrayBuffer are not guaranteed by the host-defined +// abstract operation, so they are not asserted in this test. diff --git a/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size-zero-explicit.js b/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size-zero-explicit.js new file mode 100644 index 0000000000..fc3d707e9f --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size-zero-explicit.js @@ -0,0 +1,78 @@ +// 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-arraybuffer.prototype.resize +description: > + Behavior when attempting to reset the size of a resizable array buffer to zero explicitly +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength). + [...] + + HostResizeArrayBuffer ( buffer, newByteLength ) + + The implementation of HostResizeArrayBuffer must conform to the following + requirements: + + - The abstract operation does not detach buffer. + - The abstract operation may complete normally or abruptly. + - If the abstract operation completes normally with handled, + buffer.[[ArrayBufferByteLength]] is newByteLength. + - The return value is either handled or unhandled. +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(0, {maxByteLength: 0}); +var caught = false; +var result; + +// 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 ab.resize, 'function'); + +try { + result = ab.resize(0); +} catch (_) { + caught = true; +} + +try { + ab.slice(); +} catch (_) { + throw new Test262Error('The ArrayBuffer under test was detached'); +} + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation and conforms to the +// invarient regarding [[ArrayBufferByteLength]] +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method updates [[ArrayBufferByteLength]] +// +// The final two conditions are indistinguishable. +assert(caught || ab.byteLength === 0, 'byteLength'); + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation, and the `resize` +// method returns early +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method executes its final steps +// +// All three conditions have the same effect on the value of `result`. +assert.sameValue(result, undefined, 'normal completion value'); + +// The contents of the ArrayBuffer are not guaranteed by the host-defined +// abstract operation, so they are not asserted in this test. diff --git a/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size-zero-implicit.js b/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size-zero-implicit.js new file mode 100644 index 0000000000..a2d169f06b --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size-zero-implicit.js @@ -0,0 +1,78 @@ +// 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-arraybuffer.prototype.resize +description: > + Behavior when attempting to reset the size of a resizable array buffer to zero explicitly +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength). + [...] + + HostResizeArrayBuffer ( buffer, newByteLength ) + + The implementation of HostResizeArrayBuffer must conform to the following + requirements: + + - The abstract operation does not detach buffer. + - The abstract operation may complete normally or abruptly. + - If the abstract operation completes normally with handled, + buffer.[[ArrayBufferByteLength]] is newByteLength. + - The return value is either handled or unhandled. +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(0, {maxByteLength: 0}); +var caught = false; +var result; + +// 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 ab.resize, 'function'); + +try { + result = ab.resize(); +} catch (_) { + caught = true; +} + +try { + ab.slice(); +} catch (_) { + throw new Test262Error('The ArrayBuffer under test was detached'); +} + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation and conforms to the +// invarient regarding [[ArrayBufferByteLength]] +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method updates [[ArrayBufferByteLength]] +// +// The final two conditions are indistinguishable. +assert(caught || ab.byteLength === 0, 'byteLength'); + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation, and the `resize` +// method returns early +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method executes its final steps +// +// All three conditions have the same effect on the value of `result`. +assert.sameValue(result, undefined, 'normal completion value'); + +// The contents of the ArrayBuffer are not guaranteed by the host-defined +// abstract operation, so they are not asserted in this test. diff --git a/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size.js b/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size.js new file mode 100644 index 0000000000..bccb2f9b92 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/resize-same-size.js @@ -0,0 +1,78 @@ +// 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-arraybuffer.prototype.resize +description: > + Behavior when attempting to reset the size of a resizable array buffer +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength). + [...] + + HostResizeArrayBuffer ( buffer, newByteLength ) + + The implementation of HostResizeArrayBuffer must conform to the following + requirements: + + - The abstract operation does not detach buffer. + - The abstract operation may complete normally or abruptly. + - If the abstract operation completes normally with handled, + buffer.[[ArrayBufferByteLength]] is newByteLength. + - The return value is either handled or unhandled. +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(4, {maxByteLength: 4}); +var caught = false; +var result; + +// 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 ab.resize, 'function'); + +try { + result = ab.resize(4); +} catch (_) { + caught = true; +} + +try { + ab.slice(); +} catch (_) { + throw new Test262Error('The ArrayBuffer under test was detached'); +} + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation and conforms to the +// invarient regarding [[ArrayBufferByteLength]] +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method updates [[ArrayBufferByteLength]] +// +// The final two conditions are indistinguishable. +assert(caught || ab.byteLength === 4, 'byteLength'); + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation, and the `resize` +// method returns early +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method executes its final steps +// +// All three conditions have the same effect on the value of `result`. +assert.sameValue(result, undefined, 'normal completion value'); + +// The contents of the ArrayBuffer are not guaranteed by the host-defined +// abstract operation, so they are not asserted in this test. diff --git a/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink-zero-explicit.js b/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink-zero-explicit.js new file mode 100644 index 0000000000..7425b9ac1a --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink-zero-explicit.js @@ -0,0 +1,77 @@ +// 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-arraybuffer.prototype.resize +description: Behavior when attempting to shrink a resizable array buffer to zero explicitly +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength). + [...] + + HostResizeArrayBuffer ( buffer, newByteLength ) + + The implementation of HostResizeArrayBuffer must conform to the following + requirements: + + - The abstract operation does not detach buffer. + - The abstract operation may complete normally or abruptly. + - If the abstract operation completes normally with handled, + buffer.[[ArrayBufferByteLength]] is newByteLength. + - The return value is either handled or unhandled. +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(4, {maxByteLength: 4}); +var caught = false; +var result; + +// 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 ab.resize, 'function'); + +try { + result = ab.resize(0); +} catch (_) { + caught = true; +} + +try { + ab.slice(); +} catch (_) { + throw new Test262Error('The ArrayBuffer under test was detached'); +} + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation and conforms to the +// invarient regarding [[ArrayBufferByteLength]] +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method updates [[ArrayBufferByteLength]] +// +// The final two conditions are indistinguishable. +assert(caught || ab.byteLength === 0, 'byteLength'); + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation, and the `resize` +// method returns early +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method executes its final steps +// +// All three conditions have the same effect on the value of `result`. +assert.sameValue(result, undefined, 'normal completion value'); + +// The contents of the ArrayBuffer are not guaranteed by the host-defined +// abstract operation, so they are not asserted in this test. diff --git a/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink-zero-implicit.js b/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink-zero-implicit.js new file mode 100644 index 0000000000..83b1c812a4 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink-zero-implicit.js @@ -0,0 +1,77 @@ +// 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-arraybuffer.prototype.resize +description: Behavior when attempting to shrink a resizable array buffer to zero implicitly +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength). + [...] + + HostResizeArrayBuffer ( buffer, newByteLength ) + + The implementation of HostResizeArrayBuffer must conform to the following + requirements: + + - The abstract operation does not detach buffer. + - The abstract operation may complete normally or abruptly. + - If the abstract operation completes normally with handled, + buffer.[[ArrayBufferByteLength]] is newByteLength. + - The return value is either handled or unhandled. +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(4, {maxByteLength: 4}); +var caught = false; +var result; + +// 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 ab.resize, 'function'); + +try { + result = ab.resize(); +} catch (_) { + caught = true; +} + +try { + ab.slice(); +} catch (_) { + throw new Test262Error('The ArrayBuffer under test was detached'); +} + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation and conforms to the +// invarient regarding [[ArrayBufferByteLength]] +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method updates [[ArrayBufferByteLength]] +// +// The final two conditions are indistinguishable. +assert(caught || ab.byteLength === 0, 'byteLength'); + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation, and the `resize` +// method returns early +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method executes its final steps +// +// All three conditions have the same effect on the value of `result`. +assert.sameValue(result, undefined, 'normal completion value'); + +// The contents of the ArrayBuffer are not guaranteed by the host-defined +// abstract operation, so they are not asserted in this test. diff --git a/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink.js b/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink.js new file mode 100644 index 0000000000..d1b8005035 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/resize-shrink.js @@ -0,0 +1,77 @@ +// 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-arraybuffer.prototype.resize +description: Behavior when attempting to shrink a resizable array buffer +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. Let newByteLength be ? ToIntegerOrInfinity(newLength). + 6. If newByteLength < 0 or newByteLength > O.[[ArrayBufferMaxByteLength]], + throw a RangeError exception. + 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength). + [...] + + HostResizeArrayBuffer ( buffer, newByteLength ) + + The implementation of HostResizeArrayBuffer must conform to the following + requirements: + + - The abstract operation does not detach buffer. + - The abstract operation may complete normally or abruptly. + - If the abstract operation completes normally with handled, + buffer.[[ArrayBufferByteLength]] is newByteLength. + - The return value is either handled or unhandled. +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(4, {maxByteLength: 4}); +var caught = false; +var result; + +// 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 ab.resize, 'function'); + +try { + result = ab.resize(3); +} catch (_) { + caught = true; +} + +try { + ab.slice(); +} catch (_) { + throw new Test262Error('The ArrayBuffer under test was detached'); +} + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation and conforms to the +// invarient regarding [[ArrayBufferByteLength]] +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method updates [[ArrayBufferByteLength]] +// +// The final two conditions are indistinguishable. +assert(caught || ab.byteLength === 3, 'byteLength'); + +// One of the following three conditions must be met: +// +// - HostResizeArrayBuffer returns an abrupt completion +// - HostResizeArrayBuffer handles the resize operation, and the `resize` +// method returns early +// - HostResizeArrayBuffer does not handle the resize operation, and the +// `resize` method executes its final steps +// +// All three conditions have the same effect on the value of `result`. +assert.sameValue(result, undefined, 'normal completion value'); + +// The contents of the ArrayBuffer are not guaranteed by the host-defined +// abstract operation, so they are not asserted in this test. diff --git a/test/built-ins/ArrayBuffer/prototype/resize/this-is-detached.js b/test/built-ins/ArrayBuffer/prototype/resize/this-is-detached.js new file mode 100644 index 0000000000..7cea65cb82 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/this-is-detached.js @@ -0,0 +1,27 @@ +// 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-arraybuffer.prototype.resize +description: > + Throws a TypeError if `this` does not have an [[ArrayBufferData]] internal slot. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + [...] +includes: [detachArrayBuffer.js] +features: [resizable-arraybuffer] +---*/ + +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +var ab = new ArrayBuffer(1); + +$DETACHBUFFER(ab); + +assert.throws(TypeError, function() { + ab.resize(); +}); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-arraybuffer-object.js b/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-arraybuffer-object.js new file mode 100644 index 0000000000..e259f931e0 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-arraybuffer-object.js @@ -0,0 +1,28 @@ +// 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-arraybuffer.prototype.resize +description: > + Throws a TypeError if `this` does not have an [[ArrayBufferData]] internal slot. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + [...] +features: [resizable-arraybuffer] +---*/ + +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize(); +}, '`this` value is the ArrayBuffer prototype'); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call({}); +}, '`this` value is an object'); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call([]); +}, '`this` value is an array'); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-object.js b/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-object.js new file mode 100644 index 0000000000..6274f8b808 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-object.js @@ -0,0 +1,44 @@ +// 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-arraybuffer.prototype.resize +description: Throws a TypeError if `this` valueis not an object. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + [...] +features: [resizable-arraybuffer, Symbol, BigInt] +---*/ + +assert.sameValue(typeof ArrayBuffer.prototype.resize, "function"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(undefined); +}, "`this` value is undefined"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(null); +}, "`this` value is null"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(true); +}, "`this` value is Boolean"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(""); +}, "`this` value is String"); + +var symbol = Symbol(); +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(symbol); +}, "`this` value is Symbol"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(1); +}, "`this` value is Number"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(1n); +}, "`this` value is bigint"); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-resizable-arraybuffer-object.js b/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-resizable-arraybuffer-object.js new file mode 100644 index 0000000000..1674330b43 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/this-is-not-resizable-arraybuffer-object.js @@ -0,0 +1,38 @@ +// 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-arraybuffer.prototype.resize +description: > + Throws a TypeError if `this` does not have an [[ArrayBufferMaxByteLength]] internal slot. +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + [...] +features: [resizable-arraybuffer] +---*/ + +var ab; + +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +ab = new ArrayBuffer(4); +assert.throws(TypeError, function() { + ab.resize(0); +}, 'zero byte length'); + +ab = new ArrayBuffer(4); +assert.throws(TypeError, function() { + ab.resize(3); +}, 'smaller byte length'); + +ab = new ArrayBuffer(4); +assert.throws(TypeError, function() { + ab.resize(4); +}, 'same byte length'); + +ab = new ArrayBuffer(4); +assert.throws(TypeError, function() { + ab.resize(5); +}, 'larger byte length'); diff --git a/test/built-ins/ArrayBuffer/prototype/resize/this-is-sharedarraybuffer.js b/test/built-ins/ArrayBuffer/prototype/resize/this-is-sharedarraybuffer.js new file mode 100644 index 0000000000..ff91e1f7bf --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/resize/this-is-sharedarraybuffer.js @@ -0,0 +1,20 @@ +// 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-arraybuffer.prototype.resize +description: Throws a TypeError if `this` value is a SharedArrayBuffer +info: | + ArrayBuffer.prototype.resize ( newLength ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + [...] +features: [SharedArrayBuffer, resizable-arraybuffer] +---*/ + +var sab = new SharedArrayBuffer(0); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.resize.call(sab); +}, '`this` value cannot be a SharedArrayBuffer'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/descriptor.js b/test/built-ins/ArrayBuffer/prototype/transfer/descriptor.js new file mode 100644 index 0000000000..a2c5825dc9 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/descriptor.js @@ -0,0 +1,22 @@ +// 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-arraybuffer.prototype.transfer +description: > + ArrayBuffer.prototype.transfer has default data property attributes. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 17 ECMAScript Standard Built-in Objects: + 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] +features: [resizable-arraybuffer] +---*/ + +verifyProperty(ArrayBuffer.prototype, 'transfer', { + enumerable: false, + writable: true, + configurable: true +}); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/extensible.js b/test/built-ins/ArrayBuffer/prototype/transfer/extensible.js new file mode 100644 index 0000000000..7d416e8be9 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/extensible.js @@ -0,0 +1,15 @@ +// 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-arraybuffer.prototype.transfer +description: ArrayBuffer.prototype.transfer is extensible. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 17 ECMAScript Standard Built-in Objects: + Unless specified otherwise, the [[Extensible]] internal slot + of a built-in object initially has the value true. +features: [resizable-arraybuffer] +---*/ + +assert(Object.isExtensible(ArrayBuffer.prototype.transfer)); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-larger.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-larger.js new file mode 100644 index 0000000000..7f113d7121 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-larger.js @@ -0,0 +1,55 @@ +// 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-arraybuffer.prototype.transfer +description: Transfering from a fixed-size ArrayBuffer into a larger ArrayBuffer +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(5); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 5, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 5, 'dest.maxByteLength'); + +var destArray = new Uint8Array(dest); + +assert.sameValue(destArray[0], 1, 'destArray[0]'); +assert.sameValue(destArray[1], 2, 'destArray[1]'); +assert.sameValue(destArray[2], 3, 'destArray[2]'); +assert.sameValue(destArray[3], 4, 'destArray[3]'); +assert.sameValue(destArray[4], 0, 'destArray[4]'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-same.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-same.js new file mode 100644 index 0000000000..c8c00e0a8b --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-same.js @@ -0,0 +1,56 @@ +// 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-arraybuffer.prototype.transfer +description: | + Transfering from a fixed-size ArrayBuffer into an ArrayBuffer with the same + byte length +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 4, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 4, 'dest.maxByteLength'); + +var destArray = new Uint8Array(dest); + +assert.sameValue(destArray[0], 1, 'destArray[0]'); +assert.sameValue(destArray[1], 2, 'destArray[1]'); +assert.sameValue(destArray[2], 3, 'destArray[2]'); +assert.sameValue(destArray[3], 4, 'destArray[3]'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-smaller.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-smaller.js new file mode 100644 index 0000000000..db315d5ab0 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-smaller.js @@ -0,0 +1,53 @@ +// 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-arraybuffer.prototype.transfer +description: Transfering from a fixed-size ArrayBuffer into a smaller ArrayBuffer +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(3); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 3, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 3, 'dest.maxByteLength'); + +var destArray = new Uint8Array(dest); + +assert.sameValue(destArray[0], 1, 'destArray[0]'); +assert.sameValue(destArray[1], 2, 'destArray[1]'); +assert.sameValue(destArray[2], 3, 'destArray[2]'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-zero.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-zero.js new file mode 100644 index 0000000000..c6ca107426 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-fixed-to-zero.js @@ -0,0 +1,47 @@ +// 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-arraybuffer.prototype.transfer +description: Transfering from a fixed-size ArrayBuffer into a zero-length ArrayBuffer +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(0); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 0, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 0, 'dest.maxByteLength'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-larger.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-larger.js new file mode 100644 index 0000000000..4049bcf3f3 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-larger.js @@ -0,0 +1,55 @@ +// 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-arraybuffer.prototype.transfer +description: Transfering from a resizable ArrayBuffer into a larger ArrayBuffer +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4, { maxByteLength: 8 }); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(5); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 5, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 5, 'dest.maxByteLength'); + +var destArray = new Uint8Array(dest); + +assert.sameValue(destArray[0], 1, 'destArray[0]'); +assert.sameValue(destArray[1], 2, 'destArray[1]'); +assert.sameValue(destArray[2], 3, 'destArray[2]'); +assert.sameValue(destArray[3], 4, 'destArray[3]'); +assert.sameValue(destArray[4], 0, 'destArray[4]'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-same.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-same.js new file mode 100644 index 0000000000..07141b4ce9 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-same.js @@ -0,0 +1,56 @@ +// 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-arraybuffer.prototype.transfer +description: | + Transfering from a resizable ArrayBuffer into an ArrayBuffer with the same + byte length +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4, { maxByteLength: 8 }); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 4, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 4, 'dest.maxByteLength'); + +var destArray = new Uint8Array(dest); + +assert.sameValue(destArray[0], 1, 'destArray[0]'); +assert.sameValue(destArray[1], 2, 'destArray[1]'); +assert.sameValue(destArray[2], 3, 'destArray[2]'); +assert.sameValue(destArray[3], 4, 'destArray[3]'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-smaller.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-smaller.js new file mode 100644 index 0000000000..a6332276a5 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-smaller.js @@ -0,0 +1,53 @@ +// 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-arraybuffer.prototype.transfer +description: Transfering from a resizable ArrayBuffer into a smaller ArrayBuffer +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4, { maxByteLength: 8 }); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(3); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 3, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 3, 'dest.maxByteLength'); + +var destArray = new Uint8Array(dest); + +assert.sameValue(destArray[0], 1, 'destArray[0]'); +assert.sameValue(destArray[1], 2, 'destArray[1]'); +assert.sameValue(destArray[2], 3, 'destArray[2]'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-zero.js b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-zero.js new file mode 100644 index 0000000000..203d9c05aa --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/from-resizable-to-zero.js @@ -0,0 +1,47 @@ +// 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-arraybuffer.prototype.transfer +description: Transfering from a resizable ArrayBuffer into a zero-length ArrayBuffer +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + 8. NOTE: This method returns a fixed-length ArrayBuffer. + 9. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]). + 10. Let fromBlock be O.[[ArrayBufferData]]. + 11. Let toBlock be new.[[ArrayBufferData]]. + 12. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength). + 13. NOTE: Neither creation of the new Data Block nor copying from the old + Data Block are observable. Implementations reserve the right to implement + this method as a zero-copy move or a realloc. + 14. Perform ! DetachArrayBuffer(O). + 15. Return new. +features: [resizable-arraybuffer] +---*/ + +var source = new ArrayBuffer(4, { maxByteLength: 8 }); + +var sourceArray = new Uint8Array(source); +sourceArray[0] = 1; +sourceArray[1] = 2; +sourceArray[2] = 3; +sourceArray[3] = 4; + +var dest = source.transfer(0); + +assert.sameValue(source.byteLength, 0, 'source.byteLength'); +assert.throws(TypeError, function() { + source.slice(); +}); + +assert.sameValue(dest.resizable, false, 'dest.resizable'); +assert.sameValue(dest.byteLength, 0, 'dest.byteLength'); +assert.sameValue(dest.maxByteLength, 0, 'dest.maxByteLength'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/length.js b/test/built-ins/ArrayBuffer/prototype/transfer/length.js new file mode 100644 index 0000000000..20be10747b --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/length.js @@ -0,0 +1,30 @@ +// 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-arraybuffer.prototype.transfer +description: > + ArrayBuffer.prototype.transfer.length is 0. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 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] +features: [resizable-arraybuffer] +---*/ + +verifyProperty(ArrayBuffer.prototype.transfer, 'length', { + value: 0, + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/name.js b/test/built-ins/ArrayBuffer/prototype/transfer/name.js new file mode 100644 index 0000000000..2b74705b96 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/name.js @@ -0,0 +1,27 @@ +// 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-arraybuffer.prototype.transfer +description: > + ArrayBuffer.prototype.transfer.name is "transfer". +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 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 }. +features: [resizable-arraybuffer] +includes: [propertyHelper.js] +---*/ + +verifyProperty(ArrayBuffer.prototype.transfer, 'name', { + value: 'transfer', + enumerable: false, + wrtiable: false, + configurable: true +}); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/new-length-excessive.js b/test/built-ins/ArrayBuffer/prototype/transfer/new-length-excessive.js new file mode 100644 index 0000000000..a9f7439363 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/new-length-excessive.js @@ -0,0 +1,28 @@ +// 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-arraybuffer.prototype.transfer +description: > + Throws a RangeError the newLength value is too large to create a new + ArrayBuffer. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + 7. Let new be ? Construct(%ArrayBuffer%, « 𝔽(newByteLength) »). + [...] +features: [resizable-arraybuffer] +---*/ + +var ab = new ArrayBuffer(0); + +assert.throws(RangeError, function() { + // Math.pow(2, 53) = 9007199254740992 + ab.transfer(9007199254740992); +}); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/new-length-non-number.js b/test/built-ins/ArrayBuffer/prototype/transfer/new-length-non-number.js new file mode 100644 index 0000000000..bceb755110 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/new-length-non-number.js @@ -0,0 +1,39 @@ +// 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-arraybuffer.prototype.transfer +description: Throws a TypeError if provided length cannot be coerced to a number +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + 5. If newLength is undefined, let newByteLength be + O.[[ArrayBufferByteLength]]. + 6. Else, let newByteLength be ? ToIntegerOrInfinity(newLength). + [...] +features: [resizable-arraybuffer] +---*/ + +var log = []; +var newLength = { + toString: function() { + log.push('toString'); + return {}; + }, + valueOf: function() { + log.push('valueOf'); + return {}; + } +}; +var ab = new ArrayBuffer(0); + +assert.throws(TypeError, function() { + ab.transfer(newLength); +}); + +assert.sameValue(log.length, 2); +assert.sameValue(log[0], 'valueOf'); +assert.sameValue(log[1], 'toString'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/nonconstructor.js b/test/built-ins/ArrayBuffer/prototype/transfer/nonconstructor.js new file mode 100644 index 0000000000..f615b3d982 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/nonconstructor.js @@ -0,0 +1,24 @@ +// 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-arraybuffer.prototype.transfer +description: > + ArrayBuffer.prototype.transfer is not a constructor function. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 17 ECMAScript Standard Built-in 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. +---*/ + +assert.sameValue( + Object.prototype.hasOwnProperty.call(ArrayBuffer.prototype.transfer, 'prototype'), + false +); + +var arrayBuffer = new ArrayBuffer(8); +assert.throws(TypeError, function() { + new arrayBuffer.transfer(); +}); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/this-is-detached.js b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-detached.js new file mode 100644 index 0000000000..430f180780 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-detached.js @@ -0,0 +1,27 @@ +// 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-arraybuffer.prototype.transfer +description: > + Throws a TypeError if `this` does not have an [[ArrayBufferData]] internal slot. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + 4. If IsDetachedBuffer(O) is true, throw a TypeError exception. + [...] +includes: [detachArrayBuffer.js] +features: [resizable-arraybuffer] +---*/ + +assert.sameValue(typeof ArrayBuffer.prototype.transfer, 'function'); + +var ab = new ArrayBuffer(1); + +$DETACHBUFFER(ab); + +assert.throws(TypeError, function() { + ab.transfer(); +}); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/this-is-not-arraybuffer-object.js b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-not-arraybuffer-object.js new file mode 100644 index 0000000000..571bd1b208 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-not-arraybuffer-object.js @@ -0,0 +1,28 @@ +// 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-arraybuffer.prototype.transfer +description: > + Throws a TypeError if `this` does not have an [[ArrayBufferData]] internal slot. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + [...] +features: [resizable-arraybuffer] +---*/ + +assert.sameValue(typeof ArrayBuffer.prototype.transfer, 'function'); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer(); +}, '`this` value is the ArrayBuffer prototype'); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call({}); +}, '`this` value is an object'); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call([]); +}, '`this` value is an array'); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/this-is-not-object.js b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-not-object.js new file mode 100644 index 0000000000..cc5401f3f7 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-not-object.js @@ -0,0 +1,44 @@ +// 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-arraybuffer.prototype.transfer +description: Throws a TypeError if `this` valueis not an object. +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + [...] +features: [resizable-arraybuffer, Symbol, BigInt] +---*/ + +assert.sameValue(typeof ArrayBuffer.prototype.transfer, "function"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(undefined); +}, "`this` value is undefined"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(null); +}, "`this` value is null"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(true); +}, "`this` value is Boolean"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(""); +}, "`this` value is String"); + +var symbol = Symbol(); +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(symbol); +}, "`this` value is Symbol"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(1); +}, "`this` value is Number"); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(1n); +}, "`this` value is bigint"); diff --git a/test/built-ins/ArrayBuffer/prototype/transfer/this-is-sharedarraybuffer.js b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-sharedarraybuffer.js new file mode 100644 index 0000000000..81e1b14440 --- /dev/null +++ b/test/built-ins/ArrayBuffer/prototype/transfer/this-is-sharedarraybuffer.js @@ -0,0 +1,20 @@ +// 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-arraybuffer.prototype.transfer +description: Throws a TypeError if `this` value is a SharedArrayBuffer +info: | + ArrayBuffer.prototype.transfer ( [ newLength ] ) + + 1. Let O be the this value. + 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). + 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. + [...] +features: [SharedArrayBuffer, resizable-arraybuffer] +---*/ + +var sab = new SharedArrayBuffer(0); + +assert.throws(TypeError, function() { + ArrayBuffer.prototype.transfer.call(sab); +}, '`this` value cannot be a SharedArrayBuffer');