* 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)
| 73 | * @returns {ParserState} the parser state |
| 74 | */ |
| 75 | parse(source, state) { |
| 76 | if (!Buffer.isBuffer(source)) { |
| 77 | throw new Error("WebAssemblyParser input must be a Buffer"); |
| 78 | } |
| 79 | |
| 80 | // flag it as ESM |
| 81 | /** @type {BuildInfo} */ |
| 82 | (state.module.buildInfo).strict = true; |
| 83 | /** @type {BuildMeta} */ |
| 84 | (state.module.buildMeta).exportsType = "namespace"; |
| 85 | |
| 86 | // parse it |
| 87 | const program = decode(source, decoderOpts); |
| 88 | const module = program.body[0]; |
| 89 | |
| 90 | const moduleContext = moduleContextFromModuleAST(module); |
| 91 | |
| 92 | // extract imports and exports |
| 93 | /** @type {string[]} */ |
| 94 | const exports = []; |
| 95 | const buildMeta = /** @type {SyncWasmModuleBuildMeta} */ ( |
| 96 | state.module.buildMeta |
| 97 | ); |
| 98 | /** @type {Record<string, string> | undefined} */ |
| 99 | let jsIncompatibleExports = (buildMeta.jsIncompatibleExports = undefined); |
| 100 | |
| 101 | /** @typedef {ModuleImport | null} ImportNode */ |
| 102 | /** @type {ImportNode[]} */ |
| 103 | const importedGlobals = []; |
| 104 | |
| 105 | t.traverse(module, { |
| 106 | ModuleExport({ node }) { |
| 107 | const descriptor = node.descr; |
| 108 | |
| 109 | if (descriptor.exportType === "Func") { |
| 110 | const funcIdx = descriptor.id.value; |
| 111 | |
| 112 | /** @type {t.FuncSignature} */ |
| 113 | const funcSignature = moduleContext.getFunction(funcIdx); |
| 114 | |
| 115 | const incompatibleType = |
| 116 | getJsIncompatibleTypeOfFuncSignature(funcSignature); |
| 117 | |
| 118 | if (incompatibleType) { |
| 119 | if (jsIncompatibleExports === undefined) { |
| 120 | jsIncompatibleExports = |
| 121 | /** @type {SyncWasmModuleBuildMeta} */ |
| 122 | (state.module.buildMeta).jsIncompatibleExports = {}; |
| 123 | } |
| 124 | jsIncompatibleExports[node.name] = incompatibleType; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | exports.push(node.name); |
| 129 | |
| 130 | if (node.descr && node.descr.exportType === "Global") { |
| 131 | const refNode = importedGlobals[node.descr.id.value]; |
| 132 | if (refNode) { |
nothing calls this directly
no test coverage detected