上下文隔离化一个对象意味着什么?
在 Node.js 中执行的所有 JavaScript 都在 "上下文" 的作用域内运行。 根据 V8 嵌入器指南:
在 V8 中,上下文是一个执行环境,它允许单独的、不相关的 JavaScript 应用程序在 V8 的单个实例中运行。 必须明确指定要在其中运行任何 JavaScript 代码的上下文。
当方法 vm.createContext()
被调用时,contextObject
参数(或者新创建的对象,如果 contextObject
是 undefined
)在内部与 V8 上下文的新实例相关联。
这个 V8 上下文使用 node:vm
模块的方法提供了 code
运行,它可以在隔离的全局环境中运行。
创建 V8 上下文并将其与 contextObject
相关联的过程就是本文档所指的“上下文隔离化”对象。
All JavaScript executed within Node.js runs within the scope of a "context". According to the V8 Embedder's Guide:
In V8, a context is an execution environment that allows separate, unrelated, JavaScript applications to run in a single instance of V8. You must explicitly specify the context in which you want any JavaScript code to be run.
When the method vm.createContext()
is called, the contextObject
argument
(or a newly-created object if contextObject
is undefined
) is associated
internally with a new instance of a V8 Context. This V8 Context provides the
code
run using the node:vm
module's methods with an isolated global
environment within which it can operate. The process of creating the V8 Context
and associating it with the contextObject
is what this document refers to as
"contextifying" the object.