Example of using `vm.runInThisContext` and `eval` to run the same code:
+ var vm = require('vm');
var localVar = 'initial value';
var vmResult = vm.runInThisContext('localVar = "vm";');
new one. These globals are contained in the sandbox.
var util = require('util');
- var vm = require('vm'),
+ var vm = require('vm');
var sandbox = {
animal: 'cat',
count: 2
};
+ var context = new vm.createContext(sandbox);
var script = new vm.Script('count += 1; name = "kitty"');
for (var i = 0; i < 10; ++i) {
- script.runInContext(sandbox);
+ script.runInContext(context);
}
console.log(util.inspect(sandbox));