vm, core, module: re-do vm to fix known issues
authorDomenic Denicola <domenic@domenicdenicola.com>
Sat, 27 Jul 2013 04:34:12 +0000 (00:34 -0400)
committerisaacs <i@izs.me>
Wed, 21 Aug 2013 22:52:23 +0000 (15:52 -0700)
commit7afdba6e0bc3b69c2bf5fdbd59f938ac8f7a64c5
treec1a5a28e08784720c7de7cb98cf73f0bb3748db3
parent3602d4c23bd8beccd925161adf5459068aec23e3
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
26 files changed:
lib/module.js
lib/vm.js
node.gyp
src/node.cc
src/node.js
src/node_contextify.cc [new file with mode: 0644]
src/node_contextify.h [moved from src/node_script.h with 89% similarity]
src/node_extensions.h
src/node_script.cc [deleted file]
test/message/eval_messages.out
test/message/stdin_messages.out
test/message/undefined_reference_in_new_context.js
test/message/undefined_reference_in_new_context.out
test/simple/test-debug-break-on-uncaught.js
test/simple/test-querystring.js
test/simple/test-vm-basic.js [new file with mode: 0644]
test/simple/test-vm-context-async-script.js [new file with mode: 0644]
test/simple/test-vm-context-property-forwarding.js [new file with mode: 0644]
test/simple/test-vm-context.js [moved from test/simple/test-script-context.js with 75% similarity]
test/simple/test-vm-create-and-run-in-context.js [moved from test/simple/test-script-static-context.js with 78% similarity]
test/simple/test-vm-global-identity.js [new file with mode: 0644]
test/simple/test-vm-new-script-new-context.js [moved from test/simple/test-script-new.js with 83% similarity]
test/simple/test-vm-new-script-this-context.js [moved from test/simple/test-script-this.js with 94% similarity]
test/simple/test-vm-run-in-new-context.js [moved from test/simple/test-script-static-new.js with 76% similarity]
test/simple/test-vm-static-this.js [moved from test/simple/test-script-static-this.js with 80% similarity]
test/simple/test-vm-timeout.js [moved from test/simple/test-vm-run-timeout.js with 100% similarity]