summaryrefslogtreecommitdiff
path: root/lib/vm.js
diff options
context:
space:
mode:
authorDomenic Denicola <domenic@domenicdenicola.com>2013-07-27 00:34:12 -0400
committerisaacs <i@izs.me>2013-08-21 15:52:23 -0700
commit7afdba6e0bc3b69c2bf5fdbd59f938ac8f7a64c5 (patch)
treec1a5a28e08784720c7de7cb98cf73f0bb3748db3 /lib/vm.js
parent3602d4c23bd8beccd925161adf5459068aec23e3 (diff)
downloadandroid-node-v8-7afdba6e0bc3b69c2bf5fdbd59f938ac8f7a64c5.tar.gz
android-node-v8-7afdba6e0bc3b69c2bf5fdbd59f938ac8f7a64c5.tar.bz2
android-node-v8-7afdba6e0bc3b69c2bf5fdbd59f938ac8f7a64c5.zip
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]: https://github.com/joyent/node/issues/5323#issuecomment-20250726 [4]: https://github.com/kkoopa/contextify/blob/bf123f3ef960f0943d1e30bda02e3163a004e964/src/contextify.cc [5]: https://gist.github.com/domenic/6068120
Diffstat (limited to 'lib/vm.js')
-rw-r--r--lib/vm.js68
1 files changed, 40 insertions, 28 deletions
diff --git a/lib/vm.js b/lib/vm.js
index f06d5abe98..5dd1ddea70 100644
--- a/lib/vm.js
+++ b/lib/vm.js
@@ -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);
+};