Move function declaration out of conditional
authorJonas Westerlund <jonas.westerlund@me.com>
Wed, 4 Jul 2012 21:14:27 +0000 (23:14 +0200)
committerNathan Rajlich <nathan@tootallnate.net>
Sat, 7 Jul 2012 02:28:35 +0000 (19:28 -0700)
Also avoid using eval as identifier.
Fixes crashes in strict mode.

lib/repl.js

index 3e5f33a..98c14e7 100644 (file)
@@ -75,9 +75,9 @@ exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
   'string_decoder', 'tls', 'tty', 'url', 'util', 'vm', 'zlib'];
 
 
-function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) {
+function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
   if (!(this instanceof REPLServer)) {
-    return new REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined);
+    return new REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined);
   }
 
   EventEmitter.call(this);
@@ -89,7 +89,7 @@ function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) {
     stream = options.stream || options.socket;
     input = options.input;
     output = options.output;
-    eval = options.eval;
+    eval_ = options.eval;
     useGlobal = options.useGlobal;
     ignoreUndefined = options.ignoreUndefined;
     prompt = options.prompt;
@@ -104,7 +104,7 @@ function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) {
   self.useGlobal = !!useGlobal;
   self.ignoreUndefined = !!ignoreUndefined;
 
-  self.eval = eval || function(code, context, file, cb) {
+  self.eval = eval_ || function(code, context, file, cb) {
     var err, result;
     try {
       if (self.useGlobal) {
@@ -327,8 +327,8 @@ exports.REPLServer = REPLServer;
 
 // prompt is a string to print on each line for the prompt,
 // source is a stream to use for I/O, defaulting to stdin/stdout.
-exports.start = function(prompt, source, eval, useGlobal, ignoreUndefined) {
-  var repl = new REPLServer(prompt, source, eval, useGlobal, ignoreUndefined);
+exports.start = function(prompt, source, eval_, useGlobal, ignoreUndefined) {
+  var repl = new REPLServer(prompt, source, eval_, useGlobal, ignoreUndefined);
   if (!exports.repl) exports.repl = repl;
   return repl;
 };
@@ -536,9 +536,6 @@ REPLServer.prototype.complete = function(line, callback) {
         expr = bits.join('.');
       }
 
-      // console.log("expression completion: completeOn='" + completeOn +
-      //             "' expr='" + expr + "'");
-
       // Resolve expr and get its completions.
       var obj, memberGroups = [];
       if (!expr) {
@@ -552,12 +549,12 @@ REPLServer.prototype.complete = function(line, callback) {
             completionGroups.push(Object.getOwnPropertyNames(contextProto));
           }
           completionGroups.push(Object.getOwnPropertyNames(this.context));
-          addStandardGlobals();
+          addStandardGlobals(completionGroups, filter);
           completionGroupsLoaded();
         } else {
           this.eval('.scope', this.context, 'repl', function(err, globals) {
             if (err || !globals) {
-              addStandardGlobals();
+              addStandardGlobals(completionGroups, filter);
             } else if (Array.isArray(globals[0])) {
               // Add grouped globals
               globals.forEach(function(group) {
@@ -565,34 +562,11 @@ REPLServer.prototype.complete = function(line, callback) {
               });
             } else {
               completionGroups.push(globals);
-              addStandardGlobals();
+              addStandardGlobals(completionGroups, filter);
             }
             completionGroupsLoaded();
           });
         }
-
-        function addStandardGlobals() {
-          // Global object properties
-          // (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
-          completionGroups.push(['NaN', 'Infinity', 'undefined',
-            'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI',
-            'decodeURIComponent', 'encodeURI', 'encodeURIComponent',
-            'Object', 'Function', 'Array', 'String', 'Boolean', 'Number',
-            'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
-            'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
-            'Math', 'JSON']);
-          // Common keywords. Exclude for completion on the empty string, b/c
-          // they just get in the way.
-          if (filter) {
-            completionGroups.push(['break', 'case', 'catch', 'const',
-              'continue', 'debugger', 'default', 'delete', 'do', 'else',
-              'export', 'false', 'finally', 'for', 'function', 'if',
-              'import', 'in', 'instanceof', 'let', 'new', 'null', 'return',
-              'switch', 'this', 'throw', 'true', 'try', 'typeof', 'undefined',
-              'var', 'void', 'while', 'with', 'yield']);
-          }
-        }
-
       } else {
         this.eval(expr, this.context, 'repl', function(e, obj) {
           // if (e) console.log(e);
@@ -790,6 +764,27 @@ REPLServer.prototype.memory = function memory(cmd) {
   }
 };
 
+function addStandardGlobals(completionGroups, filter) {
+  // Global object properties
+  // (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
+  completionGroups.push(['NaN', 'Infinity', 'undefined',
+    'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI',
+    'decodeURIComponent', 'encodeURI', 'encodeURIComponent',
+    'Object', 'Function', 'Array', 'String', 'Boolean', 'Number',
+    'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
+    'ReferenceError', 'SyntaxError', 'TypeError', 'URIError',
+    'Math', 'JSON']);
+  // Common keywords. Exclude for completion on the empty string, b/c
+  // they just get in the way.
+  if (filter) {
+    completionGroups.push(['break', 'case', 'catch', 'const',
+      'continue', 'debugger', 'default', 'delete', 'do', 'else',
+      'export', 'false', 'finally', 'for', 'function', 'if',
+      'import', 'in', 'instanceof', 'let', 'new', 'null', 'return',
+      'switch', 'this', 'throw', 'true', 'try', 'typeof', 'undefined',
+      'var', 'void', 'while', 'with', 'yield']);
+  }
+}
 
 function defineDefaultCommands(repl) {
   // TODO remove me after 0.3.x