Add tests for ArrayBuffer.prototype.transferToFixedLength

See https://tc39.es/proposal-arraybuffer-transfer/
This commit is contained in:
Shu-yu Guo 2023-03-14 17:03:53 -07:00 committed by Jordan Harband
parent 9964fa962a
commit 6fecc44859
19 changed files with 560 additions and 0 deletions

View File

@ -0,0 +1,23 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: >
ArrayBuffer.prototype.transferToFixedLength has default data property
attributes.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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, arraybuffer-transfer]
---*/
verifyProperty(ArrayBuffer.prototype, 'transferToFixedLength', {
enumerable: false,
writable: true,
configurable: true
});

View File

@ -0,0 +1,15 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: ArrayBuffer.prototype.transferToFixedLength is extensible.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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, arraybuffer-transfer]
---*/
assert(Object.isExtensible(ArrayBuffer.prototype.transferToFixedLength));

View File

@ -0,0 +1,34 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Transfering from a fixed-size ArrayBuffer into a larger ArrayBuffer
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength(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]');

View File

@ -0,0 +1,35 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: |
Transfering from a fixed-size ArrayBuffer into an ArrayBuffer with the same
byte length
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength();
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]');

View File

@ -0,0 +1,32 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Transfering from a fixed-size ArrayBuffer into a smaller ArrayBuffer
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength(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]');

View File

@ -0,0 +1,26 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Transfering from a fixed-size ArrayBuffer into a zero-length ArrayBuffer
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength(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');

View File

@ -0,0 +1,34 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Transfering from a resizable ArrayBuffer into a larger ArrayBuffer
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength(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]');

View File

@ -0,0 +1,35 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: |
Transfering from a resizable ArrayBuffer into an ArrayBuffer with the same
byte length
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength();
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]');

View File

@ -0,0 +1,32 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Transfering from a resizable ArrayBuffer into a smaller ArrayBuffer
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength(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]');

View File

@ -0,0 +1,26 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Transfering from a resizable ArrayBuffer into a zero-length ArrayBuffer
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
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.transferToFixedLength(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');

View File

@ -0,0 +1,30 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: >
ArrayBuffer.prototype.transferToFixedLength.length is 0.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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, arraybuffer-transfer]
---*/
verifyProperty(ArrayBuffer.prototype.transferToFixedLength, 'length', {
value: 0,
enumerable: false,
writable: false,
configurable: true
});

View File

@ -0,0 +1,27 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: >
ArrayBuffer.prototype.transferToFixedLength.name is "transfer".
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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, arraybuffer-transfer]
includes: [propertyHelper.js]
---*/
verifyProperty(ArrayBuffer.prototype.transferToFixedLength, 'name', {
value: 'transferToFixedLength',
enumerable: false,
wrtiable: false,
configurable: true
});

View File

@ -0,0 +1,28 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: >
Throws a RangeError the newLength value is too large to create a new
ArrayBuffer.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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, arraybuffer-transfer]
---*/
var ab = new ArrayBuffer(0);
assert.throws(RangeError, function() {
// Math.pow(2, 53) = 9007199254740992
ab.transferToFixedLength(9007199254740992);
});

View File

@ -0,0 +1,39 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Throws a TypeError if provided length cannot be coerced to a number
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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, arraybuffer-transfer]
---*/
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.transferToFixedLength(newLength);
});
assert.sameValue(log.length, 2);
assert.sameValue(log[0], 'valueOf');
assert.sameValue(log[1], 'toString');

View File

@ -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.transfertofixedlength
description: >
ArrayBuffer.prototype.transferToFixedLength is not a constructor function.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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.
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
assert.sameValue(
Object.prototype.hasOwnProperty.call(ArrayBuffer.prototype.transferToFixedLength, 'prototype'),
false
);
var arrayBuffer = new ArrayBuffer(8);
assert.throws(TypeError, function() {
new arrayBuffer.transfer();
});

View File

@ -0,0 +1,27 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: >
Throws a TypeError if `this` does not have an [[ArrayBufferData]] internal slot.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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, arraybuffer-transfer]
---*/
assert.sameValue(typeof ArrayBuffer.prototype.transferToFixedLength, 'function');
var ab = new ArrayBuffer(1);
$DETACHBUFFER(ab);
assert.throws(TypeError, function() {
ab.transferToFixedLength();
});

View File

@ -0,0 +1,28 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: >
Throws a TypeError if `this` does not have an [[ArrayBufferData]] internal slot.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ newLength ] )
1. Let O be the this value.
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
[...]
features: [resizable-arraybuffer, arraybuffer-transfer]
---*/
assert.sameValue(typeof ArrayBuffer.prototype.transferToFixedLength, 'function');
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength();
}, '`this` value is the ArrayBuffer prototype');
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call({});
}, '`this` value is an object');
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call([]);
}, '`this` value is an array');

View File

@ -0,0 +1,44 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Throws a TypeError if `this` valueis not an object.
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ newLength ] )
1. Let O be the this value.
2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
[...]
features: [resizable-arraybuffer, Symbol, BigInt]
---*/
assert.sameValue(typeof ArrayBuffer.prototype.transferToFixedLength, "function");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call(undefined);
}, "`this` value is undefined");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call(null);
}, "`this` value is null");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call(true);
}, "`this` value is Boolean");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call("");
}, "`this` value is String");
var symbol = Symbol();
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call(symbol);
}, "`this` value is Symbol");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call(1);
}, "`this` value is Number");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.transferToFixedLength.call(1n);
}, "`this` value is bigint");

View File

@ -0,0 +1,20 @@
// Copyright (C) 2023 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-arraybuffer.prototype.transfertofixedlength
description: Throws a TypeError if `this` value is a SharedArrayBuffer
info: |
ArrayBuffer.prototype.transferToFixedLength ( [ 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.transferToFixedLength.call(sab);
}, '`this` value cannot be a SharedArrayBuffer');