vm.createContext([contextObject[, options]])
contextObject
<Object>options
<Object>name
<string> 新创建的上下文的可读名称。 默认值:'VM Context i'
, 其中i
是创建的上下文的升序数字索引。origin
<string> 对应于新创建的用于显示目的的上下文的来源。 来源的格式应该像 URL,但只有协议、主机和端口(如果需要),就像URL
对象的url.origin
属性的值。 最值得注意的是,该字符串应省略尾部斜杠,因为它表示路径。 默认值:''
。codeGeneration
<Object>microtaskMode
<string> 如果设置为afterEvaluate
,则微任务(通过Promise
和async function
调度的任务)将在脚本运行到script.runInContext()
后立即运行。 在这种情况下,它们包含在timeout
和breakOnSigint
范围内。
- 返回: <Object> 上下文隔离化的对象。
如果给定 contextObject
,vm.createContext()
方法将准备那个对象,以便它可以用于调用 vm.runInContext()
或 script.runInContext()
。
在此类脚本中,contextObject
将是全局对象,保留其所有现有属性,但也具有任何标准全局对象具有的内置对象和函数。
在 vm 模块运行的脚本之外,全局变量将保持不变。
const vm = require('vm');
global.globalVar = 3;
const context = { globalVar: 1 };
vm.createContext(context);
vm.runInContext('globalVar *= 2;', context);
console.log(context);
// 打印: { globalVar: 2 }
console.log(global.globalVar);
// 打印: 3
如果省略 contextObject
(或显式地作为 undefined
传入),则将返回新的空的上下文隔离化的对象。
vm.createContext()
方法主要用于创建可用于运行多个脚本的单个上下文。
例如,如果模拟网络浏览器,则该方法可用于创建表示窗口全局对象的单个上下文,然后在该上下文中一起运行所有 <script>
标签。
提供的上下文的 name
和 origin
通过检查器 API 可见。
contextObject
<Object>options
<Object>name
<string> Human-readable name of the newly created context. Default:'VM Context i'
, wherei
is an ascending numerical index of the created context.origin
<string> Origin corresponding to the newly created context for display purposes. The origin should be formatted like a URL, but with only the scheme, host, and port (if necessary), like the value of theurl.origin
property of aURL
object. Most notably, this string should omit the trailing slash, as that denotes a path. Default:''
.codeGeneration
<Object>microtaskMode
<string> If set toafterEvaluate
, microtasks (tasks scheduled throughPromise
s andasync function
s) will be run immediately after a script has run throughscript.runInContext()
. They are included in thetimeout
andbreakOnSigint
scopes in that case.
- Returns: <Object> contextified object.
If given a contextObject
, the vm.createContext()
method will prepare
that object so that it can be used in calls to
vm.runInContext()
or script.runInContext()
. Inside such scripts,
the contextObject
will be the global object, retaining all of its existing
properties but also having the built-in objects and functions any standard
global object has. Outside of scripts run by the vm module, global variables
will remain unchanged.
const vm = require('vm');
global.globalVar = 3;
const context = { globalVar: 1 };
vm.createContext(context);
vm.runInContext('globalVar *= 2;', context);
console.log(context);
// Prints: { globalVar: 2 }
console.log(global.globalVar);
// Prints: 3
If contextObject
is omitted (or passed explicitly as undefined
), a new,
empty contextified object will be returned.
The vm.createContext()
method is primarily useful for creating a single
context that can be used to run multiple scripts. For instance, if emulating a
web browser, the method can be used to create a single context representing a
window's global object, then run all <script>
tags together within that
context.
The provided name
and origin
of the context are made visible through the
Inspector API.