use vm.runInContext instead of vm.Script.runInContext

This commit is contained in:
Sami Samhuri 2011-11-05 16:31:09 -07:00
parent 53d322e0ca
commit 9c36c8f9c3

View file

@ -11,6 +11,7 @@ var fs = require('fs')
, replModule = require('repl') , replModule = require('repl')
, spawn = require('child_process').spawn , spawn = require('child_process').spawn
, util = require('util') , util = require('util')
, vm = require('vm')
, Hint = 'Commands: .edit, .run, .stash <filename>, .unstash <filename>, .editor <editor>' , Hint = 'Commands: .edit, .run, .stash <filename>, .unstash <filename>, .editor <editor>'
, theRepl , theRepl
@ -176,19 +177,15 @@ function run(filename, callback) {
return return
} }
var evalcx = require('vm').Script.runInContext var read = fs.createReadStream(filename)
, read = fs.createReadStream(filename) , cmd = ''
, s = '' read.on('data', function(d) { cmd += d })
read.on('data', function(d) { s += d })
read.on('end', function() { read.on('end', function() {
// The catchall for errors // The catchall for errors
try { try {
// Use evalcx to supply the global context var ret = vm.runInContext(cmd, theRepl.context, 'repl');
var ret = evalcx(s, repl.context, "repl"); theRepl.context._ = ret
if (ret !== undefined) { theRepl.outputStream.write(replModule.writer(ret) + '\n')
repl.context._ = ret
repl.outputStream.write(replModule.writer(ret) + '\n')
}
} }
catch (e) { catch (e) {
// On error: Print the error and clear the buffer // On error: Print the error and clear the buffer