7 You can access this module with:
9 var vm = require('vm');
11 JavaScript code can be compiled and run immediately or compiled, saved, and run later.
14 ## vm.runInThisContext(code, [filename])
16 `vm.runInThisContext()` compiles `code`, runs it and returns the result. Running
17 code does not have access to local scope. `filename` is optional, it's used only
20 Example of using `vm.runInThisContext` and `eval` to run the same code:
26 usingscript = vm.runInThisContext('localVar = 1;',
28 console.log('localVar: ' + localVar + ', usingscript: ' +
30 evaled = eval('localVar = 1;');
31 console.log('localVar: ' + localVar + ', evaled: ' +
34 // localVar: 123, usingscript: 1
35 // localVar: 1, evaled: 1
37 `vm.runInThisContext` does not have access to the local scope, so `localVar` is unchanged.
38 `eval` does have access to the local scope, so `localVar` is changed.
40 In case of syntax error in `code`, `vm.runInThisContext` emits the syntax error to stderr
41 and throws an exception.
44 ## vm.runInNewContext(code, [sandbox], [filename])
46 `vm.runInNewContext` compiles `code`, then runs it in `sandbox` and returns the
47 result. Running code does not have access to local scope. The object `sandbox`
48 will be used as the global object for `code`.
49 `sandbox` and `filename` are optional, `filename` is only used in stack traces.
51 Example: compile and execute code that increments a global variable and sets a new one.
52 These globals are contained in the sandbox.
54 var util = require('util'),
61 vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm');
62 console.log(util.inspect(sandbox));
64 // { animal: 'cat', count: 3, name: 'kitty' }
66 Note that running untrusted code is a tricky business requiring great care. To prevent accidental
67 global variable leakage, `vm.runInNewContext` is quite useful, but safely running untrusted code
68 requires a separate process.
70 In case of syntax error in `code`, `vm.runInNewContext` emits the syntax error to stderr
71 and throws an exception.
73 ## vm.runInContext(code, context, [filename])
75 `vm.runInContext` compiles `code`, then runs it in `context` and returns the
76 result. A (V8) context comprises a global object, together with a set of
77 built-in objects and functions. Running code does not have access to local scope
78 and the global object held within `context` will be used as the global object
80 `filename` is optional, it's used only in stack traces.
82 Example: compile and execute code in a existing context.
84 var util = require('util'),
90 context = vm.createContext(initSandbox);
92 vm.runInContext('count += 1; name = "CATT"', context, 'myfile.vm');
93 console.log(util.inspect(context));
95 // { animal: 'cat', count: 3, name: 'CATT' }
97 Note that `createContext` will perform a shallow clone of the supplied sandbox object in order to
98 initialize the global object of the freshly constructed context.
100 Note that running untrusted code is a tricky business requiring great care. To prevent accidental
101 global variable leakage, `vm.runInContext` is quite useful, but safely running untrusted code
102 requires a separate process.
104 In case of syntax error in `code`, `vm.runInContext` emits the syntax error to stderr
105 and throws an exception.
107 ## vm.createContext([initSandbox])
109 `vm.createContext` creates a new context which is suitable for use as the 2nd argument of a subsequent
110 call to `vm.runInContext`. A (V8) context comprises a global object together with a set of
111 build-in objects and functions. The optional argument `initSandbox` will be shallow-copied
112 to seed the initial contents of the global object used by the context.
114 ## vm.createScript(code, [filename])
116 `createScript` compiles `code` but does not run it. Instead, it returns a
117 `vm.Script` object representing this compiled code. This script can be run
118 later many times using methods below. The returned script is not bound to any
119 global object. It is bound before each run, just for that run. `filename` is
120 optional, it's only used in stack traces.
122 In case of syntax error in `code`, `createScript` prints the syntax error to stderr
123 and throws an exception.
128 A class for running scripts. Returned by vm.createScript.
130 ### script.runInThisContext()
132 Similar to `vm.runInThisContext` but a method of a precompiled `Script` object.
133 `script.runInThisContext` runs the code of `script` and returns the result.
134 Running code does not have access to local scope, but does have access to the `global` object
135 (v8: in actual context).
137 Example of using `script.runInThisContext` to compile code once and run it multiple times:
139 var vm = require('vm');
143 var script = vm.createScript('globalVar += 1', 'myfile.vm');
145 for (var i = 0; i < 1000 ; i += 1) {
146 script.runInThisContext();
149 console.log(globalVar);
154 ### script.runInNewContext([sandbox])
156 Similar to `vm.runInNewContext` a method of a precompiled `Script` object.
157 `script.runInNewContext` runs the code of `script` with `sandbox` as the global object and returns the result.
158 Running code does not have access to local scope. `sandbox` is optional.
160 Example: compile code that increments a global variable and sets one, then execute this code multiple times.
161 These globals are contained in the sandbox.
163 var util = require('util'),
170 var script = vm.createScript('count += 1; name = "kitty"', 'myfile.vm');
172 for (var i = 0; i < 10 ; i += 1) {
173 script.runInNewContext(sandbox);
176 console.log(util.inspect(sandbox));
178 // { animal: 'cat', count: 12, name: 'kitty' }
180 Note that running untrusted code is a tricky business requiring great care. To prevent accidental
181 global variable leakage, `script.runInNewContext` is quite useful, but safely running untrusted code
182 requires a separate process.