Skip to content

Commit 7afdba6

Browse files
domenicisaacs
authored andcommitted
vm, core, module: re-do vm to fix known issues
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
1 parent 3602d4c commit 7afdba6

26 files changed

+799
-588
lines changed

lib/module.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121

2222
var NativeModule = require('native_module');
2323
var util = NativeModule.require('util');
24-
var Script = process.binding('evals').NodeScript;
25-
var runInThisContext = Script.runInThisContext;
26-
var runInNewContext = Script.runInNewContext;
24+
var runInThisContext = require('vm').runInThisContext;
25+
var runInNewContext = require('vm').runInNewContext;
2726
var assert = require('assert').ok;
2827

2928

@@ -413,7 +412,7 @@ Module.prototype._compile = function(content, filename) {
413412
sandbox.global = sandbox;
414413
sandbox.root = root;
415414

416-
return runInNewContext(content, sandbox, filename, 0, true);
415+
return runInNewContext(content, sandbox, filename);
417416
}
418417

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

427-
return runInThisContext(content, filename, 0, true);
426+
return runInThisContext(content, filename);
428427
}
429428

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

433-
var compiledWrapper = runInThisContext(wrapper, filename, 0, true);
432+
var compiledWrapper = runInThisContext(wrapper, filename);
434433
if (global.v8debug) {
435434
if (!resolvedArgv) {
436435
// we enter the repl if we're not given a filename argument.

lib/vm.js

+40-28
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,49 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
var binding = process.binding('evals');
23-
24-
module.exports = Script;
25-
Script.Script = Script;
22+
var binding = process.binding('contextify');
23+
var Script = binding.ContextifyScript;
2624
var util = require('util');
2725

28-
function Script(code, ctx, filename) {
29-
if (!(this instanceof Script)) {
30-
return new Script(code, ctx, filename);
26+
// The binding provides a few useful primitives:
27+
// - ContextifyScript(code, [filename]), with methods:
28+
// - runInThisContext()
29+
// - runInContext(sandbox, [timeout])
30+
// - makeContext(sandbox)
31+
// From this we build the entire documented API.
32+
33+
Script.prototype.runInNewContext = function(initSandbox, timeout) {
34+
var context = exports.createContext(initSandbox);
35+
return this.runInContext(context, timeout);
36+
};
37+
38+
exports.Script = Script;
39+
40+
exports.createScript = function(code, filename) {
41+
return new Script(code, filename);
42+
};
43+
44+
exports.createContext = function(initSandbox) {
45+
if (util.isUndefined(initSandbox)) {
46+
initSandbox = {};
3147
}
3248

33-
var ns = new binding.NodeScript(code, ctx, filename);
34-
35-
// bind all methods to this Script object
36-
Object.keys(binding.NodeScript.prototype).forEach(function(f) {
37-
if (util.isFunction(binding.NodeScript.prototype[f])) {
38-
this[f] = function() {
39-
if (!(this instanceof Script)) {
40-
throw new TypeError('invalid call to ' + f);
41-
}
42-
return ns[f].apply(ns, arguments);
43-
};
44-
}
45-
}, this);
46-
}
47-
48-
Script.createScript = function(code, ctx, name) {
49-
return new Script(code, ctx, name);
49+
binding.makeContext(initSandbox);
50+
51+
return initSandbox;
52+
};
53+
54+
exports.runInContext = function(code, sandbox, filename, timeout) {
55+
var script = exports.createScript(code, filename);
56+
return script.runInContext(sandbox, timeout);
5057
};
5158

52-
Script.createContext = binding.NodeScript.createContext;
53-
Script.runInContext = binding.NodeScript.runInContext;
54-
Script.runInThisContext = binding.NodeScript.runInThisContext;
55-
Script.runInNewContext = binding.NodeScript.runInNewContext;
59+
exports.runInNewContext = function(code, sandbox, filename, timeout) {
60+
var script = exports.createScript(code, filename);
61+
return script.runInNewContext(sandbox, timeout);
62+
};
63+
64+
exports.runInThisContext = function(code, filename, timeout) {
65+
var script = exports.createScript(code, filename);
66+
return script.runInThisContext(timeout);
67+
};

node.gyp

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@
9494
'src/node.cc',
9595
'src/node_buffer.cc',
9696
'src/node_constants.cc',
97+
'src/node_contextify.cc',
9798
'src/node_extensions.cc',
9899
'src/node_file.cc',
99100
'src/node_http_parser.cc',
100101
'src/node_javascript.cc',
101102
'src/node_main.cc',
102103
'src/node_os.cc',
103-
'src/node_script.cc',
104104
'src/node_stat_watcher.cc',
105105
'src/node_watchdog.cc',
106106
'src/node_zlib.cc',
@@ -120,13 +120,13 @@
120120
'src/node.h',
121121
'src/node_buffer.h',
122122
'src/node_constants.h',
123+
'src/node_contextify.h',
123124
'src/node_extensions.h',
124125
'src/node_file.h',
125126
'src/node_http_parser.h',
126127
'src/node_javascript.h',
127128
'src/node_os.h',
128129
'src/node_root_certs.h',
129-
'src/node_script.h',
130130
'src/node_version.h',
131131
'src/node_watchdog.h',
132132
'src/node_wrap.h',

src/node.cc

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "node_file.h"
2626
#include "node_http_parser.h"
2727
#include "node_javascript.h"
28-
#include "node_script.h"
2928
#include "node_version.h"
3029

3130
#if defined HAVE_PERFCTR

src/node.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@
426426
'global.require = require;\n' +
427427
'return require("vm").runInThisContext(' +
428428
JSON.stringify(body) + ', ' +
429-
JSON.stringify(name) + ', 0, true);\n';
429+
JSON.stringify(name) + ');\n';
430430
}
431431
var result = module._compile(script, name + '-wrapper');
432432
if (process._print_eval) console.log(result);
@@ -717,8 +717,11 @@
717717
// core modules found in lib/*.js. All core modules are compiled into the
718718
// node binary, so they can be loaded faster.
719719

720-
var Script = process.binding('evals').NodeScript;
721-
var runInThisContext = Script.runInThisContext;
720+
var ContextifyScript = process.binding('contextify').ContextifyScript;
721+
function runInThisContext(code, filename) {
722+
var script = new ContextifyScript(code, filename);
723+
return script.runInThisContext();
724+
}
722725

723726
function NativeModule(id) {
724727
this.filename = id + '.js';
@@ -779,7 +782,7 @@
779782
var source = NativeModule.getSource(this.id);
780783
source = NativeModule.wrap(source);
781784

782-
var fn = runInThisContext(source, this.filename, 0, true);
785+
var fn = runInThisContext(source, this.filename);
783786
fn(this.exports, NativeModule.require, this, this.filename);
784787

785788
this.loaded = true;

0 commit comments

Comments
 (0)