1
1
'use strict' ;
2
2
3
+ const { isModuleNamespaceObject } = require ( 'util' ) . types ;
3
4
const { URL } = require ( 'internal/url' ) ;
4
5
const { isContext } = process . binding ( 'contextify' ) ;
5
6
const {
@@ -10,7 +11,7 @@ const {
10
11
ERR_VM_MODULE_LINKING_ERRORED ,
11
12
ERR_VM_MODULE_NOT_LINKED ,
12
13
ERR_VM_MODULE_NOT_MODULE ,
13
- ERR_VM_MODULE_STATUS
14
+ ERR_VM_MODULE_STATUS ,
14
15
} = require ( 'internal/errors' ) . codes ;
15
16
const {
16
17
getConstructorOf,
@@ -21,6 +22,7 @@ const { SafePromise } = require('internal/safe_globals');
21
22
22
23
const {
23
24
ModuleWrap,
25
+ callbackMap,
24
26
kUninstantiated,
25
27
kInstantiating,
26
28
kInstantiated,
@@ -43,8 +45,6 @@ const perContextModuleId = new WeakMap();
43
45
const wrapMap = new WeakMap ( ) ;
44
46
const dependencyCacheMap = new WeakMap ( ) ;
45
47
const linkingStatusMap = new WeakMap ( ) ;
46
- // vm.SourceTextModule -> function
47
- const initImportMetaMap = new WeakMap ( ) ;
48
48
// ModuleWrap -> vm.SourceTextModule
49
49
const wrapToModuleMap = new WeakMap ( ) ;
50
50
const defaultModuleName = 'vm:module' ;
@@ -63,7 +63,8 @@ class SourceTextModule {
63
63
context,
64
64
lineOffset = 0 ,
65
65
columnOffset = 0 ,
66
- initializeImportMeta
66
+ initializeImportMeta,
67
+ importModuleDynamically,
67
68
} = options ;
68
69
69
70
if ( context !== undefined ) {
@@ -96,20 +97,39 @@ class SourceTextModule {
96
97
validateInteger ( lineOffset , 'options.lineOffset' ) ;
97
98
validateInteger ( columnOffset , 'options.columnOffset' ) ;
98
99
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 ) ;
106
110
}
107
111
108
112
const wrap = new ModuleWrap ( src , url , context , lineOffset , columnOffset ) ;
109
113
wrapMap . set ( this , wrap ) ;
110
114
linkingStatusMap . set ( this , 'unlinked' ) ;
111
115
wrapToModuleMap . set ( wrap , this ) ;
112
116
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
+
113
133
Object . defineProperties ( this , {
114
134
url : { value : url , enumerable : true } ,
115
135
context : { value : context , enumerable : true } ,
@@ -255,6 +275,7 @@ function validateInteger(prop, propName) {
255
275
256
276
module . exports = {
257
277
SourceTextModule,
258
- initImportMetaMap,
259
- wrapToModuleMap
278
+ wrapToModuleMap,
279
+ wrapMap,
280
+ linkingStatusMap,
260
281
} ;
0 commit comments