Zum Inhalt springen

API-Referenz zu node:vm

Skriptauswertung mit Kompatibilitätskontexten

Auf dieser Seite
Open in ChatGPTOpen in Claude

node:vm stellt APIs zur Skriptkompilierung und für Kontexte in der Form von Node bereit.

Ein Kontext ist keine Sicherheitsgrenze. Intrinsics werden mit dem Host-Realm geteilt. Verwenden Sie für nicht vertrauenswürdigen Code Prozess- und Dateisystemisolation.

ExportZweck
ScriptCode kompilieren und in diesem Kontext oder einem Sandbox-Kontext ausführen.
createContext() / isContext()Ein Objekt kontextualisieren und seine Markierung prüfen.
compileFunction()Einen Funktionsrumpf kompilieren.
runInThisContext()Im aktuellen globalen Gültigkeitsbereich auswerten.
runInContext()Gegen ein kontextualisiertes Objekt auswerten.
runInNewContext()Gegen ein neues Sandbox-Objekt auswerten.
measureMemory()Ein Promise für ein Speicherergebnis zurückgeben.
constantsVM-Konstanten.
const vm = require("node:vm");
const sandbox = { x: 2 };
vm.createContext(sandbox);
vm.runInContext("x + 3", sandbox); // 5

SourceTextModule stellt nur einen eingeschränkten Lebenszyklus bereit und bietet kein vollständiges Verknüpfen und Auswerten von Modulen.

Siehe auch

Reference

Status: Full · 80 of 95 suite tests pass (84.2%) · since 1.1.0

Exports

ExportKindStatusNotes
ScriptclassFullrunInThisContext(), runInNewContext()
createContext()functionFullCreates a new execution context (sandbox)
runInNewContext()functionFullCompiles code, creates a new context, and runs the code in that context
runInThisContext()functionFullCompiles and runs code within the current global context
isContext()functionFullReturns whether the given object is a contextified sandbox

Tests: node-api node:vm: 80 pass, 15 fail, 0 error, 3 skipped. Failing: test-vm-basic.js, test-vm-codegen.js, test-vm-context-dont-contextify.js, test-vm-function-declaration.js, test-vm-global-property-prototype.js, test-vm-global-setter.js, test-vm-harmony-symbols.js, test-vm-module-after-evaluate.js, and 7 more.

War diese Seite hilfreich?