Skip to content

Commit 0c924ea

Browse files
estliberitasjasnell
authored andcommitted
doc: improve styling consistency in VM docs
Improve functions styling. Connect sections with links. PR-URL: #5005 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d74eb5f commit 0c924ea

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

doc/api/vm.markdown

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ The options when creating a script are:
4747

4848
### script.runInContext(contextifiedSandbox[, options])
4949

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.
5354

54-
`script.runInContext` takes the same options as `script.runInThisContext`.
55+
`script.runInContext()` takes the same options as
56+
[`script.runInThisContext()`][].
5557

5658
Example: compile code that increments a global variable and sets one, then
5759
execute the code multiple times. These globals are contained in the sandbox.
@@ -78,18 +80,19 @@ console.log(util.inspect(sandbox));
7880
```
7981

8082
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
8284
requires a separate process.
8385

8486
### script.runInNewContext([sandbox][, options])
8587

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
8991
with the sandbox as the global object and returns the result. Running code does
9092
not have access to local scope.
9193

92-
`script.runInNewContext` takes the same options as `script.runInThisContext`.
94+
`script.runInNewContext()` takes the same options as
95+
[`script.runInThisContext()`][].
9396

9497
Example: compile code that sets a global variable, then execute the code
9598
multiple times in different contexts. These globals are set on and contained in
@@ -113,17 +116,17 @@ console.log(util.inspect(sandboxes));
113116
```
114117

115118
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
117120
requires a separate process.
118121

119122
### script.runInThisContext([options])
120123

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.
125128

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
127130
multiple times:
128131

129132
```js
@@ -160,11 +163,11 @@ The options for running a script are:
160163
## vm.createContext([sandbox])
161164

162165
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.
168171

169172
If not given a sandbox object, returns a new, empty contextified sandbox object
170173
you can use.
@@ -177,16 +180,16 @@ tags together inside that sandbox.
177180
## vm.isContext(sandbox)
178181

179182
Returns whether or not a sandbox object has been contextified by calling
180-
`vm.createContext` on it.
183+
[`vm.createContext()`][] on it.
181184

182185
## vm.runInContext(code, contextifiedSandbox[, options])
183186

184-
`vm.runInContext` compiles `code`, then runs it in `contextifiedSandbox` and
187+
`vm.runInContext()` compiles `code`, then runs it in `contextifiedSandbox` and
185188
returns the result. Running code does not have access to local scope. The
186189
`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`.
188191

189-
`vm.runInContext` takes the same options as `vm.runInThisContext`.
192+
`vm.runInContext()` takes the same options as [`vm.runInThisContext()`][].
190193

191194
Example: compile and execute different scripts in a single existing context.
192195

@@ -206,13 +209,13 @@ console.log(util.inspect(sandbox));
206209
```
207210

208211
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.
211214

212215
## vm.runInDebugContext(code)
213216

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:
216219

217220
```js
218221
const Debug = vm.runInDebugContext('Debug');
@@ -226,11 +229,11 @@ The debug object can also be exposed with the `--expose_debug_as=` switch.
226229

227230
## vm.runInNewContext(code[, sandbox][, options])
228231

229-
`vm.runInNewContext` compiles `code`, contextifies `sandbox` if passed or
232+
`vm.runInNewContext()` compiles `code`, contextifies `sandbox` if passed or
230233
creates a new contextified sandbox if it's omitted, and then runs the code with
231234
the sandbox as the global object and returns the result.
232235

233-
`vm.runInNewContext` takes the same options as `vm.runInThisContext`.
236+
`vm.runInNewContext()` takes the same options as [`vm.runInThisContext()`][].
234237

235238
Example: compile and execute code that increments a global variable and sets a
236239
new one. These globals are contained in the sandbox.
@@ -251,7 +254,7 @@ console.log(util.inspect(sandbox));
251254
```
252255

253256
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
255258
a separate process.
256259

257260
## vm.runInThisContext(code[, options])
@@ -260,7 +263,7 @@ a separate process.
260263
code does not have access to local scope, but does have access to the current
261264
`global` object.
262265

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:
264267

265268
```js
266269
const vm = require('vm');
@@ -278,10 +281,11 @@ console.log('localVar: ', localVar);
278281
// evalResult: 'eval', localVar: 'eval'
279282
```
280283

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.
283287

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][],
285289
e.g. `(0,eval)('code')`. However, it also has the following additional options:
286290

287291
- `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:
297301
- `timeout`: a number of milliseconds to execute `code` before terminating
298302
execution. If execution is terminated, an [`Error`][] will be thrown.
299303

300-
[indirect `eval` call]: https://es5.github.io/#x10.4.2
304+
[indirect `eval()` call]: https://es5.github.io/#x10.4.2
301305
[global object]: https://es5.github.io/#x15.1
302306
[`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

Comments
 (0)