Skip to content
Browse files

Add useGlobal flag for standard node repl

  • Loading branch information...
1 parent 602731f commit 3f9db955297f9004abf176c7431d2186ab4cd1d1 @isaacs committed Oct 20, 2011
Showing with 21 additions and 8 deletions.
  1. +20 −7 lib/repl.js
  2. +1 −1 src/node.js
View
27 lib/repl.js
@@ -68,12 +68,19 @@ module.paths = require('module')._nodeModulePaths(module.filename);
exports.writer = util.inspect;
-function REPLServer(prompt, stream, eval) {
+function REPLServer(prompt, stream, eval, useGlobal) {
var self = this;
+ self.useGlobal = useGlobal;
+
self.eval = eval || function(code, context, file, cb) {
+ var err, result;
try {
- var err, result = vm.runInContext(code, context, file);
+ if (useGlobal) {
+ result = vm.runInThisContext(code, file);
+ } else {
+ result = vm.runInContext(code, context, file);
+ }
} catch (e) {
err = e;
}
@@ -238,17 +245,21 @@ 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) {
- var repl = new REPLServer(prompt, source, eval);
+exports.start = function(prompt, source, eval, useGlobal) {
+ var repl = new REPLServer(prompt, source, eval, useGlobal);
if (!exports.repl) exports.repl = repl;
return repl;
};
REPLServer.prototype.createContext = function() {
- var context = vm.createContext();
+ if (!this.useGlobal) {
+ var context = vm.createContext();
+ for (var i in global) context[i] = global[i];
+ } else {
+ var context = global;
+ }
- for (var i in global) context[i] = global[i];
context.module = module;
context.require = require;
context.global = context;
@@ -413,7 +424,9 @@ REPLServer.prototype.complete = function(line, callback) {
if (!expr) {
// If context is instance of vm.ScriptContext
// Get global vars synchronously
- if (this.context.constructor.name === 'Context') {
+ if (this.useGlobal ||
+ this.context.constructor &&
+ this.context.constructor.name === 'Context') {
completionGroups.push(Object.getOwnPropertyNames(this.context));
addStandardGlobals();
completionGroupsLoaded();
View
2 src/node.js
@@ -107,7 +107,7 @@
// If stdin is a TTY.
if (NativeModule.require('tty').isatty(0)) {
// REPL
- Module.requireRepl().start();
+ var repl = Module.requireRepl().start('> ', null, null, true);
} else {
// Read all of stdin - execute it.

0 comments on commit 3f9db95

Please sign in to comment.
Something went wrong with that request. Please try again.