@@ -23,7 +23,9 @@ function containingModule(func) {
2323 return container ;
2424}
2525
26- function functionTypeDescriptor ( func ) {
26+ function getFunctionTypeDescriptor ( func ) {
27+ // TODO: Generics?
28+ func = func . instances . get ( "" ) ;
2729 return {
2830 returnType : typeName ( func . declaration . signature . returnType ) ,
2931 parameters : func . declaration . signature . parameters . map ( parameter =>
@@ -32,19 +34,29 @@ function functionTypeDescriptor(func) {
3234 } ;
3335}
3436
35- const AS_BIND_SRC = "lib/assembly/as-bind.ts" ;
37+ function extractTypeMap ( func ) {
38+ // TODO: Generics?
39+ func = func . instances . get ( "" ) ;
40+ const result = {
41+ [ typeName (
42+ func . declaration . signature . returnType
43+ ) ] : func . signature . returnType ?. getClass ?. ( ) ?. id
44+ } ;
45+ func . declaration . signature . parameters . forEach ( ( parameter , i ) => {
46+ result [ typeName ( parameter . type ) ] = func . signature . parameterTypes [
47+ i
48+ ] . getClass ?. ( ) ?. id ;
49+ } ) ;
50+ return result ;
51+ }
52+
3653const SECTION_NAME = "as-bind_bindings" ;
3754
3855class AsBindTransform extends Transform {
39- afterParse ( parser ) {
40- const bindSrc = fs . readFileSync (
41- require . resolve ( "./" + AS_BIND_SRC ) ,
42- "utf8"
43- ) ;
44- parser . parseFile ( bindSrc , "~as-bind/" + AS_BIND_SRC , true ) ;
45- }
46- afterInitialize ( program ) {
47- const flatExportedFunctions = [ ...program . elementsByDeclaration . values ( ) ]
56+ afterCompile ( module ) {
57+ const flatExportedFunctions = [
58+ ...this . program . elementsByDeclaration . values ( )
59+ ]
4860 . filter ( el =>
4961 elementHasFlag ( el , assemblyscript . CommonFlags . MODULE_EXPORT )
5062 )
@@ -53,13 +65,18 @@ class AsBindTransform extends Transform {
5365 el =>
5466 el . declaration . kind === assemblyscript . NodeKind . FUNCTIONDECLARATION
5567 ) ;
56- const flatImportedFunctions = [ ...program . elementsByDeclaration . values ( ) ]
68+ const flatImportedFunctions = [
69+ ...this . program . elementsByDeclaration . values ( )
70+ ]
5771 . filter ( el => elementHasFlag ( el , assemblyscript . CommonFlags . DECLARE ) )
5872 . filter ( el => ! isInternalElement ( el ) )
5973 . filter (
6074 v => v . declaration . kind === assemblyscript . NodeKind . FUNCTIONDECLARATION
6175 ) ;
76+
77+ const typeIds = { } ;
6278 const importedFunctions = { } ;
79+ debugger ;
6380 for ( const importedFunction of flatImportedFunctions ) {
6481 // To know under what module name an imported function will be expected,
6582 // we have to find the containing module of the given function, take the
@@ -74,17 +91,22 @@ class AsBindTransform extends Transform {
7491 }
7592 importedFunctions [ moduleName ] [
7693 importedFunction . name
77- ] = functionTypeDescriptor ( importedFunction ) ;
94+ ] = getFunctionTypeDescriptor ( importedFunction ) ;
95+ Object . assign ( typeIds , extractTypeMap ( importedFunction ) ) ;
7896 }
7997 const exportedFunctions = { } ;
8098 for ( const exportedFunction of flatExportedFunctions ) {
81- exportedFunctions [ exportedFunction . name ] = functionTypeDescriptor (
99+ exportedFunctions [ exportedFunction . name ] = getFunctionTypeDescriptor (
82100 exportedFunction
83101 ) ;
102+ Object . assign ( typeIds , extractTypeMap ( exportedFunction ) ) ;
84103 }
85- this . typeData = JSON . stringify ( { importedFunctions, exportedFunctions } ) ;
86- }
87- afterCompile ( module ) {
104+ this . typeData = JSON . stringify ( {
105+ typeIds,
106+ importedFunctions,
107+ exportedFunctions
108+ } ) ;
109+
88110 module . addCustomSection (
89111 SECTION_NAME ,
90112 new TextEncoder ( "utf8" ) . encode ( this . typeData )
0 commit comments