|
13 | 13 | // repl.start("node > ").scope.foo = "stdin is fun"; // expose foo to repl scope
|
14 | 14 |
|
15 | 15 | var sys = require('sys');
|
| 16 | +var evalcx = process.binding('evals').Script.runInNewContext; |
| 17 | +var path = require("path"); |
| 18 | +var scope; |
| 19 | + |
| 20 | +function setScope (self) { |
| 21 | + scope = {}; |
| 22 | + for (var i in global) scope[i] = global[i]; |
| 23 | + scope.module = module; |
| 24 | + scope.require = require; |
| 25 | +} |
| 26 | + |
16 | 27 |
|
17 | 28 | // Can overridden with custom print functions, such as `probe` or `eyes.js`
|
18 | 29 | exports.writer = sys.inspect;
|
19 | 30 |
|
20 | 31 | function REPLServer(prompt, stream) {
|
21 | 32 | var self = this;
|
22 |
| - |
23 |
| - self.scope = {}; |
| 33 | + if (!scope) setScope(); |
| 34 | + self.scope = scope; |
24 | 35 | self.buffered_cmd = '';
|
25 | 36 | self.prompt = prompt || "node> ";
|
26 | 37 | self.stream = stream || process.openStdin();
|
@@ -61,20 +72,20 @@ REPLServer.prototype.readline = function (cmd) {
|
61 | 72 | // This try is for determining if the command is complete, or should
|
62 | 73 | // continue onto the next line.
|
63 | 74 | try {
|
64 |
| - self.buffered_cmd = self.convertToScope(self.buffered_cmd); |
65 |
| - |
66 |
| - // Scope the readline with self.scope to provide "local" vars and make Douglas Crockford cry |
67 |
| - with (self.scope) { |
68 |
| - var ret = eval(self.buffered_cmd); |
69 |
| - if (ret !== undefined) { |
70 |
| - self.scope['_'] = ret; |
71 |
| - self.stream.write(exports.writer(ret) + "\n"); |
72 |
| - } |
| 75 | + // Scope the readline with self.scope |
| 76 | + // with(){} and eval() are considered bad. |
| 77 | + var ret = evalcx(self.buffered_cmd, scope, "repl"); |
| 78 | + if (ret !== undefined) { |
| 79 | + scope._ = ret; |
| 80 | + self.stream.write(exports.writer(ret) + "\n"); |
73 | 81 | }
|
74 |
| - |
| 82 | + |
75 | 83 | self.buffered_cmd = '';
|
76 | 84 | } catch (e) {
|
77 |
| - if (!(e instanceof SyntaxError)) throw e; |
| 85 | + // instanceof doesn't work across context switches. |
| 86 | + if (!(e && e.constructor && e.constructor.name === "SyntaxError")) { |
| 87 | + throw e; |
| 88 | + } |
78 | 89 | }
|
79 | 90 | } catch (e) {
|
80 | 91 | // On error: Print the error and clear the buffer
|
@@ -107,7 +118,7 @@ REPLServer.prototype.parseREPLKeyword = function (cmd) {
|
107 | 118 | case ".clear":
|
108 | 119 | self.stream.write("Clearing Scope...\n");
|
109 | 120 | self.buffered_cmd = '';
|
110 |
| - self.scope = {}; |
| 121 | + setScope(); |
111 | 122 | self.displayPrompt();
|
112 | 123 | return true;
|
113 | 124 | case ".exit":
|
|
0 commit comments