@ -1,6 +1,6 @@
# Executing JavaScript
Stability: 2 - Unstable. See Caveats, below.
Stability: 3 - Stable
<!-- name=vm -->
@ -8,185 +8,171 @@ You can access this module with:
var vm = require('vm');
JavaScript code can be compiled and run immediately or compiled, saved, and run later.
JavaScript code can be compiled and run immediately or compiled, saved, and run
later.
## Caveats
## vm.runInThisContext(code, [options])
The `vm` module has many known issues and edge cases. If you run into
issues or unexpected behavior, please consult [the open issues on
GitHub](https://github.com/joyent/node/issues?labels=vm& state=open).
Some of the biggest problems are described below.
`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.
### Sandboxes
Example of using `vm.runInThisContext` and `eval` to run the same code:
The `sandbox` argument to `vm.runInNewContext` and `vm.createContext` ,
along with the `initSandbox` argument to `vm.createContext` , do not
behave as one might normally expect and their behavior varies
between different versions of Node.
var localVar = 'initial value';
The key issue to be aware of is that V8 provides no way to directly
control the global object used within a context. As a result, while
properties of your `sandbox` object will be available in the context,
any properties from the `prototype` s of the `sandbox` may not be
available. Furthermore, the `this` expression within the global scope
of the context evaluates to the empty object (`{}`) instead of to
your sandbox.
var vmResult = vm.runInThisContext('localVar = "vm";');
console.log('vmResult: ', vmResult);
console.log('localVar: ', localVar);
Your sandbox's properties are also not shared directly with the script.
Instead, the properties of the sandbox are copied into the context at
the beginning of execution, and then after execution, the properties
are copied back out in an attempt to propagate any changes.
var evalResult = eval('localVar = "eval";');
console.log('evalResult: ', evalResult);
console.log('localVar: ', localVar);
### Globals
// vmResult: 'vm', localVar: 'initial value'
// evalResult: 'eval', localVar: 'eval'
Properties of the global object, like `Array` and `String` , have
different values inside of a context. This means that common
expressions like `[] instanceof Array` or
`Object.getPrototypeOf([]) === Array.prototype` may not produce
expected results when used inside of scripts evaluated via the `vm` module.
`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.
Some of these problems have known workarounds listed in the issues for
`vm` on GitHub. for example, `Array.isArray` works around
the example problem with `Array` .
In this way `vm.runInThisContext` is much like an [indirect `eval` call][1],
e.g. `(0,eval)('code')` . However, it also has the following additional options:
## vm.runInThisContext(code, [filename])
- `filename` : allows you to control the filename that shows up in any stack
traces produced.
- `displayErrors` : whether or not to print any errors to stderr, with the
line of code that caused them highlighted, before throwing an exception.
Will capture both syntax errors from compiling `code` and runtime errors
thrown by executing the compiled code. Defaults to `true` .
- `timeout` : a number of milliseconds to execute `code` before terminating
execution. If execution is terminated, an `Error` will be thrown.
`vm.runInThisContext()` compiles `code` , runs it and returns the result. Running
code does not have access to local scope. `filename` is optional, it's used only
in stack traces.
[1]: http://es5.github.io/#x10.4.2
Example of using `vm.runInThisContext` and `eval` to run the same code:
var localVar = 123,
usingscript, evaled,
vm = require('vm');
## vm.createContext([sandbox])
usingscript = vm.runInThisContext('localVar = 1;',
'myfile.vm');
console.log('localVar: ' + localVar + ', usingscript: ' +
usingscript);
evaled = eval('localVar = 1;');
console.log('localVar: ' + localVar + ', evaled: ' +
evaled);
If given a `sandbox` object, will "contextify" that sandbox so that it can be
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][2] has. Outside of scripts run by the vm module, `sandbox` will
be unchanged.
// localVar: 123, usingscript: 1
// localVar: 1, evaled: 1
If not given a sandbox object, returns a new, empty contextified sandbox object
you can use.
`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.
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.
In case of syntax error in `code` , `vm.runInThisContext` emits the syntax error to stderr
and throws an exception.
[2]: http://es5.github.io/#x15.1
## vm.runInNewContext(code, [sandbox], [filename], [timeout] )
## vm.isContext(sandbox )
`vm.runInNewContext` compiles `code` , then runs it in `sandbox` and returns the
result. Running code does not have access to local scope. The object `sandbox`
will be used as the global object for `code` .
`sandbox` and `filename` are optional, `filename` is only used in stack traces.
`timeout` specifies an optional number of milliseconds to execute `code` before
terminating execution. If execution is terminated, an `Error` will be thrown.
Returns whether or not a sandbox object has been contextified by calling
`vm.createContext` on it.
Example: compile and execute code that increments a global variable and sets a new one.
These globals are contained in the sandbox.
var util = require('util'),
vm = require('vm'),
sandbox = {
animal: 'cat',
count: 2
};
## vm.runInContext(code, contextifiedSandbox, [options])
vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm');
console.log(util.inspect(sandbox));
`vm.runInContext` compiles `code` , then runs it in `contextifiedSandbox` and
returns the result. Running code does not have access to local scope. The
`contextifiedSandbox` object must have been previously contextified via
`vm.createContext` ; it will be used as the global object for `code` .
// { animal: 'cat', count: 3, name: 'kitty' }
`vm.runInContext` takes the same options as `vm.runInThisContext` .
Note that running untrusted code is a tricky business requiring great care. To prevent accidental
global variable leakage, `vm.runInNewContext` is quite useful, but safely running untrusted code
requires a separate process.
Example: compile and execute differnt scripts in a single existing context.
In case of syntax error in `code` , `vm.runInNewContext` emits the syntax error to stderr
and throws an exception.
var util = require('util');
var vm = require('vm');
## vm.runInContext(code, context, [filename], [timeout])
var sandbox = { globalVar: 1 };
vm.createContext(sandbox);
`vm.runInContext` compiles `code` , then runs it in `context` and returns the
result. A (V8) context comprises a global object, together with a set of
built-in objects and functions. Running code does not have access to local scope
and the global object held within `context` will be used as the global object
for `code` .
`filename` is optional, it's used only in stack traces.
`timeout` specifies an optional number of milliseconds to execute `code` before
terminating execution. If execution is terminated, an `Error` will be thrown.
for (var i = 0; i < 10 ; + + i ) {
vm.runInContext('globalVar *= 2;', sandbox);
}
console.log(util.inspect(sandbox));
Example: compile and execute code in a existing context.
// { globalVar: 1024 }
var util = require('util'),
vm = require('vm'),
initSandbox = {
animal: 'cat',
count: 2
},
context = vm.createContext(initSandbox);
Note that running untrusted code is a tricky business requiring great care.
`vm.runInContext` is quite useful, but safely running untrusted code requires a
separate process.
vm.runInContext('count += 1; name = "CATT"', context, 'myfile.vm');
console.log(util.inspect(context));
// { animal: 'cat', count: 3, name: 'CATT' }
## vm.runInNewContext(code, [sandbox], [options])
Note that `createContext` will perform a shallow clone of the supplied sandbox object in order to
initialize the global object of the freshly constructed context.
`vm.runInNewContext` compiles `code` , contextifies `sandbox` if passed or
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.
Note that running untrusted code is a tricky business requiring great care. To prevent accidental
global variable leakage, `vm.runInContext` is quite useful, but safely running untrusted code
requires a separate process.
`vm.runInNewContext` takes the same options as `vm.runInThisContext` .
In case of syntax error in `code` , `vm.runInContext` emits the syntax error to stderr
and throws an exception .
Example: compile and execute code that increments a global variable and sets a
new one. These globals are contained in the sandbox .
## vm.createContext([initSandbox])
var util = require('util');
var vm = require('vm'),
`vm.createContext` creates a new context which is suitable for use as the 2nd argument of a subsequent
call to `vm.runInContext` . A (V8) context comprises a global object together with a set of
build-in objects and functions. The optional argument `initSandbox` will be shallow-copied
to seed the initial contents of the global object used by the context.
var sandbox = {
animal: 'cat',
count: 2
};
## vm.createScript(code, [filename])
vm.runInNewContext('count += 1; name = "kitty"', sandbox);
console.log(util.inspect(sandbox));
`createScript` compiles `code` but does not run it. Instead, it returns a
`vm.Script` object representing 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. `filename` is
optional, it's only used in stack traces.
// { animal: 'cat', count: 3, name: 'kitty' }
In case of syntax error in `code` , `createScript` prints the syntax error to stderr
and throws an exception.
Note that running untrusted code is a tricky business requiring great care.
`vm.runInNewContext` is quite useful, but safely running untrusted code requires
a separate process.
## Class: Script
A class for running scripts. Returned by vm.createScript.
A class for holding precompiled scripts, and running them in specific sandboxes.
### new vm.Script(code, options)
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.
- `displayErrors` : whether or not to print any errors to stderr, with the
line of code that caused them highlighted, before throwing an exception.
Applies only to syntax errors compiling the code; errors while running the
code are controlled by the options to the script's methods.
### script.runInThisContext([timeout])
### script.runInThisContext([options ])
Similar to `vm.runInThisContext` but a method of a precompiled `Script` object.
`script.runInThisContext` runs the code of `script` and returns the result.
Running code does not have access to local scope, but does have access to the `global` object
(v8: in actual context).
`timeout` specifies an optional number of milliseconds to execute `code` before
terminating execution. If execution is terminated, an `Error` will be thrown.
`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.
Example of using `script.runInThisContext` to compile code once and run it multiple times:
Example of using `script.runInThisContext` to compile code once and run it
multiple times:
var vm = require('vm');
globalVar = 0;
global.global Var = 0;
var script = vm.createScript('globalVar += 1', 'myfile.vm');
var script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' } );
for (var i = 0; i < 1000 ; i + = 1 ) {
for (var i = 0; i < 1000 ; + + i ) {
script.runInThisContext();
}
@ -194,35 +180,79 @@ Example of using `script.runInThisContext` to compile code once and run it multi
// 1000
The options for running a script are:
### script.runInNewContext([sandbox], [timeout])
- `displayErrors` : whether or not to print any runtime errors to stderr, with
the line of code that caused them highlighted, before throwing an exception.
Applies only to runtime errors executing the code; it is impossible to create
a `Script` instance with syntax errors, as the constructor will throw.
- `timeout` : a number of milliseconds to execute the script before terminating
execution. If execution is terminated, an `Error` will be thrown.
Similar to `vm.runInNewContext` a method of a precompiled `Script` object.
`script.runInNewContext` runs the code of `script` with `sandbox` as the global object and returns the result.
Running code does not have access to local scope. `sandbox` is optional.
`timeout` specifies an optional number of milliseconds to execute `code` before
terminating execution. If execution is terminated, an `Error` will be thrown.
Example: compile code that increments a global variable and sets one, then execute this code multiple times.
These globals are contained in the sandbox.
### script.runInContext(contextifiedSandbox, [options])
var util = require('util'),
vm = require('vm'),
sandbox = {
animal: 'cat',
count: 2
};
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.
var script = vm.createScript('count += 1; name = "kitty"', 'myfile.vm');
`script.runInContext` takes the same options as `script.runInThisContext` .
for (var i = 0; i < 10 ; i + = 1 ) {
script.runInNewContext(sandbox);
Example: compile code that increments a global variable and sets one, then
execute the code multiple times. These globals are contained in the sandbox.
var util = require('util');
var vm = require('vm');
var sandbox = {
animal: 'cat',
count: 2
};
var script = new vm.Script('count += 1; name = "kitty"');
for (var i = 0; i < 10 ; + + i ) {
script.runInContext(sandbox);
}
console.log(util.inspect(sandbox));
// { animal: 'cat', count: 12, name: 'kitty' }
Note that running untrusted code is a tricky business requiring great care. To prevent accidental
global variable leakage, `script.runInNewContext` is quite useful, but safely running untrusted code
Note that running untrusted code is a tricky business requiring great care.
`script.runInContext` is quite useful, but safely running untrusted code
requires a separate process.
### script.runInNewContext([sandbox], [options])
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
with the sandbox as the global object and returns the result. Running code does
not have access to local scope.
`script.runInNewContext` takes the same options as `script.runInThisContext` .
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.
var util = require('util');
var vm = require('vm');
var sandboxes = [{}, {}, {}];
var script = new vm.Script('globalVar = "set"');
sandboxes.forEach(function (sandbox) {
script.runInNewContext(sandbox);
});
console.log(util.inspect(sandboxes));
// [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
Note that running untrusted code is a tricky business requiring great care.
`script.runInNewContext` is quite useful, but safely running untrusted code
requires a separate process.