Skip to content

Instantly share code, notes, and snippets.

@paulserraino
Created June 29, 2015 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulserraino/01e7ec30971d633fa35f to your computer and use it in GitHub Desktop.
Save paulserraino/01e7ec30971d633fa35f to your computer and use it in GitHub Desktop.
default eval from node core
var repl = require('repl');
var debug = require('debuglog');
var vm = require('vm');
// copy pasta from node source
// lib/repl.js
function defaultEval(code, context, file, cb) {
var err, result;
// first, create the Script object to check the syntax
try {
var script = vm.createScript(code, {
filename: file,
displayErrors: false
});
} catch (e) {
err = e;
debug('parse error %j', code, e);
}
if (!err) {
try {
if (this.useGlobal) {
result = script.runInThisContext({ displayErrors: false });
} else {
result = script.runInContext(context, { displayErrors: false });
}
} catch (e) {
err = e;
if (err && process.domain) {
debug('not recoverable, send to domain');
process.domain.emit('error', err);
process.domain.exit();
return;
}
}
}
cb(err, result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment