VM WASI and FFI boundaries
VM contexts provide separate JavaScript global environments; WebAssembly executes portable modules through a host; WASI specifies host interfaces for system-like capabilities. None is automatically a complete hostile-code sandbox. `vm.createContext` and `vm.runInContext` can run fixed trusted scripts with an explicit context. A timeout constrains some synchronous execution but is not a comprehensive resource or security boundary. WebAssembly modules can import host functions, so the host interface and embedding matter. WASI support and capability semantics vary by implementation and version. Foreign function interfaces (FFI) call native code and require ABI, memory ownership and platform review. Node-API and FFI are not interchangeable. This lesson uses only a literal trusted expression and a tiny reviewed WebAssembly binary with no imports, and does not instantiate WASI or load native libraries. Never expose arbitrary learner code to VM or native APIs as a substitute for reviewed process/VM isolation. No sandbox bypass experiments. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/vm.html; https://nodejs.org/download/release/v22.16.0/docs/api/wasi.html.
Formal Syntax & Structure
vm.createContext(contextObject); vm.runInContext(code, contextifiedObject, options)Working Example
import vm from 'node:vm';import assert from 'node:assert/strict';const a=vm.createContext({count:1}),b=vm.createContext({count:9});vm.runInContext('count += 2',a,{timeout:100});assert.equal(b.count,9);console.log(a.count,b.count);