Skip to content

Instantly share code, notes, and snippets.

@danmactough
Last active August 29, 2015 14:02
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 danmactough/43c5fb61fadf9c7df5ad to your computer and use it in GitHub Desktop.
Save danmactough/43c5fb61fadf9c7df5ad to your computer and use it in GitHub Desktop.
var vm = require('vm');
var boss = 'Mr. Trump'
, condition = 'bald';
var script = [
'boss = name;',
'condition = type;',
'result = boss + " is " + condition;'
].join('\n');
var ctx = {
name: 'Dan',
type: 'awesome'
};
var evaled = vm.runInContext(script, vm.createContext(ctx));
console.log(evaled);
// Dan is awesome
ctx = {
name: boss,
type: condition
};
var evaled = vm.runInContext(script, vm.createContext(ctx));
console.log(evaled);
// Mr. Trump is bald
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment