Skip to content

Commit 3915710

Browse files
estliberitasMyles Borins
authored andcommitted
doc: improve styling consistency in VM docs
Improve functions styling. Connect sections with links. PR-URL: #5005 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent af7c8fd commit 3915710

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
@@ -39,11 +39,13 @@ The options when creating a script are:
3939

4040
### script.runInContext(contextifiedSandbox[, options])
4141

42-
Similar to `vm.runInContext` but a method of a precompiled `Script` object.
43-
`script.runInContext` runs `script`'s compiled code in `contextifiedSandbox`
44-
and returns the result. Running code does not have access to local scope.
42+
Similar to [`vm.runInContext()`][] but a method of a precompiled `Script`
43+
object. `script.runInContext()` runs `script`'s compiled code in
44+
`contextifiedSandbox` and returns the result. Running code does not have access
45+
to local scope.
4546

46-
`script.runInContext` takes the same options as `script.runInThisContext`.
47+
`script.runInContext()` takes the same options as
48+
[`script.runInThisContext()`][].
4749

4850
Example: compile code that increments a global variable and sets one, then
4951
execute the code multiple times. These globals are contained in the sandbox.
@@ -68,18 +70,19 @@ execute the code multiple times. These globals are contained in the sandbox.
6870
// { animal: 'cat', count: 12, name: 'kitty' }
6971

7072
Note that running untrusted code is a tricky business requiring great care.
71-
`script.runInContext` is quite useful, but safely running untrusted code
73+
`script.runInContext()` is quite useful, but safely running untrusted code
7274
requires a separate process.
7375

7476
### script.runInNewContext([sandbox][, options])
7577

76-
Similar to `vm.runInNewContext` but a method of a precompiled `Script` object.
77-
`script.runInNewContext` contextifies `sandbox` if passed or creates a new
78-
contextified sandbox if it's omitted, and then runs `script`'s compiled code
78+
Similar to [`vm.runInNewContext()`][] but a method of a precompiled `Script`
79+
object. `script.runInNewContext()` contextifies `sandbox` if passed or creates a
80+
new contextified sandbox if it's omitted, and then runs `script`'s compiled code
7981
with the sandbox as the global object and returns the result. Running code does
8082
not have access to local scope.
8183

82-
`script.runInNewContext` takes the same options as `script.runInThisContext`.
84+
`script.runInNewContext()` takes the same options as
85+
[`script.runInThisContext()`][].
8386

8487
Example: compile code that sets a global variable, then execute the code
8588
multiple times in different contexts. These globals are set on and contained in
@@ -101,17 +104,17 @@ the sandboxes.
101104
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
102105

103106
Note that running untrusted code is a tricky business requiring great care.
104-
`script.runInNewContext` is quite useful, but safely running untrusted code
107+
`script.runInNewContext()` is quite useful, but safely running untrusted code
105108
requires a separate process.
106109

107110
### script.runInThisContext([options])
108111

109-
Similar to `vm.runInThisContext` but a method of a precompiled `Script` object.
110-
`script.runInThisContext` runs `script`'s compiled code and returns the result.
111-
Running code does not have access to local scope, but does have access to the
112-
current `global` object.
112+
Similar to [`vm.runInThisContext()`]() but a method of a precompiled `Script`
113+
object. `script.runInThisContext()` runs `script`'s compiled code and returns
114+
the result. Running code does not have access to local scope, but does have
115+
access to the current `global` object.
113116

114-
Example of using `script.runInThisContext` to compile code once and run it
117+
Example of using `script.runInThisContext()` to compile code once and run it
115118
multiple times:
116119

117120
const vm = require('vm');
@@ -146,11 +149,11 @@ The options for running a script are:
146149
## vm.createContext([sandbox])
147150

148151
If given a `sandbox` object, will "contextify" that sandbox so that it can be
149-
used in calls to `vm.runInContext` or `script.runInContext`. Inside scripts run
150-
as such, `sandbox` will be the global object, retaining all its existing
151-
properties but also having the built-in objects and functions any standard
152-
[global object][] has. Outside of scripts run by the vm module, `sandbox` will
153-
be unchanged.
152+
used in calls to [`vm.runInContext()`][] or [`script.runInContext()`][]. Inside
153+
scripts run as such, `sandbox` will be the global object, retaining all its
154+
existing properties but also having the built-in objects and functions any
155+
standard [global object][] has. Outside of scripts run by the vm module,
156+
`sandbox` will be unchanged.
154157

155158
If not given a sandbox object, returns a new, empty contextified sandbox object
156159
you can use.
@@ -163,16 +166,16 @@ tags together inside that sandbox.
163166
## vm.isContext(sandbox)
164167

165168
Returns whether or not a sandbox object has been contextified by calling
166-
`vm.createContext` on it.
169+
[`vm.createContext()`][] on it.
167170

168171
## vm.runInContext(code, contextifiedSandbox[, options])
169172

170-
`vm.runInContext` compiles `code`, then runs it in `contextifiedSandbox` and
173+
`vm.runInContext()` compiles `code`, then runs it in `contextifiedSandbox` and
171174
returns the result. Running code does not have access to local scope. The
172175
`contextifiedSandbox` object must have been previously contextified via
173-
`vm.createContext`; it will be used as the global object for `code`.
176+
[`vm.createContext()`][]; it will be used as the global object for `code`.
174177

175-
`vm.runInContext` takes the same options as `vm.runInThisContext`.
178+
`vm.runInContext()` takes the same options as [`vm.runInThisContext()`][].
176179

177180
Example: compile and execute different scripts in a single existing context.
178181

@@ -190,13 +193,13 @@ Example: compile and execute different scripts in a single existing context.
190193
// { globalVar: 1024 }
191194

192195
Note that running untrusted code is a tricky business requiring great care.
193-
`vm.runInContext` is quite useful, but safely running untrusted code requires a
194-
separate process.
196+
`vm.runInContext()` is quite useful, but safely running untrusted code requires
197+
a separate process.
195198

196199
## vm.runInDebugContext(code)
197200

198-
`vm.runInDebugContext` compiles and executes `code` inside the V8 debug context.
199-
The primary use case is to get access to the V8 debug object:
201+
`vm.runInDebugContext()` compiles and executes `code` inside the V8 debug
202+
context. The primary use case is to get access to the V8 debug object:
200203

201204
const Debug = vm.runInDebugContext('Debug');
202205
Debug.scripts().forEach(function(script) { console.log(script.name); });
@@ -208,11 +211,11 @@ The debug object can also be exposed with the `--expose_debug_as=` switch.
208211

209212
## vm.runInNewContext(code[, sandbox][, options])
210213

211-
`vm.runInNewContext` compiles `code`, contextifies `sandbox` if passed or
214+
`vm.runInNewContext()` compiles `code`, contextifies `sandbox` if passed or
212215
creates a new contextified sandbox if it's omitted, and then runs the code with
213216
the sandbox as the global object and returns the result.
214217

215-
`vm.runInNewContext` takes the same options as `vm.runInThisContext`.
218+
`vm.runInNewContext()` takes the same options as [`vm.runInThisContext()`][].
216219

217220
Example: compile and execute code that increments a global variable and sets a
218221
new one. These globals are contained in the sandbox.
@@ -231,7 +234,7 @@ new one. These globals are contained in the sandbox.
231234
// { animal: 'cat', count: 3, name: 'kitty' }
232235

233236
Note that running untrusted code is a tricky business requiring great care.
234-
`vm.runInNewContext` is quite useful, but safely running untrusted code requires
237+
`vm.runInNewContext()` is quite useful, but safely running untrusted code requires
235238
a separate process.
236239

237240
## vm.runInThisContext(code[, options])
@@ -240,7 +243,7 @@ a separate process.
240243
code does not have access to local scope, but does have access to the current
241244
`global` object.
242245

243-
Example of using `vm.runInThisContext` and `eval` to run the same code:
246+
Example of using `vm.runInThisContext()` and [`eval()`][] to run the same code:
244247

245248
const vm = require('vm');
246249
var localVar = 'initial value';
@@ -256,10 +259,11 @@ Example of using `vm.runInThisContext` and `eval` to run the same code:
256259
// vmResult: 'vm', localVar: 'initial value'
257260
// evalResult: 'eval', localVar: 'eval'
258261

259-
`vm.runInThisContext` does not have access to the local scope, so `localVar` is
260-
unchanged. `eval` does have access to the local scope, so `localVar` is changed.
262+
`vm.runInThisContext()` does not have access to the local scope, so `localVar`
263+
is unchanged. [`eval()`][] does have access to the local scope, so `localVar` is
264+
changed.
261265

262-
In this way `vm.runInThisContext` is much like an [indirect `eval` call][],
266+
In this way `vm.runInThisContext()` is much like an [indirect `eval()` call][],
263267
e.g. `(0,eval)('code')`. However, it also has the following additional options:
264268

265269
- `filename`: allows you to control the filename that shows up in any stack
@@ -275,6 +279,13 @@ e.g. `(0,eval)('code')`. However, it also has the following additional options:
275279
- `timeout`: a number of milliseconds to execute `code` before terminating
276280
execution. If execution is terminated, an [`Error`][] will be thrown.
277281

278-
[indirect `eval` call]: https://es5.github.io/#x10.4.2
282+
[indirect `eval()` call]: https://es5.github.io/#x10.4.2
279283
[global object]: https://es5.github.io/#x15.1
280284
[`Error`]: errors.html#errors_class_error
285+
[`script.runInContext()`]: #vm_script_runincontext_contextifiedsandbox_options
286+
[`script.runInThisContext()`]: #vm_script_runinthiscontext_options
287+
[`vm.createContext()`]: #vm_vm_createcontext_sandbox
288+
[`vm.runInContext()`]: #vm_vm_runincontext_code_contextifiedsandbox_options
289+
[`vm.runInNewContext()`]: #vm_vm_runinnewcontext_code_sandbox_options
290+
[`vm.runInThisContext()`]: #vm_vm_runinthiscontext_code_options
291+
[`eval()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

0 commit comments

Comments
 (0)