"contextify" 一个对象是什么意思?


¥What does it mean to "contextify" an object?

在 Node.js 中执行的所有 JavaScript 都在 "context" 的范围内运行。根据 V8 嵌入器指南

¥All JavaScript executed within Node.js runs within the scope of a "context". According to the V8 Embedder's Guide:

在 V8 中,上下文是一个执行环境,它允许独立的、不相关的 JavaScript 应用在 V8 的单个实例中运行。你必须明确指定要运行任何 JavaScript 代码的上下文。

¥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.

当方法 vm.createContext() 被调用时,contextObject 参数(或者新创建的对象,如果 contextObjectundefined)在内部与 V8 上下文的新实例相关联。这个 V8 上下文使用 node:vm 模块的方法提供了 code 运行,它可以在隔离的全局环境中运行。创建 V8 Context 并将其与 contextObject 相关联的过程就是本文档所指的 "contextifying" 对象。

¥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.