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 sandboxvm.runInNewContext(code, sandbox?, opts?)→ createContext + evaluate in the new Realm, returns the last expression valuevm.runInThisContext(code, opts?)→ evaluate in caller’s Realmvm.isContext(obj)→ checks if obj was contextified by createContextvm.Script(code, opts?)→ pre-compiled script with runInContext / runInNewContext methodsvm.compileFunction(code, params?, opts?)→ compile a function body