Skip to content

Commit d7d82b2

Browse files
committed
refactor : use AST utils
1 parent 426e625 commit d7d82b2

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

declarations.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ declare module "@webassemblyjs/ast" {
6262
export class ModuleExport extends Node {
6363
name: string;
6464
}
65+
export class ModuleExportDescr extends Node {}
6566
export class IndexLiteral extends Node {}
6667
export class NumberLiteral extends Node {}
6768
export class Global extends Node {}
@@ -78,7 +79,7 @@ declare module "@webassemblyjs/ast" {
7879
params: FuncParam[];
7980
results: string[];
8081
}
81-
export class TypeInstructionFunc extends Node {}
82+
export class TypeInstruction extends Node {}
8283
export class IndexInFuncSection extends Node {}
8384
export function indexLiteral(index: number): IndexLiteral;
8485
export function numberLiteral(num: number): NumberLiteral;
@@ -92,13 +93,17 @@ declare module "@webassemblyjs/ast" {
9293
type: string,
9394
init: Node[]
9495
): ObjectInstruction;
95-
export function func(initFuncId, funcParams, funcResults, funcBody): Func;
96-
export function typeInstructionFunc(params: FuncParam[], results: string[]): TypeInstructionFunc;
96+
export function signature(params: FuncParam[], results: string[]): Signature;
97+
export function func(initFuncId, Signature, funcBody): Func;
98+
export function typeInstruction(id: Identifier, functype: Signature): TypeInstruction;
9799
export function indexInFuncSection(index: IndexLiteral): IndexInFuncSection;
98100
export function moduleExport(
99101
identifier: string,
102+
descr: ModuleExportDescr
103+
): ModuleExport;
104+
export function moduleExportDescr(
100105
type: string,
101-
index: IndexLiteral
106+
index: ModuleExportDescr
102107
): ModuleExport;
103108

104109
export function getSectionMetadata(ast: any, section: string);

lib/wasm/WebAssemblyGenerator.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,6 @@ const rewriteExportNames = ({ ast, module }) => bin => {
202202
const usedName = module.isUsed(path.node.name);
203203
if (usedName) {
204204
path.node.name = usedName;
205-
// TODO remove this when fixed in @webassemblyjs
206-
path.node.descr.id = t.numberLiteral(+path.node.descr.id.raw);
207205
} else {
208206
path.remove();
209207
}
@@ -229,10 +227,6 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
229227
} else {
230228
path.node.module = WebAssemblyUtils.MANGLED_MODULE;
231229
path.node.name = result.name;
232-
if (path.node.descr.id)
233-
path.node.descr.id = t.numberLiteral(+path.node.descr.id.raw);
234-
if (path.node.descr.name)
235-
path.node.descr.name = t.numberLiteral(+path.node.descr.name.raw);
236230
}
237231
}
238232
});
@@ -284,19 +278,20 @@ const addInitFunction = ({
284278
const funcResults = [];
285279

286280
// Code section
287-
const func = t.func(initFuncId, funcParams, funcResults, funcBody);
281+
const funcSignature = t.signature(funcParams, funcResults);
282+
const func = t.func(initFuncId, funcSignature, funcBody);
288283

289284
// Type section
290-
const functype = t.typeInstructionFunc(
291-
func.signature.params,
292-
func.signature.results
293-
);
285+
const functype = t.typeInstruction(undefined, funcSignature);
294286

295287
// Func section
296288
const funcindex = t.indexInFuncSection(nextTypeIndex);
297289

298290
// Export section
299-
const moduleExport = t.moduleExport(initFuncId.value, "Func", nextFuncIndex);
291+
const moduleExport = t.moduleExport(
292+
initFuncId.value,
293+
t.moduleExportDescr("Func", nextFuncIndex)
294+
);
300295

301296
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
302297
};
@@ -330,7 +325,8 @@ class WebAssemblyGenerator extends Generator {
330325

331326
const ast = decode(bin, {
332327
ignoreDataSection: true,
333-
ignoreCodeSection: true
328+
ignoreCodeSection: true,
329+
ignoreCustomNameSection: true
334330
});
335331

336332
const importedGlobals = getImportedGlobals(ast);

0 commit comments

Comments
 (0)