Make the REPL use the global context to be consistent with the node REPL.

This will make packages that modify prototypes - e.g. 'colors', 'sugar'
- work as expected.

To verify that the `node` REPL uses the global context, execute `global
=== module.exports.repl.context`.

Note: Tests pass, except `cluster.coffee`, which, however, failed even
before these modifications.
This commit is contained in:
Michael Klement
2013-08-06 21:28:34 -04:00
parent 9d24a3420d
commit 0235d12927
3 changed files with 7 additions and 3 deletions

View File

@@ -18,6 +18,7 @@
prompt: 'coffee> ',
historyFile: process.env.HOME ? path.join(process.env.HOME, '.coffee_history') : void 0,
historyMaxInputSize: 10240,
useGlobal: true,
"eval": function(input, context, filename, cb) {
var Assign, Block, Literal, Value, ast, err, js, _ref1;
input = input.replace(/\uFF00/g, '\n');
@@ -30,7 +31,7 @@
bare: true,
locals: Object.keys(context)
});
return cb(null, vm.runInContext(js, context, filename));
return cb(null, context === global ? vm.runInThisContext(js, filename) : vm.runInContext(js, context, filename));
} catch (_error) {
err = _error;
updateSyntaxError(err, input);

View File

@@ -9,6 +9,8 @@ replDefaults =
prompt: 'coffee> ',
historyFile: path.join process.env.HOME, '.coffee_history' if process.env.HOME
historyMaxInputSize: 10240
# Make the REPL use the global context by default so as to (a) be consistent with the `node` REPL and, therefore, (b) make packages that modify prototypes - e.g., 'colors', 'sugar' - work as expected.
useGlobal: yes
eval: (input, context, filename, cb) ->
# XXX: multiline hack.
input = input.replace /\uFF00/g, '\n'
@@ -27,7 +29,7 @@ replDefaults =
new Assign (new Value new Literal '_'), ast, '='
]
js = ast.compile bare: yes, locals: Object.keys(context)
cb null, vm.runInContext(js, context, filename)
cb null, if context is global then vm.runInThisContext(js, filename) else vm.runInContext(js, context, filename)
catch err
# AST's `compile` does not add source code information to syntax errors.
updateSyntaxError err, input

View File

@@ -102,10 +102,11 @@ testRepl "existential assignment of previously declared variable", (input, outpu
eq '42', output.lastWrite()
testRepl "keeps running after runtime error", (input, output) ->
input.emitLine 'a = 0' # Note: with the REPL option `useGlobal` set to true, variables are retained across tests.
input.emitLine 'a = b'
eq 0, output.lastWrite().indexOf 'ReferenceError: b is not defined'
input.emitLine 'a'
eq 'undefined', output.lastWrite()
eq '0', output.lastWrite()
process.on 'exit', ->
fs.unlinkSync historyFile