Skip to content

Commit 1d212df

Browse files
Merge pull request #411 from xtuc/fix-module-import-id
fix: module imports should not use type index as their function identifier
2 parents 0d45ee5 + d32a233 commit 1d212df

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

packages/wasm-edit/test/ast-sync.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,19 @@ function removeNodesOfType(t) {
6161
};
6262
}
6363

64+
function makeTypeNode() {
65+
return t.typeInstruction(undefined, t.signature([], []));
66+
}
67+
6468
function makeFuncNodes(i, params = [], results = [], body = []) {
6569
body.push(t.instruction("nop"));
6670

67-
const id = t.identifier(getUniqueName("func"));
71+
const id = t.identifier(`func_${i}`);
6872
const func = t.func(id, t.signature(params, results), body);
6973

70-
const functype = t.typeInstruction(undefined, t.signature(params, results));
71-
7274
const funcindex = t.indexInFuncSection(i);
7375

74-
return [func, functype, funcindex];
76+
return [func, funcindex];
7577
}
7678

7779
function makeFuncExportNode(i) {
@@ -127,16 +129,17 @@ describe("AST synchronization", () => {
127129
b => addWithAST(ast, b, [makeGlobalNode(10)]),
128130
b => editWithAST(ast, b, removeNodesOfType("TypeInstruction")),
129131

130-
b => addWithAST(ast, b, makeFuncNodes(0)),
132+
b => addWithAST(ast, b, [makeTypeNode()]),
133+
134+
b => addWithAST(ast, b, [makeFuncImportNode()]),
135+
b => editWithAST(ast, b, renameImports("c")),
136+
137+
b => addWithAST(ast, b, makeFuncNodes(1)),
131138
b => addWithAST(ast, b, [makeFuncExportNode(0)]),
132139

133140
b => addWithAST(ast, b, [makeGlobalImportNode()]),
134141
b => editWithAST(ast, b, renameImports("a")),
135-
b => editWithAST(ast, b, renameImports("b")),
136-
137-
b => addWithAST(ast, b, [makeFuncImportNode()]),
138-
139-
b => editWithAST(ast, b, renameImports("c"))
142+
b => editWithAST(ast, b, renameImports("b"))
140143
];
141144

142145
it("should run steps", function() {

packages/wasm-parser/src/decoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program {
450450
throw new CompileError(`function signature not found (${typeindex})`);
451451
}
452452

453-
const id = t.numberLiteralFromRaw(typeindex);
453+
const id = getUniqueName("func");
454454

455455
importDescr = t.funcImportDescr(
456456
id,

packages/wasm-parser/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function restoreFunctionNames(ast) {
5959
ModuleImport({ node }: NodePath<ModuleImport>) {
6060
if (node.descr.type === "FuncImportDescr") {
6161
// $FlowIgnore
62-
const nodeName: NumberLiteral = node.descr.id;
63-
const index = nodeName.value;
62+
const indexBasedFunctionName: string = node.descr.id;
63+
const index = Number(indexBasedFunctionName.replace("func_", ""));
6464
const functionName = functionNames.find(f => f.index === index);
6565

6666
if (functionName) {
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(type (func (result i32)))
3+
(type (func (param i32) (result i32)))
4+
(import "a" "c" (func $fff (param i32) (result i32)))
5+
)

packages/wast-parser/src/grammar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,6 @@ export function parse(tokensList: Array<Object>, source: string): Program {
405405
}
406406

407407
const name = token.value;
408-
409-
let fnName = t.identifier(`${moduleName}.${name}`);
410408
eatToken();
411409

412410
eatTokenOfType(tokens.openParen);
@@ -419,6 +417,8 @@ export function parse(tokensList: Array<Object>, source: string): Program {
419417
const fnParams = [];
420418
const fnResult = [];
421419

420+
let fnName = t.identifier(getUniqueName("func"));
421+
422422
if (token.type === tokens.identifier) {
423423
fnName = identifierFromToken(token);
424424
eatToken();

0 commit comments

Comments
 (0)