Skip to content

Instantly share code, notes, and snippets.

@gronnbeck
Last active August 29, 2015 13:59
Show Gist options
  • Save gronnbeck/10971577 to your computer and use it in GitHub Desktop.
Save gronnbeck/10971577 to your computer and use it in GitHub Desktop.
Testing the node VM module
var vm = require('vm')
try {
vm.runInThisContext('var hello = "world"; console.log("Hello " + hello);');
} catch(e) {
console.log('Failed unexpectedly')
}
try {
var secret = 'seeecret!'
vm.runInThisContext('var hello = "world"; console.log("Hello " + hello + secret);');
} catch (e) {
console.log('Fails as expected')
}
try {
var context = {
secret: '!',
log: console.log
}
var script = 'var hello = "world"; log("Hello " + hello + secret);'
vm.runInNewContext(script, context);
} catch(e) {
console.log('Failed unexpectedly')
console.log(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment