|
| 1 | +// Regenerates the two minimal wasm fixtures used by ../index.test.ts. |
| 2 | +// Run: `node src/generate.mjs`. Each module imports `<mod>.imp: () => i32` |
| 3 | +// and exports `exp: () => i32` whose body is `call imp`. |
| 4 | +import { writeFileSync } from 'node:fs'; |
| 5 | + |
| 6 | +const str = (s) => [s.length, ...[...s].map((c) => c.charCodeAt(0))]; |
| 7 | +const wasmImporting = (mod, name) => { |
| 8 | + const imp = [0x01, ...str(mod), ...str(name), 0x00, 0x00]; // import mod.name func t0 |
| 9 | + return Buffer.from([ |
| 10 | + 0x00, |
| 11 | + 0x61, |
| 12 | + 0x73, |
| 13 | + 0x6d, |
| 14 | + 0x01, |
| 15 | + 0x00, |
| 16 | + 0x00, |
| 17 | + 0x00, // magic + version |
| 18 | + 0x01, |
| 19 | + 0x05, |
| 20 | + 0x01, |
| 21 | + 0x60, |
| 22 | + 0x00, |
| 23 | + 0x01, |
| 24 | + 0x7f, // type: () -> i32 |
| 25 | + 0x02, |
| 26 | + imp.length, |
| 27 | + ...imp, // import section |
| 28 | + 0x03, |
| 29 | + 0x02, |
| 30 | + 0x01, |
| 31 | + 0x00, // func: exp uses t0 |
| 32 | + 0x07, |
| 33 | + 0x07, |
| 34 | + 0x01, |
| 35 | + 0x03, |
| 36 | + 0x65, |
| 37 | + 0x78, |
| 38 | + 0x70, |
| 39 | + 0x00, |
| 40 | + 0x01, // export "exp" func#1 |
| 41 | + 0x0a, |
| 42 | + 0x06, |
| 43 | + 0x01, |
| 44 | + 0x04, |
| 45 | + 0x00, |
| 46 | + 0x10, |
| 47 | + 0x00, |
| 48 | + 0x0b, // code: call 0; end |
| 49 | + ]); |
| 50 | +}; |
| 51 | + |
| 52 | +// Resolvable glue import (wasm-bindgen --target bundler shape). |
| 53 | +writeFileSync( |
| 54 | + new URL('./mod.wasm', import.meta.url), |
| 55 | + wasmImporting('./glue.js', 'imp'), |
| 56 | +); |
| 57 | +// Synthetic, non-resolvable import (emscripten/wasi shape). |
| 58 | +writeFileSync( |
| 59 | + new URL('./envmod.wasm', import.meta.url), |
| 60 | + wasmImporting('env', 'imp'), |
| 61 | +); |
0 commit comments