Skip to content
Snippets Groups Projects
Commit ac7711e9 authored by Gorkem Yakin's avatar Gorkem Yakin
Browse files

Merge pull request #485 from bocoup/typedarray-constructor

Add tests for _TypedArray_ constructors
parents 5cb97c29 75952bee
No related branches found
No related tags found
No related merge requests found
Showing with 376 additions and 0 deletions
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Return abrupt from buffer.constructor.@@species.prototype
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
17. If SameValue(elementType, srcType) is true, then
a. Let data be ? CloneArrayBuffer(srcData, srcByteOffset).
...
24.1.1.4 CloneArrayBuffer ( srcBuffer, srcByteOffset [ , cloneConstructor ] )
...
2. If cloneConstructor is not present, then
a. Let cloneConstructor be ? SpeciesConstructor(srcBuffer, %ArrayBuffer%).
...
8. Let targetBuffer be ? AllocateArrayBuffer(cloneConstructor, cloneLength).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
6. If S is either undefined or null, return defaultConstructor.
7. If IsConstructor(S) is true, return S.
...
24.1.1.1 AllocateArrayBuffer ( constructor, byteLength )
...
1. Let obj be ? OrdinaryCreateFromConstructor(constructor,
"%ArrayBufferPrototype%", « [[ArrayBufferData]], [[ArrayBufferByteLength]] » )
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
var ctor = {};
sample.buffer.constructor = ctor;
ctor[Symbol.species] = function(){}.bind(null);
Object.defineProperty(ctor[Symbol.species], "prototype", {
get() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
new TA(sample);
});
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Return abrupt from getting typedArray argument's buffer.constructor.@@species
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
17. If SameValue(elementType, srcType) is true, then
a. Let data be ? CloneArrayBuffer(srcData, srcByteOffset).
...
24.1.1.4 CloneArrayBuffer ( srcBuffer, srcByteOffset [ , cloneConstructor ] )
...
2. If cloneConstructor is not present, then
a. Let cloneConstructor be ? SpeciesConstructor(srcBuffer, %ArrayBuffer%).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
var ctor = {};
sample.buffer.constructor = ctor;
Object.defineProperty(ctor, Symbol.species, {
get() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
new TA(sample);
});
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Use default ArrayBuffer constructor on undefined buffer.constructor.@@species
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
17. If SameValue(elementType, srcType) is true, then
a. Let data be ? CloneArrayBuffer(srcData, srcByteOffset).
...
24.1.1.4 CloneArrayBuffer ( srcBuffer, srcByteOffset [ , cloneConstructor ] )
...
2. If cloneConstructor is not present, then
a. Let cloneConstructor be ? SpeciesConstructor(srcBuffer, %ArrayBuffer%).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
6. If S is either undefined or null, return defaultConstructor.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(4);
var ctor = {}
sample.buffer.constructor = ctor;
ctor[Symbol.species] = undefined;
var typedArray = new TA(sample);
assert.sameValue(
Object.getPrototypeOf(typedArray.buffer),
ArrayBuffer.prototype,
"buffer ctor is not called when species is undefined"
);
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Return abrupt completion from typedArray argument's buffer.constructor's value
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
17. If SameValue(elementType, srcType) is true, then
a. Let data be ? CloneArrayBuffer(srcData, srcByteOffset).
...
24.1.1.4 CloneArrayBuffer ( srcBuffer, srcByteOffset [ , cloneConstructor ] )
...
2. If cloneConstructor is not present, then
a. Let cloneConstructor be ? SpeciesConstructor(srcBuffer, %ArrayBuffer%).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
2. Let C be ? Get(O, "constructor").
...
4. If Type(C) is not Object, throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
sample.buffer.constructor = 1;
assert.throws(TypeError, function() {
new TA(sample);
});
sample.buffer.constructor = true;
assert.throws(TypeError, function() {
new TA(sample);
});
sample.buffer.constructor = '';
assert.throws(TypeError, function() {
new TA(sample);
});
sample.buffer.constructor = null;
assert.throws(TypeError, function() {
new TA(sample);
});
var s = Symbol('1');
sample.buffer.constructor = s;
assert.throws(TypeError, function() {
new TA(sample);
});
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Same typedArray ctor argument returns a new cloned typedArray
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
17. If SameValue(elementType, srcType) is true, then
a. Let data be ? CloneArrayBuffer(srcData, srcByteOffset).
...
23. Return O.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(7);
var typedArray = new TA(sample);
assert.sameValue(typedArray.length, 7);
assert.notSameValue(typedArray, sample);
assert.notSameValue(typedArray.buffer, sample.buffer);
assert.sameValue(typedArray.constructor, TA);
assert.sameValue(Object.getPrototypeOf(typedArray), TA.prototype);
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Throws a TypeError if NewTarget is undefined.
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
2. If NewTarget is undefined, throw a TypeError exception.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var typedArray = new TA(4);
assert.throws(TypeError, function() {
TA(typedArray);
});
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Use prototype from new target if it's an Object
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
4. Let O be ? AllocateTypedArray(constructorName, NewTarget,
%TypedArrayPrototype%).
...
22.2.4.2.1 Runtime Semantics: AllocateTypedArray (constructorName, newTarget,
defaultProto [ , length ])
1. Let proto be ? GetPrototypeFromConstructor(newTarget, defaultProto).
2. Let obj be IntegerIndexedObjectCreate (proto, «[[ViewedArrayBuffer]],
[[TypedArrayName]], [[ByteLength]], [[ByteOffset]], [[ArrayLength]]» ).
...
9.4.5.7 IntegerIndexedObjectCreate (prototype, internalSlotsList)
...
10. Set the [[Prototype]] internal slot of A to prototype.
...
12. Return A.
features: [Reflect]
includes: [testTypedArray.js]
---*/
function newTarget() {}
var proto = {};
newTarget.prototype = proto;
var sample = new Int8Array(8);
testWithTypedArrayConstructors(function(TA) {
var ta = Reflect.construct(TA, [sample], newTarget);
assert.sameValue(ta.constructor, Object);
assert.sameValue(Object.getPrototypeOf(ta), proto);
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
id: sec-typedarray-typedarray
description: >
Use prototype from %TypedArray% if newTarget's prototype is not an Object
info: >
22.2.4.3 TypedArray ( typedArray )
This description applies only if the TypedArray function is called with at
least one argument and the Type of the first argument is Object and that
object has a [[TypedArrayName]] internal slot.
...
4. Let O be ? AllocateTypedArray(constructorName, NewTarget,
%TypedArrayPrototype%).
...
22.2.4.2.1 Runtime Semantics: AllocateTypedArray (constructorName, newTarget,
defaultProto [ , length ])
1. Let proto be ? GetPrototypeFromConstructor(newTarget, defaultProto).
2. Let obj be IntegerIndexedObjectCreate (proto, «[[ViewedArrayBuffer]],
[[TypedArrayName]], [[ByteLength]], [[ByteOffset]], [[ArrayLength]]» ).
...
9.4.5.7 IntegerIndexedObjectCreate (prototype, internalSlotsList)
...
10. Set the [[Prototype]] internal slot of A to prototype.
...
12. Return A.
includes: [testTypedArray.js]
---*/
function newTarget() {}
newTarget.prototype = null;
var sample = new Int8Array(8);
testWithTypedArrayConstructors(function(TA) {
var ta = Reflect.construct(TA, [sample], newTarget);
assert.sameValue(ta.constructor, TA);
assert.sameValue(Object.getPrototypeOf(ta), TA.prototype);
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment