Skip to content

Commit

Permalink
vm, core, module: re-do vm to fix known issues
Browse files Browse the repository at this point in the history
As documented in #3042 and in [1], the existing vm implementation has
many problems. All of these are solved by @brianmcd's [contextify][2]
package. This commit uses contextify as a conceptual base and its code
core to overhaul the vm module and fix its many edge cases and caveats.

Functionally, this fixes #3042. In particular:

- A context is now indistinguishable from the object it is based on
  (the "sandbox"). A context is simply a sandbox that has been marked
  by the vm module, via `vm.createContext`, with special internal
  information that allows scripts to be run inside of it.
- Consequently, items added to the context from anywhere are
  immediately visible to all code that can access that context, both
  inside and outside the virtual machine.

This commit also smooths over the API very slightly:

- Parameter defaults are now uniformly triggered via `undefined`, per
  ES6 semantics and previous discussion at [3].
- Several undocumented and problematic features have been removed, e.g.
  the conflation of `vm.Script` with `vm` itself, and the fact that
  `Script` instances also had all static `vm` methods. The API is now
  exactly as documented (although arguably the existence of the
  `vm.Script` export is not yet documented, just the `Script` class
  itself).

In terms of implementation, this replaces node_script.cc with
node_contextify.cc, which is derived originally from [4] (see [5]) but
has since undergone extensive modifications and iterations to expose
the most useful C++ API and use the coding conventions and utilities of
Node core.

The bindings exposed by `process.binding('contextify')`
(node_contextify.cc) replace those formerly exposed by
`process.binding('evals')` (node_script.cc). They are:

- ContextifyScript(code, [filename]), with methods:
  - runInThisContext()
  - runInContext(sandbox, [timeout])
- makeContext(sandbox)

From this, the vm.js file builds the entire documented vm module API.

node.js and module.js were modified to use this new native binding, or
the vm module itself where possible. This introduces an extra line or
two into the stack traces of module compilation (and thus into most
stack traces), explaining the changed tests.

The tests were also updated slightly, with all vm-related simple tests
consolidated as test/simple/test-vm-* (some of them were formerly
test/simple/test-script-*). At the same time they switched from
`common.debug` to `console.error` and were updated to use
`assert.throws` instead of rolling their own error-testing methods.

New tests were also added, of course, demonstrating the new
capabilities and fixes.

[1]: http://nodejs.org/docs/v0.10.16/api/vm.html#vm_caveats
[2]: https://github.com/brianmcd/contextify
[3]: nodejs/node-v0.x-archive#5323 (comment)
[4]: https://github.com/kkoopa/contextify/blob/bf123f3ef960f0943d1e30bda02e3163a004e964/src/contextify.cc
[5]: https://gist.github.com/domenic/6068120
  • Loading branch information
domenic authored and isaacs committed Aug 21, 2013
1 parent 3602d4c commit 7afdba6
Show file tree
Hide file tree
Showing 26 changed files with 799 additions and 588 deletions.
11 changes: 5 additions & 6 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@

var NativeModule = require('native_module');
var util = NativeModule.require('util');
var Script = process.binding('evals').NodeScript;
var runInThisContext = Script.runInThisContext;
var runInNewContext = Script.runInNewContext;
var runInThisContext = require('vm').runInThisContext;
var runInNewContext = require('vm').runInNewContext;
var assert = require('assert').ok;


Expand Down Expand Up @@ -413,7 +412,7 @@ Module.prototype._compile = function(content, filename) {
sandbox.global = sandbox;
sandbox.root = root;

return runInNewContext(content, sandbox, filename, 0, true);
return runInNewContext(content, sandbox, filename);
}

debug('load root module');
Expand All @@ -424,13 +423,13 @@ Module.prototype._compile = function(content, filename) {
global.__dirname = dirname;
global.module = self;

return runInThisContext(content, filename, 0, true);
return runInThisContext(content, filename);
}

// create wrapper function
var wrapper = Module.wrap(content);

var compiledWrapper = runInThisContext(wrapper, filename, 0, true);
var compiledWrapper = runInThisContext(wrapper, filename);
if (global.v8debug) {
if (!resolvedArgv) {
// we enter the repl if we're not given a filename argument.
Expand Down
68 changes: 40 additions & 28 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,49 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var binding = process.binding('evals');

module.exports = Script;
Script.Script = Script;
var binding = process.binding('contextify');
var Script = binding.ContextifyScript;
var util = require('util');

function Script(code, ctx, filename) {
if (!(this instanceof Script)) {
return new Script(code, ctx, filename);
// The binding provides a few useful primitives:
// - ContextifyScript(code, [filename]), with methods:
// - runInThisContext()
// - runInContext(sandbox, [timeout])
// - makeContext(sandbox)
// From this we build the entire documented API.

Script.prototype.runInNewContext = function(initSandbox, timeout) {
var context = exports.createContext(initSandbox);
return this.runInContext(context, timeout);
};

exports.Script = Script;

exports.createScript = function(code, filename) {
return new Script(code, filename);
};

exports.createContext = function(initSandbox) {
if (util.isUndefined(initSandbox)) {
initSandbox = {};
}

var ns = new binding.NodeScript(code, ctx, filename);

// bind all methods to this Script object
Object.keys(binding.NodeScript.prototype).forEach(function(f) {
if (util.isFunction(binding.NodeScript.prototype[f])) {
this[f] = function() {
if (!(this instanceof Script)) {
throw new TypeError('invalid call to ' + f);
}
return ns[f].apply(ns, arguments);
};
}
}, this);
}

Script.createScript = function(code, ctx, name) {
return new Script(code, ctx, name);
binding.makeContext(initSandbox);

return initSandbox;
};

exports.runInContext = function(code, sandbox, filename, timeout) {
var script = exports.createScript(code, filename);
return script.runInContext(sandbox, timeout);
};

Script.createContext = binding.NodeScript.createContext;
Script.runInContext = binding.NodeScript.runInContext;
Script.runInThisContext = binding.NodeScript.runInThisContext;
Script.runInNewContext = binding.NodeScript.runInNewContext;
exports.runInNewContext = function(code, sandbox, filename, timeout) {
var script = exports.createScript(code, filename);
return script.runInNewContext(sandbox, timeout);
};

exports.runInThisContext = function(code, filename, timeout) {
var script = exports.createScript(code, filename);
return script.runInThisContext(timeout);
};
4 changes: 2 additions & 2 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@
'src/node.cc',
'src/node_buffer.cc',
'src/node_constants.cc',
'src/node_contextify.cc',
'src/node_extensions.cc',
'src/node_file.cc',
'src/node_http_parser.cc',
'src/node_javascript.cc',
'src/node_main.cc',
'src/node_os.cc',
'src/node_script.cc',
'src/node_stat_watcher.cc',
'src/node_watchdog.cc',
'src/node_zlib.cc',
Expand All @@ -120,13 +120,13 @@
'src/node.h',
'src/node_buffer.h',
'src/node_constants.h',
'src/node_contextify.h',
'src/node_extensions.h',
'src/node_file.h',
'src/node_http_parser.h',
'src/node_javascript.h',
'src/node_os.h',
'src/node_root_certs.h',
'src/node_script.h',
'src/node_version.h',
'src/node_watchdog.h',
'src/node_wrap.h',
Expand Down
1 change: 0 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "node_file.h"
#include "node_http_parser.h"
#include "node_javascript.h"
#include "node_script.h"
#include "node_version.h"

#if defined HAVE_PERFCTR
Expand Down
11 changes: 7 additions & 4 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
'global.require = require;\n' +
'return require("vm").runInThisContext(' +
JSON.stringify(body) + ', ' +
JSON.stringify(name) + ', 0, true);\n';
JSON.stringify(name) + ');\n';
}
var result = module._compile(script, name + '-wrapper');
if (process._print_eval) console.log(result);
Expand Down Expand Up @@ -717,8 +717,11 @@
// core modules found in lib/*.js. All core modules are compiled into the
// node binary, so they can be loaded faster.

var Script = process.binding('evals').NodeScript;
var runInThisContext = Script.runInThisContext;
var ContextifyScript = process.binding('contextify').ContextifyScript;
function runInThisContext(code, filename) {
var script = new ContextifyScript(code, filename);
return script.runInThisContext();
}

function NativeModule(id) {
this.filename = id + '.js';
Expand Down Expand Up @@ -779,7 +782,7 @@
var source = NativeModule.getSource(this.id);
source = NativeModule.wrap(source);

var fn = runInThisContext(source, this.filename, 0, true);
var fn = runInThisContext(source, this.filename);
fn(this.exports, NativeModule.require, this, this.filename);

this.loaded = true;
Expand Down
Loading

0 comments on commit 7afdba6

Please sign in to comment.