| 188 | } |
| 189 | |
| 190 | async function parseWasm(wasmFilePath: string): Promise<WasmInfo> { |
| 191 | try { |
| 192 | const wasmBinary = await fsp.readFile(wasmFilePath) |
| 193 | const wasmModule = await WebAssembly.compile(wasmBinary) |
| 194 | const importMap: Record<string, string[]> = Object.create(null) |
| 195 | for (const item of WebAssembly.Module.imports(wasmModule)) { |
| 196 | importMap[item.module] ??= [] |
| 197 | importMap[item.module].push(item.name) |
| 198 | } |
| 199 | const imports = Object.entries(importMap).map(([from, names]) => ({ |
| 200 | from, |
| 201 | names, |
| 202 | })) |
| 203 | |
| 204 | const exports = WebAssembly.Module.exports(wasmModule).map( |
| 205 | (item) => item.name, |
| 206 | ) |
| 207 | |
| 208 | return { imports, exports } |
| 209 | } catch (e) { |
| 210 | throw new Error( |
| 211 | `Failed to parse WASM file "${wasmFilePath}": ${(e as Error).message}`, |
| 212 | { cause: e }, |
| 213 | ) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | function generateGlueCode( |
| 218 | wasmInfo: WasmInfo, |