mirror of https://github.com/nodejs/node.git
Add useGlobal flag for standard node repl
parent
caf70f5e94
commit
2d02e6a992
|
@ -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) {
|
||||
|
|
27
lib/repl.js
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();
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue