Skip to main content

Module node_vm

Module node_vm 

Source
Expand description

node:vm module — SpiderMonkey Realm-isolated sandbox implementation.

§Architecture (DEC-ENG-003)

Each vm.createContext(sandbox) creates an independent SpiderMonkey Compartment via JS_NewGlobalObject. The sandbox object’s properties are copied onto the new global so that vm.runInNewContext(code, {x: 42}) makes x available as a global variable in the sandbox realm.

Code execution uses AutoRealm to temporarily switch the JSContext into the sandbox Compartment; dropping the AutoRealm restores the caller’s realm. Cross-Compartment object references are automatically wrapped by SM’s CCW (Cross-Compartment Wrapper) mechanism.

§Key APIs

  • vm.createContext(sandbox?, opts?) → creates new SM Realm, copies sandbox properties to the new global, returns the contextified sandbox
  • vm.runInNewContext(code, sandbox?, opts?) → createContext + evaluate in the new Realm, returns the last expression value
  • vm.runInThisContext(code, opts?) → evaluate in caller’s Realm
  • vm.isContext(obj) → checks if obj was contextified by createContext
  • vm.Script(code, opts?) → pre-compiled script with runInContext / runInNewContext methods
  • vm.compileFunction(code, params?, opts?) → compile a function body

Functions§

install