66
77const Template = require ( "../Template" ) ;
88const WebAssemblyImportDependency = require ( "../dependencies/WebAssemblyImportDependency" ) ;
9+ const WebAssemblyUtils = require ( "./WebAssemblyUtils" ) ;
10+
11+ /** @typedef {import("../Module") } Module */
912
1013// Get all wasm modules
1114function getAllWasmModules ( chunk ) {
@@ -22,61 +25,51 @@ function getAllWasmModules(chunk) {
2225 return array ;
2326}
2427
28+ /**
29+ * generates the import object function for a module
30+ * @param {Module } module the module
31+ * @returns {string } source code
32+ */
2533function generateImportObject ( module ) {
2634 const waitForInstances = new Map ( ) ;
2735 const properties = [ ] ;
28- let importIndex = 0 ;
29- for ( const dep of module . dependencies ) {
30- if ( dep instanceof WebAssemblyImportDependency ) {
31- // Ignore global they will be handled later
32- if ( dep . description . type === "GlobalType" ) {
33- continue ;
34- }
35-
36- if ( dep . module === null ) {
37- // Dependency was not found, an error will be thrown later
38- continue ;
39- }
36+ const usedWasmDependencies = WebAssemblyUtils . getUsedDependencies ( module ) ;
37+ for ( const usedDep of usedWasmDependencies ) {
38+ const dep = usedDep . dependency ;
39+ const importedModule = dep . module ;
40+ const exportName = dep . name ;
41+ const usedName = importedModule && importedModule . isUsed ( exportName ) ;
42+ const description = dep . description ;
43+ const direct = dep . onlyDirectImport ;
4044
41- const importedModule = dep . module ;
42- const exportName = dep . name ;
43- const usedName = importedModule && importedModule . isUsed ( exportName ) ;
44- if ( usedName !== false ) {
45- const description = dep . description ;
46- const direct = dep . onlyDirectImport ;
45+ const propertyName = usedDep . name ;
4746
48- const index = importIndex ++ ;
47+ if ( direct ) {
48+ const instanceVar = `m${ waitForInstances . size } ` ;
49+ waitForInstances . set ( instanceVar , importedModule . id ) ;
50+ properties . push (
51+ `${ JSON . stringify ( propertyName ) } : ${ instanceVar } ` +
52+ `[${ JSON . stringify ( usedName ) } ]`
53+ ) ;
54+ } else {
55+ const params = description . signature . params . map (
56+ ( param , k ) => "p" + k + param . valtype
57+ ) ;
4958
50- const propertyName = Template . numberToIdentifer ( index ) ;
59+ const mod = `installedModules[${ JSON . stringify ( importedModule . id ) } ]` ;
60+ const func = `${ mod } .exports[${ JSON . stringify ( usedName ) } ]` ;
5161
52- if ( direct ) {
53- const instanceVar = `m${ waitForInstances . size } ` ;
54- waitForInstances . set ( instanceVar , importedModule . id ) ;
55- properties . push (
56- `${ JSON . stringify ( propertyName ) } : ${ instanceVar } ` +
57- `[${ JSON . stringify ( usedName ) } ]`
58- ) ;
59- } else {
60- const params = description . signature . params . map (
61- ( param , k ) => "p" + k + param . valtype
62- ) ;
63-
64- const mod = `installedModules[${ JSON . stringify ( importedModule . id ) } ]` ;
65- const func = `${ mod } .exports[${ JSON . stringify ( usedName ) } ]` ;
66-
67- properties . push (
68- Template . asString ( [
69- `${ JSON . stringify ( propertyName ) } : ` +
70- ( importedModule . type . startsWith ( "webassembly" )
71- ? `${ mod } ? ${ func } : `
72- : "" ) +
73- `function(${ params } ) {` ,
74- Template . indent ( [ `return ${ func } (${ params } );` ] ) ,
75- "}"
76- ] )
77- ) ;
78- }
79- }
62+ properties . push (
63+ Template . asString ( [
64+ `${ JSON . stringify ( propertyName ) } : ` +
65+ ( importedModule . type . startsWith ( "webassembly" )
66+ ? `${ mod } ? ${ func } : `
67+ : "" ) +
68+ `function(${ params } ) {` ,
69+ Template . indent ( [ `return ${ func } (${ params } );` ] ) ,
70+ "}"
71+ ] )
72+ ) ;
8073 }
8174 }
8275
@@ -222,13 +215,15 @@ class WasmMainTemplatePlugin {
222215 Template . indent ( [
223216 "promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {" ,
224217 Template . indent ( [
225- "return WebAssembly.instantiate(items[0], {a:items[1]});"
218+ "return WebAssembly.instantiate(items[0], " +
219+ `{ ${ WebAssemblyUtils . MANGLED_MODULE } : items[1] });`
226220 ] ) ,
227221 "});"
228222 ] ) ,
229223 "} else if(typeof WebAssembly.instantiateStreaming === 'function') {" ,
230224 Template . indent ( [
231- "promise = WebAssembly.instantiateStreaming(req, {a:importObject});"
225+ "promise = WebAssembly.instantiateStreaming(req, " +
226+ `{ ${ WebAssemblyUtils . MANGLED_MODULE } : importObject });`
232227 ] )
233228 ] )
234229 : Template . asString ( [
@@ -242,7 +237,8 @@ class WasmMainTemplatePlugin {
242237 ] ) ,
243238 "]).then(function(items) {" ,
244239 Template . indent ( [
245- "return WebAssembly.instantiate(items[0], {a:items[1]});"
240+ "return WebAssembly.instantiate(items[0], " +
241+ `{ ${ WebAssemblyUtils . MANGLED_MODULE } : items[1] });`
246242 ] ) ,
247243 "});"
248244 ] )
@@ -252,7 +248,8 @@ class WasmMainTemplatePlugin {
252248 "var bytesPromise = req.then(function(x) { return x.arrayBuffer(); });" ,
253249 "promise = bytesPromise.then(function(bytes) {" ,
254250 Template . indent ( [
255- "return WebAssembly.instantiate(bytes, {a:importObject});"
251+ "return WebAssembly.instantiate(bytes, " +
252+ `{ ${ WebAssemblyUtils . MANGLED_MODULE } : importObject });`
256253 ] ) ,
257254 "});"
258255 ] ) ,
0 commit comments