Skip to main content

Module vm

Module vm 

Source
Expand description

Node vm module — code compilation and evaluation reusing node-js’s own engine.

Fidelity and honesty about scope: node-js runs on a single global heap with one set of module-level globals (see host::JsHost). It has NO facility for a second, isolated global object, so vm here provides genuine evaluation but NOT the context isolation Node’s vm is designed around:

  • runInThisContext(code) — REAL: compiles code and runs it on the current host through the exact compile → load_merged → run_chunk_on path the module loader and REPL use, returning the completion value (the value of the last expression). Code sees and mutates the current globals — which is precisely what runInThisContext is supposed to do.
  • runInNewContext(code[, sandbox]) — NOT ISOLATED (documented): there is no separate global object to create. As a pragmatic contextify emulation, the sandbox’s own properties are merged into the shared global scope before the run and copied back into the sandbox object afterward, so the common runInNewContext(src, sandbox) read/write pattern works. It does NOT hide the surrounding globals and does NOT restore them — this is stated plainly, never claimed as isolation.
  • createContext(obj) — no-op passthrough: returns obj (or a fresh object). node-js has no distinct context to contextify; this exists so createContext call sites don’t throw.
  • isContext(obj) — returns true for any object (everything shares the one context here).
  • Script — a compiled-code holder (@@native = "Script"): new Script(code) stores the source; .runInThisContext() / .runInNewContext([sandbox]) run it on demand via the same paths as the free functions.

Constants§

METHODS
SCRIPT_METHODS
Methods dispatched on an @@native = "Script" object (reported to the parent for instance_has_method wiring).

Functions§

call
construct
new vm.Script(code) → a Script object holding the source.
instance_call
Dispatch a method on a Script instance (@@native = "Script").