1
1
'use strict' ;
2
2
3
+ /* global WebAssembly */
4
+
3
5
const {
6
+ JSON ,
7
+ Object,
4
8
SafeMap,
5
- StringPrototype,
6
- JSON
9
+ StringPrototype
7
10
} = primordials ;
8
11
9
12
const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
@@ -72,11 +75,11 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) {
72
75
] ;
73
76
if ( module && module . loaded ) {
74
77
const exports = module . exports ;
75
- return createDynamicModule ( [ 'default' ] , url , ( reflect ) => {
78
+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( reflect ) => {
76
79
reflect . exports . default . set ( exports ) ;
77
80
} ) ;
78
81
}
79
- return createDynamicModule ( [ 'default' ] , url , ( ) => {
82
+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( ) => {
80
83
debug ( `Loading CJSModule ${ url } ` ) ;
81
84
// We don't care about the return val of _load here because Module#load
82
85
// will handle it for us by checking the loader registry and filling the
@@ -97,7 +100,7 @@ translators.set('builtin', async function builtinStrategy(url) {
97
100
}
98
101
module . compileForPublicLoader ( true ) ;
99
102
return createDynamicModule (
100
- [ ...module . exportKeys , 'default' ] , url , ( reflect ) => {
103
+ [ ] , [ ...module . exportKeys , 'default' ] , url , ( reflect ) => {
101
104
debug ( `Loading BuiltinModule ${ url } ` ) ;
102
105
module . reflect = reflect ;
103
106
for ( const key of module . exportKeys )
@@ -116,7 +119,7 @@ translators.set('json', async function jsonStrategy(url) {
116
119
let module = CJSModule . _cache [ modulePath ] ;
117
120
if ( module && module . loaded ) {
118
121
const exports = module . exports ;
119
- return createDynamicModule ( [ 'default' ] , url , ( reflect ) => {
122
+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( reflect ) => {
120
123
reflect . exports . default . set ( exports ) ;
121
124
} ) ;
122
125
}
@@ -136,8 +139,32 @@ translators.set('json', async function jsonStrategy(url) {
136
139
throw err ;
137
140
}
138
141
CJSModule . _cache [ modulePath ] = module ;
139
- return createDynamicModule ( [ 'default' ] , url , ( reflect ) => {
142
+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( reflect ) => {
140
143
debug ( `Parsing JSONModule ${ url } ` ) ;
141
144
reflect . exports . default . set ( module . exports ) ;
142
145
} ) ;
143
146
} ) ;
147
+
148
+ // Strategy for loading a wasm module
149
+ translators . set ( 'wasm' , async function ( url ) {
150
+ const pathname = fileURLToPath ( url ) ;
151
+ const buffer = await readFileAsync ( pathname ) ;
152
+ debug ( `Translating WASMModule ${ url } ` ) ;
153
+ let compiled ;
154
+ try {
155
+ compiled = await WebAssembly . compile ( buffer ) ;
156
+ } catch ( err ) {
157
+ err . message = pathname + ': ' + err . message ;
158
+ throw err ;
159
+ }
160
+
161
+ const imports =
162
+ WebAssembly . Module . imports ( compiled ) . map ( ( { module } ) => module ) ;
163
+ const exports = WebAssembly . Module . exports ( compiled ) . map ( ( { name } ) => name ) ;
164
+
165
+ return createDynamicModule ( imports , exports , url , ( reflect ) => {
166
+ const { exports } = new WebAssembly . Instance ( compiled , reflect . imports ) ;
167
+ for ( const expt of Object . keys ( exports ) )
168
+ reflect . exports [ expt ] . set ( exports [ expt ] ) ;
169
+ } ) ;
170
+ } ) ;
0 commit comments