* Parses the provided source and updates the parser state. * @param {string | Buffer | PreparsedAst} source the source to parse * @param {ParserState} state the parser state * @returns {ParserState} the parser state
(source, state)
| 37 | * @returns {ParserState} the parser state |
| 38 | */ |
| 39 | parse(source, state) { |
| 40 | if (!Buffer.isBuffer(source)) { |
| 41 | throw new Error("WebAssemblyParser input must be a Buffer"); |
| 42 | } |
| 43 | |
| 44 | const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta); |
| 45 | buildMeta.exportsType = "namespace"; |
| 46 | buildMeta.async = true; |
| 47 | |
| 48 | EnvironmentNotSupportAsyncWarning.check( |
| 49 | state.module, |
| 50 | state.compilation.runtimeTemplate, |
| 51 | "asyncWebAssembly" |
| 52 | ); |
| 53 | |
| 54 | // flag it as async module |
| 55 | const buildInfo = /** @type {BuildInfo} */ (state.module.buildInfo); |
| 56 | |
| 57 | buildInfo.strict = true; |
| 58 | |
| 59 | if (/** @type {AsyncWasmModule} */ (state.module).phase === "source") { |
| 60 | // For source phase, only validate magic header |
| 61 | if (source.length < 4 || !source.subarray(0, 4).equals(WASM_HEADER)) { |
| 62 | throw new Error( |
| 63 | "Source phase imports require valid WebAssembly modules. Invalid magic header (expected \\0asm)." |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | // Source phase exports the WebAssembly.Module as default |
| 68 | state.module.addDependency( |
| 69 | new StaticExportsDependency(["default"], false) |
| 70 | ); |
| 71 | |
| 72 | // Skip full parsing - no exports/imports needed for source phase |
| 73 | return state; |
| 74 | } |
| 75 | |
| 76 | // parse it |
| 77 | const program = decode(source, decoderOpts); |
| 78 | const module = program.body[0]; |
| 79 | /** @type {string[]} */ |
| 80 | const exports = []; |
| 81 | |
| 82 | t.traverse(module, { |
| 83 | ModuleExport({ node }) { |
| 84 | exports.push(node.name); |
| 85 | }, |
| 86 | |
| 87 | ModuleImport({ node }) { |
| 88 | const dep = new WebAssemblyImportDependency( |
| 89 | node.module, |
| 90 | node.name, |
| 91 | node.descr, |
| 92 | false |
| 93 | ); |
| 94 | |
| 95 | state.module.addDependency(dep); |
| 96 | } |
nothing calls this directly
no test coverage detected