1014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Copyright 2015 the V8 project authors. All rights reserved. 2014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// found in the LICENSE file. 4014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 5014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 6014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestDefaults() { 7014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(x = 1) { return x } 8014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f1()); 9014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f1(undefined)); 10014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, f1(2)); 11014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(null, f1(null)); 12014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 13014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(x, y = x) { return x + y; } 14014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, f2(4)); 15014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, f2(4, undefined)); 16014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(6, f2(4, 2)); 17014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 18014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f3(x = 1, y) { return x + y; } 19014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, f3(5, 3)); 20014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, f3(undefined, 2)); 21014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(6, f3(4, 2)); 22014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 23014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f4(x = () => 1) { return x() } 24014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f4()); 25014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f4(undefined)); 26014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, f4(() => 2)); 27014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f4(null), TypeError); 28014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 29014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f5(x, y = () => x) { return x + y(); } 30014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, f5(4)); 31014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, f5(4, undefined)); 32014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(6, f5(4, () => 2)); 33014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 34014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f6(x = {a: 1, m() { return 2 }}) { return x.a + x.m(); } 35014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, f6()); 36014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, f6(undefined)); 37014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(5, f6({a: 2, m() { return 3 }})); 38014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 39014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g1 = (x = 1) => { return x }; 40014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g1()); 41014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g1(undefined)); 42014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, g1(2)); 43014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(null, g1(null)); 44014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 45014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g2 = (x, y = x) => { return x + y; }; 46014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, g2(4)); 47014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, g2(4, undefined)); 48014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(6, g2(4, 2)); 49014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 50014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g3 = (x = 1, y) => { return x + y; }; 51014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, g3(5, 3)); 52014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, g3(undefined, 2)); 53014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(6, g3(4, 2)); 54014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 55014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g4 = (x = () => 1) => { return x() }; 56014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g4()); 57014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g4(undefined)); 58014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, g4(() => 2)); 59014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => g4(null), TypeError); 60014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 61014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g5 = (x, y = () => x) => { return x + y(); }; 62014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, g5(4)); 63014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(8, g5(4, undefined)); 64014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(6, g5(4, () => 2)); 65014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 66014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g6 = (x = {a: 1, m() { return 2 }}) => { return x.a + x.m(); }; 67014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, g6()); 68014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, g6(undefined)); 69014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(5, g6({a: 2, m() { return 3 }})); 70014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}()); 71014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 72014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 73014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestEvalInParameters() { 74014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(x = eval(0)) { return x } 75014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(0, f1()); 76014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(x = () => eval(1)) { return x() } 77014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f2()); 78014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 79014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 80014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 81014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestParameterScopingSloppy() { 82014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var x = 1; 83014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 84014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(a = x) { var x = 2; return a; } 85014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f1()); 86014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(a = x) { function x() {}; return a; } 87014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f2()); 88014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f3(a = eval("x")) { var x; return a; } 89014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f3()); 90014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f31(a = eval("'use strict'; x")) { var x; return a; } 91014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f31()); 92014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f4(a = function() { return x }) { var x; return a(); } 93014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f4()); 94014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f5(a = () => x) { var x; return a(); } 95014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f5()); 96014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f6(a = () => eval("x")) { var x; return a(); } 97014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f6()); 98014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f61(a = () => { 'use strict'; return eval("x") }) { var x; return a(); } 99014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f61()); 100014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f62(a = () => eval("'use strict'; x")) { var x; return a(); } 101014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f62()); 102014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 103014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g1 = (a = x) => { var x = 2; return a; }; 104014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g1()); 105014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g2 = (a = x) => { function x() {}; return a; }; 106014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g2()); 107014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g3 = (a = eval("x")) => { var x; return a; }; 108014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g3()); 109014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g31 = (a = eval("'use strict'; x")) => { var x; return a; }; 110014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g31()); 111014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g4 = (a = function() { return x }) => { var x; return a(); }; 112014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g4()); 113014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g5 = (a = () => x) => { var x; return a(); }; 114014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g5()); 115014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g6 = (a = () => eval("x")) => { var x; return a(); }; 116014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g6()); 117014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g61 = (a = () => { 'use strict'; return eval("x") }) => { var x; return a(); }; 118014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g61()); 119014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g62 = (a = () => eval("'use strict'; x")) => { var x; return a(); }; 120014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g62()); 121014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 122014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var f11 = function f(x = f) { var f; return x; } 123014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(f11, f11()); 124014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var f12 = function f(x = f) { function f() {}; return x; } 125014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(f12, f12()); 126014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var f13 = function f(f = 7, x = f) { return x; } 127014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(7, f13()); 128014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 129014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var o1 = {f: function(x = this) { return x; }}; 130014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(o1, o1.f()); 131014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(1, o1.f(1)); 132014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 133014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 134014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestParameterScopingStrict() { 135014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch "use strict"; 136014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var x = 1; 137014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 138014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(a = x) { let x = 2; return a; } 139014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f1()); 140014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(a = x) { const x = 2; return a; } 141014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f2()); 142014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f3(a = x) { function x() {}; return a; } 143014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f3()); 144014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f4(a = eval("x")) { var x; return a; } 145014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f4()); 146014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f5(a = () => eval("x")) { var x; return a(); } 147014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f5()); 148014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 149014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g1 = (a = x) => { let x = 2; return a; }; 150014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g1()); 151014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g2 = (a = x) => { const x = 2; return a; }; 152014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g2()); 153014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g3 = (a = x) => { function x() {}; return a; }; 154014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g3()); 155014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g4 = (a = eval("x")) => { var x; return a; }; 156014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g4()); 157014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g5 = (a = () => eval("x")) => { var x; return a(); }; 158014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g5()); 159014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 160014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var f11 = function f(x = f) { let f; return x; } 161014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(f11, f11()); 162014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var f12 = function f(x = f) { const f = 0; return x; } 163014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(f12, f12()); 164014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var f13 = function f(x = f) { function f() {}; return x; } 165014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertSame(f13, f13()); 166014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 167014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 168014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestSloppyEvalScoping() { 169014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var x = 1; 170014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 171014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(y = eval("var x = 2")) { with ({}) { return x; } } 172014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f1()); 173014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(y = eval("var x = 2"), z = x) { return z; } 174014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f2()); 175014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f2(0)); 176014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f3(y = eval("var x = 2"), z = eval("x")) { return z; } 177014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f3()); 178014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f3(0)); 179014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f8(y = (eval("var x = 2"), x)) { return y; } 180014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, f8()); 181014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(0, f8(0)); 182014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 183014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f11(z = eval("var y = 2")) { return y; } 184014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(f11, ReferenceError); 185014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f12(z = eval("var y = 2"), b = y) {} 186014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(f12, ReferenceError); 187014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f13(z = eval("var y = 2"), b = eval("y")) {} 188014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(f13, ReferenceError); 189014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 190014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f21(f = () => x) { eval("var x = 2"); return f() } 191014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f21()); 192014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, f21(() => 3)); 193014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f22(f = () => eval("x")) { eval("var x = 2"); return f() } 194014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f22()); 195014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, f22(() => 3)); 196014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 197014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g1 = (y = eval("var x = 2")) => { with ({}) { return x; } }; 198014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g1()); 199014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g2 = (y = eval("var x = 2"), z = x) => { return z; }; 200014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g2()); 201014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g2(0)); 202014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g3 = (y = eval("var x = 2"), z = eval("x")) => { return z; }; 203014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g3()); 204014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g3(0)); 205014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g8 = (y = (eval("var x = 2"), x)) => { return y; }; 206014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, g8()); 207014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(0, g8(0)); 208014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 209014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g11 = (z = eval("var y = 2")) => { return y; }; 210014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(g11, ReferenceError); 211014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g12 = (z = eval("var y = 2"), b = y) => {}; 212014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(g12, ReferenceError); 213014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g13 = (z = eval("var y = 2"), b = eval("y")) => {}; 214014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(g13, ReferenceError); 215014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 216014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g21 = (f = () => x) => { eval("var x = 2"); return f() }; 217014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g21()); 218014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, g21(() => 3)); 219014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g22 = (f = () => eval("x")) => { eval("var x = 2"); return f() }; 220014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, g22()); 221014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, g22(() => 3)); 222014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 223014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 224014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 225014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestStrictEvalScoping() { 226014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 'use strict'; 227014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var x = 1; 228014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 229014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(y = eval("var x = 2")) { return x; } 230014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f1()); 231014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(y = eval("var x = 2"), z = x) { return z; } 232014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f2()); 233014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f2(0)); 234014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f3(y = eval("var x = 2"), z = eval("x")) { return z; } 235014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f3()); 236014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f3(0)); 237014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f8(y = (eval("var x = 2"), x)) { return y; } 238014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f8()); 239014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(0, f8(0)); 240014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 241014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f11(z = eval("var y = 2")) { return y; } 242014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(f11, ReferenceError); 243014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f12(z = eval("var y = 2"), b = y) {} 244014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(f12, ReferenceError); 245014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f13(z = eval("var y = 2"), b = eval("y")) {} 246014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(f13, ReferenceError); 247014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 248014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f21(f = () => x) { eval("var x = 2"); return f() } 249014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f21()); 250014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, f21(() => 3)); 251014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f22(f = () => eval("x")) { eval("var x = 2"); return f() } 252014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, f22()); 253014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(3, f22(() => 3)); 254014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 255014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 256014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestParameterTDZSloppy() { 257014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(a = x, x) { return a } 258014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f1(undefined, 4), ReferenceError); 259014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f1(4, 5)); 260014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(a = eval("x"), x) { return a } 261014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f2(undefined, 4), ReferenceError); 262014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f2(4, 5)); 263014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f3(a = eval("'use strict'; x"), x) { return a } 264014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f3(undefined, 4), ReferenceError); 265014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f3(4, 5)); 266014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f4(a = () => x, x) { return a() } 267014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f4(() => 4, 5)); 268014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f5(a = () => eval("x"), x) { return a() } 269014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f5(() => 4, 5)); 270014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f6(a = () => eval("'use strict'; x"), x) { return a() } 271014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f6(() => 4, 5)); 272014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 273014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f11(a = x, x = 2) { return a } 274014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f11(), ReferenceError); 275014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f11(undefined), ReferenceError); 276014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f11(undefined, 4), ReferenceError); 277014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f1(4, 5)); 278014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f12(a = eval("x"), x = 2) { return a } 279014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f12(), ReferenceError); 280014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f12(undefined), ReferenceError); 281014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f12(undefined, 4), ReferenceError); 282014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f12(4, 5)); 283014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f13(a = eval("'use strict'; x"), x = 2) { return a } 284014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f13(), ReferenceError); 285014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f13(undefined), ReferenceError); 286014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f13(undefined, 4), ReferenceError); 287014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f13(4, 5)); 288014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 289014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f21(x = function() { return a }, ...a) { return x()[0] } 290014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f21(undefined, 4)); 291014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f22(x = () => a, ...a) { return x()[0] } 292014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f22(undefined, 4)); 293014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f23(x = () => eval("a"), ...a) { return x()[0] } 294014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f23(undefined, 4)); 295014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f24(x = () => {'use strict'; return eval("a") }, ...a) { 296014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch return x()[0] 297014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 298014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f24(undefined, 4)); 299014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f25(x = () => eval("'use strict'; a"), ...a) { return x()[0] } 300014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f25(undefined, 4)); 301014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 302014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g1 = (x = function() { return a }, ...a) => { return x()[0] }; 303014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, g1(undefined, 4)); 304014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var g2 = (x = () => a, ...a) => { return x()[0] }; 305014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, g2(undefined, 4)); 306014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 307014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 308014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestParameterTDZStrict() { 309014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch "use strict"; 310014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 311014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(a = eval("x"), x) { return a } 312014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f1(undefined, 4), ReferenceError); 313014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f1(4, 5)); 314014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(a = () => eval("x"), x) { return a() } 315014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f2(() => 4, 5)); 316014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 317014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f11(a = eval("x"), x = 2) { return a } 318014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f11(), ReferenceError); 319014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f11(undefined), ReferenceError); 320014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(() => f11(undefined, 4), ReferenceError); 321014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f11(4, 5)); 322014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 323014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f21(x = () => eval("a"), ...a) { return x()[0] } 324014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(4, f21(undefined, 4)); 325014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 326014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 327014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestArgumentsForNonSimpleParameters() { 328014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f1(x = 900) { arguments[0] = 1; return x } 329014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(9, f1(9)); 330014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(900, f1()); 331014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function f2(x = 1001) { x = 2; return arguments[0] } 332014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(10, f2(10)); 333014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(undefined, f2()); 334014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}()); 335014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 336014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 337014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestFunctionLength() { 338014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(0, (function(x = 1) {}).length); 339014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(0, (function(x = 1, ...a) {}).length); 340014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, (function(x, y = 1) {}).length); 341014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, (function(x, y = 1, ...a) {}).length); 342014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, (function(x, y, z = 1) {}).length); 343014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(2, (function(x, y, z = 1, ...a) {}).length); 344014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, (function(x, y = 1, z) {}).length); 345014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, (function(x, y = 1, z, ...a) {}).length); 346014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, (function(x, y = 1, z, v = 2) {}).length); 347014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(1, (function(x, y = 1, z, v = 2, ...a) {}).length); 348014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 349014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 350014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch(function TestDirectiveThrows() { 351014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch "use strict"; 352014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 353014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(function(){ eval("function(x=1){'use strict';}") }, SyntaxError); 354014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(function(){ eval("(x=1) => {'use strict';}") }, SyntaxError); 355014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows( 356014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function(){ eval("(class{foo(x=1) {'use strict';}});") }, SyntaxError); 357014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 358014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows( 359014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function(){ eval("function(a, x=1){'use strict';}") }, SyntaxError); 360014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows(function(){ eval("(a, x=1) => {'use strict';}") }, SyntaxError); 361014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertThrows( 362014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch function(){ eval("(class{foo(a, x=1) {'use strict';}});") }, SyntaxError); 363014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 364