Diff
Modified: trunk/LayoutTests/ChangeLog (198988 => 198989)
--- trunk/LayoutTests/ChangeLog 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/LayoutTests/ChangeLog 2016-04-03 19:45:05 UTC (rev 198989)
@@ -1,3 +1,19 @@
+2016-04-03 Saam barati <[email protected]>
+
+ Implement Annex B.3.3 function hoisting rules for function code
+ https://bugs.webkit.org/show_bug.cgi?id=155672
+
+ Reviewed by Geoffrey Garen.
+
+ * js/kde/func-decl-expected.txt:
+ * js/kde/script-tests/func-decl.js:
+ * js/parser-syntax-check-expected.txt:
+ * js/script-tests/parser-syntax-check.js:
+ (valid):
+ (onlyValidGlobally):
+ (onlyInvalidGlobally):
+ (invalid):
+
2016-04-03 David Kilzer <[email protected]>
REGRESSION (r198859): fast/scrolling/rtl-scrollbars-animation-property.html fails on non-Mac platforms
Modified: trunk/LayoutTests/js/function-declaration-statement-expected.txt (198988 => 198989)
--- trunk/LayoutTests/js/function-declaration-statement-expected.txt 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/LayoutTests/js/function-declaration-statement-expected.txt 2016-04-03 19:45:05 UTC (rev 198989)
@@ -5,14 +5,6 @@
PASS ifTest() is true
PASS ifElseTest() is true
-PASS doWhileTest() is true
-PASS whileTest() is true
-PASS forTest() is true
-PASS forVarTest() is true
-PASS forInTest() is true
-PASS forInVarTest() is true
-PASS forInVarInitTest() is true
-PASS withTest() is true
PASS labelTest() is true
PASS successfullyParsed is true
Modified: trunk/LayoutTests/js/kde/func-decl-expected.txt (198988 => 198989)
--- trunk/LayoutTests/js/kde/func-decl-expected.txt 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/LayoutTests/js/kde/func-decl-expected.txt 2016-04-03 19:45:05 UTC (rev 198989)
@@ -3,8 +3,8 @@
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
-PASS Function declaration takes effect at entry
-PASS Decl not yet overwritten
+PASS Function declaration takes effect at declaration block
+PASS Decl already overwritten
PASS After assign (0)
PASS function decls have no execution content
PASS After assign #2 (0)
Modified: trunk/LayoutTests/js/kde/script-tests/func-decl.js (198988 => 198989)
--- trunk/LayoutTests/js/kde/script-tests/func-decl.js 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/LayoutTests/js/kde/script-tests/func-decl.js 2016-04-03 19:45:05 UTC (rev 198989)
@@ -17,17 +17,14 @@
function test() {
try {
- shouldBeOfType("Function declaration takes effect at entry", f, "function");
+ shouldBeOfType("Function declaration takes effect at declaration block", f, "undefined");
}
catch (e) {
testFailed("Scoping very broken!");
}
for (var i = 0; i < 3; ++i) {
- if (i == 0)
- shouldBeOfType("Decl not yet overwritten", f, 'function');
- else
- shouldBeOfType("Decl already overwritten", f, 'number');
+ shouldBeOfType("Decl already overwritten", f, 'function');
f = 3;
shouldBeVal("After assign ("+i+")", f, 3);
Modified: trunk/LayoutTests/js/parser-syntax-check-expected.txt (198988 => 198989)
--- trunk/LayoutTests/js/parser-syntax-check-expected.txt 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/LayoutTests/js/parser-syntax-check-expected.txt 2016-04-03 19:45:05 UTC (rev 198989)
@@ -302,8 +302,8 @@
PASS Invalid: "function f() { while if (a) ; }"
PASS Valid: "if (a) function f() {} else function g() {}"
PASS Valid: "function f() { if (a) function f() {} else function g() {} }"
-PASS Valid: "if (a()) while(0) function f() {} else function g() {}" with TypeError
-PASS Valid: "function f() { if (a()) while(0) function f() {} else function g() {} }"
+PASS Invalid: "if (a()) while(0) function f() {} else function g() {}"
+PASS Invalid: "function f() { if (a()) while(0) function f() {} else function g() {} }"
PASS Invalid: "if (a()) function f() { else function g() }"
PASS Invalid: "function f() { if (a()) function f() { else function g() } }"
PASS Invalid: "if (a) if (b) ; else function f {}"
@@ -676,16 +676,50 @@
PASS Invalid: "function f() { 'use strict'; function f1(a) {}; let f1; }"
PASS Invalid: "let f1; function f1(a) {};"
PASS Invalid: "function f() { let f1; function f1(a) {}; }"
-PASS Invalid: "let f1; { function f1(a) {}; }"
-PASS Invalid: "function f() { let f1; { function f1(a) {}; } }"
-PASS Invalid: "{ function f1(a) {}; } let f1;"
-PASS Invalid: "function f() { { function f1(a) {}; } let f1; }"
-PASS Valid: "{ let f1; function f1(a) {}; }"
-PASS Valid: "function f() { { let f1; function f1(a) {}; } }"
-PASS Valid: "{ function f1(a) {}; let f1; }"
-PASS Valid: "function f() { { function f1(a) {}; let f1; } }"
+PASS Valid: "function foo() { let f1; { function f1(a) {}; } }"
+PASS Valid: "function f() { function foo() { let f1; { function f1(a) {}; } } }"
+PASS Valid: "function foo() { { function f1(a) {}; } let f1; }"
+PASS Valid: "function f() { function foo() { { function f1(a) {}; } let f1; } }"
+PASS Valid: "function foo() { { function foo() { }; function foo() { } } }"
+PASS Valid: "function f() { function foo() { { function foo() { }; function foo() { } } } }"
+PASS Invalid: "function foo() { 'use strict'; { function foo() { }; function foo() { } } }"
+PASS Invalid: "function f() { function foo() { 'use strict'; { function foo() { }; function foo() { } } } }"
+PASS Invalid: "function foo() { let f1; function f1(a) {}; }"
+PASS Invalid: "function f() { function foo() { let f1; function f1(a) {}; } }"
+PASS Invalid: "let f1; function f1(a) {};"
+PASS Invalid: "function f() { let f1; function f1(a) {}; }"
+PASS Valid: "{ function f1(a) {}; let f1; }"
+PASS Invalid: "function f() { { function f1(a) {}; let f1; } }"
+PASS Valid: "{ function f1(a) {}; const f1 = 25; }"
+PASS Invalid: "function f() { { function f1(a) {}; const f1 = 25; } }"
+PASS Valid: "{ function f1(a) {}; class f1{}; }"
+PASS Invalid: "function f() { { function f1(a) {}; class f1{}; } }"
+PASS Invalid: "function foo() { { let bar; function bar() { } } }"
+PASS Invalid: "function f() { function foo() { { let bar; function bar() { } } } }"
+PASS Invalid: "function foo() { { function bar() { }; let bar; } }"
+PASS Invalid: "function f() { function foo() { { function bar() { }; let bar; } } }"
+PASS Invalid: "function foo() { { const bar; function bar() { } } }"
+PASS Invalid: "function f() { function foo() { { const bar; function bar() { } } } }"
+PASS Invalid: "function foo() { { function bar() { }; const bar; } }"
+PASS Invalid: "function f() { function foo() { { function bar() { }; const bar; } } }"
+PASS Invalid: "function foo() { { class bar{}; function bar() { } } }"
+PASS Invalid: "function f() { function foo() { { class bar{}; function bar() { } } } }"
+PASS Invalid: "function foo() { { function bar() { }; class bar{}; } }"
+PASS Invalid: "function f() { function foo() { { function bar() { }; class bar{}; } } }"
PASS Valid: "switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; }"
PASS Valid: "function f() { switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; } }"
+PASS Valid: "switch('foo') { case 1: let foo; function foo() {}; break; case 2: function foo() {}; break; }"
+PASS Invalid: "function f() { switch('foo') { case 1: let foo; function foo() {}; break; case 2: function foo() {}; break; } }"
+PASS Valid: "switch('foo') { case 1: function foo() {}; let foo; break; case 2: function foo() {}; break; }"
+PASS Invalid: "function f() { switch('foo') { case 1: function foo() {}; let foo; break; case 2: function foo() {}; break; } }"
+PASS Valid: "switch('foo') { case 1: function foo() {}; const foo = 25; break; case 2: function foo() {}; break; }"
+PASS Invalid: "function f() { switch('foo') { case 1: function foo() {}; const foo = 25; break; case 2: function foo() {}; break; } }"
+PASS Valid: "switch('foo') { case 1: function foo() {}; class foo {} ; break; case 2: function foo() {}; break; }"
+PASS Invalid: "function f() { switch('foo') { case 1: function foo() {}; class foo {} ; break; case 2: function foo() {}; break; } }"
+PASS Valid: "switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; case 3: let foo; }"
+PASS Invalid: "function f() { switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; case 3: let foo; } }"
+PASS Valid: "function foo() { switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; case 3: { let foo; } } }"
+PASS Valid: "function f() { function foo() { switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; case 3: { let foo; } } } }"
PASS Invalid: "'use strict'; switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; }"
PASS Invalid: "function f() { 'use strict'; switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; } }"
PASS Invalid: "'use strict'; switch('foo') { case 1: function foo() {}; break; case 2: let foo; break; }"
@@ -700,6 +734,74 @@
PASS Invalid: "function f() { 'use strict'; if (true) function foo() { }; }"
PASS Valid: "if (true) function foo() { }; "
PASS Valid: "function f() { if (true) function foo() { }; }"
+PASS Invalid: " let foo; if (true) function foo() { };"
+PASS Valid: "function f() { let foo; if (true) function foo() { }; }"
+PASS Valid: "function baz() { let foo; if (true) function foo() { }; }"
+PASS Valid: "function f() { function baz() { let foo; if (true) function foo() { }; } }"
+PASS Invalid: "if (true) function foo() { }; let foo;"
+PASS Valid: "function f() { if (true) function foo() { }; let foo; }"
+PASS Invalid: "{ if (true) function foo() { }; } let foo;"
+PASS Valid: "function f() { { if (true) function foo() { }; } let foo; }"
+PASS Invalid: "let foo; while (false) function foo() { }; "
+PASS Invalid: "function f() { let foo; while (false) function foo() { }; }"
+PASS Invalid: "let foo; { while (false) function foo() { }; } "
+PASS Invalid: "function f() { let foo; { while (false) function foo() { }; } }"
+PASS Invalid: "while (false) function foo() { }; let foo;"
+PASS Invalid: "function f() { while (false) function foo() { }; let foo; }"
+PASS Invalid: "let foo; while (false) label: function foo() { }; "
+PASS Invalid: "function f() { let foo; while (false) label: function foo() { }; }"
+PASS Invalid: "while (false) label: function foo() { }; let foo;"
+PASS Invalid: "function f() { while (false) label: function foo() { }; let foo; }"
+PASS Invalid: "'use strict'; while (false) function foo() { }; "
+PASS Invalid: "function f() { 'use strict'; while (false) function foo() { }; }"
+PASS Invalid: "'use strict'; if (false) function foo() { }; "
+PASS Invalid: "function f() { 'use strict'; if (false) function foo() { }; }"
+PASS Invalid: "'use strict'; do function foo() { } while (false); "
+PASS Invalid: "function f() { 'use strict'; do function foo() { } while (false); }"
+PASS Invalid: "while (false) function foo() { }; "
+PASS Invalid: "function f() { while (false) function foo() { }; }"
+PASS Valid: "if (false) function foo() { }; "
+PASS Valid: "function f() { if (false) function foo() { }; }"
+PASS Invalid: "do function foo() { } while (false); "
+PASS Invalid: "function f() { do function foo() { } while (false); }"
+PASS Invalid: "if (cond) label: function foo() { }"
+PASS Invalid: "function f() { if (cond) label: function foo() { } }"
+PASS Invalid: "while (true) { while (true) function bar() { } }"
+PASS Invalid: "function f() { while (true) { while (true) function bar() { } } }"
+PASS Invalid: "with ({}) function bar() { }"
+PASS Invalid: "function f() { with ({}) function bar() { } }"
+PASS Valid: "function bar() { label: function baz() { } }"
+PASS Valid: "function f() { function bar() { label: function baz() { } } }"
+PASS Valid: "function bar() { let: function baz() { } }"
+PASS Valid: "function f() { function bar() { let: function baz() { } } }"
+PASS Invalid: "function bar() { 'use strict'; let: function baz() { } }"
+PASS Invalid: "function f() { function bar() { 'use strict'; let: function baz() { } } }"
+PASS Valid: "function bar() { yield: function baz() { } }"
+PASS Valid: "function f() { function bar() { yield: function baz() { } } }"
+PASS Valid: "function bar() { label: label2: function baz() { } }"
+PASS Valid: "function f() { function bar() { label: label2: function baz() { } } }"
+PASS Valid: "function bar() { label: label2: label3: function baz() { } }"
+PASS Valid: "function f() { function bar() { label: label2: label3: function baz() { } } }"
+PASS Invalid: "function bar() { label: label2: label weird: function baz() { } }"
+PASS Invalid: "function f() { function bar() { label: label2: label weird: function baz() { } } }"
+PASS Valid: "function bar() { label: label2: label3: function baz() { } }"
+PASS Valid: "function f() { function bar() { label: label2: label3: function baz() { } } }"
+PASS Invalid: "function bar() { 'use strict'; label: label2: label 3: function baz() { } }"
+PASS Invalid: "function f() { function bar() { 'use strict'; label: label2: label 3: function baz() { } } }"
+PASS Invalid: "function bar() { if (cond) label: function foo() { } }"
+PASS Invalid: "function f() { function bar() { if (cond) label: function foo() { } } }"
+PASS Invalid: "function bar() { while (cond) label: function foo() { } }"
+PASS Invalid: "function f() { function bar() { while (cond) label: function foo() { } } }"
+PASS Valid: "label: function foo() { }"
+PASS Valid: "function f() { label: function foo() { } }"
+PASS Valid: "let: function foo() { }"
+PASS Valid: "function f() { let: function foo() { } }"
+PASS Valid: "yield: function foo() { }"
+PASS Valid: "function f() { yield: function foo() { } }"
+PASS Valid: "yield: let: function foo() { }"
+PASS Valid: "function f() { yield: let: function foo() { } }"
+PASS Invalid: "'use strict'; yield: let: function foo() { }"
+PASS Invalid: "function f() { 'use strict'; yield: let: function foo() { } }"
PASS Valid: "var str = "'use strict'; function f1(a) { function f2(b) { return b; } return f2(a); } return f1(arguments[0]);"; var foo = new Function(str); foo(5);"
PASS Valid: "function f() { var str = "'use strict'; function f1(a) { function f2(b) { return b; } return f2(a); } return f1(arguments[0]);"; var foo = new Function(str); foo(5); }"
PASS Valid: "var str = "'use strict'; function f1(a) { function f2(b) { function f3(c) { return c; } return f3(b); } return f2(a); } return f1(arguments[0]);"; var foo = new Function(str); foo(5);"
Modified: trunk/LayoutTests/js/script-tests/function-declaration-statement.js (198988 => 198989)
--- trunk/LayoutTests/js/script-tests/function-declaration-statement.js 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/LayoutTests/js/script-tests/function-declaration-statement.js 2016-04-03 19:45:05 UTC (rev 198989)
@@ -35,117 +35,6 @@
shouldBeTrue("ifElseTest()");
-function doWhileTest()
-{
- var i = 0;
- do
- function f()
- {
- return true;
- }
- while (i++ < 10)
-
- return f();
-}
-
-shouldBeTrue("doWhileTest()");
-
-function whileTest()
-{
- var i = 0;
- while (i++ < 10)
- function f()
- {
- return true;
- }
-
- return f();
-}
-
-shouldBeTrue("whileTest()");
-
-function forTest()
-{
- var i;
- for (i = 0; i < 10; ++i)
- function f()
- {
- return true;
- }
-
- return f();
-}
-
-shouldBeTrue("forTest()");
-
-function forVarTest()
-{
- for (var i = 0; i < 10; ++i)
- function f()
- {
- return true;
- }
-
- return f();
-}
-
-shouldBeTrue("forVarTest()");
-
-function forInTest()
-{
- var a;
- for (a in { field: false })
- function f()
- {
- return true;
- }
-
- return f();
-}
-
-shouldBeTrue("forInTest()");
-
-function forInVarTest()
-{
- var a;
- for (var a in { field: false })
- function f()
- {
- return true;
- }
-
- return f();
-}
-
-shouldBeTrue("forInVarTest()");
-
-function forInVarInitTest()
-{
- var a;
- for (var a in { field: false })
- function f()
- {
- return true;
- }
-
- return f();
-}
-
-shouldBeTrue("forInVarInitTest()");
-
-function withTest()
-{
- with ({ })
- function f()
- {
- return true;
- }
-
- return f();
-}
-
-shouldBeTrue("withTest()");
-
function labelTest()
{
label:
Modified: trunk/LayoutTests/js/script-tests/parser-syntax-check.js (198988 => 198989)
--- trunk/LayoutTests/js/script-tests/parser-syntax-check.js 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/LayoutTests/js/script-tests/parser-syntax-check.js 2016-04-03 19:45:05 UTC (rev 198989)
@@ -43,6 +43,18 @@
runTest("function f() { " + _a + " }", false);
}
+function onlyValidGlobally(_a)
+{
+ runTest(_a, false);
+ runTest("function f() { " + _a + " }", true);
+}
+
+function onlyInvalidGlobally(_a)
+{
+ runTest(_a, true);
+ runTest("function f() { " + _a + " }", false);
+}
+
function invalid(_a)
{
// Test both the grammar and the syntax checker
@@ -226,7 +238,7 @@
invalid("do if (a) while (false) else debugger");
invalid("while if (a) ;");
valid ("if (a) function f() {} else function g() {}");
-valid ("if (a()) while(0) function f() {} else function g() {}");
+invalid("if (a()) while(0) function f() {} else function g() {}");
invalid("if (a()) function f() { else function g() }");
invalid("if (a) if (b) ; else function f {}");
invalid("if (a) if (b) ; else function (){}");
@@ -428,11 +440,28 @@
invalid("'use strict'; let f1; function f1(a) {};")
invalid("'use strict'; function f1(a) {}; let f1; ")
invalid("let f1; function f1(a) {};")
-invalid("let f1; { function f1(a) {}; }")
-invalid("{ function f1(a) {}; } let f1;")
-valid("{ let f1; function f1(a) {}; }")
-valid("{ function f1(a) {}; let f1; }")
+valid("function foo() { let f1; { function f1(a) {}; } }")
+valid("function foo() { { function f1(a) {}; } let f1; }")
+valid("function foo() { { function foo() { }; function foo() { } } }")
+invalid("function foo() { 'use strict'; { function foo() { }; function foo() { } } }")
+invalid("function foo() { let f1; function f1(a) {}; }")
+invalid("let f1; function f1(a) {};")
+onlyValidGlobally("{ function f1(a) {}; let f1; }")
+onlyValidGlobally("{ function f1(a) {}; const f1 = 25; }")
+onlyValidGlobally("{ function f1(a) {}; class f1{}; }")
+invalid("function foo() { { let bar; function bar() { } } }")
+invalid("function foo() { { function bar() { }; let bar; } }")
+invalid("function foo() { { const bar; function bar() { } } }")
+invalid("function foo() { { function bar() { }; const bar; } }")
+invalid("function foo() { { class bar{}; function bar() { } } }")
+invalid("function foo() { { function bar() { }; class bar{}; } }")
valid("switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; }")
+onlyValidGlobally("switch('foo') { case 1: let foo; function foo() {}; break; case 2: function foo() {}; break; }")
+onlyValidGlobally("switch('foo') { case 1: function foo() {}; let foo; break; case 2: function foo() {}; break; }")
+onlyValidGlobally("switch('foo') { case 1: function foo() {}; const foo = 25; break; case 2: function foo() {}; break; }")
+onlyValidGlobally("switch('foo') { case 1: function foo() {}; class foo {} ; break; case 2: function foo() {}; break; }")
+onlyValidGlobally("switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; case 3: let foo; }")
+valid("function foo() { switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; case 3: { let foo; } } }")
invalid("'use strict'; switch('foo') { case 1: function foo() {}; break; case 2: function foo() {}; break; }");
invalid("'use strict'; switch('foo') { case 1: function foo() {}; break; case 2: let foo; break; }");
invalid("'use strict'; switch('foo') { case 1: let foo; break; case 2: function foo() {}; break; }");
@@ -440,6 +469,40 @@
valid("'use strict'; switch('foo') { case 1: { function foo() { }; break; } case 2: function foo() {}; break; }");
invalid("'use strict'; if (true) function foo() { }; ");
valid("if (true) function foo() { }; ");
+onlyInvalidGlobally(" let foo; if (true) function foo() { };");
+valid("function baz() { let foo; if (true) function foo() { }; }");
+onlyInvalidGlobally("if (true) function foo() { }; let foo;");
+onlyInvalidGlobally("{ if (true) function foo() { }; } let foo;");
+invalid("let foo; while (false) function foo() { }; ");
+invalid("let foo; { while (false) function foo() { }; } ");
+invalid("while (false) function foo() { }; let foo;");
+invalid("let foo; while (false) label: function foo() { }; ");
+invalid("while (false) label: function foo() { }; let foo;");
+invalid("'use strict'; while (false) function foo() { }; ");
+invalid("'use strict'; if (false) function foo() { }; ");
+invalid("'use strict'; do function foo() { } while (false); ");
+invalid("while (false) function foo() { }; ");
+valid("if (false) function foo() { }; ");
+invalid("do function foo() { } while (false); ");
+invalid("if (cond) label: function foo() { }");
+invalid("while (true) { while (true) function bar() { } }");
+invalid("with ({}) function bar() { }");
+valid("function bar() { label: function baz() { } }");
+valid("function bar() { let: function baz() { } }");
+invalid("function bar() { 'use strict'; let: function baz() { } }");
+valid("function bar() { yield: function baz() { } }");
+valid("function bar() { label: label2: function baz() { } }");
+valid("function bar() { label: label2: label3: function baz() { } }");
+invalid("function bar() { label: label2: label weird: function baz() { } }");
+valid("function bar() { label: label2: label3: function baz() { } }");
+invalid("function bar() { 'use strict'; label: label2: label 3: function baz() { } }");
+invalid("function bar() { if (cond) label: function foo() { } }");
+invalid("function bar() { while (cond) label: function foo() { } }");
+valid("label: function foo() { }");
+valid("let: function foo() { }");
+valid("yield: function foo() { }");
+valid("yield: let: function foo() { }");
+invalid("'use strict'; yield: let: function foo() { }");
valid("var str = \"'use strict'; function f1(a) { function f2(b) { return b; } return f2(a); } return f1(arguments[0]);\"; var foo = new Function(str); foo(5);")
valid("var str = \"'use strict'; function f1(a) { function f2(b) { function f3(c) { return c; } return f3(b); } return f2(a); } return f1(arguments[0]);\"; var foo = new Function(str); foo(5);")
Modified: trunk/Source/_javascript_Core/ChangeLog (198988 => 198989)
--- trunk/Source/_javascript_Core/ChangeLog 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/ChangeLog 2016-04-03 19:45:05 UTC (rev 198989)
@@ -1,3 +1,138 @@
+2016-04-03 Saam barati <[email protected]>
+
+ Implement Annex B.3.3 function hoisting rules for function code
+ https://bugs.webkit.org/show_bug.cgi?id=155672
+
+ Reviewed by Geoffrey Garen.
+
+ The spec states that functions declared inside a function
+ inside a block scope are subject to the rules of Annex B.3.3:
+ https://tc39.github.io/ecma262/#sec-block-level-function-declarations-web-legacy-compatibility-semantics
+
+ The rule states that functions declared in such blocks should
+ be local bindings of the block. If declaring the function's name
+ as a "var" in the function would not lead to a syntax error (i.e,
+ if we don't have a let/const/class variable with the same name)
+ and if we don't have a parameter with the same name, then we
+ implictly also declare the funcion name as a "var". When evaluating
+ the block statement we bind the hoisted "var" to be the value
+ of the local function binding.
+
+ There is one more thing we do for web compatibility. We allow
+ function declarations inside if/else statements that aren't
+ blocks. For such statements, we transform the code as if the
+ function were declared inside a block statement. For example:
+ ``` function foo() { if (cond) function baz() { } }```
+ is transformed into:
+ ``` function foo() { if (cond) { function baz() { } } }```
+
+ * bytecompiler/BytecodeGenerator.cpp:
+ (JSC::BytecodeGenerator::initializeDefaultParameterValuesAndSetupFunctionScopeStack):
+ (JSC::BytecodeGenerator::initializeBlockScopedFunctions):
+ * bytecompiler/BytecodeGenerator.h:
+ * parser/Nodes.cpp:
+ (JSC::ScopeNode::ScopeNode):
+ (JSC::ProgramNode::ProgramNode):
+ (JSC::ModuleProgramNode::ModuleProgramNode):
+ (JSC::EvalNode::EvalNode):
+ (JSC::FunctionNode::FunctionNode):
+ * parser/Nodes.h:
+ (JSC::ScopeNode::hasCapturedVariables):
+ (JSC::ScopeNode::captures):
+ (JSC::ScopeNode::hasSloppyModeHoistedFunction):
+ (JSC::ScopeNode::varDeclarations):
+ (JSC::ProgramNode::startColumn):
+ (JSC::ProgramNode::endColumn):
+ (JSC::EvalNode::startColumn):
+ (JSC::EvalNode::endColumn):
+ (JSC::ModuleProgramNode::startColumn):
+ (JSC::ModuleProgramNode::endColumn):
+ * parser/Parser.cpp:
+ (JSC::Parser<LexerType>::Parser):
+ (JSC::Parser<LexerType>::parseInner):
+ (JSC::Parser<LexerType>::didFinishParsing):
+ (JSC::Parser<LexerType>::parseStatement):
+ (JSC::Parser<LexerType>::parseIfStatement):
+ * parser/Parser.h:
+ (JSC::Scope::declareVariable):
+ (JSC::Scope::declareFunction):
+ (JSC::Scope::addSloppyModeHoistableFunctionCandidate):
+ (JSC::Scope::appendFunction):
+ (JSC::Scope::declareParameter):
+ (JSC::Scope::mergeInnerArrowFunctionFeatures):
+ (JSC::Scope::getSloppyModeHoistedFunctions):
+ (JSC::Scope::getCapturedVars):
+ (JSC::ScopeRef::containingScope):
+ (JSC::ScopeRef::operator==):
+ (JSC::ScopeRef::operator!=):
+ (JSC::Parser::declareFunction):
+ (JSC::Parser::hasDeclaredVariable):
+ (JSC::Parser::isFunctionMetadataNode):
+ (JSC::Parser::DepthManager::DepthManager):
+ (JSC::Parser<LexerType>::parse):
+ * parser/VariableEnvironment.h:
+ (JSC::VariableEnvironmentEntry::isImported):
+ (JSC::VariableEnvironmentEntry::isImportedNamespace):
+ (JSC::VariableEnvironmentEntry::isFunction):
+ (JSC::VariableEnvironmentEntry::isParameter):
+ (JSC::VariableEnvironmentEntry::isSloppyModeHoistingCandidate):
+ (JSC::VariableEnvironmentEntry::setIsCaptured):
+ (JSC::VariableEnvironmentEntry::setIsConst):
+ (JSC::VariableEnvironmentEntry::setIsImported):
+ (JSC::VariableEnvironmentEntry::setIsImportedNamespace):
+ (JSC::VariableEnvironmentEntry::setIsFunction):
+ (JSC::VariableEnvironmentEntry::setIsParameter):
+ (JSC::VariableEnvironmentEntry::setIsSloppyModeHoistingCandidate):
+ (JSC::VariableEnvironmentEntry::clearIsVar):
+ * runtime/CodeCache.h:
+ (JSC::SourceCodeValue::SourceCodeValue):
+ * runtime/JSScope.cpp:
+ * runtime/JSScope.h:
+ * tests/es6.yaml:
+ * tests/stress/sloppy-mode-function-hoisting.js: Added.
+ (assert):
+ (test):
+ (falsey):
+ (truthy):
+ (test.):
+ (test.a):
+ (test.f):
+ (test.let.funcs.f):
+ (test.catch.f):
+ (test.foo):
+ (test.bar):
+ (test.switch.case.0):
+ (test.else.f):
+ (test.b):
+ (test.c):
+ (test.d):
+ (test.e):
+ (test.g):
+ (test.h):
+ (test.i):
+ (test.j):
+ (test.k):
+ (test.l):
+ (test.m):
+ (test.n):
+ (test.o):
+ (test.p):
+ (test.q):
+ (test.r):
+ (test.s):
+ (test.t):
+ (test.u):
+ (test.v):
+ (test.w):
+ (test.x):
+ (test.y):
+ (test.z):
+ (foo):
+ (bar):
+ (falsey.bar):
+ (baz):
+ (falsey.baz):
+
2016-04-03 Yusuke Suzuki <[email protected]>
Unreviewed, turn ES6 for-in loop test success
Modified: trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.cpp (198988 => 198989)
--- trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.cpp 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.cpp 2016-04-03 19:45:05 UTC (rev 198989)
@@ -861,6 +861,8 @@
pushScopedControlFlowContext();
m_symbolTableStack.append(SymbolTableStackEntry{ Strong<SymbolTable>(*m_vm, functionSymbolTable), m_lexicalEnvironmentRegister, false, symbolTableConstantIndex });
+ m_varScopeSymbolTableIndex = m_symbolTableStack.size() - 1;
+
// This completes step 28 of section 9.2.12.
for (unsigned i = 0; i < valuesToMoveIntoVars.size(); i++) {
ASSERT(!isSimpleParameterList);
@@ -1931,6 +1933,18 @@
emitNewFunctionExpressionCommon(temp.get(), function);
bool isLexicallyScoped = true;
emitPutToScope(scope, variableForLocalEntry(name, entry, symbolTableIndex, isLexicallyScoped), temp.get(), DoNotThrowIfNotFound, Initialization);
+
+ if (iter->value.isSloppyModeHoistingCandidate() && m_scopeNode->hasSloppyModeHoistedFunction(name.impl())) {
+ ASSERT(m_varScopeSymbolTableIndex);
+ ASSERT(*m_varScopeSymbolTableIndex < m_symbolTableStack.size());
+ SymbolTableStackEntry& varScope = m_symbolTableStack[*m_varScopeSymbolTableIndex];
+ SymbolTable* varSymbolTable = varScope.m_symbolTable.get();
+ RELEASE_ASSERT(varSymbolTable->scopeType() == SymbolTable::ScopeType::VarScope);
+ SymbolTableEntry entry = varSymbolTable->get(name.impl());
+ RELEASE_ASSERT(!entry.isNull());
+ bool isLexicallyScoped = false;
+ emitPutToScope(varScope.m_scope, variableForLocalEntry(name, entry, varScope.m_symbolTableConstantIndex, isLexicallyScoped), temp.get(), DoNotThrowIfNotFound, Initialization);
+ }
}
}
Modified: trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.h (198988 => 198989)
--- trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.h 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/bytecompiler/BytecodeGenerator.h 2016-04-03 19:45:05 UTC (rev 198989)
@@ -863,6 +863,7 @@
};
Vector<SymbolTableStackEntry> m_symbolTableStack;
Vector<std::pair<VariableEnvironment, TDZCheckOptimization>> m_TDZStack;
+ Optional<size_t> m_varScopeSymbolTableIndex;
void pushTDZVariables(VariableEnvironment, TDZCheckOptimization);
ScopeNode* const m_scopeNode;
Modified: trunk/Source/_javascript_Core/parser/Nodes.cpp (198988 => 198989)
--- trunk/Source/_javascript_Core/parser/Nodes.cpp 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/parser/Nodes.cpp 2016-04-03 19:45:05 UTC (rev 198989)
@@ -93,7 +93,7 @@
{
}
-ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, const SourceCode& source, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
+ScopeNode::ScopeNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, const SourceCode& source, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
: StatementNode(endLocation)
, ParserArenaRoot(parserArena)
, VariableEnvironmentNode(lexicalVariables, WTFMove(funcStack))
@@ -103,6 +103,7 @@
, m_features(features)
, m_innerArrowFunctionCodeFeatures(innerArrowFunctionCodeFeatures)
, m_source(source)
+ , m_sloppyModeHoistedFunctions(WTFMove(sloppyModeHoistedFunctions))
, m_numConstants(numConstants)
, m_statements(children)
{
@@ -116,8 +117,8 @@
// ------------------------------ ProgramNode -----------------------------
-ProgramNode::ProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
- : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, features, innerArrowFunctionCodeFeatures, numConstants)
+ProgramNode::ProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
+ : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
, m_startColumn(startColumn)
, m_endColumn(endColumn)
{
@@ -125,8 +126,8 @@
// ------------------------------ ModuleProgramNode -----------------------------
-ModuleProgramNode::ModuleProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
- : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, features, innerArrowFunctionCodeFeatures, numConstants)
+ModuleProgramNode::ModuleProgramNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
+ : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
, m_startColumn(startColumn)
, m_endColumn(endColumn)
{
@@ -134,8 +135,8 @@
// ------------------------------ EvalNode -----------------------------
-EvalNode::EvalNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
- : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, features, innerArrowFunctionCodeFeatures, numConstants)
+EvalNode::EvalNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters*, const SourceCode& source, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
+ : ScopeNode(parserArena, startLocation, endLocation, source, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
, m_endColumn(endColumn)
{
}
@@ -180,8 +181,8 @@
// ------------------------------ FunctionNode -----------------------------
-FunctionNode::FunctionNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, FunctionParameters* parameters, const SourceCode& sourceCode, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
- : ScopeNode(parserArena, startLocation, endLocation, sourceCode, children, varEnvironment, WTFMove(funcStack), lexicalVariables, features, innerArrowFunctionCodeFeatures, numConstants)
+FunctionNode::FunctionNode(ParserArena& parserArena, const JSTokenLocation& startLocation, const JSTokenLocation& endLocation, unsigned startColumn, unsigned endColumn, SourceElements* children, VariableEnvironment& varEnvironment, FunctionStack&& funcStack, VariableEnvironment& lexicalVariables, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, FunctionParameters* parameters, const SourceCode& sourceCode, CodeFeatures features, InnerArrowFunctionCodeFeatures innerArrowFunctionCodeFeatures, int numConstants)
+ : ScopeNode(parserArena, startLocation, endLocation, sourceCode, children, varEnvironment, WTFMove(funcStack), lexicalVariables, WTFMove(sloppyModeHoistedFunctions), features, innerArrowFunctionCodeFeatures, numConstants)
, m_parameters(parameters)
, m_startColumn(startColumn)
, m_endColumn(endColumn)
Modified: trunk/Source/_javascript_Core/parser/Nodes.h (198988 => 198989)
--- trunk/Source/_javascript_Core/parser/Nodes.h 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/parser/Nodes.h 2016-04-03 19:45:05 UTC (rev 198989)
@@ -36,6 +36,7 @@
#include "SymbolTable.h"
#include "VariableEnvironment.h"
#include <wtf/MathExtras.h>
+#include <wtf/SmallPtrSet.h>
namespace JSC {
@@ -51,6 +52,8 @@
class ScopeNode;
class ModuleAnalyzer;
+ typedef SmallPtrSet<UniquedStringImpl*> UniquedStringImplPtrSet;
+
enum Operator {
OpEqual,
OpPlusEq,
@@ -1563,7 +1566,7 @@
public:
ScopeNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, bool inStrictContext);
- ScopeNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, const SourceCode&, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
+ ScopeNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, const SourceCode&, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, UniquedStringImplPtrSet&&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
using ParserArenaRoot::operator new;
@@ -1601,6 +1604,7 @@
bool hasCapturedVariables() const { return m_varDeclarations.hasCapturedVariables(); }
bool captures(UniquedStringImpl* uid) { return m_varDeclarations.captures(uid); }
bool captures(const Identifier& ident) { return captures(ident.impl()); }
+ bool hasSloppyModeHoistedFunction(UniquedStringImpl* uid) const { return m_sloppyModeHoistedFunctions.contains(uid); }
VariableEnvironment& varDeclarations() { return m_varDeclarations; }
@@ -1627,13 +1631,14 @@
InnerArrowFunctionCodeFeatures m_innerArrowFunctionCodeFeatures;
SourceCode m_source;
VariableEnvironment m_varDeclarations;
+ UniquedStringImplPtrSet m_sloppyModeHoistedFunctions;
int m_numConstants;
SourceElements* m_statements;
};
class ProgramNode : public ScopeNode {
public:
- ProgramNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
+ ProgramNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, UniquedStringImplPtrSet&&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
unsigned startColumn() const { return m_startColumn; }
unsigned endColumn() const { return m_endColumn; }
@@ -1648,7 +1653,7 @@
class EvalNode : public ScopeNode {
public:
- EvalNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
+ EvalNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, UniquedStringImplPtrSet&&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
ALWAYS_INLINE unsigned startColumn() const { return 0; }
unsigned endColumn() const { return m_endColumn; }
@@ -1663,7 +1668,7 @@
class ModuleProgramNode : public ScopeNode {
public:
- ModuleProgramNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
+ ModuleProgramNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, UniquedStringImplPtrSet&&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
unsigned startColumn() const { return m_startColumn; }
unsigned endColumn() const { return m_endColumn; }
@@ -1904,7 +1909,7 @@
class FunctionNode final : public ScopeNode {
public:
- FunctionNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
+ FunctionNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&&, VariableEnvironment&, UniquedStringImplPtrSet&&, FunctionParameters*, const SourceCode&, CodeFeatures, InnerArrowFunctionCodeFeatures, int numConstants);
FunctionParameters* parameters() const { return m_parameters; }
Modified: trunk/Source/_javascript_Core/parser/Parser.cpp (198988 => 198989)
--- trunk/Source/_javascript_Core/parser/Parser.cpp 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/parser/Parser.cpp 2016-04-03 19:45:05 UTC (rev 198989)
@@ -203,6 +203,7 @@
, m_superBinding(superBinding)
, m_defaultConstructorKind(defaultConstructorKind)
, m_thisTDZMode(thisTDZMode)
+ , m_immediateParentAllowsFunctionDeclarationInStatement(false)
{
m_lexer = std::make_unique<LexerType>(vm, builtinMode);
m_lexer->setCode(source, &m_parserArena);
@@ -304,9 +305,11 @@
}
IdentifierSet capturedVariables;
+ UniquedStringImplPtrSet sloppyModeHoistedFunctions;
bool modifiedParameter = false;
bool modifiedArguments = false;
- scope->getCapturedVars(capturedVariables, modifiedParameter, modifiedArguments);
+ scope->getSloppyModeHoistedFunctions(sloppyModeHoistedFunctions);
+ scope->getCapturedVars(capturedVariables, modifiedParameter, modifiedArguments);
VariableEnvironment& varDeclarations = scope->declaredVariables();
for (auto& entry : capturedVariables)
@@ -341,19 +344,20 @@
}
}
#endif // NDEBUG
- didFinishParsing(sourceElements, scope->takeFunctionDeclarations(), varDeclarations, features, context.numConstants());
+ didFinishParsing(sourceElements, scope->takeFunctionDeclarations(), varDeclarations, WTFMove(sloppyModeHoistedFunctions), features, context.numConstants());
return parseError;
}
template <typename LexerType>
void Parser<LexerType>::didFinishParsing(SourceElements* sourceElements, DeclarationStacks::FunctionStack&& funcStack,
- VariableEnvironment& varDeclarations, CodeFeatures features, int numConstants)
+ VariableEnvironment& varDeclarations, UniquedStringImplPtrSet&& sloppyModeHoistedFunctions, CodeFeatures features, int numConstants)
{
m_sourceElements = sourceElements;
m_funcDeclarations = WTFMove(funcStack);
m_varDeclarations.swap(varDeclarations);
m_features = features;
+ m_sloppyModeHoistedFunctions = WTFMove(sloppyModeHoistedFunctions);
m_numConstants = numConstants;
}
@@ -562,8 +566,10 @@
}
if (shouldParseVariableDeclaration)
result = parseVariableDeclaration(context, DeclarationType::LetDeclaration);
- else
- result = parseExpressionOrLabelStatement(context); // Treat this as an IDENT. This is how ::parseStatement() handles IDENT.
+ else {
+ bool allowFunctionDeclarationAsStatement = true;
+ result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
+ }
break;
}
@@ -575,6 +581,16 @@
case FUNCTION:
result = parseFunctionDeclaration(context);
break;
+ case IDENT:
+ case YIELD: {
+ // This is a convenient place to notice labeled statements
+ // (even though we also parse them as normal statements)
+ // because we allow the following type of code in sloppy mode:
+ // ``` function foo() { label: function bar() { } } ```
+ bool allowFunctionDeclarationAsStatement = true;
+ result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
+ break;
+ }
default:
m_statementDepth--; // parseStatement() increments the depth.
result = parseStatement(context, directive, directiveLiteralLength);
@@ -1603,6 +1619,8 @@
failIfStackOverflow();
TreeStatement result = 0;
bool shouldSetEndOffset = true;
+ bool parentAllowsFunctionDeclarationAsStatement = m_immediateParentAllowsFunctionDeclarationInStatement;
+ m_immediateParentAllowsFunctionDeclarationInStatement = false;
switch (m_token.m_type) {
case OPENBRACE:
@@ -1612,12 +1630,46 @@
case VAR:
result = parseVariableDeclaration(context, DeclarationType::VarDeclaration);
break;
- case FUNCTION:
- if (!strictMode())
- result = parseFunctionDeclaration(context);
- else
+ case FUNCTION: {
+ if (!strictMode()) {
+ failIfFalse(parentAllowsFunctionDeclarationAsStatement, "Function declarations are only allowed inside block statements or at the top level of a program");
+ if (currentScope()->isFunction()) {
+ // Any function declaration that isn't in a block is a syntax error unless it's
+ // in an if/else statement. If it's in an if/else statement, we will magically
+ // treat it as if the if/else statement is inside a block statement.
+ // to the very top like a "var". For example:
+ // function a() {
+ // if (cond) function foo() { }
+ // }
+ // will be rewritten as:
+ // function a() {
+ // if (cond) { function foo() { } }
+ // }
+ AutoPopScopeRef blockScope(this, pushScope());
+ blockScope->setIsLexicalScope();
+ blockScope->preventVarDeclarations();
+ JSTokenLocation location(tokenLocation());
+ int start = tokenLine();
+
+ TreeStatement function = parseFunctionDeclaration(context);
+ propagateError();
+ failIfFalse(function, "Expected valid function statement after 'function' keyword");
+ TreeSourceElements sourceElements = context.createSourceElements();
+ context.appendStatement(sourceElements, function);
+ result = context.createBlockStatement(location, sourceElements, start, m_lastTokenEndPosition.line, currentScope()->finalizeLexicalEnvironment(), currentScope()->takeFunctionDeclarations());
+ popScope(blockScope, TreeBuilder::NeedsFreeVariableInfo);
+ } else {
+ // We only implement annex B.3.3 if we're in function mode. Otherwise, we fall back
+ // to hoisting behavior.
+ // FIXME: https://bugs.webkit.org/show_bug.cgi?id=155813
+ DepthManager statementDepth(&m_statementDepth);
+ m_statementDepth = 1;
+ result = parseFunctionDeclaration(context);
+ }
+ } else
failWithMessage("Function declarations are only allowed inside blocks or switch statements in strict mode");
break;
+ }
case SEMICOLON: {
JSTokenLocation location(tokenLocation());
next();
@@ -1667,9 +1719,11 @@
// These tokens imply the end of a set of source elements
return 0;
case IDENT:
- case YIELD:
- result = parseExpressionOrLabelStatement(context);
+ case YIELD: {
+ bool allowFunctionDeclarationAsStatement = false;
+ result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
break;
+ }
case STRING:
directive = m_token.m_data.ident;
if (directiveLiteralLength)
@@ -2372,7 +2426,7 @@
};
template <typename LexerType>
-template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrLabelStatement(TreeBuilder& context)
+template <class TreeBuilder> TreeStatement Parser<LexerType>::parseExpressionOrLabelStatement(TreeBuilder& context, bool allowFunctionDeclarationAsStatement)
{
/* _expression_ and Label statements are ambiguous at LL(1), so we have a
@@ -2423,6 +2477,7 @@
for (size_t i = 0; i < labels.size(); i++)
pushLabel(labels[i].m_ident, isLoop);
}
+ m_immediateParentAllowsFunctionDeclarationInStatement = allowFunctionDeclarationAsStatement;
TreeStatement statement = parseStatement(context, unused);
if (!m_syntaxAlreadyValidated) {
for (size_t i = 0; i < labels.size(); i++)
@@ -2475,6 +2530,7 @@
handleProductionOrFail(CLOSEPAREN, ")", "end", "'if' condition");
const Identifier* unused = 0;
+ m_immediateParentAllowsFunctionDeclarationInStatement = true;
TreeStatement trueBlock = parseStatement(context, unused);
failIfFalse(trueBlock, "Expected a statement as the body of an if block");
@@ -2491,6 +2547,7 @@
next();
if (!match(IF)) {
const Identifier* unused = 0;
+ m_immediateParentAllowsFunctionDeclarationInStatement = true;
TreeStatement block = parseStatement(context, unused);
failIfFalse(block, "Expected a statement as the body of an else block");
statementStack.append(block);
@@ -2507,6 +2564,7 @@
int innerEnd = tokenLine();
handleProductionOrFail(CLOSEPAREN, ")", "end", "'if' condition");
const Identifier* unused = 0;
+ m_immediateParentAllowsFunctionDeclarationInStatement = true;
TreeStatement innerTrueBlock = parseStatement(context, unused);
failIfFalse(innerTrueBlock, "Expected a statement as the body of an if block");
tokenLocationStack.append(tempLocation);
Modified: trunk/Source/_javascript_Core/parser/Parser.h (198988 => 198989)
--- trunk/Source/_javascript_Core/parser/Parser.h 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/parser/Parser.h 2016-04-03 19:45:05 UTC (rev 198989)
@@ -40,7 +40,6 @@
#include <wtf/Forward.h>
#include <wtf/Noncopyable.h>
#include <wtf/RefPtr.h>
-#include <wtf/SmallPtrSet.h>
namespace JSC {
@@ -52,8 +51,6 @@
class ProgramNode;
class SourceCode;
-typedef SmallPtrSet<UniquedStringImpl*> UniquedStringImplPtrSet;
-
// Macros to make the more common TreeBuilder types a little less verbose
#define TreeStatement typename TreeBuilder::Statement
#define TreeExpression typename TreeBuilder::_expression_
@@ -369,7 +366,7 @@
return result;
}
- DeclarationResultMask declareFunction(const Identifier* ident, bool declareAsVar)
+ DeclarationResultMask declareFunction(const Identifier* ident, bool declareAsVar, bool isSloppyModeHoistingCandidate)
{
ASSERT(m_allowsVarDeclarations || m_allowsLexicalDeclarations);
DeclarationResultMask result = DeclarationResult::Valid;
@@ -378,7 +375,8 @@
result |= DeclarationResult::InvalidStrictMode;
m_isValidStrictMode = m_isValidStrictMode && isValidStrictMode;
auto addResult = declareAsVar ? m_declaredVariables.add(ident->impl()) : m_lexicalVariables.add(ident->impl());
- addResult.iterator->value.setIsFunction();
+ if (isSloppyModeHoistingCandidate)
+ addResult.iterator->value.setIsSloppyModeHoistingCandidate();
if (declareAsVar) {
addResult.iterator->value.setIsVar();
if (m_lexicalVariables.contains(ident->impl()))
@@ -386,12 +384,23 @@
} else {
addResult.iterator->value.setIsLet();
ASSERT_WITH_MESSAGE(!m_declaredVariables.size(), "We should only declare a function as a lexically scoped variable in scopes where var declarations aren't allowed. I.e, in strict mode and not at the top-level scope of a function or program.");
- if (!addResult.isNewEntry)
- result |= DeclarationResult::InvalidDuplicateDeclaration;
+ if (!addResult.isNewEntry) {
+ if (!isSloppyModeHoistingCandidate || !addResult.iterator->value.isFunction())
+ result |= DeclarationResult::InvalidDuplicateDeclaration;
+ }
}
+
+ addResult.iterator->value.setIsFunction();
+
return result;
}
+ void addSloppyModeHoistableFunctionCandidate(const Identifier* ident)
+ {
+ ASSERT(m_allowsVarDeclarations);
+ m_sloppyModeHoistableFunctionCandidates.add(ident->impl());
+ }
+
void appendFunction(FunctionMetadataNode* node)
{
ASSERT(node);
@@ -478,6 +487,7 @@
bool isArgumentsIdent = isArguments(m_vm, ident);
auto addResult = m_declaredVariables.add(ident->impl());
addResult.iterator->value.clearIsVar();
+ addResult.iterator->value.setIsParameter();
bool isValidStrictMode = addResult.isNewEntry && m_vm->propertyNames->eval != *ident && !isArgumentsIdent;
m_isValidStrictMode = m_isValidStrictMode && isValidStrictMode;
m_declaredParameters.add(ident->impl());
@@ -603,6 +613,25 @@
m_innerArrowFunctionFeatures = m_innerArrowFunctionFeatures | arrowFunctionCodeFeatures;
}
+ void getSloppyModeHoistedFunctions(UniquedStringImplPtrSet& sloppyModeHoistedFunctions)
+ {
+ for (UniquedStringImpl* function : m_sloppyModeHoistableFunctionCandidates) {
+ // ES6 Annex B.3.3. The only time we can't hoist a function is if a syntax error would
+ // be caused by declaring a var with that function's name or if we have a parameter with
+ // that function's name. Note that we would only cause a syntax error if we had a let/const/class
+ // variable with the same name.
+ if (!m_lexicalVariables.contains(function)) {
+ auto iter = m_declaredVariables.find(function);
+ bool isParameter = iter != m_declaredVariables.end() && iter->value.isParameter();
+ if (!isParameter) {
+ auto addResult = m_declaredVariables.add(function);
+ addResult.iterator->value.setIsVar();
+ sloppyModeHoistedFunctions.add(function);
+ }
+ }
+ }
+ }
+
void getCapturedVars(IdentifierSet& capturedVariables, bool& modifiedParameter, bool& modifiedArguments)
{
if (m_needsFullActivation || m_usesEval) {
@@ -742,6 +771,7 @@
VariableEnvironment m_declaredVariables;
VariableEnvironment m_lexicalVariables;
Vector<UniquedStringImplPtrSet, 6> m_usedVariables;
+ UniquedStringImplPtrSet m_sloppyModeHoistableFunctionCandidates;
IdentifierSet m_closedVariableCandidates;
UniquedStringImplPtrSet m_writtenVariables;
RefPtr<ModuleScopeData> m_moduleScopeData;
@@ -770,6 +800,17 @@
return ScopeRef(m_scopeStack, m_index - 1);
}
+ bool operator==(const ScopeRef& other)
+ {
+ ASSERT(other.m_scopeStack == m_scopeStack);
+ return m_index == other.m_index;
+ }
+
+ bool operator!=(const ScopeRef& other)
+ {
+ return !(*this == other);
+ }
+
private:
ScopeStack* m_scopeStack;
unsigned m_index;
@@ -1076,18 +1117,40 @@
std::pair<DeclarationResultMask, ScopeRef> declareFunction(const Identifier* ident)
{
- if (m_statementDepth == 1 || !strictMode()) {
+ if (m_statementDepth == 1 || (!strictMode() && !currentScope()->isFunction())) {
// Functions declared at the top-most scope (both in sloppy and strict mode) are declared as vars
// for backwards compatibility. This allows us to declare functions with the same name more than once.
// In sloppy mode, we always declare functions as vars.
bool declareAsVar = true;
+ bool isSloppyModeHoistingCandidate = false;
ScopeRef variableScope = currentVariableScope();
- return std::make_pair(variableScope->declareFunction(ident, declareAsVar), variableScope);
+ return std::make_pair(variableScope->declareFunction(ident, declareAsVar, isSloppyModeHoistingCandidate), variableScope);
}
+ if (!strictMode()) {
+ ASSERT(currentScope()->isFunction());
+
+ // Functions declared inside a function inside a nested block scope in sloppy mode are subject to this
+ // crazy rule defined inside Annex B.3.3 in the ES6 spec. It basically states that we will create
+ // the function as a local block scoped variable, but when we evaluate the block that the function is
+ // contained in, we will assign the function to a "var" variable only if declaring such a "var" wouldn't
+ // be a syntax error and if there isn't a parameter with the same name. (It would only be a syntax error if
+ // there are is a let/class/const with the same name). Note that this mean we only do the "var" hoisting
+ // binding if the block evaluates. For example, this means we wont won't perform the binding if it's inside
+ // the untaken branch of an if statement.
+ bool declareAsVar = false;
+ bool isSloppyModeHoistingCandidate = true;
+ ScopeRef lexicalVariableScope = currentLexicalDeclarationScope();
+ ScopeRef varScope = currentVariableScope();
+ varScope->addSloppyModeHoistableFunctionCandidate(ident);
+ ASSERT(varScope != lexicalVariableScope);
+ return std::make_pair(lexicalVariableScope->declareFunction(ident, declareAsVar, isSloppyModeHoistingCandidate), lexicalVariableScope);
+ }
+
bool declareAsVar = false;
+ bool isSloppyModeHoistingCandidate = false;
ScopeRef lexicalVariableScope = currentLexicalDeclarationScope();
- return std::make_pair(lexicalVariableScope->declareFunction(ident, declareAsVar), lexicalVariableScope);
+ return std::make_pair(lexicalVariableScope->declareFunction(ident, declareAsVar, isSloppyModeHoistingCandidate), lexicalVariableScope);
}
NEVER_INLINE bool hasDeclaredVariable(const Identifier& ident)
@@ -1134,7 +1197,7 @@
Parser();
String parseInner(const Identifier&, SourceParseMode);
- void didFinishParsing(SourceElements*, DeclarationStacks::FunctionStack&&, VariableEnvironment&, CodeFeatures, int);
+ void didFinishParsing(SourceElements*, DeclarationStacks::FunctionStack&&, VariableEnvironment&, UniquedStringImplPtrSet&&, CodeFeatures, int);
// Used to determine type of error to report.
bool isFunctionMetadataNode(ScopeNode*) { return false; }
@@ -1349,7 +1412,7 @@
template <class TreeBuilder> TreeStatement parseTryStatement(TreeBuilder&);
template <class TreeBuilder> TreeStatement parseDebuggerStatement(TreeBuilder&);
template <class TreeBuilder> TreeStatement parseExpressionStatement(TreeBuilder&);
- template <class TreeBuilder> TreeStatement parseExpressionOrLabelStatement(TreeBuilder&);
+ template <class TreeBuilder> TreeStatement parseExpressionOrLabelStatement(TreeBuilder&, bool allowFunctionDeclarationAsStatement);
template <class TreeBuilder> TreeStatement parseIfStatement(TreeBuilder&);
template <class TreeBuilder> TreeStatement parseBlockStatement(TreeBuilder&);
template <class TreeBuilder> TreeExpression parseExpression(TreeBuilder&);
@@ -1544,10 +1607,12 @@
ThisTDZMode m_thisTDZMode;
VariableEnvironment m_varDeclarations;
DeclarationStacks::FunctionStack m_funcDeclarations;
+ UniquedStringImplPtrSet m_sloppyModeHoistedFunctions;
CodeFeatures m_features;
int m_numConstants;
ExpressionErrorClassifier* m_expressionErrorClassifier;
bool m_isEvalContext;
+ bool m_immediateParentAllowsFunctionDeclarationInStatement;
struct DepthManager {
DepthManager(int* depth)
@@ -1617,6 +1682,7 @@
m_varDeclarations,
WTFMove(m_funcDeclarations),
currentScope()->finalizeLexicalEnvironment(),
+ WTFMove(m_sloppyModeHoistedFunctions),
m_parameters,
*m_source,
m_features,
Modified: trunk/Source/_javascript_Core/parser/VariableEnvironment.h (198988 => 198989)
--- trunk/Source/_javascript_Core/parser/VariableEnvironment.h 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/parser/VariableEnvironment.h 2016-04-03 19:45:05 UTC (rev 198989)
@@ -41,6 +41,8 @@
ALWAYS_INLINE bool isImported() const { return m_bits & IsImported; }
ALWAYS_INLINE bool isImportedNamespace() const { return m_bits & IsImportedNamespace; }
ALWAYS_INLINE bool isFunction() const { return m_bits & IsFunction; }
+ ALWAYS_INLINE bool isParameter() const { return m_bits & IsParameter; }
+ ALWAYS_INLINE bool isSloppyModeHoistingCandidate() const { return m_bits & IsSloppyModeHoistingCandidate; }
ALWAYS_INLINE void setIsCaptured() { m_bits |= IsCaptured; }
ALWAYS_INLINE void setIsConst() { m_bits |= IsConst; }
@@ -50,11 +52,13 @@
ALWAYS_INLINE void setIsImported() { m_bits |= IsImported; }
ALWAYS_INLINE void setIsImportedNamespace() { m_bits |= IsImportedNamespace; }
ALWAYS_INLINE void setIsFunction() { m_bits |= IsFunction; }
+ ALWAYS_INLINE void setIsParameter() { m_bits |= IsParameter; }
+ ALWAYS_INLINE void setIsSloppyModeHoistingCandidate() { m_bits |= IsSloppyModeHoistingCandidate; }
ALWAYS_INLINE void clearIsVar() { m_bits &= ~IsVar; }
private:
- enum Traits : uint8_t {
+ enum Traits : uint16_t {
IsCaptured = 1 << 0,
IsConst = 1 << 1,
IsVar = 1 << 2,
@@ -62,9 +66,11 @@
IsExported = 1 << 4,
IsImported = 1 << 5,
IsImportedNamespace = 1 << 6,
- IsFunction = 1 << 7
+ IsFunction = 1 << 7,
+ IsParameter = 1 << 8,
+ IsSloppyModeHoistingCandidate = 1 << 9
};
- uint8_t m_bits { 0 };
+ uint16_t m_bits { 0 };
};
struct VariableEnvironmentEntryHashTraits : HashTraits<VariableEnvironmentEntry> {
Modified: trunk/Source/_javascript_Core/runtime/CodeCache.h (198988 => 198989)
--- trunk/Source/_javascript_Core/runtime/CodeCache.h 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/runtime/CodeCache.h 2016-04-03 19:45:05 UTC (rev 198989)
@@ -32,7 +32,6 @@
#include "SourceCode.h"
#include "SourceCodeKey.h"
#include "Strong.h"
-#include "VariableEnvironment.h"
#include <wtf/CurrentTime.h>
#include <wtf/Forward.h>
#include <wtf/RandomNumber.h>
@@ -57,6 +56,7 @@
class VM;
class SourceCode;
class SourceProvider;
+class VariableEnvironment;
struct SourceCodeValue {
SourceCodeValue()
Modified: trunk/Source/_javascript_Core/runtime/JSScope.cpp (198988 => 198989)
--- trunk/Source/_javascript_Core/runtime/JSScope.cpp 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/runtime/JSScope.cpp 2016-04-03 19:45:05 UTC (rev 198989)
@@ -32,6 +32,7 @@
#include "JSModuleRecord.h"
#include "JSWithScope.h"
#include "JSCInlines.h"
+#include "VariableEnvironment.h"
namespace JSC {
Modified: trunk/Source/_javascript_Core/runtime/JSScope.h (198988 => 198989)
--- trunk/Source/_javascript_Core/runtime/JSScope.h 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/runtime/JSScope.h 2016-04-03 19:45:05 UTC (rev 198989)
@@ -28,12 +28,12 @@
#include "GetPutInfo.h"
#include "JSObject.h"
-#include "VariableEnvironment.h"
namespace JSC {
class ScopeChainIterator;
class WatchpointSet;
+class VariableEnvironment;
class JSScope : public JSNonFinalObject {
public:
Modified: trunk/Source/_javascript_Core/tests/es6.yaml (198988 => 198989)
--- trunk/Source/_javascript_Core/tests/es6.yaml 2016-04-03 19:38:54 UTC (rev 198988)
+++ trunk/Source/_javascript_Core/tests/es6.yaml 2016-04-03 19:45:05 UTC (rev 198989)
@@ -881,7 +881,7 @@
- path: es6/new.target_assignment_is_an_early_error.js
cmd: runES6 :normal
- path: es6/non-strict_function_semantics_hoisted_block-level_function_declaration.js
- cmd: runES6 :fail
+ cmd: runES6 :normal
- path: es6/Promise_is_subclassable_Promise.all.js
cmd: runES6 :fail
- path: es6/Promise_is_subclassable_Promise.race.js
Added: trunk/Source/_javascript_Core/tests/stress/sloppy-mode-function-hoisting.js (0 => 198989)
--- trunk/Source/_javascript_Core/tests/stress/sloppy-mode-function-hoisting.js (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/sloppy-mode-function-hoisting.js 2016-04-03 19:45:05 UTC (rev 198989)
@@ -0,0 +1,655 @@
+function assert(b) {
+ if (!b)
+ throw new Error("Bad assertion.");
+}
+
+function test(f, ...args) {
+ for (let i = 0; i < 500; i++)
+ f(...args);
+}
+
+function falsey() { return false; }
+noInline(falsey);
+function truthy() { return true; }
+noInline(truthy);
+
+test(function() {
+ var a;
+ assert(a === undefined);
+ {
+ function a() { return 20; }
+ }
+ assert(a() === 20);
+});
+
+test(function(a) {
+ var a;
+ assert(a === undefined);
+ {
+ function a() { return 20; }
+ }
+ assert(a === undefined);
+});
+
+test(function({a}) {
+ var a;
+ assert(a === undefined);
+ {
+ function a() { return 20; }
+ }
+ assert(a === undefined);
+}, {});
+
+test(function() {
+ let a;
+ assert(a === undefined);
+ {
+ function a() { return 20; }
+ }
+ assert(a === undefined);
+});
+
+test(function() {
+ assert(a === undefined);
+ function foo() { return a(); }
+ {
+ function a() { return 20; }
+ }
+ assert(a() === 20);
+ assert(foo() === 20);
+});
+
+test(function(a = 30) {
+ assert(a === 30);
+ function foo() { return a; }
+ assert(foo() === 30);
+ {
+ function a() { return 20; }
+ assert(a() === 20);
+ assert(foo() === 30);
+ }
+ assert(a === 30);
+ assert(foo() === 30);
+});
+
+test(function() {
+ let x = 15;
+ assert(x === 15);
+ assert(a === undefined);
+ {
+ let x = {x: 20};
+ function a() { return x; }
+ assert(a() === x);
+ assert(a().x === 20);
+ }
+ assert(a().x === 20);
+ assert(x === 15);
+});
+
+test(function() {
+ let x = 15;
+ assert(x === 15);
+ assert(a === undefined);
+ let f;
+ {
+ let x = {x: 20};
+ assert(a() === x);
+ assert(a().x === 20);
+
+ function a() { throw new Error; }
+ function a() { return x; }
+ f = a;
+ }
+ assert(a().x === 20);
+ assert(x === 15);
+ assert(f().x === 20);
+});
+
+test(function() {
+ let x = 15;
+ let f;
+ assert(x === 15);
+ assert(a === undefined);
+ assert(f === undefined);
+ {
+ function a() { return f; }
+ f = a;
+ }
+ assert(x === 15);
+ assert(f() === f);
+});
+
+test(function() {
+ function a() { return 20; }
+ let f = a;
+ assert(a() === 20);
+ {
+ function a() { return 25; }
+ assert(a() === 25);
+ }
+ assert(f() === 20);
+ assert(a() === 25);
+});
+
+test(function() {
+ assert(f === undefined);
+ for (let i = 0; i < 10; i++) {
+ function f() { return i; }
+ assert(f() === i);
+ }
+ assert(f() === 9);
+});
+
+test(function() {
+ assert(f === undefined);
+ let nums = [0, 1, 2, 3];
+ for (let i of nums) {
+ function f() { return i; }
+ assert(f() === i);
+ }
+ assert(f() === 3);
+});
+
+test(function() {
+ assert(f === undefined);
+ let obj = {0:0, 1:1, 2:2, 3:3};
+ for (let i in obj) {
+ function f() { return i; }
+ assert(f() === i);
+ }
+ assert(f() === "3");
+});
+
+test(function() {
+ assert(f === undefined);
+ let nums = [0, 1, 2, 3];
+ let funcs = []
+ for (let i of nums) {
+ function f() { return i; }
+ funcs.push(f);
+ assert(f() === i);
+ }
+ assert(f() === 3);
+ assert(funcs.length === nums.length);
+ for (let i = 0; i < funcs.length; i++) {
+ assert(funcs[i]() === nums[i]);
+ }
+});
+
+test(function() {
+ assert(f === undefined);
+ try {
+ throw new Error("foo");
+ } catch(e) {
+ function f() { return 20; }
+ }
+ assert(f() === 20);
+});
+
+test(function() {
+ assert(f === undefined);
+ try {
+ ;
+ } catch(e) {
+ function f() { return 20; }
+ }
+ assert(f === undefined);
+});
+
+test(function() {
+ assert(foo === undefined);
+ if (falsey()) {
+ function foo() { return 20; }
+ }
+ assert(foo === undefined);
+});
+
+test(function() {
+ assert(foo === undefined);
+ if (falsey())
+ function foo() { return 20; }
+ assert(foo === undefined);
+});
+
+test(function() {
+ assert(foo === undefined);
+ if (truthy()) {
+ function foo() { return 20; }
+ }
+ assert(foo() === 20);
+});
+
+test(function() {
+ assert(foo === undefined);
+ while (truthy()) {
+ break;
+
+ function foo() { return 20; }
+ }
+ assert(foo() === 20);
+});
+
+test(function() {
+ function bar() { return foo; }
+ assert(foo === undefined);
+ assert(bar() === undefined);
+ while (truthy()) {
+ break;
+
+ function foo() { return 20; }
+ }
+ assert(foo() === 20);
+ assert(bar()() === 20);
+});
+
+test(function() {
+ function bar() { return foo; }
+ assert(foo === undefined);
+ assert(bar() === undefined);
+ while (falsey()) {
+ function foo() { return 20; }
+ }
+ assert(foo === undefined);
+ assert(bar() === undefined);
+});
+
+test(function() {
+ var a = "a";
+ assert(a === "a");
+ {
+ let b = 1;
+ assert(a === "a");
+ {
+ let c = 2;
+ assert(a === "a");
+ {
+ let d = 3;
+ function a() { return b + c+ d; }
+ assert(a() === 6);
+ }
+ assert(a() === 6);
+ }
+ assert(a() === 6);
+ }
+ assert(a() === 6);
+});
+
+test(function() {
+ assert(foo === undefined);
+ switch(1) {
+ case 0:
+ function foo() { return 20; }
+ break;
+ }
+ assert(foo() === 20);
+});
+
+test(function() {
+ assert(foo === undefined);
+ switch(1) {
+ case 0:{
+ function foo() { return 20; }
+ break;
+ }
+ }
+ assert(foo === undefined);
+});
+
+test(function() {
+ assert(foo === undefined);
+ switch(0) {
+ case 0:{
+ function foo() { return 20; }
+ break;
+ }
+ }
+ assert(foo() === 20);
+});
+
+test(function() {
+ assert(foo === undefined);
+ switch(0) {
+ case 0:
+ function foo() { return bar; }
+ break;
+ case 1:
+ let bar = 20;
+ break;
+ }
+
+ let threw = false;
+ try {
+ foo();
+ } catch (e) {
+ assert(e instanceof ReferenceError);
+ threw = true;
+ }
+ assert(threw);
+});
+
+test(function() {
+ assert(foo === undefined);
+ switch(0) {
+ case 0:
+ function foo() { return bar; }
+ case 1:
+ let bar = 20;
+ break;
+ }
+
+ assert(foo() === 20);
+});
+
+test(function() {
+ assert(foo === undefined);
+ switch(1) {
+ case 0:
+ function foo() { return bar; }
+ case 1:
+ let bar = 20;
+ assert(foo() === 20);
+ break;
+ }
+
+ assert(foo() === 20);
+});
+
+test(function(a) {
+ assert(a === 25);
+ switch(1) {
+ case 0:
+ function a() { return bar; }
+ case 1:
+ let bar = 20;
+ assert(a() === 20);
+ break;
+ }
+
+ assert(a === 25);
+}, 25);
+
+test(function() {
+ let a = 25;
+ assert(a === 25);
+ switch(1) {
+ case 0:
+ function a() { return bar; }
+ case 1:
+ let bar = 20;
+ assert(a() === 20);
+ break;
+ }
+
+ assert(a === 25);
+});
+
+test(function() {
+ const a = 25;
+ assert(a === 25);
+ switch(1) {
+ case 0:
+ function a() { return bar; }
+ case 1:
+ let bar = 20;
+ assert(a() === 20);
+ break;
+ }
+
+ assert(a === 25);
+});
+
+test(function() {
+ let foo = {};
+ class a { constructor() { return foo; } }
+ assert(new a === foo);
+ switch(1) {
+ case 0:
+ function a() { return bar; }
+ case 1:
+ let bar = 20;
+ assert(a() === 20);
+ break;
+ }
+
+ assert(new a === foo);
+});
+
+test(function() {
+ assert(f === undefined);
+ {
+ if (true)
+ function f() { return 20; }
+ assert(f() === 20);
+ }
+ assert(f() === 20);
+});
+
+test(function() {
+ assert(f === undefined);
+ {
+ if (false)
+ function f() { return 20; }
+ assert(f === undefined);
+ }
+ assert(f === undefined);
+});
+
+test(function() {
+ var x;
+ assert(f === undefined);
+ if (true)
+ if (true)
+ if (true)
+ function f() { return 20; }
+ assert(f() === 20);
+});
+
+test(function() {
+ var x;
+ assert(f === undefined);
+ {
+ if (true)
+ if (false)
+ if (true)
+ function f() { return 20; }
+ }
+ assert(f === undefined);
+});
+
+test(function() {
+ var x;
+ assert(f === undefined);
+ {
+ while (false)
+ while (false)
+ if (true)
+ function f() { return 20; }
+ }
+ assert(f === undefined);
+});
+
+test(function() {
+ assert(f === undefined);
+ var f = 20;
+ assert(f === 20);
+ while (false)
+ while (false)
+ if (true)
+ function f() { return 20; }
+ assert(f === 20);
+});
+
+test(function() {
+ assert(f === undefined);
+ var f = 20;
+ assert(f === 20);
+ var i = 2;
+ {
+ while (i-- > 0)
+ while (i-- > 0)
+ if (true)
+ function f() { return 20; }
+ }
+ assert(f() === 20);
+});
+
+test(function() {
+ assert(f === undefined);
+ var f = 20;
+ assert(f === 20);
+ var i = 2;
+ {
+ while (i-- > 0)
+ while (i-- > 0)
+ if (false)
+ function f() { return 20; }
+ }
+ assert(f === 20);
+});
+
+test(function() {
+ assert(f === undefined);
+ var f = 20;
+ assert(f === 20);
+ var i = 2;
+ {
+ while (i-- > 0)
+ while (i-- > 0)
+ if (false)
+ function f() { return 20; }
+ else
+ function f() { return 30; }
+ }
+ assert(f() === 30);
+});
+
+test(function() {
+ assert(f === undefined);
+ if (true) {
+ label: function f() { return 20; }
+ }
+ assert(f() === 20);
+});
+
+test(function() {
+ assert(f === undefined);
+ if (true) {
+ label: label2: label3: function f() { return 20; }
+ }
+ assert(f() === 20);
+});
+
+test(function() {
+ assert(a === undefined);
+ assert(b === undefined);
+ assert(c === undefined);
+ assert(d === undefined);
+ assert(e === undefined);
+ assert(f === undefined);
+ assert(g === undefined);
+ assert(h === undefined);
+ assert(i === undefined);
+ assert(j === undefined);
+ assert(k === undefined);
+ assert(l === undefined);
+ assert(m === undefined);
+ assert(n === undefined);
+ assert(o === undefined);
+ assert(p === undefined);
+ assert(q === undefined);
+ assert(r === undefined);
+ assert(s === undefined);
+ assert(t === undefined);
+ assert(u === undefined);
+ assert(v === undefined);
+ assert(w === undefined);
+ assert(x === undefined);
+ assert(y === undefined);
+ assert(z === undefined);
+ {
+ function a() { }
+ function b() { }
+ function c() { }
+ function d() { }
+ function e() { }
+ function f() { }
+ function g() { }
+ function h() { }
+ function i() { }
+ function j() { }
+ function k() { }
+ function l() { }
+ function m() { }
+ function n() { }
+ function o() { }
+ function p() { }
+ function q() { }
+ function r() { }
+ function s() { }
+ function t() { }
+ function u() { }
+ function v() { }
+ function w() { }
+ function x() { }
+ function y() { }
+ function z() { }
+ }
+ assert(typeof a === "function");
+ assert(typeof b === "function");
+ assert(typeof c === "function");
+ assert(typeof d === "function");
+ assert(typeof e === "function");
+ assert(typeof f === "function");
+ assert(typeof g === "function");
+ assert(typeof h === "function");
+ assert(typeof i === "function");
+ assert(typeof j === "function");
+ assert(typeof k === "function");
+ assert(typeof l === "function");
+ assert(typeof m === "function");
+ assert(typeof n === "function");
+ assert(typeof o === "function");
+ assert(typeof p === "function");
+ assert(typeof q === "function");
+ assert(typeof r === "function");
+ assert(typeof s === "function");
+ assert(typeof t === "function");
+ assert(typeof u === "function");
+ assert(typeof v === "function");
+ assert(typeof w === "function");
+ assert(typeof x === "function");
+ assert(typeof y === "function");
+ assert(typeof z === "function");
+});
+
+for (let i = 0; i < 500; i++)
+ assert(foo() === 25);
+function foo() { return 20; }
+
+{
+ function foo() { return 25; }
+ assert(foo() === 25);
+}
+assert(foo() === 25);
+
+for (let i = 0; i < 500; i++)
+ assert(bar() === "bar2");
+function bar() { return "bar1"; }
+if (falsey()) {
+ {
+ if (falsey()) {
+ function bar() { return "bar2"; }
+ }
+ }
+}
+assert(bar() === "bar2");
+
+for (let i = 0; i < 500; i++)
+ assert(baz() === "baz2");
+function baz() { return "baz1"; }
+while (falsey()) {
+ if (falsey()) {
+ function baz() { return "baz2"; }
+ }
+}
+assert(baz() === "baz2");