| 1 |
|
| 2 |
load(libdir + 'asserts.js'); |
| 3 |
load(libdir + 'eqArrayHelper.js'); |
| 4 |
|
| 5 |
var arrayPattern = '[a = 1, b = 2, c = 3, d = 4, e = 5, f = 6]'; |
| 6 |
var objectPattern = '{0: a = 1, 1: b = 2, 2: c = 3, 3: d = 4, 4: e = 5, 5: f = 6}'; |
| 7 |
var objectPatternShorthand = '{a = 1, b = 2, c = 3, d = 4, e = 5, f = 6}'; |
| 8 |
var nestedPattern = '{a: a = 1, b: [b = 2] = [], c: {c: [c]} = {c: [3]}, d: {d, e} = {d: 4, e: 5}, f: f = 6}'; |
| 9 |
|
| 10 |
function testAll(fn) { |
| 11 |
assertEqArray(fn(arrayPattern, []), [1, 2, 3, 4, 5, 6]); |
| 12 |
assertEqArray(fn(arrayPattern, [2, 3, 4, 5, 6, 7, 8, 9]), [2, 3, 4, 5, 6, 7]); |
| 13 |
assertEqArray(fn(arrayPattern, [undefined, 0, false, null, "", undefined]), [1, 0, false, null, "", 6]); |
| 14 |
assertEqArray(fn(arrayPattern, [0, false]), [0, false, 3, 4, 5, 6]); |
| 15 |
|
| 16 |
assertEqArray(fn(objectPattern, []), [1, 2, 3, 4, 5, 6]); |
| 17 |
assertEqArray(fn(objectPattern, [2, 3, 4, 5, 6, 7, 8, 9]), [2, 3, 4, 5, 6, 7]); |
| 18 |
assertEqArray(fn(objectPattern, [undefined, 0, false, null, "", undefined]), [1, 0, false, null, "", 6]); |
| 19 |
assertEqArray(fn(objectPattern, [0, false]), [0, false, 3, 4, 5, 6]); |
| 20 |
|
| 21 |
assertEqArray(fn(objectPatternShorthand, {}), [1, 2, 3, 4, 5, 6]); |
| 22 |
assertEqArray(fn(objectPatternShorthand, {a: 2, b: 3, c: 4, d: 5, e: 6, f: 7, g: 8, h: 9}), [2, 3, 4, 5, 6, 7]); |
| 23 |
assertEqArray(fn(objectPatternShorthand, {a: undefined, b: 0, c: false, d: null, e: "", f: undefined}), |
| 24 |
[1, 0, false, null, "", 6]); |
| 25 |
assertEqArray(fn(objectPatternShorthand, {a: 0, b: false}), [0, false, 3, 4, 5, 6]); |
| 26 |
assertEqArray(fn(nestedPattern, {}), [1, 2, 3, 4, 5, 6]); |
| 27 |
assertEqArray(fn(nestedPattern, {a: 2, b: [], c: undefined}), [2, 2, 3, 4, 5, 6]); |
| 28 |
assertEqArray(fn(nestedPattern, {a: undefined, b: [3], c: {c: [4]}}), [1, 3, 4, 4, 5, 6]); |
| 29 |
assertEqArray(fn(nestedPattern, {a: undefined, b: [3], c: {c: [4]}, d: {d: 5, e: 6}}), [1, 3, 4, 5, 6, 6]); |
| 30 |
} |
| 31 |
|
| 32 |
function testVar(pattern, input) { |
| 33 |
return new Function('input', |
| 34 |
'var ' + pattern + ' = input;' + |
| 35 |
'return [a, b, c, d, e, f];' |
| 36 |
)(input); |
| 37 |
} |
| 38 |
testAll(testVar); |
| 39 |
|
| 40 |
function testLet(pattern, input) { |
| 41 |
return new Function('input', |
| 42 |
'let ' + pattern + ' = input;' + |
| 43 |
'return [a, b, c, d, e, f];' |
| 44 |
)(input); |
| 45 |
} |
| 46 |
testAll(testLet); |
| 47 |
|
| 48 |
function testConst(pattern, input) { |
| 49 |
return new Function('input', |
| 50 |
'const ' + pattern + ' = input;' + |
| 51 |
'return [a, b, c, d, e, f];' |
| 52 |
)(input); |
| 53 |
} |
| 54 |
testAll(testConst); |
| 55 |
|
| 56 |
function testGlobal(pattern, input) { |
| 57 |
return new Function('input', |
| 58 |
'(' + pattern + ' = input);' + |
| 59 |
'return [a, b, c, d, e, f];' |
| 60 |
)(input); |
| 61 |
} |
| 62 |
testAll(testGlobal); |
| 63 |
|
| 64 |
function testClosure(pattern, input) { |
| 65 |
return new Function('input', |
| 66 |
'var rest; (function () {' + |
| 67 |
'(' + pattern + ' = input);' + |
| 68 |
'})();' + |
| 69 |
'return [a, b, c, d, e, f];' |
| 70 |
)(input); |
| 71 |
} |
| 72 |
testAll(testClosure); |
| 73 |
|
| 74 |
function testArgument(pattern, input) { |
| 75 |
return new Function('input', |
| 76 |
'return (function (' + pattern + ') {' + |
| 77 |
'return [a, b, c, d, e, f]; })(input);' |
| 78 |
)(input); |
| 79 |
} |
| 80 |
testAll(testArgument); |
| 81 |
|
| 82 |
function testArgumentFunction(pattern, input) { |
| 83 |
return new Function(pattern, |
| 84 |
'return [a, b, c, d, e, f];' |
| 85 |
)(input); |
| 86 |
} |
| 87 |
testAll(testArgumentFunction); |
| 88 |
|
| 89 |
function testThrow(pattern, input) { |
| 90 |
return new Function('input', |
| 91 |
'try { throw input }' + |
| 92 |
'catch(' + pattern + ') {' + |
| 93 |
'return [a, b, c, d, e, f]; }' |
| 94 |
)(input); |
| 95 |
} |
| 96 |
testAll(testThrow); |
| 97 |
|
| 98 |
// test global const |
| 99 |
const [ca = 1, cb = 2] = []; |
| 100 |
assertEq(ca, 1); |
| 101 |
assertEq(cb, 2); |
| 102 |
|
| 103 |
const {a: {a: cc = 3} = {a: undefined}} = {}; |
| 104 |
assertEq(cc, 3); |
| 105 |
|
| 106 |
// test that the assignment happens in source order |
| 107 |
var a = undefined, b = undefined, c = undefined; |
| 108 |
({a: a = 1, c: c = 2, b: b = 3} = { |
| 109 |
get a() { |
| 110 |
assertEq(a, undefined); |
| 111 |
assertEq(c, undefined); |
| 112 |
assertEq(b, undefined); |
| 113 |
return undefined; |
| 114 |
}, |
| 115 |
get b() { |
| 116 |
assertEq(a, 1); |
| 117 |
assertEq(c, 2); |
| 118 |
assertEq(b, undefined); |
| 119 |
return 4; |
| 120 |
}, |
| 121 |
get c() { |
| 122 |
assertEq(a, 1); |
| 123 |
assertEq(c, undefined); |
| 124 |
assertEq(b, undefined); |
| 125 |
return undefined; |
| 126 |
} |
| 127 |
}); |
| 128 |
assertEq(b, 4); |
| 129 |
|
| 130 |
assertThrowsInstanceOf(() => { var {a: {a} = null} = {}; }, TypeError); |
| 131 |
assertThrowsInstanceOf(() => { var [[a] = 2] = []; }, TypeError); |
| 132 |
|
| 133 |
// destructuring assignment might have duplicate variable names. |
| 134 |
var [a = 1, a = 2] = [3]; |
| 135 |
assertEq(a, 2); |
| 136 |
|
| 137 |
// assignment to properties of default params |
| 138 |
[a = {y: 2}, a.x = 1] = []; |
| 139 |
assertEq(typeof a, 'object'); |
| 140 |
assertEq(a.x, 1); |
| 141 |
assertEq(a.y, 2); |
| 142 |
|
| 143 |
// defaults are evaluated even if there is no binding |
| 144 |
var evaled = false; |
| 145 |
({a: {} = (evaled = true, {})} = {}); |
| 146 |
assertEq(evaled, true); |
| 147 |
evaled = false; |
| 148 |
assertThrowsInstanceOf(() => { [[] = (evaled = true, 2)] = [] }, TypeError); |
| 149 |
assertEq(evaled, true); |
| 150 |
|
| 151 |
assertThrowsInstanceOf(() => new Function('var [...rest = defaults] = [];'), SyntaxError); |
| 152 |
|
| 153 |
new Function(` |
| 154 |
b = undefined; |
| 155 |
for (var [a, b = 10] in " ") {} |
| 156 |
assertEq(b, 10);`)(); |
| 157 |
|
| 158 |
new Function(` |
| 159 |
b = undefined; |
| 160 |
for (let [a, c = 10] in " ") { b = c; } |
| 161 |
assertEq(b, 10);`)(); |
| 162 |
|
| 163 |
new Function(` |
| 164 |
b = undefined; |
| 165 |
for (let [a, c = (x => y)] in " ") { b = c; } |
| 166 |
assertEq(typeof b, "function");`)(); |
| 167 |
|
| 168 |
new Function(` |
| 169 |
b = undefined; |
| 170 |
for (let [a, __proto__ = 10] in " ") { b = __proto__; } |
| 171 |
assertEq(b, 10);`)(); |
| 172 |
|
| 173 |
new Function(` |
| 174 |
b = undefined; |
| 175 |
for (let [a, __proto__ = (x => y)] in " ") { b = __proto__; } |
| 176 |
assertEq(typeof b, "function");`)(); |
| 177 |
|
| 178 |
new Function(` |
| 179 |
b = undefined; |
| 180 |
for (var {1: b = 10} in " ") {} |
| 181 |
assertEq(b, 10);`)(); |
| 182 |
|
| 183 |
new Function(` |
| 184 |
b = undefined; |
| 185 |
for (let {1: c = 10} in " ") { b = c; } |
| 186 |
assertEq(b, 10);`)(); |
| 187 |
|
| 188 |
new Function(` |
| 189 |
b = undefined; |
| 190 |
for (let { c = 10 } in " ") { b = c; } |
| 191 |
assertEq(b, 10);`)(); |
| 192 |
|
| 193 |
new Function(` |
| 194 |
b = undefined; |
| 195 |
assertEq(Number.prototype.a, undefined); |
| 196 |
for (var { a: c = (x => y) } in [{}]) { b = c; } |
| 197 |
assertEq(typeof b, "function");`)(); |
| 198 |
|
| 199 |
new Function(` |
| 200 |
b = undefined; |
| 201 |
Object.defineProperty(String.prototype, "__proto__", |
| 202 |
{ value: undefined, configurable: true }); |
| 203 |
for (var { __proto__: c = (x => y) } in [{}]) { b = c; } |
| 204 |
delete String.prototype.__proto__; |
| 205 |
assertEq(typeof b, "function");`)(); |
| 206 |
|
| 207 |
new Function(` |
| 208 |
b = undefined; |
| 209 |
for (var { a: c = (x => y) } of [{ a: undefined }]) { b = c; } |
| 210 |
assertEq(typeof b, "function");`)(); |
| 211 |
|
| 212 |
new Function(` |
| 213 |
b = undefined; |
| 214 |
for (var { __proto__: c = (x => y) } of [{ ["__proto__"]: undefined }]) { b = c; } |
| 215 |
assertEq(typeof b, "function");`)(); |
| 216 |
|
| 217 |
new Function(` |
| 218 |
b = undefined; |
| 219 |
var ts = Function.prototype.toString; |
| 220 |
Function.prototype.toString = () => 'hi'; |
| 221 |
String.prototype.hi = 42; |
| 222 |
for (var { [(x => y)]: c } in [0]) { b = c; } |
| 223 |
Function.prototype.toString = ts; |
| 224 |
delete String.prototype.hi; |
| 225 |
assertEq(b, 42);`)(); |
| 226 |
|
| 227 |
new Function(` |
| 228 |
b = undefined; |
| 229 |
var ts = Function.prototype.toString; |
| 230 |
Function.prototype.toString = () => 'hi'; |
| 231 |
String.prototype.hi = 42; |
| 232 |
for (var { [(x => y)]: __proto__ } in [0]) { b = __proto__; } |
| 233 |
Function.prototype.toString = ts; |
| 234 |
delete String.prototype.hi; |
| 235 |
assertEq(b, 42);`)(); |
| 236 |
|
| 237 |
new Function(` |
| 238 |
b = undefined; |
| 239 |
var ts = Function.prototype.toString; |
| 240 |
Function.prototype.toString = () => 'hi'; |
| 241 |
for (var { [(x => y)]: c } of [{ 'hi': 42 }]) { b = c; } |
| 242 |
Function.prototype.toString = ts; |
| 243 |
assertEq(b, 42);`)(); |
| 244 |
|
| 245 |
new Function(` |
| 246 |
b = undefined; |
| 247 |
var ts = Function.prototype.toString; |
| 248 |
Function.prototype.toString = () => 'hi'; |
| 249 |
for (var { [(x => y)]: __proto__ } of [{ hi: 42 }]) { b = __proto__; } |
| 250 |
Function.prototype.toString = ts; |
| 251 |
assertEq(b, 42);`)(); |