11'use strict' ;
22
3+ const { isModuleNamespaceObject } = require ( 'util' ) . types ;
34const { URL } = require ( 'internal/url' ) ;
45const { isContext } = process . binding ( 'contextify' ) ;
56const {
@@ -10,7 +11,7 @@ const {
1011 ERR_VM_MODULE_LINKING_ERRORED ,
1112 ERR_VM_MODULE_NOT_LINKED ,
1213 ERR_VM_MODULE_NOT_MODULE ,
13- ERR_VM_MODULE_STATUS
14+ ERR_VM_MODULE_STATUS ,
1415} = require ( 'internal/errors' ) . codes ;
1516const {
1617 getConstructorOf,
@@ -21,6 +22,7 @@ const { SafePromise } = require('internal/safe_globals');
2122
2223const {
2324 ModuleWrap,
25+ callbackMap,
2426 kUninstantiated,
2527 kInstantiating,
2628 kInstantiated,
@@ -43,8 +45,6 @@ const perContextModuleId = new WeakMap();
4345const wrapMap = new WeakMap ( ) ;
4446const dependencyCacheMap = new WeakMap ( ) ;
4547const linkingStatusMap = new WeakMap ( ) ;
46- // vm.SourceTextModule -> function
47- const initImportMetaMap = new WeakMap ( ) ;
4848// ModuleWrap -> vm.SourceTextModule
4949const wrapToModuleMap = new WeakMap ( ) ;
5050const defaultModuleName = 'vm:module' ;
@@ -63,7 +63,8 @@ class SourceTextModule {
6363 context,
6464 lineOffset = 0 ,
6565 columnOffset = 0 ,
66- initializeImportMeta
66+ initializeImportMeta,
67+ importModuleDynamically,
6768 } = options ;
6869
6970 if ( context !== undefined ) {
@@ -96,20 +97,39 @@ class SourceTextModule {
9697 validateInteger ( lineOffset , 'options.lineOffset' ) ;
9798 validateInteger ( columnOffset , 'options.columnOffset' ) ;
9899
99- if ( initializeImportMeta !== undefined ) {
100- if ( typeof initializeImportMeta === 'function' ) {
101- initImportMetaMap . set ( this , initializeImportMeta ) ;
102- } else {
103- throw new ERR_INVALID_ARG_TYPE (
104- 'options.initializeImportMeta' , 'function' , initializeImportMeta ) ;
105- }
100+ if ( initializeImportMeta !== undefined &&
101+ typeof initializeImportMeta !== 'function' ) {
102+ throw new ERR_INVALID_ARG_TYPE (
103+ 'options.initializeImportMeta' , 'function' , initializeImportMeta ) ;
104+ }
105+
106+ if ( importModuleDynamically !== undefined &&
107+ typeof importModuleDynamically !== 'function' ) {
108+ throw new ERR_INVALID_ARG_TYPE (
109+ 'options.importModuleDynamically' , 'function' , importModuleDynamically ) ;
106110 }
107111
108112 const wrap = new ModuleWrap ( src , url , context , lineOffset , columnOffset ) ;
109113 wrapMap . set ( this , wrap ) ;
110114 linkingStatusMap . set ( this , 'unlinked' ) ;
111115 wrapToModuleMap . set ( wrap , this ) ;
112116
117+ callbackMap . set ( wrap , {
118+ initializeImportMeta,
119+ importModuleDynamically : importModuleDynamically ? async ( ...args ) => {
120+ const m = await importModuleDynamically ( ...args ) ;
121+ if ( isModuleNamespaceObject ( m ) ) {
122+ return m ;
123+ }
124+ if ( ! m || ! wrapMap . has ( m ) )
125+ throw new ERR_VM_MODULE_NOT_MODULE ( ) ;
126+ const childLinkingStatus = linkingStatusMap . get ( m ) ;
127+ if ( childLinkingStatus === 'errored' )
128+ throw m . error ;
129+ return m . namespace ;
130+ } : undefined ,
131+ } ) ;
132+
113133 Object . defineProperties ( this , {
114134 url : { value : url , enumerable : true } ,
115135 context : { value : context , enumerable : true } ,
@@ -255,6 +275,7 @@ function validateInteger(prop, propName) {
255275
256276module . exports = {
257277 SourceTextModule,
258- initImportMetaMap,
259- wrapToModuleMap
278+ wrapToModuleMap,
279+ wrapMap,
280+ linkingStatusMap,
260281} ;
0 commit comments