| 15 | const PAGE_SIZE = 65536; |
| 16 | |
| 17 | const getBinaryHeaderData = (wasmBytes) => { |
| 18 | const magicBytes = new Uint32Array(new Uint8Array(wasmBytes.subarray(0, 24)).buffer); |
| 19 | if (magicBytes[0] !== 0x6d736100) { |
| 20 | console.error("Wasm magic number is missing!"); |
| 21 | } |
| 22 | if (wasmBytes[8] !== 0) { |
| 23 | log("Dylink section wasn't found in wasm binary, assuming static wasm."); |
| 24 | return "static"; |
| 25 | } |
| 26 | |
| 27 | let next = 9; |
| 28 | function getLEB() { |
| 29 | let returnValue = 0; |
| 30 | let mul = 1; |
| 31 | while (1) { |
| 32 | const byte = wasmBytes[next++]; |
| 33 | returnValue += (byte & 0x7f) * mul; |
| 34 | mul *= 0x80; |
| 35 | if (!(byte & 0x80)) break; |
| 36 | } |
| 37 | return returnValue; |
| 38 | } |
| 39 | const sectionSize = getLEB(); |
| 40 | // 6, size of "dylink" string = 7 |
| 41 | next += 7; |
| 42 | const memorySize = getLEB(); |
| 43 | const memoryAlign = getLEB(); |
| 44 | const tableSize = getLEB(); |
| 45 | const tableAlign = getLEB(); |
| 46 | const neededDynlibsCount = getLEB(); |
| 47 | return { sectionSize, memorySize, memoryAlign, neededDynlibsCount, tableSize, tableAlign }; |
| 48 | }; |
| 49 | |
| 50 | export default async function ({ withPlugins = [] }) { |
| 51 | const fileBuffer = await readFilePromise(wasmDylibPath); |