Debian Codegraph @

js/src/jit-test/tests/basic/destructuring-rest.js

raw
1
2 load(libdir + 'asserts.js');
3 load(libdir + 'eqArrayHelper.js');
4
5 assertThrowsInstanceOf(() => new Function('[...a, ,] = []'), SyntaxError, 'trailing elision');
6 assertThrowsInstanceOf(() => new Function('[a, ...b, c] = []'), SyntaxError, 'trailing param');
7 assertThrowsInstanceOf(() => new Function('[...a=b] = []'), SyntaxError, 'assignment expression');
8 assertThrowsInstanceOf(() => new Function('[...a()] = []'), SyntaxError, 'call expression');
9 assertThrowsInstanceOf(() => new Function('[...(a,b)] = []'), SyntaxError, 'comma expression');
10 assertThrowsInstanceOf(() => new Function('[...a++] = []'), SyntaxError, 'postfix expression');
11 assertThrowsInstanceOf(() => new Function('[...!a] = []'), SyntaxError, 'unary expression');
12 assertThrowsInstanceOf(() => new Function('[...a+b] = []'), SyntaxError, 'binary expression');
13 assertThrowsInstanceOf(() => new Function('var [...a.x] = []'), SyntaxError, 'lvalue expression in declaration');
14 assertThrowsInstanceOf(() => new Function('var [...(b)] = []'), SyntaxError);
15 assertThrowsInstanceOf(() => new Function('[...b,] = []'), SyntaxError);
16
17 assertThrowsInstanceOf(() => {
18 try {
19 eval('let [...[...x]] = (() => { throw "foo"; } )();');
20 } catch(e) {
21 assertEq(e, "foo");
22 }
23 x;
24 }, ReferenceError);
25
26 var inputArray = [1, 2, 3];
27 var inputDeep = [1, inputArray];
28 var inputObject = {a: inputArray};
29 var inputStr = 'str';
30 function *inputGenerator() {
31 yield 1;
32 yield 2;
33 yield 3;
34 }
35
36 var o = {prop: null, call: function () { return o; }};
37
38 var expected = [2, 3];
39 var expectedStr = ['t', 'r'];
40
41 function testAll(fn) {
42 testDeclaration(fn);
43
44 o.prop = null;
45 assertEqArray(fn('[, ...(o.prop)]', inputArray, 'o.prop'), expected);
46 o.prop = null;
47 assertEqArray(fn('[, ...(o.call().prop)]', inputArray, 'o.prop'), expected);
48
49 o.prop = null;
50 assertEqArray(fn('[, ...[...(o.prop)]]', inputArray, 'o.prop'), expected);
51 o.prop = null;
52 assertEqArray(fn('[, ...[...(o.call().prop)]]', inputArray, 'o.prop'), expected);
53 }
54 function testDeclaration(fn) {
55 testStr(fn);
56
57 assertEqArray(fn('[, ...rest]', inputArray), expected);
58 assertEqArray(fn('[, ...rest]', inputGenerator()), expected);
59 assertEqArray(fn('[, [, ...rest]]', inputDeep), expected);
60 assertEqArray(fn('{a: [, ...rest]}', inputObject), expected);
61
62 assertEqArray(fn('[, ...[...rest]]', inputArray), expected);
63 assertEqArray(fn('[, ...[...rest]]', inputGenerator()), expected);
64 assertEqArray(fn('[, [, ...[...rest]]]', inputDeep), expected);
65 assertEqArray(fn('{a: [, ...[...rest]]}', inputObject), expected);
66
67 assertEqArray(fn('[, ...{0: a, 1: b}]', inputArray, '[a, b]'), expected);
68 assertEqArray(fn('[, ...{0: a, 1: b}]', inputGenerator(), '[a, b]'), expected);
69 assertEqArray(fn('[, [, ...{0: a, 1: b}]]', inputDeep, '[a, b]'), expected);
70 assertEqArray(fn('{a: [, ...{0: a, 1: b}]}', inputObject, '[a, b]'), expected);
71 }
72
73 function testStr(fn) {
74 assertEqArray(fn('[, ...rest]', inputStr), expectedStr);
75
76 assertEqArray(fn('[, ...[...rest]]', inputStr), expectedStr);
77
78 assertEqArray(fn('[, ...{0: a, 1: b}]', inputStr, '[a, b]'), expectedStr);
79 }
80
81 function testForIn(pattern, input, binding) {
82 binding = binding || 'rest';
83 return new Function('input',
84 'for (var ' + pattern + ' in {[input]: 1}) {}' +
85 'return ' + binding
86 )(input);
87 }
88 testStr(testForIn);
89
90 function testVar(pattern, input, binding) {
91 binding = binding || 'rest';
92 return new Function('input',
93 'var ' + pattern + ' = input;' +
94 'return ' + binding
95 )(input);
96 }
97 testDeclaration(testVar);
98
99 function testGlobal(pattern, input, binding) {
100 binding = binding || 'rest';
101 return new Function('input',
102 '(' + pattern + ' = input);' +
103 'return ' + binding
104 )(input);
105 }
106 testAll(testGlobal);
107
108 function testClosure(pattern, input, binding) {
109 binding = binding || 'rest';
110 const decl = binding.replace('[', '').replace(']', '');
111 return new Function('input',
112 'var ' + decl + '; (function () {' +
113 '(' + pattern + ' = input);' +
114 '})();' +
115 'return ' + binding
116 )(input);
117 }
118 testDeclaration(testClosure);
119
120 function testArgument(pattern, input, binding) {
121 binding = binding || 'rest';
122 return new Function('input',
123 'return (function (' + pattern + ') {' +
124 'return ' + binding + '; })(input);'
125 )(input);
126 }
127 testDeclaration(testArgument);
128
129 function testArgumentFunction(pattern, input, binding) {
130 binding = binding || 'rest';
131 return new Function(pattern,
132 'return ' + binding
133 )(input);
134 }
135 // ES6 requires the `Function` constructor to accept arbitrary
136 // `BindingElement`s as formal parameters.
137 testDeclaration(testArgumentFunction);
138
139 function testThrow(pattern, input, binding) {
140 binding = binding || 'rest';
141 return new Function('input',
142 'try { throw input }' +
143 'catch(' + pattern + ') {' +
144 'return ' + binding + '; }'
145 )(input);
146 }
147 testDeclaration(testThrow);