Skip to content

Commit

Permalink
Execute repl code in new context
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs authored and ry committed May 29, 2010
1 parent 3c7873b commit 82813f2
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@
// repl.start("node > ").scope.foo = "stdin is fun"; // expose foo to repl scope

var sys = require('sys');
var evalcx = process.binding('evals').Script.runInNewContext;
var path = require("path");
var scope;

function setScope (self) {
scope = {};
for (var i in global) scope[i] = global[i];
scope.module = module;
scope.require = require;
}


// Can overridden with custom print functions, such as `probe` or `eyes.js`
exports.writer = sys.inspect;

function REPLServer(prompt, stream) {
var self = this;

self.scope = {};
if (!scope) setScope();
self.scope = scope;
self.buffered_cmd = '';
self.prompt = prompt || "node> ";
self.stream = stream || process.openStdin();
Expand Down Expand Up @@ -61,20 +72,20 @@ REPLServer.prototype.readline = function (cmd) {
// This try is for determining if the command is complete, or should
// continue onto the next line.
try {
self.buffered_cmd = self.convertToScope(self.buffered_cmd);

// Scope the readline with self.scope to provide "local" vars and make Douglas Crockford cry
with (self.scope) {
var ret = eval(self.buffered_cmd);
if (ret !== undefined) {
self.scope['_'] = ret;
self.stream.write(exports.writer(ret) + "\n");
}
// Scope the readline with self.scope
// with(){} and eval() are considered bad.
var ret = evalcx(self.buffered_cmd, scope, "repl");
if (ret !== undefined) {
scope._ = ret;
self.stream.write(exports.writer(ret) + "\n");
}

self.buffered_cmd = '';
} catch (e) {
if (!(e instanceof SyntaxError)) throw e;
// instanceof doesn't work across context switches.
if (!(e && e.constructor && e.constructor.name === "SyntaxError")) {
throw e;
}
}
} catch (e) {
// On error: Print the error and clear the buffer
Expand Down Expand Up @@ -107,7 +118,7 @@ REPLServer.prototype.parseREPLKeyword = function (cmd) {
case ".clear":
self.stream.write("Clearing Scope...\n");
self.buffered_cmd = '';
self.scope = {};
setScope();
self.displayPrompt();
return true;
case ".exit":
Expand Down

0 comments on commit 82813f2

Please sign in to comment.