Skip to content

Commit 47de95c

Browse files
tflanaganrvagg
authored andcommitted
doc: sort vm alphabetically
Reorders, with no contextual changes, the vm documentation alphabetically. PR-URL: #3662 Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 482e15b commit 47de95c

File tree

1 file changed

+143
-153
lines changed

1 file changed

+143
-153
lines changed

doc/api/vm.markdown

+143-153
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,125 @@ You can access this module with:
1111
JavaScript code can be compiled and run immediately or compiled, saved, and run
1212
later.
1313

14-
## vm.runInThisContext(code[, options])
14+
## Class: Script
1515

16-
`vm.runInThisContext()` compiles `code`, runs it and returns the result. Running
17-
code does not have access to local scope, but does have access to the current
18-
`global` object.
16+
A class for holding precompiled scripts, and running them in specific sandboxes.
1917

20-
Example of using `vm.runInThisContext` and `eval` to run the same code:
18+
### new vm.Script(code, options)
2119

20+
Creating a new `Script` compiles `code` but does not run it. Instead, the
21+
created `vm.Script` object represents this compiled code. This script can be run
22+
later many times using methods below. The returned script is not bound to any
23+
global object. It is bound before each run, just for that run.
24+
25+
The options when creating a script are:
26+
27+
- `filename`: allows you to control the filename that shows up in any stack
28+
traces produced from this script.
29+
- `displayErrors`: whether or not to print any errors to stderr, with the
30+
line of code that caused them highlighted, before throwing an exception.
31+
Applies only to syntax errors compiling the code; errors while running the
32+
code are controlled by the options to the script's methods.
33+
34+
### script.runInContext(contextifiedSandbox[, options])
35+
36+
Similar to `vm.runInContext` but a method of a precompiled `Script` object.
37+
`script.runInContext` runs `script`'s compiled code in `contextifiedSandbox`
38+
and returns the result. Running code does not have access to local scope.
39+
40+
`script.runInContext` takes the same options as `script.runInThisContext`.
41+
42+
Example: compile code that increments a global variable and sets one, then
43+
execute the code multiple times. These globals are contained in the sandbox.
44+
45+
var util = require('util');
2246
var vm = require('vm');
23-
var localVar = 'initial value';
2447

25-
var vmResult = vm.runInThisContext('localVar = "vm";');
26-
console.log('vmResult: ', vmResult);
27-
console.log('localVar: ', localVar);
48+
var sandbox = {
49+
animal: 'cat',
50+
count: 2
51+
};
2852

29-
var evalResult = eval('localVar = "eval";');
30-
console.log('evalResult: ', evalResult);
31-
console.log('localVar: ', localVar);
53+
var context = new vm.createContext(sandbox);
54+
var script = new vm.Script('count += 1; name = "kitty"');
3255

33-
// vmResult: 'vm', localVar: 'initial value'
34-
// evalResult: 'eval', localVar: 'eval'
56+
for (var i = 0; i < 10; ++i) {
57+
script.runInContext(context);
58+
}
3559

36-
`vm.runInThisContext` does not have access to the local scope, so `localVar` is
37-
unchanged. `eval` does have access to the local scope, so `localVar` is changed.
60+
console.log(util.inspect(sandbox));
3861

39-
In this way `vm.runInThisContext` is much like an [indirect `eval` call][1],
40-
e.g. `(0,eval)('code')`. However, it also has the following additional options:
62+
// { animal: 'cat', count: 12, name: 'kitty' }
4163

42-
- `filename`: allows you to control the filename that shows up in any stack
43-
traces produced.
44-
- `displayErrors`: whether or not to print any errors to stderr, with the
45-
line of code that caused them highlighted, before throwing an exception.
46-
Will capture both syntax errors from compiling `code` and runtime errors
47-
thrown by executing the compiled code. Defaults to `true`.
48-
- `timeout`: a number of milliseconds to execute `code` before terminating
49-
execution. If execution is terminated, an `Error` will be thrown.
64+
Note that running untrusted code is a tricky business requiring great care.
65+
`script.runInContext` is quite useful, but safely running untrusted code
66+
requires a separate process.
5067

51-
[1]: http://es5.github.io/#x10.4.2
68+
### script.runInNewContext([sandbox][, options])
69+
70+
Similar to `vm.runInNewContext` but a method of a precompiled `Script` object.
71+
`script.runInNewContext` contextifies `sandbox` if passed or creates a new
72+
contextified sandbox if it's omitted, and then runs `script`'s compiled code
73+
with the sandbox as the global object and returns the result. Running code does
74+
not have access to local scope.
75+
76+
`script.runInNewContext` takes the same options as `script.runInThisContext`.
77+
78+
Example: compile code that sets a global variable, then execute the code
79+
multiple times in different contexts. These globals are set on and contained in
80+
the sandboxes.
81+
82+
var util = require('util');
83+
var vm = require('vm');
84+
85+
var sandboxes = [{}, {}, {}];
86+
87+
var script = new vm.Script('globalVar = "set"');
88+
89+
sandboxes.forEach(function (sandbox) {
90+
script.runInNewContext(sandbox);
91+
});
92+
93+
console.log(util.inspect(sandboxes));
94+
95+
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
96+
97+
Note that running untrusted code is a tricky business requiring great care.
98+
`script.runInNewContext` is quite useful, but safely running untrusted code
99+
requires a separate process.
100+
101+
### script.runInThisContext([options])
102+
103+
Similar to `vm.runInThisContext` but a method of a precompiled `Script` object.
104+
`script.runInThisContext` runs `script`'s compiled code and returns the result.
105+
Running code does not have access to local scope, but does have access to the
106+
current `global` object.
107+
108+
Example of using `script.runInThisContext` to compile code once and run it
109+
multiple times:
110+
111+
var vm = require('vm');
112+
113+
global.globalVar = 0;
52114

115+
var script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
116+
117+
for (var i = 0; i < 1000; ++i) {
118+
script.runInThisContext();
119+
}
120+
121+
console.log(globalVar);
122+
123+
// 1000
124+
125+
The options for running a script are:
126+
127+
- `displayErrors`: whether or not to print any runtime errors to stderr, with
128+
the line of code that caused them highlighted, before throwing an exception.
129+
Applies only to runtime errors executing the code; it is impossible to create
130+
a `Script` instance with syntax errors, as the constructor will throw.
131+
- `timeout`: a number of milliseconds to execute the script before terminating
132+
execution. If execution is terminated, an `Error` will be thrown.
53133

54134
## vm.createContext([sandbox])
55135

@@ -70,13 +150,11 @@ tags together inside that sandbox.
70150

71151
[2]: http://es5.github.io/#x15.1
72152

73-
74153
## vm.isContext(sandbox)
75154

76155
Returns whether or not a sandbox object has been contextified by calling
77156
`vm.createContext` on it.
78157

79-
80158
## vm.runInContext(code, contextifiedSandbox[, options])
81159

82160
`vm.runInContext` compiles `code`, then runs it in `contextifiedSandbox` and
@@ -105,6 +183,18 @@ Note that running untrusted code is a tricky business requiring great care.
105183
`vm.runInContext` is quite useful, but safely running untrusted code requires a
106184
separate process.
107185

186+
## vm.runInDebugContext(code)
187+
188+
`vm.runInDebugContext` compiles and executes `code` inside the V8 debug context.
189+
The primary use case is to get access to the V8 debug object:
190+
191+
var Debug = vm.runInDebugContext('Debug');
192+
Debug.scripts().forEach(function(script) { console.log(script.name); });
193+
194+
Note that the debug context and object are intrinsically tied to V8's debugger
195+
implementation and may change (or even get removed) without prior warning.
196+
197+
The debug object can also be exposed with the `--expose_debug_as=` switch.
108198

109199
## vm.runInNewContext(code[, sandbox][, options])
110200

@@ -134,141 +224,41 @@ Note that running untrusted code is a tricky business requiring great care.
134224
`vm.runInNewContext` is quite useful, but safely running untrusted code requires
135225
a separate process.
136226

227+
## vm.runInThisContext(code[, options])
137228

138-
## vm.runInDebugContext(code)
139-
140-
`vm.runInDebugContext` compiles and executes `code` inside the V8 debug context.
141-
The primary use case is to get access to the V8 debug object:
142-
143-
var Debug = vm.runInDebugContext('Debug');
144-
Debug.scripts().forEach(function(script) { console.log(script.name); });
145-
146-
Note that the debug context and object are intrinsically tied to V8's debugger
147-
implementation and may change (or even get removed) without prior warning.
148-
149-
The debug object can also be exposed with the `--expose_debug_as=` switch.
229+
`vm.runInThisContext()` compiles `code`, runs it and returns the result. Running
230+
code does not have access to local scope, but does have access to the current
231+
`global` object.
150232

233+
Example of using `vm.runInThisContext` and `eval` to run the same code:
151234

152-
## Class: Script
235+
var vm = require('vm');
236+
var localVar = 'initial value';
153237

154-
A class for holding precompiled scripts, and running them in specific sandboxes.
238+
var vmResult = vm.runInThisContext('localVar = "vm";');
239+
console.log('vmResult: ', vmResult);
240+
console.log('localVar: ', localVar);
155241

242+
var evalResult = eval('localVar = "eval";');
243+
console.log('evalResult: ', evalResult);
244+
console.log('localVar: ', localVar);
156245

157-
### new vm.Script(code, options)
246+
// vmResult: 'vm', localVar: 'initial value'
247+
// evalResult: 'eval', localVar: 'eval'
158248

159-
Creating a new `Script` compiles `code` but does not run it. Instead, the
160-
created `vm.Script` object represents this compiled code. This script can be run
161-
later many times using methods below. The returned script is not bound to any
162-
global object. It is bound before each run, just for that run.
249+
`vm.runInThisContext` does not have access to the local scope, so `localVar` is
250+
unchanged. `eval` does have access to the local scope, so `localVar` is changed.
163251

164-
The options when creating a script are:
252+
In this way `vm.runInThisContext` is much like an [indirect `eval` call][1],
253+
e.g. `(0,eval)('code')`. However, it also has the following additional options:
165254

166255
- `filename`: allows you to control the filename that shows up in any stack
167-
traces produced from this script.
256+
traces produced.
168257
- `displayErrors`: whether or not to print any errors to stderr, with the
169258
line of code that caused them highlighted, before throwing an exception.
170-
Applies only to syntax errors compiling the code; errors while running the
171-
code are controlled by the options to the script's methods.
172-
173-
174-
### script.runInThisContext([options])
175-
176-
Similar to `vm.runInThisContext` but a method of a precompiled `Script` object.
177-
`script.runInThisContext` runs `script`'s compiled code and returns the result.
178-
Running code does not have access to local scope, but does have access to the
179-
current `global` object.
180-
181-
Example of using `script.runInThisContext` to compile code once and run it
182-
multiple times:
183-
184-
var vm = require('vm');
185-
186-
global.globalVar = 0;
187-
188-
var script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
189-
190-
for (var i = 0; i < 1000; ++i) {
191-
script.runInThisContext();
192-
}
193-
194-
console.log(globalVar);
195-
196-
// 1000
197-
198-
The options for running a script are:
199-
200-
- `displayErrors`: whether or not to print any runtime errors to stderr, with
201-
the line of code that caused them highlighted, before throwing an exception.
202-
Applies only to runtime errors executing the code; it is impossible to create
203-
a `Script` instance with syntax errors, as the constructor will throw.
204-
- `timeout`: a number of milliseconds to execute the script before terminating
259+
Will capture both syntax errors from compiling `code` and runtime errors
260+
thrown by executing the compiled code. Defaults to `true`.
261+
- `timeout`: a number of milliseconds to execute `code` before terminating
205262
execution. If execution is terminated, an `Error` will be thrown.
206263

207-
208-
### script.runInContext(contextifiedSandbox[, options])
209-
210-
Similar to `vm.runInContext` but a method of a precompiled `Script` object.
211-
`script.runInContext` runs `script`'s compiled code in `contextifiedSandbox`
212-
and returns the result. Running code does not have access to local scope.
213-
214-
`script.runInContext` takes the same options as `script.runInThisContext`.
215-
216-
Example: compile code that increments a global variable and sets one, then
217-
execute the code multiple times. These globals are contained in the sandbox.
218-
219-
var util = require('util');
220-
var vm = require('vm');
221-
222-
var sandbox = {
223-
animal: 'cat',
224-
count: 2
225-
};
226-
227-
var context = new vm.createContext(sandbox);
228-
var script = new vm.Script('count += 1; name = "kitty"');
229-
230-
for (var i = 0; i < 10; ++i) {
231-
script.runInContext(context);
232-
}
233-
234-
console.log(util.inspect(sandbox));
235-
236-
// { animal: 'cat', count: 12, name: 'kitty' }
237-
238-
Note that running untrusted code is a tricky business requiring great care.
239-
`script.runInContext` is quite useful, but safely running untrusted code
240-
requires a separate process.
241-
242-
243-
### script.runInNewContext([sandbox][, options])
244-
245-
Similar to `vm.runInNewContext` but a method of a precompiled `Script` object.
246-
`script.runInNewContext` contextifies `sandbox` if passed or creates a new
247-
contextified sandbox if it's omitted, and then runs `script`'s compiled code
248-
with the sandbox as the global object and returns the result. Running code does
249-
not have access to local scope.
250-
251-
`script.runInNewContext` takes the same options as `script.runInThisContext`.
252-
253-
Example: compile code that sets a global variable, then execute the code
254-
multiple times in different contexts. These globals are set on and contained in
255-
the sandboxes.
256-
257-
var util = require('util');
258-
var vm = require('vm');
259-
260-
var sandboxes = [{}, {}, {}];
261-
262-
var script = new vm.Script('globalVar = "set"');
263-
264-
sandboxes.forEach(function (sandbox) {
265-
script.runInNewContext(sandbox);
266-
});
267-
268-
console.log(util.inspect(sandboxes));
269-
270-
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
271-
272-
Note that running untrusted code is a tricky business requiring great care.
273-
`script.runInNewContext` is quite useful, but safely running untrusted code
274-
requires a separate process.
264+
[1]: http://es5.github.io/#x10.4.2

0 commit comments

Comments
 (0)