2012-02-27 20:09:35 +01:00
|
|
|
# Executing JavaScript
|
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
<!--name=vm-->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2010-11-15 09:51:01 +01:00
|
|
|
You can access this module with:
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const vm = require('vm');
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
JavaScript code can be compiled and run immediately or compiled, saved, and run
|
|
|
|
later.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
## Class: Script
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
A class for holding precompiled scripts, and running them in specific sandboxes.
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
### new vm.Script(code, options)
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
Creating a new `Script` compiles `code` but does not run it. Instead, the
|
|
|
|
created `vm.Script` object represents this compiled code. This script can be run
|
|
|
|
later many times using methods below. The returned script is not bound to any
|
|
|
|
global object. It is bound before each run, just for that run.
|
|
|
|
|
|
|
|
The options when creating a script are:
|
|
|
|
|
|
|
|
- `filename`: allows you to control the filename that shows up in any stack
|
|
|
|
traces produced from this script.
|
2015-09-15 00:26:59 +02:00
|
|
|
- `lineOffset`: allows you to add an offset to the line number that is
|
|
|
|
displayed in stack traces
|
|
|
|
- `columnOffset`: allows you to add an offset to the column number that is
|
|
|
|
displayed in stack traces
|
2016-01-26 04:44:17 +01:00
|
|
|
- `displayErrors`: if `true`, on error, attach the line of code that caused
|
|
|
|
the error to the stack trace. Applies only to syntax errors compiling the
|
|
|
|
code; errors while running the code are controlled by the options to the
|
|
|
|
script's methods.
|
2015-09-15 00:26:59 +02:00
|
|
|
- `timeout`: a number of milliseconds to execute `code` before terminating
|
2015-11-28 00:30:32 +01:00
|
|
|
execution. If execution is terminated, an [`Error`][] will be thrown.
|
2016-01-21 01:34:19 +01:00
|
|
|
- `cachedData`: an optional `Buffer` with V8's code cache data for the supplied
|
|
|
|
source. When supplied `cachedDataRejected` value will be set to either
|
|
|
|
`true` or `false` depending on acceptance of the data by V8.
|
|
|
|
- `produceCachedData`: if `true` and no `cachedData` is present - a `Buffer`
|
|
|
|
with V8's code cache data will be produced and stored in `cachedData` property
|
|
|
|
of the returned `vm.Script` instance.
|
2015-11-04 18:30:11 +01:00
|
|
|
|
|
|
|
### script.runInContext(contextifiedSandbox[, options])
|
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
Similar to [`vm.runInContext()`][] but a method of a precompiled `Script`
|
|
|
|
object. `script.runInContext()` runs `script`'s compiled code in
|
|
|
|
`contextifiedSandbox` and returns the result. Running code does not have access
|
|
|
|
to local scope.
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`script.runInContext()` takes the same options as
|
|
|
|
[`script.runInThisContext()`][].
|
2015-11-04 18:30:11 +01:00
|
|
|
|
|
|
|
Example: compile code that increments a global variable and sets one, then
|
|
|
|
execute the code multiple times. These globals are contained in the sandbox.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const vm = require('vm');
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
var sandbox = {
|
|
|
|
animal: 'cat',
|
|
|
|
count: 2
|
|
|
|
};
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
var context = new vm.createContext(sandbox);
|
|
|
|
var script = new vm.Script('count += 1; name = "kitty"');
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
for (var i = 0; i < 10; ++i) {
|
|
|
|
script.runInContext(context);
|
|
|
|
}
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log(util.inspect(sandbox));
|
2012-04-25 09:11:06 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// { animal: 'cat', count: 12, name: 'kitty' }
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
Note that running untrusted code is a tricky business requiring great care.
|
2016-01-31 15:29:27 +01:00
|
|
|
`script.runInContext()` is quite useful, but safely running untrusted code
|
2015-11-04 18:30:11 +01:00
|
|
|
requires a separate process.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
### script.runInNewContext([sandbox][, options])
|
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
Similar to [`vm.runInNewContext()`][] but a method of a precompiled `Script`
|
|
|
|
object. `script.runInNewContext()` contextifies `sandbox` if passed or creates a
|
|
|
|
new contextified sandbox if it's omitted, and then runs `script`'s compiled code
|
2015-11-04 18:30:11 +01:00
|
|
|
with the sandbox as the global object and returns the result. Running code does
|
|
|
|
not have access to local scope.
|
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`script.runInNewContext()` takes the same options as
|
|
|
|
[`script.runInThisContext()`][].
|
2015-11-04 18:30:11 +01:00
|
|
|
|
|
|
|
Example: compile code that sets a global variable, then execute the code
|
|
|
|
multiple times in different contexts. These globals are set on and contained in
|
|
|
|
the sandboxes.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const vm = require('vm');
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const sandboxes = [{}, {}, {}];
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const script = new vm.Script('globalVar = "set"');
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
sandboxes.forEach((sandbox) => {
|
|
|
|
script.runInNewContext(sandbox);
|
|
|
|
});
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log(util.inspect(sandboxes));
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
|
|
|
|
```
|
2015-11-04 18:30:11 +01:00
|
|
|
|
|
|
|
Note that running untrusted code is a tricky business requiring great care.
|
2016-01-31 15:29:27 +01:00
|
|
|
`script.runInNewContext()` is quite useful, but safely running untrusted code
|
2015-11-04 18:30:11 +01:00
|
|
|
requires a separate process.
|
|
|
|
|
|
|
|
### script.runInThisContext([options])
|
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
Similar to [`vm.runInThisContext()`]() but a method of a precompiled `Script`
|
|
|
|
object. `script.runInThisContext()` runs `script`'s compiled code and returns
|
|
|
|
the result. Running code does not have access to local scope, but does have
|
|
|
|
access to the current `global` object.
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
Example of using `script.runInThisContext()` to compile code once and run it
|
2015-11-04 18:30:11 +01:00
|
|
|
multiple times:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const vm = require('vm');
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
global.globalVar = 0;
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
for (var i = 0; i < 1000; ++i) {
|
|
|
|
script.runInThisContext();
|
|
|
|
}
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log(globalVar);
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// 1000
|
|
|
|
```
|
2015-11-04 18:30:11 +01:00
|
|
|
|
|
|
|
The options for running a script are:
|
|
|
|
|
2015-09-15 00:26:59 +02:00
|
|
|
- `filename`: allows you to control the filename that shows up in any stack
|
|
|
|
traces produced.
|
|
|
|
- `lineOffset`: allows you to add an offset to the line number that is
|
|
|
|
displayed in stack traces
|
|
|
|
- `columnOffset`: allows you to add an offset to the column number that is
|
|
|
|
displayed in stack traces
|
2016-01-26 04:44:17 +01:00
|
|
|
- `displayErrors`: if `true`, on error, attach the line of code that caused
|
|
|
|
the error to the stack trace. Applies only to runtime errors executing the
|
|
|
|
code; it is impossible to create a `Script` instance with syntax errors, as
|
|
|
|
the constructor will throw.
|
2015-11-04 18:30:11 +01:00
|
|
|
- `timeout`: a number of milliseconds to execute the script before terminating
|
2015-11-28 00:30:32 +01:00
|
|
|
execution. If execution is terminated, an [`Error`][] will be thrown.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
## vm.createContext([sandbox])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
If given a `sandbox` object, will "contextify" that sandbox so that it can be
|
2016-01-31 15:29:27 +01:00
|
|
|
used in calls to [`vm.runInContext()`][] or [`script.runInContext()`][]. Inside
|
|
|
|
scripts run as such, `sandbox` will be the global object, retaining all its
|
|
|
|
existing properties but also having the built-in objects and functions any
|
|
|
|
standard [global object][] has. Outside of scripts run by the vm module,
|
|
|
|
`sandbox` will be unchanged.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
If not given a sandbox object, returns a new, empty contextified sandbox object
|
|
|
|
you can use.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
This function is useful for creating a sandbox that can be used to run multiple
|
|
|
|
scripts, e.g. if you were emulating a web browser it could be used to create a
|
|
|
|
single sandbox representing a window's global object, then run all `<script>`
|
|
|
|
tags together inside that sandbox.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
## vm.isContext(sandbox)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
Returns whether or not a sandbox object has been contextified by calling
|
2016-01-31 15:29:27 +01:00
|
|
|
[`vm.createContext()`][] on it.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-25 00:41:31 +02:00
|
|
|
## vm.runInContext(code, contextifiedSandbox[, options])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInContext()` compiles `code`, then runs it in `contextifiedSandbox` and
|
2013-08-25 01:46:31 +02:00
|
|
|
returns the result. Running code does not have access to local scope. The
|
|
|
|
`contextifiedSandbox` object must have been previously contextified via
|
2016-01-31 15:29:27 +01:00
|
|
|
[`vm.createContext()`][]; it will be used as the global object for `code`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInContext()` takes the same options as [`vm.runInThisContext()`][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-09-19 07:09:24 +02:00
|
|
|
Example: compile and execute different scripts in a single existing context.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const vm = require('vm');
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const sandbox = { globalVar: 1 };
|
|
|
|
vm.createContext(sandbox);
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
for (var i = 0; i < 10; ++i) {
|
|
|
|
vm.runInContext('globalVar *= 2;', sandbox);
|
|
|
|
}
|
|
|
|
console.log(util.inspect(sandbox));
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// { globalVar: 1024 }
|
|
|
|
```
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
Note that running untrusted code is a tricky business requiring great care.
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInContext()` is quite useful, but safely running untrusted code requires
|
|
|
|
a separate process.
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
## vm.runInDebugContext(code)
|
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInDebugContext()` compiles and executes `code` inside the V8 debug
|
|
|
|
context. The primary use case is to get access to the V8 debug object:
|
2015-11-04 18:30:11 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const Debug = vm.runInDebugContext('Debug');
|
|
|
|
Debug.scripts().forEach(function(script) { console.log(script.name); });
|
|
|
|
```
|
2015-11-04 18:30:11 +01:00
|
|
|
|
|
|
|
Note that the debug context and object are intrinsically tied to V8's debugger
|
|
|
|
implementation and may change (or even get removed) without prior warning.
|
|
|
|
|
|
|
|
The debug object can also be exposed with the `--expose_debug_as=` switch.
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
## vm.runInNewContext(code[, sandbox][, options])
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInNewContext()` compiles `code`, contextifies `sandbox` if passed or
|
2013-08-25 01:46:31 +02:00
|
|
|
creates a new contextified sandbox if it's omitted, and then runs the code with
|
|
|
|
the sandbox as the global object and returns the result.
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInNewContext()` takes the same options as [`vm.runInThisContext()`][].
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
Example: compile and execute code that increments a global variable and sets a
|
|
|
|
new one. These globals are contained in the sandbox.
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const vm = require('vm');
|
2011-06-02 21:45:30 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const sandbox = {
|
|
|
|
animal: 'cat',
|
|
|
|
count: 2
|
|
|
|
};
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
vm.runInNewContext('count += 1; name = "kitty"', sandbox);
|
|
|
|
console.log(util.inspect(sandbox));
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// { animal: 'cat', count: 3, name: 'kitty' }
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-08-25 01:46:31 +02:00
|
|
|
Note that running untrusted code is a tricky business requiring great care.
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInNewContext()` is quite useful, but safely running untrusted code requires
|
2013-08-25 01:46:31 +02:00
|
|
|
a separate process.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
## vm.runInThisContext(code[, options])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:30:11 +01:00
|
|
|
`vm.runInThisContext()` compiles `code`, runs it and returns the result. Running
|
|
|
|
code does not have access to local scope, but does have access to the current
|
|
|
|
`global` object.
|
2014-08-22 15:09:24 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
Example of using `vm.runInThisContext()` and [`eval()`][] to run the same code:
|
2014-08-22 15:09:24 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
const vm = require('vm');
|
|
|
|
var localVar = 'initial value';
|
2012-02-27 20:09:35 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const vmResult = vm.runInThisContext('localVar = "vm";');
|
|
|
|
console.log('vmResult: ', vmResult);
|
|
|
|
console.log('localVar: ', localVar);
|
2013-08-25 01:46:31 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const evalResult = eval('localVar = "eval";');
|
|
|
|
console.log('evalResult: ', evalResult);
|
|
|
|
console.log('localVar: ', localVar);
|
2013-08-25 01:46:31 +02:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// vmResult: 'vm', localVar: 'initial value'
|
|
|
|
// evalResult: 'eval', localVar: 'eval'
|
|
|
|
```
|
2013-08-25 01:46:31 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
`vm.runInThisContext()` does not have access to the local scope, so `localVar`
|
|
|
|
is unchanged. [`eval()`][] does have access to the local scope, so `localVar` is
|
|
|
|
changed.
|
2013-08-25 01:46:31 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
In this way `vm.runInThisContext()` is much like an [indirect `eval()` call][],
|
2015-11-04 18:30:11 +01:00
|
|
|
e.g. `(0,eval)('code')`. However, it also has the following additional options:
|
2013-08-25 01:46:31 +02:00
|
|
|
|
|
|
|
- `filename`: allows you to control the filename that shows up in any stack
|
2015-11-04 18:30:11 +01:00
|
|
|
traces produced.
|
2015-09-15 00:26:59 +02:00
|
|
|
- `lineOffset`: allows you to add an offset to the line number that is
|
|
|
|
displayed in stack traces
|
|
|
|
- `columnOffset`: allows you to add an offset to the column number that is
|
|
|
|
displayed in stack traces
|
2016-01-26 04:44:17 +01:00
|
|
|
- `displayErrors`: if `true`, on error, attach the line of code that caused
|
|
|
|
the error to the stack trace. Will capture both syntax errors from compiling
|
|
|
|
`code` and runtime errors thrown by executing the compiled code. Defaults to
|
|
|
|
`true`.
|
2015-11-04 18:30:11 +01:00
|
|
|
- `timeout`: a number of milliseconds to execute `code` before terminating
|
2015-11-28 00:30:32 +01:00
|
|
|
execution. If execution is terminated, an [`Error`][] will be thrown.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-01-31 15:29:27 +01:00
|
|
|
[indirect `eval()` call]: https://es5.github.io/#x10.4.2
|
2015-12-02 17:17:19 +01:00
|
|
|
[global object]: https://es5.github.io/#x15.1
|
2015-11-28 00:30:32 +01:00
|
|
|
[`Error`]: errors.html#errors_class_error
|
2016-01-31 15:29:27 +01:00
|
|
|
[`script.runInContext()`]: #vm_script_runincontext_contextifiedsandbox_options
|
|
|
|
[`script.runInThisContext()`]: #vm_script_runinthiscontext_options
|
|
|
|
[`vm.createContext()`]: #vm_vm_createcontext_sandbox
|
|
|
|
[`vm.runInContext()`]: #vm_vm_runincontext_code_contextifiedsandbox_options
|
|
|
|
[`vm.runInNewContext()`]: #vm_vm_runinnewcontext_code_sandbox_options
|
|
|
|
[`vm.runInThisContext()`]: #vm_vm_runinthiscontext_code_options
|
|
|
|
[`eval()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
|