11'use strict' ;
22
3+ /* global WebAssembly */
4+
35const {
6+ JSON ,
7+ Object,
48 SafeMap,
5- StringPrototype,
6- JSON
9+ StringPrototype
710} = primordials ;
811
912const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
@@ -72,11 +75,11 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) {
7275 ] ;
7376 if ( module && module . loaded ) {
7477 const exports = module . exports ;
75- return createDynamicModule ( [ 'default' ] , url , ( reflect ) => {
78+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( reflect ) => {
7679 reflect . exports . default . set ( exports ) ;
7780 } ) ;
7881 }
79- return createDynamicModule ( [ 'default' ] , url , ( ) => {
82+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( ) => {
8083 debug ( `Loading CJSModule ${ url } ` ) ;
8184 // We don't care about the return val of _load here because Module#load
8285 // will handle it for us by checking the loader registry and filling the
@@ -97,7 +100,7 @@ translators.set('builtin', async function builtinStrategy(url) {
97100 }
98101 module . compileForPublicLoader ( true ) ;
99102 return createDynamicModule (
100- [ ...module . exportKeys , 'default' ] , url , ( reflect ) => {
103+ [ ] , [ ...module . exportKeys , 'default' ] , url , ( reflect ) => {
101104 debug ( `Loading BuiltinModule ${ url } ` ) ;
102105 module . reflect = reflect ;
103106 for ( const key of module . exportKeys )
@@ -116,7 +119,7 @@ translators.set('json', async function jsonStrategy(url) {
116119 let module = CJSModule . _cache [ modulePath ] ;
117120 if ( module && module . loaded ) {
118121 const exports = module . exports ;
119- return createDynamicModule ( [ 'default' ] , url , ( reflect ) => {
122+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( reflect ) => {
120123 reflect . exports . default . set ( exports ) ;
121124 } ) ;
122125 }
@@ -136,8 +139,32 @@ translators.set('json', async function jsonStrategy(url) {
136139 throw err ;
137140 }
138141 CJSModule . _cache [ modulePath ] = module ;
139- return createDynamicModule ( [ 'default' ] , url , ( reflect ) => {
142+ return createDynamicModule ( [ ] , [ 'default' ] , url , ( reflect ) => {
140143 debug ( `Parsing JSONModule ${ url } ` ) ;
141144 reflect . exports . default . set ( module . exports ) ;
142145 } ) ;
143146} ) ;
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