( chunkGraph, module, mangle, declarations, runtime )
| 59 | * @returns {string} source code |
| 60 | */ |
| 61 | const generateImportObject = ( |
| 62 | chunkGraph, |
| 63 | module, |
| 64 | mangle, |
| 65 | declarations, |
| 66 | runtime |
| 67 | ) => { |
| 68 | const moduleGraph = chunkGraph.moduleGraph; |
| 69 | /** @type {Map<string, ModuleId>} */ |
| 70 | const waitForInstances = new Map(); |
| 71 | /** @type {{ module: string, name: string, value: string }[]} */ |
| 72 | const properties = []; |
| 73 | const usedWasmDependencies = WebAssemblyUtils.getUsedDependencies( |
| 74 | moduleGraph, |
| 75 | module, |
| 76 | mangle |
| 77 | ); |
| 78 | for (const usedDep of usedWasmDependencies) { |
| 79 | const dep = usedDep.dependency; |
| 80 | const importedModule = moduleGraph.getModule(dep); |
| 81 | const exportName = dep.name; |
| 82 | const usedName = |
| 83 | importedModule && |
| 84 | moduleGraph |
| 85 | .getExportsInfo(importedModule) |
| 86 | .getUsedName(exportName, runtime); |
| 87 | const description = dep.description; |
| 88 | const direct = dep.onlyDirectImport; |
| 89 | |
| 90 | const module = usedDep.module; |
| 91 | const name = usedDep.name; |
| 92 | |
| 93 | if (direct) { |
| 94 | const instanceVar = `m${waitForInstances.size}`; |
| 95 | waitForInstances.set( |
| 96 | instanceVar, |
| 97 | /** @type {ModuleId} */ |
| 98 | (chunkGraph.getModuleId(/** @type {Module} */ (importedModule))) |
| 99 | ); |
| 100 | properties.push({ |
| 101 | module, |
| 102 | name, |
| 103 | value: `${instanceVar}[${JSON.stringify(usedName)}]` |
| 104 | }); |
| 105 | } else { |
| 106 | const params = |
| 107 | /** @type {Signature} */ |
| 108 | (description.signature).params.map( |
| 109 | (param, k) => `p${k}${param.valtype}` |
| 110 | ); |
| 111 | |
| 112 | const mod = `${RuntimeGlobals.moduleCache}[${JSON.stringify( |
| 113 | chunkGraph.getModuleId(/** @type {Module} */ (importedModule)) |
| 114 | )}]`; |
| 115 | const modExports = `${mod}.exports`; |
| 116 | |
| 117 | const cache = `wasmImportedFuncCache${declarations.length}`; |
| 118 | declarations.push(`var ${cache};`); |
no test coverage detected