new vm.Script(code[, options])


  • code <string> 需要被解析的 JavaScript 代码。
  • options <Object> | <string>
    • filename <string> 定义供脚本生成的堆栈跟踪信息所使用的文件名。默认值: 'evalmachine.<anonymous>'
    • lineOffset <number> 定义脚本生成的堆栈跟踪信息所显示的行号偏移。默认值: 0
    • columnOffset <number> 定义脚本生成的堆栈跟踪信息所显示的列号偏移。默认值: 0
    • cachedData <Buffer> | <TypedArray> | <DataView> 为源码提供一个可选的存有 V8 代码缓存数据的 BufferTypedArrayTypedArray。 如果提供了,则 cachedDataRejected 的值将会被设为要么为 true 要么为 false,这取决于 v8 引擎对数据的接受状况。
    • produceCachedData <boolean> 当值为 truecachedData 不存在的时候,V8 将会试图为 code 生成代码缓存数据。 一旦成功,则一个有 V8 代码缓存数据的 Buffer 将会被生成和储存在返回的 vm.Script 实例的 cachedData 属性里。 cachedDataProduced 的值会被设置为 true 或者 false,这取决于代码缓存数据是否被成功生成。 不推荐使用此选项,而应该使用 script.createCachedData()默认值: false
    • importModuleDynamically <Function> 在调用 import() 时评估此模块期间调用。 如果未指定此选项,则对 import() 的调用将拒绝 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING。 此选项是实验的模块的 API 的一部分。 不建议在生产环境中使用。

    如果 options 是字符串,则它指定文件名。

创建一个新的 vm.Script 对象只编译 code 但不会执行它。 编译过的 vm.Script 此后可以被多次执行。 code 是不绑定于任何全局对象的,相反,它仅仅绑定于每次执行它的对象。

  • code <string> The JavaScript code to compile.
  • options <Object> | <string>
    • filename <string> Specifies the filename used in stack traces produced by this script. Default: 'evalmachine.<anonymous>'.
    • lineOffset <number> Specifies the line number offset that is displayed in stack traces produced by this script. Default: 0.
    • columnOffset <number> Specifies the column number offset that is displayed in stack traces produced by this script. Default: 0.
    • cachedData <Buffer> | <TypedArray> | <DataView> Provides an optional Buffer or TypedArray, or DataView with V8's code cache data for the supplied source. When supplied, the cachedDataRejected value will be set to either true or false depending on acceptance of the data by V8.
    • produceCachedData <boolean> When true and no cachedData is present, V8 will attempt to produce code cache data for code. Upon success, a Buffer with V8's code cache data will be produced and stored in the cachedData property of the returned vm.Script instance. The cachedDataProduced value will be set to either true or false depending on whether code cache data is produced successfully. This option is deprecated in favor of script.createCachedData(). Default: false.
    • importModuleDynamically <Function> Called during evaluation of this module when import() is called. If this option is not specified, calls to import() will reject with ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING. This option is part of the experimental modules API. We do not recommend using it in a production environment.

If options is a string, then it specifies the filename.

Creating a new vm.Script object compiles code but does not run it. The compiled vm.Script can be run later multiple times. The code is not bound to any global object; rather, it is bound before each run, just for that run.