mirror of
https://github.com/tc39/test262.git
synced 2025-07-21 13:04:39 +02:00
Resizable ArrayBuffer: TypedArray constructor (#3026)
* Add "feature" for "Resizable ArrayBuffer" proposal * Resizable ArrayBuffer: TypedArray constructor
This commit is contained in:
parent
884ed7f6f8
commit
13d7b79e90
@ -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-typedarray-buffer-byteoffset-length
|
||||||
|
description: >
|
||||||
|
Throws a RangeError for resizable ArrayBuffers when offset > byteLength
|
||||||
|
includes: [testTypedArray.js]
|
||||||
|
features: [TypedArray]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
testWithTypedArrayConstructors(function(TA) {
|
||||||
|
var BPE = TA.BYTES_PER_ELEMENT;
|
||||||
|
var buffer = new ArrayBuffer(BPE, {maxByteLength: BPE});
|
||||||
|
|
||||||
|
assert.throws(RangeError, function() {
|
||||||
|
new TA(buffer, BPE * 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(RangeError, function() {
|
||||||
|
new TA(buffer, BPE * 2, undefined);
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,80 @@
|
|||||||
|
// 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-typedarray
|
||||||
|
description: >
|
||||||
|
Error when a TypedArray is created from another TypedArray with a different
|
||||||
|
element-type and SpeciesConstructor causes the "source" array to go
|
||||||
|
out-of-bounds.
|
||||||
|
includes: [testTypedArray.js, compareArray.js]
|
||||||
|
features: [TypedArray, Symbol.species, 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 TargetCtor = TA !== Int32Array ? Int32Array : Uint32Array;
|
||||||
|
var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
|
||||||
|
var speciesConstructor = Object.defineProperty(function(){}.bind(), 'prototype', {
|
||||||
|
get: function() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var onGetSpecies;
|
||||||
|
ab.constructor = Object.defineProperty({}, Symbol.species, {
|
||||||
|
get: function() {
|
||||||
|
onGetSpecies();
|
||||||
|
return speciesConstructor;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var source = new TA(ab, BPE);
|
||||||
|
var expected = [10, 20, 30];
|
||||||
|
|
||||||
|
source[0] = 10;
|
||||||
|
source[1] = 20;
|
||||||
|
source[2] = 30;
|
||||||
|
|
||||||
|
onGetSpecies = function() {
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 5);
|
||||||
|
expected = [10, 20, 30, 0];
|
||||||
|
} catch (_) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert(compareArray(new TargetCtor(source), expected), 'following grow');
|
||||||
|
|
||||||
|
onGetSpecies = function() {
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 3);
|
||||||
|
expected = [10, 20];
|
||||||
|
} catch (_) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert(compareArray(new TargetCtor(source), expected), 'following shrink (within bounds)');
|
||||||
|
|
||||||
|
// `assert.throws` cannot be used in this case because the expected error
|
||||||
|
// is derived only after the constructor is invoked.
|
||||||
|
var expectedError;
|
||||||
|
var actualError;
|
||||||
|
onGetSpecies = function() {
|
||||||
|
try {
|
||||||
|
ab.resize(BPE);
|
||||||
|
expectedError = TypeError;
|
||||||
|
} catch (_) {
|
||||||
|
expectedError = Test262Error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
new TargetCtor(source);
|
||||||
|
throw new Test262Error('the operation completed successfully');
|
||||||
|
} catch (caught) {
|
||||||
|
actualError = caught;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.sameValue(actualError.constructor, expectedError);
|
||||||
|
});
|
@ -0,0 +1,80 @@
|
|||||||
|
// 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-typedarray
|
||||||
|
description: >
|
||||||
|
Error when a TypedArray is created from another TypedArray with the same
|
||||||
|
element-type and SpeciesConstructor causes the "source" array to go
|
||||||
|
out-of-bounds.
|
||||||
|
includes: [testTypedArray.js, compareArray.js]
|
||||||
|
features: [TypedArray, Symbol.species, 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 ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
|
||||||
|
var speciesConstructor = Object.defineProperty(function(){}.bind(), 'prototype', {
|
||||||
|
get: function() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var onGetSpecies;
|
||||||
|
ab.constructor = Object.defineProperty({}, Symbol.species, {
|
||||||
|
get: function() {
|
||||||
|
onGetSpecies();
|
||||||
|
return speciesConstructor;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var source = new TA(ab, BPE);
|
||||||
|
var expected = [10, 20, 30];
|
||||||
|
|
||||||
|
source[0] = 10;
|
||||||
|
source[1] = 20;
|
||||||
|
source[2] = 30;
|
||||||
|
|
||||||
|
onGetSpecies = function() {
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 5);
|
||||||
|
expected = [10, 20, 30, 0];
|
||||||
|
} catch (_) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.sameValue((new TA(source)).join(','), expected.join(','));
|
||||||
|
assert(compareArray(new TA(source), expected), 'following grow');
|
||||||
|
|
||||||
|
onGetSpecies = function() {
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 3);
|
||||||
|
expected = [10, 20];
|
||||||
|
} catch (_) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert(compareArray(new TA(source), expected), 'following shrink (within bounds)');
|
||||||
|
|
||||||
|
// `assert.throws` cannot be used in this case because the expected error
|
||||||
|
// is derived only after the constructor is invoked.
|
||||||
|
var expectedError;
|
||||||
|
var actualError;
|
||||||
|
onGetSpecies = function() {
|
||||||
|
try {
|
||||||
|
ab.resize(BPE);
|
||||||
|
expectedError = TypeError;
|
||||||
|
} catch (_) {
|
||||||
|
expectedError = Test262Error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
new TA(source);
|
||||||
|
throw new Test262Error('the operation completed successfully');
|
||||||
|
} catch (caught) {
|
||||||
|
actualError = caught;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.sameValue(actualError.constructor, expectedError);
|
||||||
|
});
|
@ -0,0 +1,54 @@
|
|||||||
|
// 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-integer-indexed-exotic-objects-hasproperty-p
|
||||||
|
description: returned keys reflect resized ArrayBuffer for a dynamically-sized TypedArray
|
||||||
|
includes: [testTypedArray.js]
|
||||||
|
features: [Reflect, 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");
|
||||||
|
|
||||||
|
function inspect(array) {
|
||||||
|
return [
|
||||||
|
Reflect.has(array, 0),
|
||||||
|
Reflect.has(array, 1),
|
||||||
|
Reflect.has(array, 2),
|
||||||
|
Reflect.has(array, 3),
|
||||||
|
Reflect.has(array, 4)
|
||||||
|
].join(",");
|
||||||
|
}
|
||||||
|
|
||||||
|
testWithTypedArrayConstructors(function(TA) {
|
||||||
|
var BPE = TA.BYTES_PER_ELEMENT;
|
||||||
|
var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
|
||||||
|
var array = new TA(ab, BPE);
|
||||||
|
var expected = "true,true,true,false,false";
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), expected, "initial");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 5);
|
||||||
|
expected = "true,true,true,true,false";
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), expected, "following grow");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 3);
|
||||||
|
expected = "true,true,false,false,false";
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), expected, "following shrink (within bounds)");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE);
|
||||||
|
expected = "false,false,false,false,false";
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), expected, "following shrink (out of bounds)");
|
||||||
|
});
|
@ -0,0 +1,54 @@
|
|||||||
|
// 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-integer-indexed-exotic-objects-hasproperty-p
|
||||||
|
description: returned keys reflect resized ArrayBuffer for a fixed-sized TypedArray
|
||||||
|
includes: [testTypedArray.js]
|
||||||
|
features: [Reflect, 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");
|
||||||
|
|
||||||
|
function inspect(array) {
|
||||||
|
return [
|
||||||
|
Reflect.has(array, 0),
|
||||||
|
Reflect.has(array, 1),
|
||||||
|
Reflect.has(array, 2),
|
||||||
|
Reflect.has(array, 3),
|
||||||
|
Reflect.has(array, 4)
|
||||||
|
].join(",");
|
||||||
|
}
|
||||||
|
|
||||||
|
testWithTypedArrayConstructors(function(TA) {
|
||||||
|
var BPE = TA.BYTES_PER_ELEMENT;
|
||||||
|
var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
|
||||||
|
var array = new TA(ab, BPE, 2);
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), "true,true,false,false,false", "initial");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 5);
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), "true,true,false,false,false", "following grow");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 3);
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), "true,true,false,false,false", "following shrink (within bounds)");
|
||||||
|
|
||||||
|
var expected;
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 2);
|
||||||
|
expected = "false,false,false,false,falsex";
|
||||||
|
} catch (_) {
|
||||||
|
expected = "true,true,false,false,false";
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.sameValue(inspect(array), expected, "following shrink (out of bounds)");
|
||||||
|
});
|
@ -0,0 +1,58 @@
|
|||||||
|
// 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-integer-indexed-exotic-objects-ownpropertykeys
|
||||||
|
description: returned keys reflect resized ArrayBuffer for a dynamically-sized TypedArray
|
||||||
|
info: |
|
||||||
|
9.4.5.6 [[OwnPropertyKeys]] ()
|
||||||
|
|
||||||
|
...
|
||||||
|
3. Let getBufferByteLength be !
|
||||||
|
MakeIdempotentArrayBufferByteLengthGetter(SeqCst).
|
||||||
|
4. Let len be IntegerIndexedObjectLength(O, getBufferByteLength).
|
||||||
|
5. For each integer i starting with 0 such that i < len, in ascending order,
|
||||||
|
a. Add ! ToString(i) as the last element of keys.
|
||||||
|
...
|
||||||
|
includes: [testTypedArray.js]
|
||||||
|
features: [Reflect, 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 ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
|
||||||
|
var array = new TA(ab, BPE);
|
||||||
|
var expected = "0,1,2";
|
||||||
|
|
||||||
|
assert.sameValue(Reflect.ownKeys(array).join(","), expected, "initial");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 5);
|
||||||
|
expected = "0,1,2,3";
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(Reflect.ownKeys(array).join(","), expected, "following grow");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 3);
|
||||||
|
expected = "0,1";
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Reflect.ownKeys(array).join(","), expected, "following shrink (within bounds)"
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE);
|
||||||
|
expected = "";
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Reflect.ownKeys(array).join(","), expected, "following shrink (out of bounds)"
|
||||||
|
);
|
||||||
|
});
|
@ -0,0 +1,58 @@
|
|||||||
|
// 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-integer-indexed-exotic-objects-ownpropertykeys
|
||||||
|
description: returned keys reflect resized ArrayBuffer for a fixed-sized TypedArray
|
||||||
|
info: |
|
||||||
|
9.4.5.6 [[OwnPropertyKeys]] ()
|
||||||
|
|
||||||
|
...
|
||||||
|
3. Let getBufferByteLength be !
|
||||||
|
MakeIdempotentArrayBufferByteLengthGetter(SeqCst).
|
||||||
|
4. Let len be IntegerIndexedObjectLength(O, getBufferByteLength).
|
||||||
|
5. For each integer i starting with 0 such that i < len, in ascending order,
|
||||||
|
a. Add ! ToString(i) as the last element of keys.
|
||||||
|
...
|
||||||
|
includes: [testTypedArray.js]
|
||||||
|
features: [Reflect, 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 ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5});
|
||||||
|
var array = new TA(ab, BPE, 2);
|
||||||
|
|
||||||
|
assert.sameValue(Reflect.ownKeys(array).join(","), "0,1", "initial");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 5);
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(Reflect.ownKeys(array).join(","), "0,1", "following grow");
|
||||||
|
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 3);
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Reflect.ownKeys(array).join(","), "0,1", "following shrink (within bounds)"
|
||||||
|
);
|
||||||
|
|
||||||
|
var expected;
|
||||||
|
try {
|
||||||
|
ab.resize(BPE * 2);
|
||||||
|
expected = "";
|
||||||
|
} catch (_) {
|
||||||
|
expected = "0,1";
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Reflect.ownKeys(array).join(","), expected, "following shrink (out of bounds)"
|
||||||
|
);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user