@@ -6,16 +6,18 @@ import { FunctionSignature } from "./types.js";
66
77export 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 ( / ^ e x p o r t : / , "" ) ) ;
5164 }
5265
0 commit comments