diff --git a/packages/wasm-edit/test/ast-sync.js b/packages/wasm-edit/test/ast-sync.js index 58be23714..0c57487f1 100644 --- a/packages/wasm-edit/test/ast-sync.js +++ b/packages/wasm-edit/test/ast-sync.js @@ -61,17 +61,19 @@ function removeNodesOfType(t) { }; } +function makeTypeNode() { + return t.typeInstruction(undefined, t.signature([], [])); +} + function makeFuncNodes(i, params = [], results = [], body = []) { body.push(t.instruction("nop")); - const id = t.identifier(getUniqueName("func")); + const id = t.identifier(`func_${i}`); const func = t.func(id, t.signature(params, results), body); - const functype = t.typeInstruction(undefined, t.signature(params, results)); - const funcindex = t.indexInFuncSection(i); - return [func, functype, funcindex]; + return [func, funcindex]; } function makeFuncExportNode(i) { @@ -127,16 +129,17 @@ describe("AST synchronization", () => { b => addWithAST(ast, b, [makeGlobalNode(10)]), b => editWithAST(ast, b, removeNodesOfType("TypeInstruction")), - b => addWithAST(ast, b, makeFuncNodes(0)), + b => addWithAST(ast, b, [makeTypeNode()]), + + b => addWithAST(ast, b, [makeFuncImportNode()]), + b => editWithAST(ast, b, renameImports("c")), + + b => addWithAST(ast, b, makeFuncNodes(1)), b => addWithAST(ast, b, [makeFuncExportNode(0)]), b => addWithAST(ast, b, [makeGlobalImportNode()]), b => editWithAST(ast, b, renameImports("a")), - b => editWithAST(ast, b, renameImports("b")), - - b => addWithAST(ast, b, [makeFuncImportNode()]), - - b => editWithAST(ast, b, renameImports("c")) + b => editWithAST(ast, b, renameImports("b")) ]; it("should run steps", function() { diff --git a/packages/wasm-parser/src/decoder.js b/packages/wasm-parser/src/decoder.js index 11e6e044c..f0e69379d 100644 --- a/packages/wasm-parser/src/decoder.js +++ b/packages/wasm-parser/src/decoder.js @@ -432,7 +432,7 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program { throw new CompileError(`function signature not found (${typeindex})`); } - const id = t.numberLiteralFromRaw(typeindex); + const id = getUniqueName("func"); importDescr = t.funcImportDescr( id, diff --git a/packages/wasm-parser/src/index.js b/packages/wasm-parser/src/index.js index ba999fcfd..cecde2823 100644 --- a/packages/wasm-parser/src/index.js +++ b/packages/wasm-parser/src/index.js @@ -59,8 +59,8 @@ function restoreFunctionNames(ast) { ModuleImport({ node }: NodePath) { if (node.descr.type === "FuncImportDescr") { // $FlowIgnore - const nodeName: NumberLiteral = node.descr.id; - const index = nodeName.value; + const indexBasedFunctionName: string = node.descr.id; + const index = Number(indexBasedFunctionName.replace("func_", "")); const functionName = functionNames.find(f => f.index === index); if (functionName) { diff --git a/packages/wasm-parser/test/fixtures/import/actual.wat b/packages/wasm-parser/test/fixtures/import/basic/actual.wat similarity index 100% rename from packages/wasm-parser/test/fixtures/import/actual.wat rename to packages/wasm-parser/test/fixtures/import/basic/actual.wat diff --git a/packages/wasm-parser/test/fixtures/import/complex/actual.wat b/packages/wasm-parser/test/fixtures/import/complex/actual.wat new file mode 100644 index 000000000..092ab5dd9 --- /dev/null +++ b/packages/wasm-parser/test/fixtures/import/complex/actual.wat @@ -0,0 +1,5 @@ +(module + (type (func (result i32))) + (type (func (param i32) (result i32))) + (import "a" "c" (func $fff (param i32) (result i32))) +) diff --git a/packages/wast-parser/src/grammar.js b/packages/wast-parser/src/grammar.js index 48a8eb81e..6b45bd2b7 100644 --- a/packages/wast-parser/src/grammar.js +++ b/packages/wast-parser/src/grammar.js @@ -405,8 +405,6 @@ export function parse(tokensList: Array, source: string): Program { } const name = token.value; - - let fnName = t.identifier(`${moduleName}.${name}`); eatToken(); eatTokenOfType(tokens.openParen); @@ -419,6 +417,8 @@ export function parse(tokensList: Array, source: string): Program { const fnParams = []; const fnResult = []; + let fnName = t.identifier(getUniqueName("func")); + if (token.type === tokens.identifier) { fnName = identifierFromToken(token); eatToken();