@@ -47,11 +47,13 @@ The options when creating a script are:
47
47
48
48
### script.runInContext(contextifiedSandbox[ , options] )
49
49
50
- Similar to ` vm.runInContext ` but a method of a precompiled ` Script ` object.
51
- ` script.runInContext ` runs ` script ` 's compiled code in ` contextifiedSandbox `
52
- and returns the result. Running code does not have access to local scope.
50
+ Similar to [ ` vm.runInContext() ` ] [ ] but a method of a precompiled ` Script `
51
+ object. ` script.runInContext() ` runs ` script ` 's compiled code in
52
+ ` contextifiedSandbox ` and returns the result. Running code does not have access
53
+ to local scope.
53
54
54
- ` script.runInContext ` takes the same options as ` script.runInThisContext ` .
55
+ ` script.runInContext() ` takes the same options as
56
+ [ ` script.runInThisContext() ` ] [ ] .
55
57
56
58
Example: compile code that increments a global variable and sets one, then
57
59
execute the code multiple times. These globals are contained in the sandbox.
@@ -78,18 +80,19 @@ console.log(util.inspect(sandbox));
78
80
```
79
81
80
82
Note that running untrusted code is a tricky business requiring great care.
81
- ` script.runInContext ` is quite useful, but safely running untrusted code
83
+ ` script.runInContext() ` is quite useful, but safely running untrusted code
82
84
requires a separate process.
83
85
84
86
### script.runInNewContext([ sandbox] [ , options ] )
85
87
86
- Similar to ` vm.runInNewContext ` but a method of a precompiled ` Script ` object.
87
- ` script.runInNewContext ` contextifies ` sandbox ` if passed or creates a new
88
- contextified sandbox if it's omitted, and then runs ` script ` 's compiled code
88
+ Similar to [ ` vm.runInNewContext() ` ] [ ] but a method of a precompiled ` Script `
89
+ object. ` script.runInNewContext() ` contextifies ` sandbox ` if passed or creates a
90
+ new contextified sandbox if it's omitted, and then runs ` script ` 's compiled code
89
91
with the sandbox as the global object and returns the result. Running code does
90
92
not have access to local scope.
91
93
92
- ` script.runInNewContext ` takes the same options as ` script.runInThisContext ` .
94
+ ` script.runInNewContext() ` takes the same options as
95
+ [ ` script.runInThisContext() ` ] [ ] .
93
96
94
97
Example: compile code that sets a global variable, then execute the code
95
98
multiple times in different contexts. These globals are set on and contained in
@@ -113,17 +116,17 @@ console.log(util.inspect(sandboxes));
113
116
```
114
117
115
118
Note that running untrusted code is a tricky business requiring great care.
116
- ` script.runInNewContext ` is quite useful, but safely running untrusted code
119
+ ` script.runInNewContext() ` is quite useful, but safely running untrusted code
117
120
requires a separate process.
118
121
119
122
### script.runInThisContext([ options] )
120
123
121
- Similar to ` vm.runInThisContext ` but a method of a precompiled ` Script ` object.
122
- ` script.runInThisContext ` runs ` script ` 's compiled code and returns the result.
123
- Running code does not have access to local scope, but does have access to the
124
- current ` global ` object.
124
+ Similar to [ ` vm.runInThisContext() ` ] ( ) but a method of a precompiled ` Script `
125
+ object. ` script.runInThisContext() ` runs ` script ` 's compiled code and returns
126
+ the result. Running code does not have access to local scope, but does have
127
+ access to the current ` global ` object.
125
128
126
- Example of using ` script.runInThisContext ` to compile code once and run it
129
+ Example of using ` script.runInThisContext() ` to compile code once and run it
127
130
multiple times:
128
131
129
132
``` js
@@ -160,11 +163,11 @@ The options for running a script are:
160
163
## vm.createContext([ sandbox] )
161
164
162
165
If given a ` sandbox ` object, will "contextify" that sandbox so that it can be
163
- used in calls to ` vm.runInContext ` or ` script.runInContext ` . Inside scripts run
164
- as such, ` sandbox ` will be the global object, retaining all its existing
165
- properties but also having the built-in objects and functions any standard
166
- [ global object] [ ] has. Outside of scripts run by the vm module, ` sandbox ` will
167
- be unchanged.
166
+ used in calls to [ ` vm.runInContext() ` ] [ ] or [ ` script.runInContext() ` ] [ ] . Inside
167
+ scripts run as such, ` sandbox ` will be the global object, retaining all its
168
+ existing properties but also having the built-in objects and functions any
169
+ standard [ global object] [ ] has. Outside of scripts run by the vm module,
170
+ ` sandbox ` will be unchanged.
168
171
169
172
If not given a sandbox object, returns a new, empty contextified sandbox object
170
173
you can use.
@@ -177,16 +180,16 @@ tags together inside that sandbox.
177
180
## vm.isContext(sandbox)
178
181
179
182
Returns whether or not a sandbox object has been contextified by calling
180
- ` vm.createContext ` on it.
183
+ [ ` vm.createContext() ` ] [ ] on it.
181
184
182
185
## vm.runInContext(code, contextifiedSandbox[ , options] )
183
186
184
- ` vm.runInContext ` compiles ` code ` , then runs it in ` contextifiedSandbox ` and
187
+ ` vm.runInContext() ` compiles ` code ` , then runs it in ` contextifiedSandbox ` and
185
188
returns the result. Running code does not have access to local scope. The
186
189
` contextifiedSandbox ` object must have been previously contextified via
187
- ` vm.createContext ` ; it will be used as the global object for ` code ` .
190
+ [ ` vm.createContext() ` ] [ ] ; it will be used as the global object for ` code ` .
188
191
189
- ` vm.runInContext ` takes the same options as ` vm.runInThisContext ` .
192
+ ` vm.runInContext() ` takes the same options as [ ` vm.runInThisContext() ` ] [ ] .
190
193
191
194
Example: compile and execute different scripts in a single existing context.
192
195
@@ -206,13 +209,13 @@ console.log(util.inspect(sandbox));
206
209
```
207
210
208
211
Note that running untrusted code is a tricky business requiring great care.
209
- ` vm.runInContext ` is quite useful, but safely running untrusted code requires a
210
- separate process.
212
+ ` vm.runInContext() ` is quite useful, but safely running untrusted code requires
213
+ a separate process.
211
214
212
215
## vm.runInDebugContext(code)
213
216
214
- ` vm.runInDebugContext ` compiles and executes ` code ` inside the V8 debug context.
215
- The primary use case is to get access to the V8 debug object:
217
+ ` vm.runInDebugContext() ` compiles and executes ` code ` inside the V8 debug
218
+ context. The primary use case is to get access to the V8 debug object:
216
219
217
220
``` js
218
221
const Debug = vm .runInDebugContext (' Debug' );
@@ -226,11 +229,11 @@ The debug object can also be exposed with the `--expose_debug_as=` switch.
226
229
227
230
## vm.runInNewContext(code[ , sandbox] [ , options ] )
228
231
229
- ` vm.runInNewContext ` compiles ` code ` , contextifies ` sandbox ` if passed or
232
+ ` vm.runInNewContext() ` compiles ` code ` , contextifies ` sandbox ` if passed or
230
233
creates a new contextified sandbox if it's omitted, and then runs the code with
231
234
the sandbox as the global object and returns the result.
232
235
233
- ` vm.runInNewContext ` takes the same options as ` vm.runInThisContext ` .
236
+ ` vm.runInNewContext() ` takes the same options as [ ` vm.runInThisContext() ` ] [ ] .
234
237
235
238
Example: compile and execute code that increments a global variable and sets a
236
239
new one. These globals are contained in the sandbox.
@@ -251,7 +254,7 @@ console.log(util.inspect(sandbox));
251
254
```
252
255
253
256
Note that running untrusted code is a tricky business requiring great care.
254
- ` vm.runInNewContext ` is quite useful, but safely running untrusted code requires
257
+ ` vm.runInNewContext() ` is quite useful, but safely running untrusted code requires
255
258
a separate process.
256
259
257
260
## vm.runInThisContext(code[ , options] )
@@ -260,7 +263,7 @@ a separate process.
260
263
code does not have access to local scope, but does have access to the current
261
264
` global ` object.
262
265
263
- Example of using ` vm.runInThisContext ` and ` eval ` to run the same code:
266
+ Example of using ` vm.runInThisContext() ` and [ ` eval() ` ] [ ] to run the same code:
264
267
265
268
``` js
266
269
const vm = require (' vm' );
@@ -278,10 +281,11 @@ console.log('localVar: ', localVar);
278
281
// evalResult: 'eval', localVar: 'eval'
279
282
```
280
283
281
- ` vm.runInThisContext ` does not have access to the local scope, so ` localVar ` is
282
- unchanged. ` eval ` does have access to the local scope, so ` localVar ` is changed.
284
+ ` vm.runInThisContext() ` does not have access to the local scope, so ` localVar `
285
+ is unchanged. [ ` eval() ` ] [ ] does have access to the local scope, so ` localVar ` is
286
+ changed.
283
287
284
- In this way ` vm.runInThisContext ` is much like an [ indirect ` eval ` call] [ ] ,
288
+ In this way ` vm.runInThisContext() ` is much like an [ indirect ` eval() ` call] [ ] ,
285
289
e.g. ` (0,eval)('code') ` . However, it also has the following additional options:
286
290
287
291
- ` filename ` : allows you to control the filename that shows up in any stack
@@ -297,6 +301,13 @@ e.g. `(0,eval)('code')`. However, it also has the following additional options:
297
301
- ` timeout ` : a number of milliseconds to execute ` code ` before terminating
298
302
execution. If execution is terminated, an [ ` Error ` ] [ ] will be thrown.
299
303
300
- [ indirect `eval` call ] : https://es5.github.io/#x10.4.2
304
+ [ indirect `eval() ` call ] : https://es5.github.io/#x10.4.2
301
305
[ global object ] : https://es5.github.io/#x15.1
302
306
[ `Error` ] : errors.html#errors_class_error
307
+ [ `script.runInContext()` ] : #vm_script_runincontext_contextifiedsandbox_options
308
+ [ `script.runInThisContext()` ] : #vm_script_runinthiscontext_options
309
+ [ `vm.createContext()` ] : #vm_vm_createcontext_sandbox
310
+ [ `vm.runInContext()` ] : #vm_vm_runincontext_code_contextifiedsandbox_options
311
+ [ `vm.runInNewContext()` ] : #vm_vm_runinnewcontext_code_sandbox_options
312
+ [ `vm.runInThisContext()` ] : #vm_vm_runinthiscontext_code_options
313
+ [ `eval()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
0 commit comments