File tree Expand file tree Collapse file tree 1 file changed +4
-2
lines changed Expand file tree Collapse file tree 1 file changed +4
-2
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ code does not have access to local scope, but does have access to the current
19
19
20
20
Example of using ` vm.runInThisContext ` and ` eval ` to run the same code:
21
21
22
+ var vm = require('vm');
22
23
var localVar = 'initial value';
23
24
24
25
var vmResult = vm.runInThisContext('localVar = "vm";');
@@ -117,7 +118,7 @@ Example: compile and execute code that increments a global variable and sets a
117
118
new one. These globals are contained in the sandbox.
118
119
119
120
var util = require('util');
120
- var vm = require('vm'),
121
+ var vm = require('vm');
121
122
122
123
var sandbox = {
123
124
animal: 'cat',
@@ -223,10 +224,11 @@ execute the code multiple times. These globals are contained in the sandbox.
223
224
count: 2
224
225
};
225
226
227
+ var context = new vm.createContext(sandbox);
226
228
var script = new vm.Script('count += 1; name = "kitty"');
227
229
228
230
for (var i = 0; i < 10; ++i) {
229
- script.runInContext(sandbox );
231
+ script.runInContext(context );
230
232
}
231
233
232
234
console.log(util.inspect(sandbox));
You can’t perform that action at this time.
0 commit comments