895329553bfe441124df495f4e55e89fe297390a
[platform/upstream/nodejs.git] / doc / api / vm.markdown
1 # Executing JavaScript
2
3     Stability: 3 - Stable
4
5 <!--name=vm-->
6
7 You can access this module with:
8
9     var vm = require('vm');
10
11 JavaScript code can be compiled and run immediately or compiled, saved, and run later.
12
13
14 ## vm.runInThisContext(code, [filename])
15
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
18 in stack traces.
19
20 Example of using `vm.runInThisContext` and `eval` to run the same code:
21
22     var localVar = 123,
23         usingscript, evaled,
24         vm = require('vm');
25
26     usingscript = vm.runInThisContext('localVar = 1;',
27       'myfile.vm');
28     console.log('localVar: ' + localVar + ', usingscript: ' +
29       usingscript);
30     evaled = eval('localVar = 1;');
31     console.log('localVar: ' + localVar + ', evaled: ' +
32       evaled);
33
34     // localVar: 123, usingscript: 1
35     // localVar: 1, evaled: 1
36
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.
39
40 In case of syntax error in `code`, `vm.runInThisContext` emits the syntax error to stderr
41 and throws an exception.
42
43
44 ## vm.runInNewContext(code, [sandbox], [filename])
45
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.
50
51 Example: compile and execute code that increments a global variable and sets a new one.
52 These globals are contained in the sandbox.
53
54     var util = require('util'),
55         vm = require('vm'),
56         sandbox = {
57           animal: 'cat',
58           count: 2
59         };
60
61     vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm');
62     console.log(util.inspect(sandbox));
63
64     // { animal: 'cat', count: 3, name: 'kitty' }
65
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.
69
70 In case of syntax error in `code`, `vm.runInNewContext` emits the syntax error to stderr
71 and throws an exception.
72
73 ## vm.runInContext(code, context, [filename])
74
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
79 for `code`.
80 `filename` is optional, it's used only in stack traces.
81
82 Example: compile and execute code in a existing context.
83
84     var util = require('util'),
85         vm = require('vm'),
86         initSandbox = {
87           animal: 'cat',
88           count: 2
89         },
90         context = vm.createContext(initSandbox);
91
92     vm.runInContext('count += 1; name = "CATT"', context, 'myfile.vm');
93     console.log(util.inspect(context));
94
95     // { animal: 'cat', count: 3, name: 'CATT' }
96
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.
99
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.
103
104 In case of syntax error in `code`, `vm.runInContext` emits the syntax error to stderr
105 and throws an exception.
106
107 ## vm.createContext([initSandbox])
108
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.
113
114 ## vm.createScript(code, [filename])
115
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.
121
122 In case of syntax error in `code`, `createScript` prints the syntax error to stderr
123 and throws an exception.
124
125
126 ## Class: Script
127
128 A class for running scripts.  Returned by vm.createScript.
129
130 ### script.runInThisContext()
131
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).
136
137 Example of using `script.runInThisContext` to compile code once and run it multiple times:
138
139     var vm = require('vm');
140
141     globalVar = 0;
142
143     var script = vm.createScript('globalVar += 1', 'myfile.vm');
144
145     for (var i = 0; i < 1000 ; i += 1) {
146       script.runInThisContext();
147     }
148
149     console.log(globalVar);
150
151     // 1000
152
153
154 ### script.runInNewContext([sandbox])
155
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.
159
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.
162
163     var util = require('util'),
164         vm = require('vm'),
165         sandbox = {
166           animal: 'cat',
167           count: 2
168         };
169
170     var script = vm.createScript('count += 1; name = "kitty"', 'myfile.vm');
171
172     for (var i = 0; i < 10 ; i += 1) {
173       script.runInNewContext(sandbox);
174     }
175
176     console.log(util.inspect(sandbox));
177
178     // { animal: 'cat', count: 12, name: 'kitty' }
179
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.