mirror of
https://github.com/tc39/test262.git
synced 2025-05-03 14:30:27 +02:00
* [javascriptcore-test262-automation] changes from git@github.com:WebKit/webkit.git at sha 949e26452cfa153a7f4afe593da97e2fe9e1b706 on Tue Jul 03 2018 14:35:15 GMT-0400 (Eastern Daylight Time)
84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
C = class extends Array { }
|
|
N = class { }
|
|
N[Symbol.species] = function() { throw "this should never be called"; }
|
|
|
|
function id(x) { return x; }
|
|
|
|
testFunctions = [
|
|
[Array.prototype.concat, []],
|
|
[Array.prototype.slice, [1,2]],
|
|
[Array.prototype.splice, []],
|
|
[Array.prototype.splice, [0,1]],
|
|
[Array.prototype.map, [id]],
|
|
[Array.prototype.filter, [id]]
|
|
];
|
|
|
|
objProp = Object.defineProperty;
|
|
|
|
function funcThrows(func, args) {
|
|
try {
|
|
func.call(...args)
|
|
return false;
|
|
} catch (e) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function test(testData) {
|
|
"use strict";
|
|
let [protoFunction, args] = testData;
|
|
let foo = new C(10);
|
|
let n = new N();
|
|
|
|
// Test non-array ignores constructor.
|
|
objProp(n, "constructor", { value: C });
|
|
let bar = protoFunction.call(n, ...args);
|
|
if (!(bar instanceof Array) || bar instanceof N || bar instanceof C)
|
|
throw Error();
|
|
|
|
objProp(foo, "constructor", { value: null });
|
|
if (!funcThrows(protoFunction, [foo, ...args]))
|
|
throw "didn't throw";
|
|
|
|
// Test array defaults cases.
|
|
foo = new C(10);
|
|
|
|
objProp(C, Symbol.species, { value: undefined, writable: true});
|
|
bar = protoFunction.call(foo, ...args);
|
|
if (!(bar instanceof Array) || bar instanceof C)
|
|
throw Error();
|
|
|
|
C[Symbol.species] = null;
|
|
bar = protoFunction.call(foo, ...args);
|
|
if (!(bar instanceof Array) || bar instanceof C)
|
|
throw Error();
|
|
|
|
// Test species is custom constructor.
|
|
let called = false;
|
|
function species(...args) {
|
|
called = true;
|
|
return new C(...args);
|
|
}
|
|
|
|
C[Symbol.species] = species;
|
|
bar = protoFunction.call(foo, ...args);
|
|
|
|
if (!(bar instanceof Array) || !(bar instanceof C) || !called)
|
|
throw Error("failed on " + protoFunction);
|
|
|
|
function speciesThrows() {
|
|
throw Error();
|
|
}
|
|
|
|
C[Symbol.species] = speciesThrows;
|
|
if (!funcThrows(protoFunction, [foo, ...args]))
|
|
throw "didn't throw";
|
|
|
|
C[Symbol.species] = true;
|
|
if (!funcThrows(protoFunction, [foo, ...args]))
|
|
throw "didn't throw";
|
|
|
|
}
|
|
|
|
testFunctions.forEach(test);
|