انتقل إلى المحتوى

مرجع واجهة ⁦node:vm⁩

تقييم السكربتات مع سياقات التوافق

في هذه الصفحة
Open in ChatGPTOpen in Claude

توفر ⁦node:vm⁩ واجهات تجميع السكربتات والسياقات بشكل ⁦Node⁩.

السياق ليس حدًّا أمنيًا. فالكائنات الجوهرية مشتركة مع نطاق المضيف. استخدم عزل العمليات ونظام الملفات للشيفرة غير الموثوقة.

الصادرالغرض
Scriptتجميع الشيفرة وتشغيلها في هذا السياق أو في سياق معزول.
createContext() / isContext()تحويل كائن إلى سياق والتحقق من علامته.
compileFunction()تجميع جسم دالة.
runInThisContext()التقييم في النطاق العام الحالي.
runInContext()التقييم مقابل كائن محوَّل إلى سياق.
runInNewContext()التقييم مقابل كائن معزول جديد.
measureMemory()إعادة ⁦Promise⁩ لنتيجة ذاكرة.
constantsثوابت الآلة الافتراضية.
const vm = require("node:vm");
const sandbox = { x: 2 };
vm.createContext(sandbox);
vm.runInContext("x + 3", sandbox); // 5

لا يكشف ⁦SourceTextModule⁩ إلا دورة حياة محدودة ولا يوفر ربط الوحدات وتقييمها الكاملين.

انظر أيضًا

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.

هل كانت هذه الصفحة مفيدة؟