michael@0: var otherGlobal = newGlobal(); michael@0: michael@0: function test(str, arg, result) michael@0: { michael@0: arg = arg || 'ponies'; michael@0: result = result || 'ponies'; michael@0: michael@0: var fun = new Function('x', str); michael@0: michael@0: var got = fun.toSource(); michael@0: var expect = '(function anonymous(x) {\n' + str + '\n})'; michael@0: if (got !== expect) { michael@0: print("GOT: " + got); michael@0: print("EXPECT: " + expect); michael@0: assertEq(got, expect); michael@0: } michael@0: michael@0: // test reflection logic michael@0: Reflect.parse(got); michael@0: michael@0: // test xdr by cloning a cross-compartment function michael@0: var code = "(function (x) { " + str + " })"; michael@0: var c = clone(otherGlobal.evaluate(code, {compileAndGo: false})); michael@0: assertEq(c.toSource(), eval(code).toSource()); michael@0: michael@0: var got = fun(arg); michael@0: var expect = result; michael@0: if (got !== expect) { michael@0: print("GOT:" + got); michael@0: print("EXPECT: " + expect); michael@0: assertEq(got, expect); michael@0: } michael@0: } michael@0: michael@0: function isError(str) michael@0: { michael@0: var caught = false; michael@0: try { michael@0: new Function(str); michael@0: } catch(e) { michael@0: assertEq(String(e).indexOf('TypeError') == 0 || String(e).indexOf('SyntaxError') == 0, true); michael@0: caught = true; michael@0: } michael@0: assertEq(caught, true); michael@0: } michael@0: michael@0: // let expr michael@0: test('return let (y) x;'); michael@0: test('return let (x) "" + x;', 'unicorns', 'undefined'); michael@0: test('return let (y = x) (y++, "" + y);', 'unicorns', 'NaN'); michael@0: test('return let (y = 1) (y = x, y);'); michael@0: test('return let ([] = x) x;'); michael@0: test('return let (x = {a: x}) x.a;'); michael@0: test('return let ({a: x} = {a: x}) x;'); michael@0: test('return let ([x] = {0: x}) x;'); michael@0: test('return let ({0: x} = [x]) x;'); michael@0: test('return let ({0: []} = []) x;'); michael@0: test('return let ([, ] = x) x;'); michael@0: test('return let ([, , , , ] = x) x;'); michael@0: test('return let ([[]] = x) x;'); michael@0: test('return let ([[[[[[[[[[[[[]]]]]]]]]]]]] = x) x;'); michael@0: test('return let ([[], []] = x) x;'); michael@0: test('return let ([[[[]]], [], , [], [[]]] = x) x;'); michael@0: test('return let ({x: []} = x) x;'); michael@0: test('return let ({x: [], y: {x: []}} = x) "ponies";', {y:{}}); michael@0: test('return let ({x: []} = x, [{x: []}] = x) "ponies";'); michael@0: test('return let (x = x) x;'); michael@0: test('return let (x = eval("x")) x;'); michael@0: test('return let (x = (let (x = x + 1) x) + 1) x;', 1, 3); michael@0: test('return let (x = (let (x = eval("x") + 1) eval("x")) + 1) eval("x");', 1, 3); michael@0: test('return let (x = x + 1, y = x) y;'); michael@0: test('return let (x = x + 1, [] = x, [[, , ]] = x, y = x) y;'); michael@0: test('return let ([{a: x}] = x, [, {b: y}] = x) let (x = x + 1, y = y + 2) x + y;', [{a:"p"},{b:"p"}], "p1p2"); michael@0: test('return let ([] = []) x;'); michael@0: test('return let ([] = [x]) x;'); michael@0: test('return let ([a] = (1, [x])) a;'); michael@0: test('return let ([a] = (1, x, 1, x)) a;', ['ponies']); michael@0: test('return let ([x] = [x]) x;'); michael@0: test('return let ([[a, [b, c]]] = [[x, []]]) a;'); michael@0: test('return let ([x, y] = [x, x + 1]) x + y;', 1, 3); michael@0: test('return let ([x, y, z] = [x, x + 1, x + 2]) x + y + z;', 1, 6); michael@0: test('return let ([[x]] = [[x]]) x;'); michael@0: test('return let ([x, y] = [x, x + 1]) x;'); michael@0: test('return let ([x, [y, z]] = [x, x + 1]) x;'); michael@0: test('return let ([{x: [x]}, {y1: y, z1: z}] = [x, x + 1]) x;',{x:['ponies']}); michael@0: test('return let (x = (3, x)) x;'); michael@0: test('return let (x = x + "s") x;', 'ponie'); michael@0: test('return let ([x] = (3, [x])) x;'); michael@0: test('return let ([] = [[]] = {}) x;'); michael@0: test('return let (y = x) function () {return eval("y");}();'); michael@0: test('return eval("let (y = x) y");'); michael@0: test('return let (y = x) (eval("var y = 2"), y);', 'ponies', 2); michael@0: test('"use strict";return let (y = x) (eval("var y = 2"), y);'); michael@0: test('this.y = x;return let (y = 1) this.eval("y");'); michael@0: test('try {let (x = x) eval("throw x");} catch (e) {return e;}'); michael@0: test('try {return let (x = eval("throw x")) x;} catch (e) {return e;}'); michael@0: isError('let (x = 1, x = 2) x'); michael@0: isError('let ([x, y] = a, {a:x} = b) x'); michael@0: isError('let ([x, y, x] = a) x'); michael@0: isError('let ([x, [y, [x]]] = a) x'); michael@0: isError('let (x = function() { return x}) x()return x;'); michael@0: isError('(let (x = function() { return x}) x())return x;'); michael@0: michael@0: // let block michael@0: test('let (y) {return x;}'); michael@0: test('let (y = x) {y++;return "" + y;}', 'unicorns', 'NaN'); michael@0: test('let (y = 1) {y = x;return y;}'); michael@0: test('let (x) {return "" + x;}', 'unicorns', 'undefined'); michael@0: test('let ([] = x) {return x;}'); michael@0: test('let (x) {}return x;'); michael@0: test('let (x = {a: x}) {return x.a;}'); michael@0: test('let ({a: x} = {a: x}) {return x;}'); michael@0: test('let ([x] = {0: x}) {return x;}'); michael@0: test('let ({0: x} = [x]) {return x;}'); michael@0: test('let ({0: []} = []) {return x;}'); michael@0: test('let ([, ] = x) {return x;}'); michael@0: test('let ([, , , , ] = x) {return x;}'); michael@0: test('let ([[]] = x) {return x;}'); michael@0: test('let ([[[[[[[[[[[[[]]]]]]]]]]]]] = x) {return x;}'); michael@0: test('let ([[], []] = x) {return x;}'); michael@0: test('let ([[[[]]], [], , [], [[]]] = x) {return x;}'); michael@0: test('let ({x: []} = x) {return x;}'); michael@0: test('let ({x: [], y: {x: []}} = x) {return "ponies";}', {y:{}}); michael@0: test('let ({x: []} = x, [{x: []}] = x) {return "ponies";}'); michael@0: test('let (x = x) {return x;}'); michael@0: test('let (x = eval("x")) {return x;}'); michael@0: test('let (x = (let (x = x + 1) x) + 1) {return x;}', 1, 3); michael@0: test('let (x = (let (x = eval("x") + 1) eval("x")) + 1) {return eval("x");}', 1, 3); michael@0: test('let (x = x + 1, y = x) {return y;}'); michael@0: test('let (x = x + 1, [] = x, [[, , ]] = x, y = x) {return y;}'); michael@0: test('let ([{a: x}] = x, [, {b: y}] = x) {let (x = x + 1, y = y + 2) {return x + y;}}', [{a:"p"},{b:"p"}], "p1p2"); michael@0: test('let ([] = []) {return x;}'); michael@0: test('let ([] = [x]) {return x;}'); michael@0: test('let ([a] = (1, [x])) {return a;}'); michael@0: test('let ([a] = (1, x, 1, x)) {return a;}', ['ponies']); michael@0: test('let ([x] = [x]) {return x;}'); michael@0: test('let ([[a, [b, c]]] = [[x, []]]) {return a;}'); michael@0: test('let ([x, y] = [x, x + 1]) {return x + y;}', 1, 3); michael@0: test('let ([x, y, z] = [x, x + 1, x + 2]) {return x + y + z;}', 1, 6); michael@0: test('let ([[x]] = [[x]]) {return x;}'); michael@0: test('let ([x, y] = [x, x + 1]) {return x;}'); michael@0: test('let ([x, [y, z]] = [x, x + 1]) {return x;}'); michael@0: test('let ([{x: [x]}, {y1: y, z1: z}] = [x, x + 1]) {return x;}',{x:['ponies']}); michael@0: test('let (y = x[1]) {let (x = x[0]) {try {let (y = "unicorns") {throw y;}} catch (e) {return x + y;}}}', ['pon','ies']); michael@0: test('let (x = x) {try {let (x = "unicorns") eval("throw x");} catch (e) {return x;}}'); michael@0: test('let ([] = [[]] = {}) {return x;}'); michael@0: test('let (y = x) {return function () {return eval("y");}();}'); michael@0: test('return eval("let (y = x) {y;}");'); michael@0: test('let (y = x) {eval("var y = 2");return y;}', 'ponies', 2); michael@0: test('"use strict";let (y = x) {eval("var y = 2");return y;}'); michael@0: test('this.y = x;let (y = 1) {return this.eval("y");}'); michael@0: isError('let (x = 1, x = 2) {x}'); michael@0: isError('let ([x, y] = a, {a:x} = b) {x}'); michael@0: isError('let ([x, y, x] = a) {x}'); michael@0: isError('let ([x, [y, [x]]] = a) {x}'); michael@0: michael@0: // var declarations michael@0: test('var y;return x;'); michael@0: test('var y = x;return x;'); michael@0: test('var [] = x;return x;'); michael@0: test('var [, ] = x;return x;'); michael@0: test('var [, , , , ] = x;return x;'); michael@0: test('var [[]] = x;return x;'); michael@0: test('var [[[[[[[[[[[[[]]]]]]]]]]]]] = x;return x;'); michael@0: test('var [[], []] = x;return x;'); michael@0: test('var [[[[]]], [], , [], [[]]] = x;return x;'); michael@0: test('var {x: []} = x;return x;'); michael@0: test('var {x: [], y: {x: []}} = x;return "ponies";', {y:{}}); michael@0: test('var {x: []} = x, [{x: []}] = x;return "ponies";'); michael@0: test('var x = x;return x;'); michael@0: test('var y = y;return "" + y;', 'unicorns', 'undefined'); michael@0: test('var x = eval("x");return x;'); michael@0: test('var x = (let (x = x + 1) x) + 1;return x;', 1, 3); michael@0: test('var x = (let (x = eval("x") + 1) eval("x")) + 1;return eval("x");', 1, 3); michael@0: test('var X = x + 1, y = x;return y;'); michael@0: test('var X = x + 1, [] = X, [[, , ]] = X, y = x;return y;'); michael@0: test('var [{a: X}] = x, [, {b: y}] = x;var X = X + 1, y = y + 2;return X + y;', [{a:"p"},{b:"p"}], "p1p2"); michael@0: test('var [x] = [x];return x;'); michael@0: test('var [[a, [b, c]]] = [[x, []]];return a;'); michael@0: test('var [y] = [x];return y;'); michael@0: test('var [a] = (1, [x]);return a;'); michael@0: test('var [a] = (1, x, 1, x);return a;', ['ponies']); michael@0: test('var [x, y] = [x, x + 1];return x + y;', 1, 3); michael@0: test('var [x, y, z] = [x, x + 1, x + 2];return x + y + z;', 1, 6); michael@0: test('var [[x]] = [[x]];return x;'); michael@0: test('var [x, y] = [x, x + 1];return x;'); michael@0: test('var [x, [y, z]] = [x, x + 1];return x;'); michael@0: test('var [{x: [x]}, {y1: y, z1: z}] = [x, x + 1];return x;',{x:['ponies']}); michael@0: test('var [] = [[]] = {};return x;'); michael@0: test('if (x) {var y = x;return x;}'); michael@0: test('if (x) {y = x;var y = y;return y;}'); michael@0: test('if (x) {var z = y;var [y] = x;z += y;}return z;', ['-'], 'undefined-'); michael@0: michael@0: // let declaration in context michael@0: test('if (x) {let y;return x;}'); michael@0: test('if (x) {let x;return "" + x;}', 'unicorns', 'undefined'); michael@0: test('if (x) {let y = x;return x;}'); michael@0: test('if (x) {y = x;let y = y;return y;}'); michael@0: test('if (x) {var z = y;let [y] = x;z += y;}return z;', ['-'], 'undefined-'); michael@0: test('if (x) {let y = x;return x;}'); michael@0: test('if (x) {let [] = x;return x;}'); michael@0: test('if (x) {let [, ] = x;return x;}'); michael@0: test('if (x) {let [, , , , ] = x;return x;}'); michael@0: test('if (x) {let [[]] = x;return x;}'); michael@0: test('if (x) {let [[[[[[[[[[[[[]]]]]]]]]]]]] = x;return x;}'); michael@0: test('if (x) {let [[], []] = x;return x;}'); michael@0: test('if (x) {let [[[[]]], [], , [], [[]]] = x;return x;}'); michael@0: test('if (x) {let {x: []} = x;return x;}'); michael@0: test('if (x) {let {x: [], y: {x: []}} = x;return "ponies";}', {y:{}}); michael@0: test('if (x) {let {x: []} = x, [{x: []}] = x;return "ponies";}'); michael@0: test('if (x) {let x = x;return "" + x;}', 'unicorns', 'undefined'); michael@0: test('if (x) {let y = y;return "" + y;}', 'unicorns', 'undefined'); michael@0: test('if (x) {let x = eval("x");return "" + x;}', 'unicorns', 'undefined'); michael@0: test('if (x) {let y = (let (x = x + 1) x) + 1;return y;}', 1, 3); michael@0: test('if (x) {let y = (let (x = eval("x") + 1) eval("x")) + 1;return eval("y");}', 1, 3); michael@0: test('if (x) {let X = x + 1, y = x;return y;}'); michael@0: test('if (x) {let X = x + 1, [] = X, [[, , ]] = X, y = x;return y;}'); michael@0: test('if (x) {let [{a: X}] = x, [, {b: Y}] = x;var XX = X + 1, YY = Y + 2;return XX + YY;}', [{a:"p"},{b:"p"}], "p1p2"); michael@0: test('if (x) {let [[a, [b, c]]] = [[x, []]];return a;}'); michael@0: test('if (x) {let [X] = [x];return X;}'); michael@0: test('if (x) {let [y] = [x];return y;}'); michael@0: test('if (x) {let [a] = (1, [x]);return a;}'); michael@0: test('if (x) {let [a] = (1, x, 1, x);return a;}', ['ponies']); michael@0: test('if (x) {let [X, y] = [x, x + 1];return X + y;}', 1, 3); michael@0: test('if (x) {let [X, y, z] = [x, x + 1, x + 2];return X + y + z;}', 1, 6); michael@0: test('if (x) {let [[X]] = [[x]];return X;}'); michael@0: test('if (x) {let [X, y] = [x, x + 1];return X;}'); michael@0: test('if (x) {let [X, [y, z]] = [x, x + 1];return X;}'); michael@0: test('if (x) {let [{x: [X]}, {y1: y, z1: z}] = [x, x + 1];return X;}',{x:['ponies']}); michael@0: test('if (x) {let y = x;try {let x = 1;throw 2;} catch (e) {return y;}}'); michael@0: test('if (x) {let [] = [[]] = {};return x;}'); michael@0: test('let (y, [] = x) {}try {let a = b(), b;} catch (e) {return x;}'); michael@0: test('try {let x = 1;throw 2;} catch (e) {return x;}'); michael@0: test('let (y = x) {let x;return y;}'); michael@0: test('let (y = x) {let x = y;return x;}'); michael@0: test('let ([y, z] = x) {let a = x, b = y;return a;}'); michael@0: test('let ([y, z] = x, a = x, [] = x) {let b = x, c = y;return a;}'); michael@0: test('function f() {return unicorns;}try {let (x = 1) {let a, b;f();}} catch (e) {return x;}'); michael@0: test('function f() {return unicorns;}try {let (x = 1) {let a, b;}f();} catch (e) {return x;}'); michael@0: test('x.foo;{let y = x;return y;}'); michael@0: test('x.foo;if (x) {x.bar;let y = x;return y;}'); michael@0: test('if (x) {let y = x;return function () {return eval("y");}();}'); michael@0: test('return eval("let y = x; y");'); michael@0: test('if (x) {let y = x;eval("var y = 2");return y;}', 'ponies', 2); michael@0: test('"use strict";if (x) {let y = x;eval("var y = 2");return y;}'); michael@0: test('"use strict";if (x) {let y = x;eval("let y = 2");return y;}'); michael@0: test('"use strict";if (x) {let y = 1;return eval("let y = x;y;");}'); michael@0: test('this.y = x;if (x) {let y = 1;return this.eval("y");}'); michael@0: isError('if (x) {let (x = 1, x = 2) {x}}'); michael@0: isError('if (x) {let ([x, y] = a, {a:x} = b) {x}}'); michael@0: isError('if (x) {let ([x, y, x] = a) {x}}'); michael@0: isError('if (x) {let ([x, [y, [x]]] = a) {x}}'); michael@0: isError('let ([x, y] = x) {let x;}'); michael@0: michael@0: // for(;;) michael@0: test('for (;;) {return x;}'); michael@0: test('for (let y = 1;;) {return x;}'); michael@0: test('for (let y = 1;; ++y) {return x;}'); michael@0: test('for (let y = 1; ++y;) {return x;}'); michael@0: test('for (let (x = 1) x; x != 1; ++x) {return x;}'); michael@0: test('for (let [, {a: [], b: []}] = x, [] = x; x;) {return x;}'); michael@0: test('for (let x = 1, [y, z] = x, a = x; z < 4; ++z) {return x + y;}', [2,3], 3); michael@0: test('for (let (x = 1, [{a: b, c: d}] = [{a: 1, c: 2}]) x; x != 1; ++x) {return x;}'); michael@0: test('for (let [[a, [b, c]]] = [[x, []]];;) {return a;}'); michael@0: test('var sum = 0;for (let y = x; y < 4; ++y) {sum += y;}return sum;', 1, 6); michael@0: test('var sum = 0;for (let x = x, y = 10; x < 4; ++x) {sum += x;}return sum;', 1, 6); michael@0: test('var sum = 0;for (let x = x; x < 4; ++x) {sum += x;}return x;', 1, 1); michael@0: test('var sum = 0;for (let x = eval("x"); x < 4; ++x) {sum += x;}return sum;', 1, 6); michael@0: test('var sum = 0;for (let x = x; eval("x") < 4; ++x) {sum += eval("x");}return sum;', 1, 6); michael@0: test('var sum = 0;for (let x = eval("x"); eval("x") < 4; ++x) {sum += eval("x");}return sum;', 1, 6); michael@0: test('for (var y = 1;;) {return x;}'); michael@0: test('for (var y = 1;; ++y) {return x;}'); michael@0: test('for (var y = 1; ++y;) {return x;}'); michael@0: test('for (var [, {a: [], b: []}] = x, [] = x; x;) {return x;}'); michael@0: test('for (var X = 1, [y, z] = x, a = x; z < 4; ++z) {return X + y;}', [2,3], 3); michael@0: test('var sum = 0;for (var y = x; y < 4; ++y) {sum += y;}return sum;', 1, 6); michael@0: test('var sum = 0;for (var X = x, y = 10; X < 4; ++X) {sum += X;}return sum;', 1, 6); michael@0: test('var sum = 0;for (var X = x; X < 4; ++X) {sum += X;}return x;', 1, 1); michael@0: test('var sum = 0;for (var X = eval("x"); X < 4; ++X) {sum += X;}return sum;', 1, 6); michael@0: test('var sum = 0;for (var X = x; eval("X") < 4; ++X) {sum += eval("X");}return sum;', 1, 6); michael@0: test('var sum = 0;for (var X = eval("x"); eval("X") < 4; ++X) {sum += eval("X");}return sum;', 1, 6); michael@0: test('try {for (let x = eval("throw x");;) {}} catch (e) {return e;}'); michael@0: test('try {for (let x = x + "s"; eval("throw x");) {}} catch (e) {return e;}', 'ponie'); michael@0: test('for (let y = x;;) {let x;return y;}'); michael@0: test('for (let y = x;;) {let y;return x;}'); michael@0: test('for (let y;;) {let y;return x;}'); michael@0: test('for (let a = x;;) {let c = x, d = x;return c;}'); michael@0: test('for (let [a, b] = x;;) {let c = x, d = x;return c;}'); michael@0: test('for (let [] = [[]] = {};;) {return x;}'); michael@0: test('for (let [a] = (1, [x]);;) {return a;}'); michael@0: test('for (let [a] = (1, x, 1, x);;) {return a;}', ['ponies']); michael@0: isError('for (let x = 1, x = 2;;) {}'); michael@0: isError('for (let [x, y] = a, {a:x} = b;;) {}'); michael@0: isError('for (let [x, y, x] = a;;) {}'); michael@0: isError('for (let [x, [y, [x]]] = a;;) {}'); michael@0: michael@0: // for(in) michael@0: test('for (let i in x) {return x;}'); michael@0: test('for (let i in x) {let y;return x;}'); michael@0: test('for each (let [a, b] in x) {let y;return x;}'); michael@0: test('for (let i in x) {let (i = x) {return i;}}'); michael@0: test('for (let i in x) {let i = x;return i;}'); michael@0: test('for each (let [x, y] in x) {return x + y;}', [['ponies', '']]); michael@0: test('for each (let [{0: x, 1: y}, z] in x) {return x + y + z;}', [[['po','nies'], '']]); michael@0: test('var s = "";for (let a in x) {for (let b in x) {s += a + b;}}return s;', [1,2], '00011011'); michael@0: test('var res = "";for (let i in x) {res += x[i];}return res;'); michael@0: test('var res = "";for (var i in x) {res += x[i];}return res;'); michael@0: test('for each (let {x: y, y: x} in [{x: x, y: x}]) {return y;}'); michael@0: test('for (let x in eval("x")) {return x;}', {ponies:true}); michael@0: test('for (let x in x) {return eval("x");}', {ponies:true}); michael@0: test('for (let x in eval("x")) {return eval("x");}', {ponies:true}); michael@0: test('for ((let (x = {y: true}) x).y in eval("x")) {return eval("x");}'); michael@0: test('for (let i in x) {break;}return x;'); michael@0: test('for (let i in x) {break;}return eval("x");'); michael@0: test('for (let x in x) {break;}return x;'); michael@0: test('for (let x in x) {break;}return eval("x");'); michael@0: test('a:for (let i in x) {for (let j in x) {break a;}}return x;'); michael@0: test('a:for (let i in x) {for (let j in x) {break a;}}return eval("x");'); michael@0: test('var j;for (let i in x) {j = i;break;}return j;', {ponies:true}); michael@0: test('try {for (let x in eval("throw x")) {}} catch (e) {return e;}'); michael@0: test('try {for each (let x in x) {eval("throw x");}} catch (e) {return e;}', ['ponies']); michael@0: isError('for (let [x, x] in o) {}'); michael@0: isError('for (let [x, y, x] in o) {}'); michael@0: isError('for (let [x, [y, [x]]] in o) {}'); michael@0: michael@0: // genexps michael@0: test('return (i for (i in x)).next();', {ponies:true}); michael@0: test('return (eval("i") for (i in x)).next();', {ponies:true}); michael@0: test('return (eval("i") for (i in eval("x"))).next();', {ponies:true}); michael@0: test('try {return (eval("throw i") for (i in x)).next();} catch (e) {return e;}', {ponies:true}); michael@0: michael@0: // array comprehension michael@0: test('return [i for (i in x)][0];', {ponies:true}); michael@0: test('return [eval("i") for (i in x)][0];', {ponies:true}); michael@0: test('return [eval("i") for (i in eval("x"))][0];', {ponies:true}); michael@0: test('try {return [eval("throw i") for (i in x)][0];} catch (e) {return e;}', {ponies:true}); michael@0: michael@0: // don't forget about switch craziness michael@0: test('var y = 3;switch (function () {return eval("y");}()) {case 3:let y;return x;default:;}'); michael@0: test('switch (x) {case 3:let y;return 3;case 4:let z;return 4;default:return x;}'); michael@0: test('switch (x) {case 3:let x;break;default:if (x === undefined) {return "ponies";}}'); michael@0: test('switch (x) {case 3:default:let y;let (y = x) {return y;}}'); michael@0: isError('switch (x) {case 3:let y;return 3;case 4:let y;return 4;default:;}');