From 07152c41c45aff290b3f74fee83c661141781031 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 12 Oct 2012 23:09:23 +0200 Subject: [PATCH] Use 'new Function' instead of '(1, eval)' In the hope that it'll be less confusing. --- acorn.js | 4 ++-- index.html | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/acorn.js b/acorn.js index 9aca7c29bf..2a107deaf6 100644 --- a/acorn.js +++ b/acorn.js @@ -262,7 +262,7 @@ function makePredicate(words) { words = words.split(" "); - var f = "(function(str){", cats = []; + var f = "", cats = []; out: for (var i = 0; i < words.length; ++i) { for (var j = 0; j < cats.length; ++j) if (cats[j][0].length == words[i].length) { @@ -296,7 +296,7 @@ } else { compareTo(words); } - return (1, eval)(f + "})"); + return new Function("str", f); } // The ECMAScript 3 reserved word list. diff --git a/index.html b/index.html index 31df065527..0afca7fb38 100644 --- a/index.html +++ b/index.html @@ -11,20 +11,21 @@ https://github.com/marijnh/acorn.git

Please use the github bug tracker to report issues.

(function(exports) {
-  "strict mode";
+  "use strict";
 
   exports.version = "0.0.1";

The main exported interface (under window.acorn when in the browser) is a parse function that takes a code string and returns an abstract syntax tree as specified by Mozilla parser API, with the caveat that the SpiderMonkey-specific syntax -(let, yield, inline XML, etc) is not recognized.

  var options, input, inputLen;
+(let, yield, inline XML, etc) is not recognized.

  var options, input, inputLen, sourceFile;
 
   exports.parse = function(inpt, opts) {
     input = String(inpt); inputLen = input.length;
     options = opts || {};
     for (var opt in defaultOptions) if (!options.hasOwnProperty(opt))
       options[opt] = defaultOptions[opt];
-    return parseTopLevel();
+    sourceFile = options.sourceFile || null;
+    return parseTopLevel(options.program);
   };

A second optional argument can be given to further configure the parser process. These options are recognized:

  var defaultOptions = exports.defaultOptions = {

ecmaVersion indicates the ECMAScript version to parse. Must be either 3 or 5. This @@ -152,7 +153,7 @@ predicate from a space-separated string of words.

It starts by sorting the words by length.

  function makePredicate(words) {
     words = words.split(" ");
-    var f = "(function(str){", cats = [];
+    var f = "", cats = [];
     out: for (var i = 0; i < words.length; ++i) {
       for (var j = 0; j < cats.length; ++j)
         if (cats[j][0].length == words[i].length) {
@@ -178,7 +179,7 @@ switch first dispatches on the lengths, to save on comparisons.

f += "}";

Otherwise, simply generate a flat switch statement.

    } else {
       compareTo(words);
     }
-    return (1, eval)(f + "})");
+    return new Function("str", f);
   }

The ECMAScript 3 reserved word list.

  var isReservedWord3 = makePredicate("abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile");

ECMAScript 5 reserved words.

  var isReservedWord5 = makePredicate("class enum extends super const export import");

The additional reserved words in strict mode.

  var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield");

The forbidden variable names in strict mode.

  var isStrictBadIdWord = makePredicate("eval arguments");

And the keywords.

  var isKeyword = makePredicate("break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in");

Character categories

Big ugly regular expressions that match characters in the whitespace, identifier, and identifier-start categories. These are only applied when a character is found to actually have a @@ -551,7 +552,7 @@ tests ("use strict"; 010; -- should fail).

tokCommentsBefore = null; } if (options.locations) - node.loc = {start: tokStartLoc, end: null}; + node.loc = {start: tokStartLoc, end: null, source: sourceFile}; return node; }

Start a node whose start offset/comments information should be based on the start of another node. For example, a binary @@ -563,7 +564,7 @@ already been parsed.

other.commentsBefore = null; } if (options.locations) - node.loc = {start: other.loc.start, end: null}; + node.loc = {start: other.loc.start, end: null, source: other.loc.source}; return node; }

Finish an AST node, adding type, end, and commentsAfter @@ -619,7 +620,9 @@ to.

if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name)) raise(expr.start, "Assigning to " + expr.name + " in strict mode"); }

Statement parsing

Parse a program. Initializes the parser, reads any number of -statements, and wraps them in a Program node.

  function parseTopLevel() {
+statements, and wraps them in a Program node.  Optionally takes a
+program argument.  If present, the statements will be appended
+to its body instead of creating a new node.

  function parseTopLevel(program) {
     initTokenState();
     lastStart = lastEnd = tokPos;
     if (options.locations) lastEndLoc = curLineLoc();
@@ -627,8 +630,8 @@ statements, and wraps them in a Program node.

labels = []; readToken(); - var node = startNode(), first = true; - node.body = []; + var node = program || startNode(), first = true; + if (!program) node.body = []; while (tokType !== _eof) { var stmt = parseStatement(); node.body.push(stmt);