From a6873a38590a9176c607bcdbff726daa93e1fec7 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 4 Jan 2018 18:45:51 +0800 Subject: forbid block-scoped `AST_Defun` in strict mode (#2718) --- lib/parse.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'lib/parse.js') diff --git a/lib/parse.js b/lib/parse.js index 03455348..001587bc 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -800,7 +800,7 @@ function parse($TEXT, options) { function embed_tokens(parser) { return function() { var start = S.token; - var expr = parser(); + var expr = parser.apply(null, arguments); var end = prev(); expr.start = start; expr.end = end; @@ -815,7 +815,7 @@ function parse($TEXT, options) { } }; - var statement = embed_tokens(function() { + var statement = embed_tokens(function(strict_defun) { handle_regexp(); switch (S.token.type) { case "string": @@ -901,6 +901,9 @@ function parse($TEXT, options) { return for_(); case "function": + if (!strict_defun && S.input.has_directive("use strict")) { + croak("In strict mode code, functions can only be declared at top level or immediately within another function."); + } next(); return function_(AST_Defun); @@ -1083,7 +1086,7 @@ function parse($TEXT, options) { S.input.push_directives_stack(); S.in_loop = 0; S.labels = []; - var body = block_(); + var body = block_(true); if (S.input.has_directive("use strict")) { if (name) strict_verify_symbol(name); argnames.forEach(strict_verify_symbol); @@ -1112,12 +1115,12 @@ function parse($TEXT, options) { }); }; - function block_() { + function block_(strict_defun) { expect("{"); var a = []; while (!is("punc", "}")) { if (is("eof")) unexpected(); - a.push(statement()); + a.push(statement(strict_defun)); } next(); return a; @@ -1630,7 +1633,7 @@ function parse($TEXT, options) { var body = []; S.input.push_directives_stack(); while (!is("eof")) - body.push(statement()); + body.push(statement(true)); S.input.pop_directives_stack(); var end = prev(); var toplevel = options.toplevel; -- cgit v1.2.3