diff options
Diffstat (limited to 'js/src/tests/ecma_7/Destructuring')
9 files changed, 406 insertions, 0 deletions
diff --git a/js/src/tests/ecma_7/Destructuring/browser.js b/js/src/tests/ecma_7/Destructuring/browser.js new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/browser.js diff --git a/js/src/tests/ecma_7/Destructuring/duplicate-__proto__.js b/js/src/tests/ecma_7/Destructuring/duplicate-__proto__.js new file mode 100644 index 000000000..a8f76014e --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/duplicate-__proto__.js @@ -0,0 +1,54 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +// Destructuring assignment. +var a, b; +({__proto__: a, __proto__: b} = {}); +assertEq(a, Object.prototype); +assertEq(b, Object.prototype); + +// Destructuring binding with "var". +var {__proto__: a, __proto__: b} = {}; +assertEq(a, Object.prototype); +assertEq(b, Object.prototype); + +// Destructuring binding with "let". +{ + let {__proto__: a, __proto__: b} = {}; + assertEq(a, Object.prototype); + assertEq(b, Object.prototype); +} + +// Destructuring binding with "const". +{ + const {__proto__: a, __proto__: b} = {}; + assertEq(a, Object.prototype); + assertEq(b, Object.prototype); +} + +// Function parameters. +function f1({__proto__: a, __proto__: b}) { + assertEq(a, Object.prototype); + assertEq(b, Object.prototype); +} +f1({}); + +// Arrow function parameters. +var f2 = ({__proto__: a, __proto__: b}) => { + assertEq(a, Object.prototype); + assertEq(b, Object.prototype); +}; +f2({}); + +// Arrow function parameters with defaults (initially parsed as destructuring assignment). +var f3 = ({__proto__: a, __proto__: b} = {}) => { + assertEq(a, Object.prototype); + assertEq(b, Object.prototype); +}; +f3({}); + + +if (typeof reportCompare === "function") + reportCompare(0, 0); diff --git a/js/src/tests/ecma_7/Destructuring/rest-parameter-aray-iterator.js b/js/src/tests/ecma_7/Destructuring/rest-parameter-aray-iterator.js new file mode 100644 index 000000000..bf9643fd7 --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/rest-parameter-aray-iterator.js @@ -0,0 +1,40 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Destructuring rest arrays call the array iterator. This behaviour is +// observable when Array.prototype[Symbol.iterator] is overridden. + +const oldArrayIterator = Array.prototype[Symbol.iterator]; +try { + let callCount = 0; + Array.prototype[Symbol.iterator] = function() { + callCount += 1; + return oldArrayIterator.call(this); + }; + + // Array iterator called exactly once. + function arrayIterCalledOnce(...[]) { } + assertEq(callCount, 0); + arrayIterCalledOnce(); + assertEq(callCount, 1); + + // Array iterator not called before rest parameter. + callCount = 0; + function arrayIterNotCalledBeforeRest(t = assertEq(callCount, 0), ...[]) { } + assertEq(callCount, 0); + arrayIterNotCalledBeforeRest(); + assertEq(callCount, 1); + + // Array iterator called when rest parameter is processed. + callCount = 0; + function arrayIterCalledWhenDestructuring(...[t = assertEq(callCount, 1)]) { } + assertEq(callCount, 0); + arrayIterCalledWhenDestructuring(); + assertEq(callCount, 1); +} finally { + Array.prototype[Symbol.iterator] = oldArrayIterator; +} + +if (typeof reportCompare === "function") + reportCompare(0, 0); diff --git a/js/src/tests/ecma_7/Destructuring/rest-parameter-arguments.js b/js/src/tests/ecma_7/Destructuring/rest-parameter-arguments.js new file mode 100644 index 000000000..e87cdff33 --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/rest-parameter-arguments.js @@ -0,0 +1,101 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Ensure the |arguments| object works as expected when a destructuring rest +// parameter is present. + +// |arguments.length| with destructuring rest array. +function argsLengthEmptyRestArray(...[]) { + return arguments.length; +} +assertEq(argsLengthEmptyRestArray(), 0); +assertEq(argsLengthEmptyRestArray(10), 1); +assertEq(argsLengthEmptyRestArray(10, 20), 2); + +function argsLengthRestArray(...[a]) { + return arguments.length; +} +assertEq(argsLengthRestArray(), 0); +assertEq(argsLengthRestArray(10), 1); +assertEq(argsLengthRestArray(10, 20), 2); + +function argsLengthRestArrayWithDefault(...[a = 0]) { + return arguments.length; +} +assertEq(argsLengthRestArrayWithDefault(), 0); +assertEq(argsLengthRestArrayWithDefault(10), 1); +assertEq(argsLengthRestArrayWithDefault(10, 20), 2); + + +// |arguments.length| with destructuring rest object. +function argsLengthEmptyRestObject(...{}) { + return arguments.length; +} +assertEq(argsLengthEmptyRestObject(), 0); +assertEq(argsLengthEmptyRestObject(10), 1); +assertEq(argsLengthEmptyRestObject(10, 20), 2); + +function argsLengthRestObject(...{a}) { + return arguments.length; +} +assertEq(argsLengthRestObject(), 0); +assertEq(argsLengthRestObject(10), 1); +assertEq(argsLengthRestObject(10, 20), 2); + +function argsLengthRestObjectWithDefault(...{a = 0}) { + return arguments.length; +} +assertEq(argsLengthRestObjectWithDefault(), 0); +assertEq(argsLengthRestObjectWithDefault(10), 1); +assertEq(argsLengthRestObjectWithDefault(10, 20), 2); + + +// |arguments| access with destructuring rest array. +function argsAccessEmptyRestArray(...[]) { + return arguments[0]; +} +assertEq(argsAccessEmptyRestArray(), undefined); +assertEq(argsAccessEmptyRestArray(10), 10); +assertEq(argsAccessEmptyRestArray(10, 20), 10); + +function argsAccessRestArray(...[a]) { + return arguments[0]; +} +assertEq(argsAccessRestArray(), undefined); +assertEq(argsAccessRestArray(10), 10); +assertEq(argsAccessRestArray(10, 20), 10); + +function argsAccessRestArrayWithDefault(...[a = 0]) { + return arguments[0]; +} +assertEq(argsAccessRestArrayWithDefault(), undefined); +assertEq(argsAccessRestArrayWithDefault(10), 10); +assertEq(argsAccessRestArrayWithDefault(10, 20), 10); + + +// |arguments| access with destructuring rest object. +function argsAccessEmptyRestObject(...{}) { + return arguments[0]; +} +assertEq(argsAccessEmptyRestObject(), undefined); +assertEq(argsAccessEmptyRestObject(10), 10); +assertEq(argsAccessEmptyRestObject(10, 20), 10); + +function argsAccessRestObject(...{a}) { + return arguments[0]; +} +assertEq(argsAccessRestObject(), undefined); +assertEq(argsAccessRestObject(10), 10); +assertEq(argsAccessRestObject(10, 20), 10); + +function argsAccessRestObjectWithDefault(...{a = 0}) { + return arguments[0]; +} +assertEq(argsAccessRestObjectWithDefault(), undefined); +assertEq(argsAccessRestObjectWithDefault(10), 10); +assertEq(argsAccessRestObjectWithDefault(10, 20), 10); + + +if (typeof reportCompare === "function") + reportCompare(0, 0); diff --git a/js/src/tests/ecma_7/Destructuring/rest-parameter-function-length.js b/js/src/tests/ecma_7/Destructuring/rest-parameter-function-length.js new file mode 100644 index 000000000..5924e799a --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/rest-parameter-function-length.js @@ -0,0 +1,41 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Ensure function length is set correctly when a destructuring rest parameter +// is present. + +assertEq(function(...[]) {}.length, 0); +assertEq(function(...[a]) {}.length, 0); +assertEq(function(...[a = 0]) {}.length, 0); +assertEq(function(...{}) {}.length, 0); +assertEq(function(...{p: a}) {}.length, 0); +assertEq(function(...{p: a = 0}) {}.length, 0); +assertEq(function(...{a = 0}) {}.length, 0); + +assertEq(function(x, ...[]) {}.length, 1); +assertEq(function(x, ...[a]) {}.length, 1); +assertEq(function(x, ...[a = 0]) {}.length, 1); +assertEq(function(x, ...{}) {}.length, 1); +assertEq(function(x, ...{p: a}) {}.length, 1); +assertEq(function(x, ...{p: a = 0}) {}.length, 1); +assertEq(function(x, ...{a = 0}) {}.length, 1); + +assertEq(function(x, y, ...[]) {}.length, 2); +assertEq(function(x, y, ...[a]) {}.length, 2); +assertEq(function(x, y, ...[a = 0]) {}.length, 2); +assertEq(function(x, y, ...{}) {}.length, 2); +assertEq(function(x, y, ...{p: a}) {}.length, 2); +assertEq(function(x, y, ...{p: a = 0}) {}.length, 2); +assertEq(function(x, y, ...{a = 0}) {}.length, 2); + +assertEq(function(x, y = 0, ...[]) {}.length, 1); +assertEq(function(x, y = 0, ...[a]) {}.length, 1); +assertEq(function(x, y = 0, ...[a = 0]) {}.length, 1); +assertEq(function(x, y = 0, ...{}) {}.length, 1); +assertEq(function(x, y = 0, ...{p: a}) {}.length, 1); +assertEq(function(x, y = 0, ...{p: a = 0}) {}.length, 1); +assertEq(function(x, y = 0, ...{a = 0}) {}.length, 1); + +if (typeof reportCompare === "function") + reportCompare(0, 0); diff --git a/js/src/tests/ecma_7/Destructuring/rest-parameter-spread-call-optimization.js b/js/src/tests/ecma_7/Destructuring/rest-parameter-spread-call-optimization.js new file mode 100644 index 000000000..20f6a529d --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/rest-parameter-spread-call-optimization.js @@ -0,0 +1,29 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Ensure the spread-call optimization doesn't break when a destructuring rest +// parameter is used. + +function spreadTarget() { return arguments.length; } + +function spreadOpt(...[r]){ return spreadTarget(...r); } +assertEq(spreadOpt([]), 0); +assertEq(spreadOpt([10]), 1); +assertEq(spreadOpt([10, 20]), 2); +assertEq(spreadOpt([10, 20, 30]), 3); + +function spreadOpt2(...[...r]){ return spreadTarget(...r); } +assertEq(spreadOpt2(), 0); +assertEq(spreadOpt2(10), 1); +assertEq(spreadOpt2(10, 20), 2); +assertEq(spreadOpt2(10, 20, 30), 3); + +function spreadOpt3(r, ...[]){ return spreadTarget(...r); } +assertEq(spreadOpt3([]), 0); +assertEq(spreadOpt3([10]), 1); +assertEq(spreadOpt3([10, 20]), 2); +assertEq(spreadOpt3([10, 20, 30]), 3); + +if (typeof reportCompare === "function") + reportCompare(0, 0); diff --git a/js/src/tests/ecma_7/Destructuring/rest-parameter-syntax.js b/js/src/tests/ecma_7/Destructuring/rest-parameter-syntax.js new file mode 100644 index 000000000..a145d1a2d --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/rest-parameter-syntax.js @@ -0,0 +1,87 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +const bindingPatterns = [ + "[]", + "[a]", + "[a, b]", + "[a, ...b]", + "[...a]", + "[...[]]", + + "{}", + "{p: a}", + "{p: a = 0}", + "{p: {}}", + "{p: a, q: b}", + "{a}", + "{a, b}", + "{a = 0}", +]; + +const functions = [ + p => `function f(${p}) {}`, + p => `function* g(${p}) {}`, + p => `({m(${p}) {}});`, + p => `(class {m(${p}) {}});`, + p => `(${p}) => {};`, +]; + +for (let pattern of bindingPatterns) { + for (let fn of functions) { + // No leading parameters. + eval(fn(`...${pattern}`)); + + // Leading normal parameters. + eval(fn(`x, ...${pattern}`)); + eval(fn(`x, y, ...${pattern}`)); + + // Leading parameters with defaults. + eval(fn(`x = 0, ...${pattern}`)); + eval(fn(`x = 0, y = 0, ...${pattern}`)); + + // Leading array destructuring parameters. + eval(fn(`[], ...${pattern}`)); + eval(fn(`[x], ...${pattern}`)); + eval(fn(`[x = 0], ...${pattern}`)); + eval(fn(`[...x], ...${pattern}`)); + + // Leading object destructuring parameters. + eval(fn(`{}, ...${pattern}`)); + eval(fn(`{p: x}, ...${pattern}`)); + eval(fn(`{x}, ...${pattern}`)); + eval(fn(`{x = 0}, ...${pattern}`)); + + // Trailing parameters after rest parameter. + assertThrowsInstanceOf(() => eval(fn(`...${pattern},`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...${pattern}, x`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...${pattern}, x = 0`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...${pattern}, ...x`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...${pattern}, []`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...${pattern}, {}`)), SyntaxError); + + // Rest parameter with defaults. + assertThrowsInstanceOf(() => eval(fn(`...${pattern} = 0`)), SyntaxError); + } +} + +for (let fn of functions) { + // Missing name, incomplete patterns. + assertThrowsInstanceOf(() => eval(fn(`...`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...[`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...{`)), SyntaxError); + + // Invalid binding name. + assertThrowsInstanceOf(() => eval(fn(`...[0]`)), SyntaxError); + assertThrowsInstanceOf(() => eval(fn(`...[p.q]`)), SyntaxError); +} + +// Rest parameters aren't valid in getter/setter methods. +assertThrowsInstanceOf(() => eval(`({get p(...[]) {}})`), SyntaxError); +assertThrowsInstanceOf(() => eval(`({set p(...[]) {}})`), SyntaxError); + + +if (typeof reportCompare === "function") + reportCompare(0, 0); diff --git a/js/src/tests/ecma_7/Destructuring/rest-parameter.js b/js/src/tests/ecma_7/Destructuring/rest-parameter.js new file mode 100644 index 000000000..50d77f3cc --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/rest-parameter.js @@ -0,0 +1,54 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Simple functional test for destructuring rest parameters. + +function arrayRest(...[a, b]) { + return a + b; +} +assertEq(arrayRest(3, 7), 10); + + +function arrayRestWithDefault(...[a, b = 1]) { + return a + b; +} +assertEq(arrayRestWithDefault(3, 7), 10); +assertEq(arrayRestWithDefault(4), 5); +assertEq(arrayRestWithDefault(4, undefined), 5); + + +function objectRest(...{length: len}) { + return len; +} +assertEq(objectRest(), 0); +assertEq(objectRest(10), 1); +assertEq(objectRest(10, 20), 2); + + +function objectRestWithDefault(...{0: a, 1: b = 1}) { + return a + b; +} +assertEq(objectRestWithDefault(3, 7), 10); +assertEq(objectRestWithDefault(4), 5); +assertEq(objectRestWithDefault(4, undefined), 5); + + +function arrayRestWithNestedRest(...[...r]) { + return r.length; +} +assertEq(arrayRestWithNestedRest(), 0); +assertEq(arrayRestWithNestedRest(10), 1); +assertEq(arrayRestWithNestedRest(10, 20), 2); + + +function arrayRestTDZ(...[a = a]) { } +assertThrowsInstanceOf(() => arrayRestTDZ(), ReferenceError); + + +function objectRestTDZ(...{a = a}) { } +assertThrowsInstanceOf(() => objectRestTDZ(), ReferenceError); + + +if (typeof reportCompare === "function") + reportCompare(0, 0); diff --git a/js/src/tests/ecma_7/Destructuring/shell.js b/js/src/tests/ecma_7/Destructuring/shell.js new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/js/src/tests/ecma_7/Destructuring/shell.js |