Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit d515644

Browse files
refactoring
1 parent 6f30694 commit d515644

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/transform/src/extractor.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ import { FunctionSignature } from "./types.js";
66

77
export class Extractor {
88
binaryen: typeof binaryen;
9+
module: binaryen.Module;
910
program: Program;
1011

11-
constructor(transform: Transform) {
12-
this.binaryen = transform.binaryen;
12+
constructor(transform: Transform, module: binaryen.Module) {
1313
this.program = transform.program;
14+
this.binaryen = transform.binaryen;
15+
this.module = module;
1416
}
1517

16-
getExportedFunctions(module: binaryen.Module): FunctionSignature[] {
18+
getExportedFunctions(): FunctionSignature[] {
1719
const functions = this.getAllFunctions();
18-
const paths = this.getExportedFunctionPaths(module);
20+
const paths = this.getExportedFunctionPaths();
1921

2022
const results = paths
2123
.map((path) => functions.get(path))
@@ -31,12 +33,18 @@ export class Extractor {
3133
return visitor.functions;
3234
}
3335

34-
private getExportedFunctionPaths(module: binaryen.Module): string[] {
36+
private getExportedFunctionPaths(): string[] {
3537
const paths = [];
36-
const n = module.getNumExports();
3738

38-
for (let i = 0; i < n; ++i) {
39-
const ref = module.getExportByIndex(i);
39+
const funcs = new Map<string, binaryen.FunctionInfo>();
40+
for (let i = 0; i < this.module.getNumFunctions(); ++i) {
41+
const ref = this.module.getFunctionByIndex(i);
42+
const info = this.binaryen.getFunctionInfo(ref);
43+
funcs.set(info.name, info);
44+
}
45+
46+
for (let i = 0; i < this.module.getNumExports(); ++i) {
47+
const ref = this.module.getExportByIndex(i);
4048
const info = this.binaryen.getExportInfo(ref);
4149

4250
if (info.kind !== binaryen.ExternalFunction) {
@@ -47,6 +55,11 @@ export class Extractor {
4755
continue;
4856
}
4957

58+
const f = funcs.get(info.value);
59+
if (f === undefined) {
60+
continue;
61+
}
62+
5063
paths.push(info.value.replace(/^export:/, ""));
5164
}
5265

src/transform/src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
import { Transform } from "assemblyscript/dist/transform.js";
12
import { HypermodeMetadata } from "./metadata.js";
23
import { Extractor } from "./extractor.js";
34
import writeLogo from "./logo.js";
45

5-
import { Transform } from "assemblyscript/dist/transform.js";
6-
import binaryen from "assemblyscript/lib/binaryen.js";
7-
86
export default class HypermodeTransform extends Transform {
9-
afterCompile(module: binaryen.Module) {
10-
const extractor = new Extractor(this);
11-
const functions = extractor.getExportedFunctions(module);
7+
afterCompile(module) {
8+
const extractor = new Extractor(this, module);
9+
const functions = extractor.getExportedFunctions();
1210

1311
const m = HypermodeMetadata.generate();
1412
m.addFunctions(functions);

0 commit comments

Comments
 (0)