Add tests for TypedArrays slice

This commit is contained in:
Leonardo Balter 2016-04-26 11:51:35 -04:00 committed by Mike Pennisi
parent 022888be9e
commit bace781a5b
34 changed files with 1483 additions and 0 deletions

View File

@ -0,0 +1,37 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Use internal ArrayLength instead of getting a length property
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
3. Let len be the value of O's [[ArrayLength]] internal slot.
...
includes: [testTypedArray.js]
---*/
var getCalls = 0;
var desc = {
get: function getLen() {
getCalls++;
return 0;
}
};
Object.defineProperty(TypedArray.prototype, "length", desc);
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43]);
Object.defineProperty(TA.prototype, "length", desc);
Object.defineProperty(sample, "length", desc);
var result = sample.slice();
assert.sameValue(getCalls, 0, "ignores length properties");
assert.sameValue(result[0], 42);
assert.sameValue(result[1], 43);
assert.sameValue(result.hasOwnProperty(2), false);
});

View File

@ -0,0 +1,38 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Throws a TypeError buffer is detached on Get custom constructor. Using other
targetType
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
14. If SameValue(srcType, targetType) is false, then
a. Let n be 0.
b. Repeat, while k < final
...
ii. Let kValue be ? Get(O, Pk).
...
...
includes: [testTypedArray.js, detachArrayBuffer.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(1);
sample.constructor = {};
sample.constructor[Symbol.species] = function(count) {
var other = TA === Int8Array ? Int16Array : Int8Array;
$DETACHBUFFER(sample.buffer);
return new other(count);
};
assert.throws(TypeError, function() {
sample.slice();
}, "step 14.b.ii - ? Get(O, Pk), O has a detached buffer");
});

View File

@ -0,0 +1,34 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Throws a TypeError buffer is detached on Get custom constructor.
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
14. If SameValue(srcType, targetType) is false, then
...
15. Else if count > 0, then
a. Let srcBuffer be the value of O's [[ViewedArrayBuffer]] internal slot.
b. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
...
includes: [testTypedArray.js, detachArrayBuffer.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(1);
sample.constructor = {};
sample.constructor[Symbol.species] = function(count) {
$DETACHBUFFER(sample.buffer);
return new TA(count);
};
assert.throws(TypeError, function() {
sample.slice();
}, "step 15.b, IsDetachedBuffer(srcBuffer) is true");
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Throws a TypeError buffer is detached on Get constructor.
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
14. If SameValue(srcType, targetType) is false, then
...
15. Else if count > 0, then
a. Let srcBuffer be the value of O's [[ViewedArrayBuffer]] internal slot.
b. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
...
includes: [testTypedArray.js, detachArrayBuffer.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(1);
Object.defineProperty(sample, "constructor", {
get: function() {
$DETACHBUFFER(sample.buffer);
}
});
assert.throws(TypeError, function() {
sample.slice();
});
});

View File

@ -0,0 +1,43 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Custom @@species constructor throws if it returns an instance with a detached
buffer
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
...
includes: [testTypedArray.js, detachArrayBuffer.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
sample.constructor = {};
sample.constructor[Symbol.species] = function(count) {
var other = new TA(count);
$DETACHBUFFER(other.buffer);
return other;
};
assert.throws(TypeError, function() {
sample.slice();
});
});

View File

@ -0,0 +1,48 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Does not throw a TypeError if buffer is detached on custom constructor and
`k >= final`. Using other targetType.
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
14. If SameValue(srcType, targetType) is false, then
a. Let n be 0.
b. Repeat, while k < final
...
ii. Let kValue be ? Get(O, Pk).
...
...
16. Return A.
includes: [testTypedArray.js, detachArrayBuffer.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample, result, other;
var ctor = {};
ctor[Symbol.species] = function(count) {
other = TA === Int8Array ? Int16Array : Int8Array;
$DETACHBUFFER(sample.buffer);
return new other(count);
};
sample = new TA(0);
sample.constructor = ctor;
result = sample.slice();
assert.sameValue(result.length, 0, "#1: result.length");
assert.notSameValue(result.buffer, sample.buffer, "#1: creates a new buffer");
assert.sameValue(result.constructor, other, "#1: ctor");
sample = new TA(4);
sample.constructor = ctor;
result = sample.slice(1, 1);
assert.sameValue(result.length, 0, "#2: result.length");
assert.notSameValue(result.buffer, sample.buffer, "#2: creates a new buffer");
assert.sameValue(result.constructor, other, "#2: ctor");
});

View File

@ -0,0 +1,45 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Does not throw a TypeError if buffer is detached on custom constructor and
`k >= final`. Using same targetType.
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
14. If SameValue(srcType, targetType) is false, then
...
15. Else if count > 0, then
a. Let srcBuffer be the value of O's [[ViewedArrayBuffer]] internal slot.
b. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
...
includes: [testTypedArray.js, detachArrayBuffer.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample, result;
var ctor = {};
ctor[Symbol.species] = function(count) {
$DETACHBUFFER(sample.buffer);
return new TA(count);
};
sample = new TA(0);
sample.constructor = ctor;
result = sample.slice();
assert.sameValue(result.length, 0, "#1: result.length");
assert.notSameValue(result.buffer, sample.buffer, "#1: creates a new buffer");
assert.sameValue(result.constructor, TA, "#1: ctor");
sample = new TA(4);
sample.constructor = ctor;
result = sample.slice(1, 1);
assert.sameValue(result.length, 0, "#2: result.length");
assert.notSameValue(result.buffer, sample.buffer, "#2: creates a new buffer");
assert.sameValue(result.constructor, TA, "#2: ctor");
});

View File

@ -0,0 +1,28 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Infinity values on start and end
includes: [testTypedArray.js, compareArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
assert(
compareArray(sample.slice(-Infinity), [40, 41, 42, 43]),
"start == -Infinity"
);
assert(
compareArray(sample.slice(Infinity), []),
"start == Infinity"
);
assert(
compareArray(sample.slice(0, -Infinity), []),
"end == -Infinity"
);
assert(
compareArray(sample.slice(0, Infinity), [40, 41, 42, 43]),
"end == Infinity"
);
});

View File

@ -0,0 +1,30 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: -0 values on start and end
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
includes: [testTypedArray.js, compareArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
assert(
compareArray(sample.slice(-0), [40, 41, 42, 43]),
"start == -0"
);
assert(
compareArray(sample.slice(-0, 4), [40, 41, 42, 43]),
"start == -0, end == length"
);
assert(
compareArray(sample.slice(0, -0), []),
"start == 0, end == -0"
);
assert(
compareArray(sample.slice(-0, -0), []),
"start == -0, end == -0"
);
});

View File

@ -0,0 +1,21 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Result does not import own properties
info: >
22.2.3.24 %TypedArray%.prototype.slice( start , end )
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([41, 42, 43, 44]);
sample.foo = 42;
var result = sample.slice();
assert.sameValue(
result.hasOwnProperty("foo"),
false,
"does not import own property"
);
});

View File

@ -0,0 +1,52 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: slice may return a new instance with a smaller length
includes: [testTypedArray.js, compareArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
function testRes(result, expected, msg) {
assert(compareArray(result, expected), msg + ", result: [" + result + "]");
}
testRes(sample.slice(1), [41, 42, 43], "begin == 1");
testRes(sample.slice(2), [42, 43], "begin == 2");
testRes(sample.slice(3), [43], "begin == 3");
testRes(sample.slice(1, 4), [41, 42, 43], "begin == 1, end == length");
testRes(sample.slice(2, 4), [42, 43], "begin == 2, end == length");
testRes(sample.slice(3, 4), [43], "begin == 3, end == length");
testRes(sample.slice(0, 1), [40], "begin == 0, end == 1");
testRes(sample.slice(0, 2), [40, 41], "begin == 0, end == 2");
testRes(sample.slice(0, 3), [40, 41, 42], "begin == 0, end == 3");
testRes(sample.slice(-1), [43], "begin == -1");
testRes(sample.slice(-2), [42, 43], "begin == -2");
testRes(sample.slice(-3), [41, 42, 43], "begin == -3");
testRes(sample.slice(-1, 4), [43], "begin == -1, end == length");
testRes(sample.slice(-2, 4), [42, 43], "begin == -2, end == length");
testRes(sample.slice(-3, 4), [41, 42, 43], "begin == -3, end == length");
testRes(sample.slice(0, -1), [40, 41, 42], "begin == 0, end == -1");
testRes(sample.slice(0, -2), [40, 41], "begin == 0, end == -2");
testRes(sample.slice(0, -3), [40], "begin == 0, end == -3");
testRes(sample.slice(-0, -1), [40, 41, 42], "begin == -0, end == -1");
testRes(sample.slice(-0, -2), [40, 41], "begin == -0, end == -2");
testRes(sample.slice(-0, -3), [40], "begin == -0, end == -3");
testRes(sample.slice(-2, -1), [42], "length == 4, begin == -2, end == -1");
testRes(sample.slice(1, -1), [41, 42], "length == 4, begin == 1, end == -1");
testRes(sample.slice(1, -2), [41], "length == 4, begin == 1, end == -2");
testRes(sample.slice(2, -1), [42], "length == 4, begin == 2, end == -1");
testRes(sample.slice(-1, 5), [43], "begin == -1, end > length");
testRes(sample.slice(-2, 4), [42, 43], "begin == -2, end > length");
testRes(sample.slice(-3, 4), [41, 42, 43], "begin == -3, end > length");
});

View File

@ -0,0 +1,51 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: slice may return a new empty instance
includes: [testTypedArray.js, compareArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
function testRes(result, msg) {
assert.sameValue(result.length, 0, msg);
assert.sameValue(
result.hasOwnProperty(0),
false,
msg + " & result.hasOwnProperty(0) === false"
);
}
testRes(sample.slice(4), "begin == length");
testRes(sample.slice(5), "begin > length");
testRes(sample.slice(4, 4), "begin == length, end == length");
testRes(sample.slice(5, 4), "begin > length, end == length");
testRes(sample.slice(4, 5), "begin == length, end > length");
testRes(sample.slice(5, 5), "begin > length, end > length");
testRes(sample.slice(0, 0), "begin == 0, end == 0");
testRes(sample.slice(-0, -0), "begin == -0, end == -0");
testRes(sample.slice(1, 0), "begin > 0, end == 0");
testRes(sample.slice(-1, 0), "being < 0, end == 0");
testRes(sample.slice(2, 1), "begin > 0, begin < length, begin > end, end > 0");
testRes(sample.slice(2, 2), "begin > 0, begin < length, begin == end");
testRes(sample.slice(2, -2), "begin > 0, begin < length, end == -2");
testRes(sample.slice(-1, -1), "length = 4, begin == -1, end == -1");
testRes(sample.slice(-1, -2), "length = 4, begin == -1, end == -2");
testRes(sample.slice(-2, -2), "length = 4, begin == -2, end == -2");
testRes(sample.slice(0, -4), "begin == 0, end == -length");
testRes(sample.slice(-4, -4), "begin == -length, end == -length");
testRes(sample.slice(-5, -4), "begin < -length, end == -length");
testRes(sample.slice(0, -5), "begin == 0, end < -length");
testRes(sample.slice(-4, -5), "begin == -length, end < -length");
testRes(sample.slice(-5, -5), "begin < -length, end < -length");
});

View File

@ -0,0 +1,31 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: slice may return a new instance with the same length
includes: [testTypedArray.js, compareArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
function testRes(result, msg) {
assert.sameValue(result.length, 4, msg);
assert.sameValue(result[0], 40, msg + " & result[0] === 40");
assert.sameValue(result[1], 41, msg + " & result[1] === 41");
assert.sameValue(result[2], 42, msg + " & result[2] === 42");
assert.sameValue(result[3], 43, msg + " & result[3] === 43");
}
testRes(sample.slice(0), "begin == 0");
testRes(sample.slice(-4), "begin == -srcLength");
testRes(sample.slice(-5), "begin < -srcLength");
testRes(sample.slice(0, 4), "begin == 0, end == srcLength");
testRes(sample.slice(-4, 4), "begin == -srcLength, end == srcLength");
testRes(sample.slice(-5, 4), "begin < -srcLength, end == srcLength");
testRes(sample.slice(0, 5), "begin == 0, end > srcLength");
testRes(sample.slice(-4, 5), "begin == -srcLength, end > srcLength");
testRes(sample.slice(-5, 5), "begin < -srcLength, end > srcLength");
});

View File

@ -0,0 +1,25 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Return abrupt from ToInteger(end), end is symbol
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
6. If end is undefined, let relativeEnd be len; else let relativeEnd be ?
ToInteger(end).
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
var s = Symbol("1");
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.throws(TypeError, function() {
sample.slice(0, s);
});
});

View File

@ -0,0 +1,38 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Return abrupt from ToInteger(end)
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
6. If end is undefined, let relativeEnd be len; else let relativeEnd be ?
ToInteger(end).
...
includes: [testTypedArray.js]
---*/
var o1 = {
valueOf: function() {
throw new Test262Error();
}
};
var o2 = {
toString: function() {
throw new Test262Error();
}
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.throws(Test262Error, function() {
sample.slice(0, o1);
});
assert.throws(Test262Error, function() {
sample.slice(0, o2);
});
});

View File

@ -0,0 +1,24 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Return abrupt from ToInteger(start), start is symbol
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
4. Let relativeStart be ? ToInteger(start).
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
var s = Symbol("1");
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.throws(TypeError, function() {
sample.slice(s);
});
});

View File

@ -0,0 +1,37 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Return abrupt from ToInteger(start)
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
4. Let relativeStart be ? ToInteger(start).
...
includes: [testTypedArray.js]
---*/
var o1 = {
valueOf: function() {
throw new Test262Error();
}
};
var o2 = {
toString: function() {
throw new Test262Error();
}
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.throws(Test262Error, function() {
sample.slice(o1);
});
assert.throws(Test262Error, function() {
sample.slice(o2);
});
});

View File

@ -0,0 +1,47 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Perform regular set if target's uses a different element type
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
10. Let srcName be the String value of O's [[TypedArrayName]] internal slot.
11. Let srcType be the String value of the Element Type value in Table 50 for
srcName.
12. Let targetName be the String value of A's [[TypedArrayName]] internal
slot.
13. Let targetType be the String value of the Element Type value in Table 50
for targetName.
14. If SameValue(srcType, targetType) is false, then
a. Let n be 0.
b. Repeat, while k < final
i. Let Pk be ! ToString(k).
ii. Let kValue be ? Get(O, Pk).
iii. Perform ? Set(A, ! ToString(n), kValue, true).
iv. Increase k by 1.
v. Increase n by 1.
...
16. Return A
includes: [testTypedArray.js, compareArray.js]
features: [Symbol.species]
---*/
var arr = [42, 43, 44];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
var other = TA === Int8Array ? Uint8Array : Int8Array;
sample.constructor = {};
sample.constructor[Symbol.species] = other;
var result = sample.slice();
assert(compareArray(result, arr), "values are set");
assert.notSameValue(result.buffer, sample.buffer, "creates a new buffer");
assert.sameValue(result.constructor, other, "used the custom ctor");
});

View File

@ -0,0 +1,40 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: Return abrupt from SpeciesConstructor's get Constructor
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
Object.defineProperty(sample, "constructor", {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
sample.slice();
});
});

View File

@ -0,0 +1,61 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: get inherited constructor on SpeciesConstructor
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
var calls = 0;
var result;
Object.defineProperty(TA.prototype, "constructor", {
get: function() {
calls++;
}
});
result = sample.slice();
assert.sameValue(calls, 1, "called custom ctor get accessor once");
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"use defaultCtor on an undefined return - getPrototypeOf check"
);
assert.sameValue(
result.constructor,
undefined,
"used defaultCtor but still checks the inherited .constructor"
);
calls = 6;
result.constructor;
assert.sameValue(
calls,
7,
"result.constructor triggers the inherited accessor property"
);
});

View File

@ -0,0 +1,63 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Throws if O.constructor returns a non-Object and non-undefined value
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
4. If Type(C) is not Object, throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
sample.constructor = 42;
assert.throws(TypeError, function() {
sample.slice();
}, "42");
sample.constructor = "1";
assert.throws(TypeError, function() {
sample.slice();
}, "string");
sample.constructor = null;
assert.throws(TypeError, function() {
sample.slice();
}, "null");
sample.constructor = NaN;
assert.throws(TypeError, function() {
sample.slice();
}, "NaN");
sample.constructor = false;
assert.throws(TypeError, function() {
sample.slice();
}, "false");
sample.constructor = Symbol("1");
assert.throws(TypeError, function() {
sample.slice();
}, "symbol");
});

View File

@ -0,0 +1,53 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: get constructor on SpeciesConstructor
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
var calls = 0;
var result;
Object.defineProperty(sample, "constructor", {
get: function() {
calls++;
}
});
result = sample.slice();
assert.sameValue(calls, 1, "called custom ctor get accessor once");
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"use defaultCtor on an undefined return - getPrototypeOf check"
);
assert.sameValue(
result.constructor,
TA,
"use defaultCtor on an undefined return - .constructor check"
);
});

View File

@ -0,0 +1,45 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Returns abrupt from get @@species on found constructor
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
...
5. Let S be ? Get(C, @@species).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
sample.constructor = {};
Object.defineProperty(sample.constructor, Symbol.species, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
sample.slice();
});
});

View File

@ -0,0 +1,59 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Verify arguments on custom @@species construct call
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
3. If argumentList is a List of a single Number, then
...
4. Return newTypedArray.
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42]);
var result, ctorThis;
sample.constructor = {};
sample.constructor[Symbol.species] = function(count) {
result = arguments;
ctorThis = this;
return new TA(count);
};
sample.slice(1);
assert.sameValue(result.length, 1, "called with 1 arguments");
assert.sameValue(result[0], 2, "[0] is the new length count");
assert(
ctorThis instanceof sample.constructor[Symbol.species],
"`this` value in the @@species fn is an instance of the function itself"
);
});

View File

@ -0,0 +1,41 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Throws a TypeError if new typedArray's length < count
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
4. Return ? TypedArrayCreate(constructor, argumentList).
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
...
3. If argumentList is a List of a single Number, then
a. If the value of newTypedArray's [[ArrayLength]] internal slot <
argumentList[0], throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
sample.constructor = {};
sample.constructor[Symbol.species] = function() {
return new TA();
};
assert.throws(TypeError, function() {
sample.slice();
});
});

View File

@ -0,0 +1,46 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Does not throw a TypeError if new typedArray's length >= count
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
4. Return ? TypedArrayCreate(constructor, argumentList).
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
...
3. If argumentList is a List of a single Number, then
a. If the value of newTypedArray's [[ArrayLength]] internal slot <
argumentList[0], throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
var customCount, result;
sample.constructor = {};
sample.constructor[Symbol.species] = function() {
return new TA(customCount);
};
customCount = 2;
result = sample.slice();
assert.sameValue(result.length, customCount, "length == count");
customCount = 5;
result = sample.slice();
assert.sameValue(result.length, customCount, "length > count");
});

View File

@ -0,0 +1,53 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Custom @@species constructor may return a totally different TypedArray
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
3. If argumentList is a List of a single Number, then
...
4. Return newTypedArray.
includes: [testTypedArray.js, compareArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40]);
var other = new Int8Array([1, 0, 1]);
var result;
sample.constructor = {};
sample.constructor[Symbol.species] = function() {
return other;
};
result = sample.slice(0, 0);
assert.sameValue(result, other, "returned another typedarray");
assert(compareArray(result, [1, 0, 1]), "the returned object is preserved");
});

View File

@ -0,0 +1,47 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Custom @@species constructor throws if it does not return a compatible object
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
var ctor = function() {};
sample.constructor = {};
sample.constructor[Symbol.species] = ctor;
assert.throws(TypeError, function() {
sample.slice();
});
});

View File

@ -0,0 +1,54 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Use custom @@species constructor if available
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
3. If argumentList is a List of a single Number, then
...
4. Return newTypedArray.
includes: [testTypedArray.js, compareArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42]);
var calls = 0;
var result;
sample.constructor = {};
sample.constructor[Symbol.species] = function(count) {
calls++;
return new TA(count);
};
result = sample.slice(1);
assert.sameValue(calls, 1, "ctor called once");
assert(compareArray(result, [41, 42]), "expected object");
});

View File

@ -0,0 +1,66 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Throws if returned @@species is not a constructor, null or undefined.
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
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.
8. Throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
sample.constructor = {};
sample.constructor[Symbol.species] = 0;
assert.throws(TypeError, function() {
sample.slice();
}, "0");
sample.constructor[Symbol.species] = "string";
assert.throws(TypeError, function() {
sample.slice();
}, "string");
sample.constructor[Symbol.species] = {};
assert.throws(TypeError, function() {
sample.slice();
}, "{}");
sample.constructor[Symbol.species] = NaN;
assert.throws(TypeError, function() {
sample.slice();
}, "NaN");
sample.constructor[Symbol.species] = false;
assert.throws(TypeError, function() {
sample.slice();
}, "false");
sample.constructor[Symbol.species] = true;
assert.throws(TypeError, function() {
sample.slice();
}, "true");
});

View File

@ -0,0 +1,54 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
Use defaultConstructor if @@species is either undefined or null
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
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(2);
var result;
sample.constructor = {};
result = sample.slice();
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"undefined @@species - prototype check "
);
assert.sameValue(result.constructor, TA, "undefined @@species - ctor check");
sample.constructor[Symbol.species] = null;
result = sample.slice();
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"null @@species - prototype check "
);
assert.sameValue(result.constructor, TA, "null @@species - ctor check");
});

View File

@ -0,0 +1,46 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: >
get @@species from found constructor
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
9. Let A be ? TypedArraySpeciesCreate(O, « count »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
...
5. Let S be ? Get(C, @@species).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
var calls = 0;
sample.constructor = {};
Object.defineProperty(sample.constructor, Symbol.species, {
get: function() {
calls++;
}
});
sample.slice();
assert.sameValue(calls, 1);
});

View File

@ -0,0 +1,47 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: ToInteger(end)
info: >
22.2.3.24 %TypedArray%.prototype.slice( start , end )
...
6. If end is undefined, let relativeEnd be len; else let relativeEnd be ?
ToInteger(end).
...
includes: [testTypedArray.js, compareArray.js]
---*/
var obj = {
valueOf: function() {
return 2;
}
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
assert(compareArray(sample.slice(0, false), []), "false");
assert(compareArray(sample.slice(0, true), [40]), "true");
assert(compareArray(sample.slice(0, NaN), []), "NaN");
assert(compareArray(sample.slice(0, null), []), "null");
assert(compareArray(sample.slice(0, undefined), [40, 41, 42, 43]), "undefined");
assert(compareArray(sample.slice(0, 0.6), []), "0.6");
assert(compareArray(sample.slice(0, 1.1), [40]), "1.1");
assert(compareArray(sample.slice(0, 1.5), [40]), "1.5");
assert(compareArray(sample.slice(0, -0.6), []), "-0.6");
assert(compareArray(sample.slice(0, -1.1), [40, 41, 42]), "-1.1");
assert(compareArray(sample.slice(0, -1.5), [40, 41, 42]), "-1.5");
assert(compareArray(sample.slice(0, "3"), [40, 41, 42]), "string");
assert(
compareArray(
sample.slice(0, obj),
[40, 41]
),
"object"
);
});

View File

@ -0,0 +1,47 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.slice
description: ToInteger(begin)
info: >
22.2.3.24 %TypedArray%.prototype.slice ( start, end )
...
4. Let relativeStart be ? ToInteger(start).
...
includes: [testTypedArray.js, compareArray.js]
---*/
var obj = {
valueOf: function() {
return 2;
}
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
assert(compareArray(sample.slice(false), [40, 41, 42, 43]), "false");
assert(compareArray(sample.slice(true), [41, 42, 43]), "true");
assert(compareArray(sample.slice(NaN), [40, 41, 42, 43]), "NaN");
assert(compareArray(sample.slice(null), [40, 41, 42, 43]), "null");
assert(compareArray(sample.slice(undefined), [40, 41, 42, 43]), "undefined");
assert(compareArray(sample.slice(1.1), [41, 42, 43]), "1.1");
assert(compareArray(sample.slice(1.5), [41, 42, 43]), "1.5");
assert(compareArray(sample.slice(0.6), [40, 41, 42, 43]), "0.6");
assert(compareArray(sample.slice(-1.5), [43]), "-1.5");
assert(compareArray(sample.slice(-1.1), [43]), "-1.1");
assert(compareArray(sample.slice(-0.6), [40, 41, 42, 43]), "-0.6");
assert(compareArray(sample.slice("3"), [43]), "string");
assert(
compareArray(
sample.slice(obj),
[42, 43]
),
"object"
);
});