Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add useGlobal flag for standard node repl
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 21, 2011
1 parent caf70f5 commit 2d02e6a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
5 changes: 4 additions & 1 deletion doc/api/repl.markdown
Expand Up @@ -27,12 +27,15 @@ For example, you could add this to your bashrc file:
alias node="env NODE_NO_READLINE=1 rlwrap node"


### repl.start(prompt='> ', stream=process.stdin, eval=eval)
### repl.start(prompt='> ', stream=process.stdin, eval=eval, useGlobal=false)

Starts a REPL with `prompt` as the prompt and `stream` for all I/O. `prompt`
is optional and defaults to `> `. `stream` is optional and defaults to
`process.stdin`. `eval` is optional too and defaults to async wrapper for `eval`.

If `useGlobal` is set to true, then the repl will use the global object,
instead of running scripts in a separate context.

You can use your own `eval` function if it has following signature:

function eval(cmd, callback) {
Expand Down
27 changes: 20 additions & 7 deletions lib/repl.js
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/node.js
Expand Up @@ -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.
Expand Down

0 comments on commit 2d02e6a

Please sign in to comment.