| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9824b0d commit ee01371
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,11 +41,13 @@ The options when creating a script are: | |||
| 41 | 41 | ||
| 42 | 42 | ### script.runInContext(contextifiedSandbox[, options]) | |
| 43 | 43 | ||
| 44 | - Similar to `vm.runInContext` but a method of a precompiled `Script` object. | ||
| 45 | - `script.runInContext` runs `script`'s compiled code in `contextifiedSandbox` | ||
| 46 | - and returns the result. Running code does not have access to local scope. | ||
| 44 | + Similar to [`vm.runInContext()`][] but a method of a precompiled `Script` | ||
| 45 | + object. `script.runInContext()` runs `script`'s compiled code in | ||
| 46 | + `contextifiedSandbox` and returns the result. Running code does not have access | ||
| 47 | + to local scope. | ||
| 47 | 48 | ||
| 48 | - `script.runInContext` takes the same options as `script.runInThisContext`. | ||
| 49 | + `script.runInContext()` takes the same options as | ||
| 50 | + [`script.runInThisContext()`][]. | ||
| 49 | 51 | ||
| 50 | 52 | Example: compile code that increments a global variable and sets one, then | |
| 51 | 53 | execute the code multiple times. These globals are contained in the sandbox. | |
@@ -72,18 +74,19 @@ console.log(util.inspect(sandbox)); | |||
| 72 | 74 | ``` | |
| 73 | 75 | ||
| 74 | 76 | Note that running untrusted code is a tricky business requiring great care. | |
| 75 | - `script.runInContext` is quite useful, but safely running untrusted code | ||
| 77 | + `script.runInContext()` is quite useful, but safely running untrusted code | ||
| 76 | 78 | requires a separate process. | |
| 77 | 79 | ||
| 78 | 80 | ### script.runInNewContext([sandbox][, options]) | |
| 79 | 81 | ||
| 80 | - Similar to `vm.runInNewContext` but a method of a precompiled `Script` object. | ||
| 81 | - `script.runInNewContext` contextifies `sandbox` if passed or creates a new | ||
| 82 | - contextified sandbox if it's omitted, and then runs `script`'s compiled code | ||
| 82 | + Similar to [`vm.runInNewContext()`][] but a method of a precompiled `Script` | ||
| 83 | + object. `script.runInNewContext()` contextifies `sandbox` if passed or creates a | ||
| 84 | + new contextified sandbox if it's omitted, and then runs `script`'s compiled code | ||
| 83 | 85 | with the sandbox as the global object and returns the result. Running code does | |
| 84 | 86 | not have access to local scope. | |
| 85 | 87 | ||
| 86 | - `script.runInNewContext` takes the same options as `script.runInThisContext`. | ||
| 88 | + `script.runInNewContext()` takes the same options as | ||
| 89 | + [`script.runInThisContext()`][]. | ||
| 87 | 90 | ||
| 88 | 91 | Example: compile code that sets a global variable, then execute the code | |
| 89 | 92 | multiple times in different contexts. These globals are set on and contained in | |
@@ -107,17 +110,17 @@ console.log(util.inspect(sandboxes)); | |||
| 107 | 110 | ``` | |
| 108 | 111 | ||
| 109 | 112 | Note that running untrusted code is a tricky business requiring great care. | |
| 110 | - `script.runInNewContext` is quite useful, but safely running untrusted code | ||
| 113 | + `script.runInNewContext()` is quite useful, but safely running untrusted code | ||
| 111 | 114 | requires a separate process. | |
| 112 | 115 | ||
| 113 | 116 | ### script.runInThisContext([options]) | |
| 114 | 117 | ||
| 115 | - Similar to `vm.runInThisContext` but a method of a precompiled `Script` object. | ||
| 116 | - `script.runInThisContext` runs `script`'s compiled code and returns the result. | ||
| 117 | - Running code does not have access to local scope, but does have access to the | ||
| 118 | - current `global` object. | ||
| 118 | + Similar to [`vm.runInThisContext()`]() but a method of a precompiled `Script` | ||
| 119 | + object. `script.runInThisContext()` runs `script`'s compiled code and returns | ||
| 120 | + the result. Running code does not have access to local scope, but does have | ||
| 121 | + access to the current `global` object. | ||
| 119 | 122 | ||
| 120 | - Example of using `script.runInThisContext` to compile code once and run it | ||
| 123 | + Example of using `script.runInThisContext()` to compile code once and run it | ||
| 121 | 124 | multiple times: | |
| 122 | 125 | ||
| 123 | 126 | ```js | |
@@ -154,11 +157,11 @@ The options for running a script are: | |||
| 154 | 157 | ## vm.createContext([sandbox]) | |
| 155 | 158 | ||
| 156 | 159 | If given a `sandbox` object, will "contextify" that sandbox so that it can be | |
| 157 | - used in calls to `vm.runInContext` or `script.runInContext`. Inside scripts run | ||
| 158 | - as such, `sandbox` will be the global object, retaining all its existing | ||
| 159 | - properties but also having the built-in objects and functions any standard | ||
| 160 | - [global object][] has. Outside of scripts run by the vm module, `sandbox` will | ||
| 161 | - be unchanged. | ||
| 160 | + used in calls to [`vm.runInContext()`][] or [`script.runInContext()`][]. Inside | ||
| 161 | + scripts run as such, `sandbox` will be the global object, retaining all its | ||
| 162 | + existing properties but also having the built-in objects and functions any | ||
| 163 | + standard [global object][] has. Outside of scripts run by the vm module, | ||
| 164 | + `sandbox` will be unchanged. | ||
| 162 | 165 | ||
| 163 | 166 | If not given a sandbox object, returns a new, empty contextified sandbox object | |
| 164 | 167 | you can use. | |
@@ -171,16 +174,16 @@ tags together inside that sandbox. | |||
| 171 | 174 | ## vm.isContext(sandbox) | |
| 172 | 175 | ||
| 173 | 176 | Returns whether or not a sandbox object has been contextified by calling | |
| 174 | - `vm.createContext` on it. | ||
| 177 | + [`vm.createContext()`][] on it. | ||
| 175 | 178 | ||
| 176 | 179 | ## vm.runInContext(code, contextifiedSandbox[, options]) | |
| 177 | 180 | ||
| 178 | - `vm.runInContext` compiles `code`, then runs it in `contextifiedSandbox` and | ||
| 181 | + `vm.runInContext()` compiles `code`, then runs it in `contextifiedSandbox` and | ||
| 179 | 182 | returns the result. Running code does not have access to local scope. The | |
| 180 | 183 | `contextifiedSandbox` object must have been previously contextified via | |
| 181 | - `vm.createContext`; it will be used as the global object for `code`. | ||
| 184 | + [`vm.createContext()`][]; it will be used as the global object for `code`. | ||
| 182 | 185 | ||
| 183 | - `vm.runInContext` takes the same options as `vm.runInThisContext`. | ||
| 186 | + `vm.runInContext()` takes the same options as [`vm.runInThisContext()`][]. | ||
| 184 | 187 | ||
| 185 | 188 | Example: compile and execute different scripts in a single existing context. | |
| 186 | 189 | ||
@@ -200,13 +203,13 @@ console.log(util.inspect(sandbox)); | |||
| 200 | 203 | ``` | |
| 201 | 204 | ||
| 202 | 205 | Note that running untrusted code is a tricky business requiring great care. | |
| 203 | - `vm.runInContext` is quite useful, but safely running untrusted code requires a | ||
| 204 | - separate process. | ||
| 206 | + `vm.runInContext()` is quite useful, but safely running untrusted code requires | ||
| 207 | + a separate process. | ||
| 205 | 208 | ||
| 206 | 209 | ## vm.runInDebugContext(code) | |
| 207 | 210 | ||
| 208 | - `vm.runInDebugContext` compiles and executes `code` inside the V8 debug context. | ||
| 209 | - The primary use case is to get access to the V8 debug object: | ||
| 211 | + `vm.runInDebugContext()` compiles and executes `code` inside the V8 debug | ||
| 212 | + context. The primary use case is to get access to the V8 debug object: | ||
| 210 | 213 | ||
| 211 | 214 | ```js | |
| 212 | 215 | const Debug = vm.runInDebugContext('Debug'); | |
@@ -220,11 +223,11 @@ The debug object can also be exposed with the `--expose_debug_as=` switch. | |||
| 220 | 223 | ||
| 221 | 224 | ## vm.runInNewContext(code[, sandbox][, options]) | |
| 222 | 225 | ||
| 223 | - `vm.runInNewContext` compiles `code`, contextifies `sandbox` if passed or | ||
| 226 | + `vm.runInNewContext()` compiles `code`, contextifies `sandbox` if passed or | ||
| 224 | 227 | creates a new contextified sandbox if it's omitted, and then runs the code with | |
| 225 | 228 | the sandbox as the global object and returns the result. | |
| 226 | 229 | ||
| 227 | - `vm.runInNewContext` takes the same options as `vm.runInThisContext`. | ||
| 230 | + `vm.runInNewContext()` takes the same options as [`vm.runInThisContext()`][]. | ||
| 228 | 231 | ||
| 229 | 232 | Example: compile and execute code that increments a global variable and sets a | |
| 230 | 233 | new one. These globals are contained in the sandbox. | |
@@ -245,7 +248,7 @@ console.log(util.inspect(sandbox)); | |||
| 245 | 248 | ``` | |
| 246 | 249 | ||
| 247 | 250 | Note that running untrusted code is a tricky business requiring great care. | |
| 248 | - `vm.runInNewContext` is quite useful, but safely running untrusted code requires | ||
| 251 | + `vm.runInNewContext()` is quite useful, but safely running untrusted code requires | ||
| 249 | 252 | a separate process. | |
| 250 | 253 | ||
| 251 | 254 | ## vm.runInThisContext(code[, options]) | |
@@ -254,7 +257,7 @@ a separate process. | |||
| 254 | 257 | code does not have access to local scope, but does have access to the current | |
| 255 | 258 | `global` object. | |
| 256 | 259 | ||
| 257 | - Example of using `vm.runInThisContext` and `eval` to run the same code: | ||
| 260 | + Example of using `vm.runInThisContext()` and [`eval()`][] to run the same code: | ||
| 258 | 261 | ||
| 259 | 262 | ```js | |
| 260 | 263 | const vm = require('vm'); | |
@@ -272,10 +275,11 @@ console.log('localVar: ', localVar); | |||
| 272 | 275 | // evalResult: 'eval', localVar: 'eval' | |
| 273 | 276 | ``` | |
| 274 | 277 | ||
| 275 | - `vm.runInThisContext` does not have access to the local scope, so `localVar` is | ||
| 276 | - unchanged. `eval` does have access to the local scope, so `localVar` is changed. | ||
| 278 | + `vm.runInThisContext()` does not have access to the local scope, so `localVar` | ||
| 279 | + is unchanged. [`eval()`][] does have access to the local scope, so `localVar` is | ||
| 280 | + changed. | ||
| 277 | 281 | ||
| 278 | - In this way `vm.runInThisContext` is much like an [indirect `eval` call][], | ||
| 282 | + In this way `vm.runInThisContext()` is much like an [indirect `eval()` call][], | ||
| 279 | 283 | e.g. `(0,eval)('code')`. However, it also has the following additional options: | |
| 280 | 284 | ||
| 281 | 285 | - `filename`: allows you to control the filename that shows up in any stack | |
@@ -291,6 +295,13 @@ e.g. `(0,eval)('code')`. However, it also has the following additional options: | |||
| 291 | 295 | - `timeout`: a number of milliseconds to execute `code` before terminating | |
| 292 | 296 | execution. If execution is terminated, an [`Error`][] will be thrown. | |
| 293 | 297 | ||
| 294 | - [indirect `eval` call]: https://es5.github.io/#x10.4.2 | ||
| 298 | + [indirect `eval()` call]: https://es5.github.io/#x10.4.2 | ||
| 295 | 299 | [global object]: https://es5.github.io/#x15.1 | |
| 296 | 300 | [`Error`]: errors.html#errors_class_error | |
| 301 | + [`script.runInContext()`]: #vm_script_runincontext_contextifiedsandbox_options | ||
| 302 | + [`script.runInThisContext()`]: #vm_script_runinthiscontext_options | ||
| 303 | + [`vm.createContext()`]: #vm_vm_createcontext_sandbox | ||
| 304 | + [`vm.runInContext()`]: #vm_vm_runincontext_code_contextifiedsandbox_options | ||
| 305 | + [`vm.runInNewContext()`]: #vm_vm_runinnewcontext_code_sandbox_options | ||
| 306 | + [`vm.runInThisContext()`]: #vm_vm_runinthiscontext_code_options | ||
| 307 | + [`eval()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments