Created
June 29, 2015 19:27
-
-
Save paulserraino/01e7ec30971d633fa35f to your computer and use it in GitHub Desktop.
default eval from node core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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