script.runInNewContext([contextObject[, options]])
contextObject
<Object> 一个将会被上下文隔离化的对象。如果是undefined
, 则会生成一个新的对象。options
<Object>displayErrors
<boolean> 当值为true
的时候,假如在解析代码的时候发生错误Error
,引起错误的行将会被加入堆栈跟踪信息。默认值:true
。timeout
<integer> 定义在被终止执行之前此code
被允许执行的最大毫秒数。假如执行被终止,将会抛出一个错误Error
。该值必须是严格正整数。breakOnSigint
<boolean> 若值为true
,接收到SIGINT
(Ctrl+C)会终止执行并抛出Error
。 通过process.on('SIGINT')
方法所设置的消息响应机制在代码被执行时会被屏蔽,但代码被终止后会被恢复。 默认值:false
。contextName
<string> 新创建的上下文的人类可读名称。 默认值:'VM Context i'
,其中i
是创建的上下文的升序数字索引。contextOrigin
<string> 对应于新创建用于显示目的的上下文的原点。 原点应格式化为类似一个 URL,但只包含协议,主机和端口(如果需要),例如URL
对象的url.origin
属性的值。 最值得注意的是,此字符串应省略尾部斜杠,因为它表示路径。 默认值:''
。contextCodeGeneration
<Object>microtaskMode
<string> If set toafterEvaluate
, microtasks (tasks scheduled throughPromise
s andasync function
s) will be run immediately after the script has run. They are included in thetimeout
andbreakOnSigint
scopes in that case.
- 返回: <any> 脚本中执行的最后一个语句的结果。
首先给指定的 contextObject
提供一个隔离的上下文, 再在此上下文中执行 vm.Script
中被编译的代码,最后返回结果。运行中的代码无法获取本地作用域。
以下的例子会编译一段代码,该代码会递增一个全局变量,给另外一个全局变量赋值。同时该代码被编译后会被多次执行。全局变量会被置于各个独立的 context
对象内。
const vm = require('vm');
const script = new vm.Script('globalVar = "set"');
const contexts = [{}, {}, {}];
contexts.forEach((context) => {
script.runInNewContext(context);
});
console.log(contexts);
// 打印: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
contextObject
<Object> An object that will be contextified. Ifundefined
, a new object will be created.options
<Object>displayErrors
<boolean> Whentrue
, if anError
occurs while compiling thecode
, the line of code causing the error is attached to the stack trace. Default:true
.timeout
<integer> Specifies the number of milliseconds to executecode
before terminating execution. If execution is terminated, anError
will be thrown. This value must be a strictly positive integer.breakOnSigint
<boolean> Iftrue
, receivingSIGINT
(Ctrl+C) will terminate execution and throw anError
. Existing handlers for the event that have been attached viaprocess.on('SIGINT')
are disabled during script execution, but continue to work after that. Default:false
.contextName
<string> Human-readable name of the newly created context. Default:'VM Context i'
, wherei
is an ascending numerical index of the created context.contextOrigin
<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:''
.contextCodeGeneration
<Object>microtaskMode
<string> If set toafterEvaluate
, microtasks (tasks scheduled throughPromise
s andasync function
s) will be run immediately after the script has run. They are included in thetimeout
andbreakOnSigint
scopes in that case.
- Returns: <any> the result of the very last statement executed in the script.
First contextifies the given contextObject
, runs the compiled code contained
by the vm.Script
object within the created context, and returns the result.
Running code does not have access to local scope.
The following example compiles code that sets a global variable, then executes
the code multiple times in different contexts. The globals are set on and
contained within each individual context
.
const vm = require('vm');
const script = new vm.Script('globalVar = "set"');
const contexts = [{}, {}, {}];
contexts.forEach((context) => {
script.runInNewContext(context);
});
console.log(contexts);
// Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]