new vm.SourceTextModule(code[, options])
历史
| 版本 | 变化 |
|---|---|
| v17.0.0、v16.12.0 | 向 |
参数
code<string> JavaScript 模块代码解析optionsidentifier<string> 堆栈跟踪中使用的字符串。 默认:'vm:module(i)'其中i是 context-specific 升序索引。cachedData<Buffer> | <TypedArray> | <DataView> 为提供的源提供可选的Buffer或TypedArray或DataView以及 V8 的代码缓存数据。code必须与创建此cachedData的模块相同。context<Object>vm.createContext()方法返回的 contextified 对象,用于在其中编译和评估此Module。lineOffset<integer> 指定在此Module生成的堆栈跟踪中显示的行号偏移量。 默认:0。columnOffset<integer> 指定在此Module生成的堆栈跟踪中显示的 first-line 列号偏移量。 默认:0。initializeImportMeta<Function>在评估这个时调用Module初始化import.meta.meta<import.meta>module<vm.SourceTextModule>
importModuleDynamically<Function>在评估此模块时调用import()叫做。如果未指定此选项,则调用import()会拒绝ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING.specifier<string> 说明符传递给import()modulevm.ModuleimportAssertions<Object>"assert"值传递给可选参数,如果未提供值,则为空对象。optionsExpression- 返回: <Module Namespace Object> | <vm.Module> 建议返回
vm.Module以利用错误跟踪,并避免包含then函数导出的命名空间出现问题。
创建一个新的 SourceTextModule 实例。
分配给作为对象的 import.meta 对象的属性可能允许模块访问指定 context 之外的信息。使用 vm.runInContext() 在特定上下文中创建对象。
import vm from 'node:vm';
const contextifiedObject = vm.createContext({ secret: 42 });
const module = new vm.SourceTextModule(
'Object.getPrototypeOf(import.meta.prop).secret = secret;',
{
initializeImportMeta(meta) {
// Note: this object is created in the top context. As such,
// Object.getPrototypeOf(import.meta.prop) points to the
// Object.prototype in the top context rather than that in
// the contextified object.
meta.prop = {};
}
});
// Since module has no dependencies, the linker function will never be called.
await module.link(() => {});
await module.evaluate();
// Now, Object.prototype.secret will be equal to 42.
//
// To fix this problem, replace
// meta.prop = {};
// above with
// meta.prop = vm.runInContext('{}', contextifiedObject);const vm = require('node:vm');
const contextifiedObject = vm.createContext({ secret: 42 });
(async () => {
const module = new vm.SourceTextModule(
'Object.getPrototypeOf(import.meta.prop).secret = secret;',
{
initializeImportMeta(meta) {
// Note: this object is created in the top context. As such,
// Object.getPrototypeOf(import.meta.prop) points to the
// Object.prototype in the top context rather than that in
// the contextified object.
meta.prop = {};
}
});
// Since module has no dependencies, the linker function will never be called.
await module.link(() => {});
await module.evaluate();
// Now, Object.prototype.secret will be equal to 42.
//
// To fix this problem, replace
// meta.prop = {};
// above with
// meta.prop = vm.runInContext('{}', contextifiedObject);
})();
相关用法
- Node.js new assert.AssertionError(options)用法及代码示例
- Node.js new AsyncResource(type[, options])用法及代码示例
- Node.js new stream.Duplex(options)用法及代码示例
- Node.js new stream.Readable([options])用法及代码示例
- Node.js new Console(options)用法及代码示例
- Node.js new URLSearchParams(obj)用法及代码示例
- Node.js new crypto.Certificate()用法及代码示例
- Node.js new stream.Writable([options])用法及代码示例
- Node.js new URLSearchParams(iterable)用法及代码示例
- Node.js new Agent([options])用法及代码示例
- Node.js new stream.Transform([options])用法及代码示例
- Node.js new PerformanceObserver(callback)用法及代码示例
- Node.js new URL(input[, base])用法及代码示例
- Node.js new URLSearchParams(string)用法及代码示例
- Node.js new assert.CallTracker()用法及代码示例
- Node.js net.isIP(input)用法及代码示例
- Node.js net.createConnection(options[, connectListener])用法及代码示例
- Node.js net.isIPv6(input)用法及代码示例
- Node.js net.Server.address()用法及代码示例
- Node.js net.createServer([options][, connectionListener])用法及代码示例
- Node.js net.Server.listen()用法及代码示例
- Node.js net.Socket.setTimeout(timeout[, callback])用法及代码示例
- Node.js net.BlockList.check(address[, type])用法及代码示例
- Node.js net.isIPv4(input)用法及代码示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 new vm.SourceTextModule(code[, options])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
