@@ -26,7 +26,6 @@ import {
2626 NativeType ,
2727 FunctionRef ,
2828 ExpressionId ,
29- FunctionTypeRef ,
3029 GlobalRef ,
3130 EventRef ,
3231 FeatureFlags ,
@@ -46,7 +45,8 @@ import {
4645 needsExplicitUnreachable ,
4746 getLocalSetValue ,
4847 getGlobalGetName ,
49- isGlobalMutable
48+ isGlobalMutable ,
49+ createType
5050} from "./module" ;
5151
5252import {
@@ -395,11 +395,8 @@ export class Compiler extends DiagnosticEmitter {
395395 }
396396 let funcRef = module . addFunction (
397397 startFunctionInstance . internalName ,
398- this . ensureFunctionType (
399- signature . parameterTypes ,
400- signature . returnType ,
401- signature . thisType
402- ) ,
398+ signature . nativeParams ,
399+ signature . nativeResults ,
403400 typesToNativeTypes ( startFunctionInstance . additionalLocals ) ,
404401 module . block ( null , startFunctionBody )
405402 ) ;
@@ -456,7 +453,7 @@ export class Compiler extends DiagnosticEmitter {
456453 // set up function table
457454 var functionTable = this . functionTable ;
458455 module . setFunctionTable ( functionTable . length , Module . UNLIMITED_TABLE , functionTable , module . i32 ( 0 ) ) ;
459- module . addFunction ( "null" , this . ensureFunctionType ( null , Type . void ) , null , module . unreachable ( ) ) ;
456+ module . addFunction ( "null" , NativeType . None , NativeType . None , null , module . unreachable ( ) ) ;
460457
461458 // import table if requested (default table is named '0' by Binaryen)
462459 if ( options . importTable ) module . addTableImport ( "0" , "env" , "table" ) ;
@@ -628,7 +625,8 @@ export class Compiler extends DiagnosticEmitter {
628625 if ( type . isManaged ) loadExpr = this . makeRetain ( loadExpr ) ;
629626 module . addFunction (
630627 name ,
631- this . ensureFunctionType ( null , type , usizeType ) ,
628+ usizeType . toNativeType ( ) ,
629+ type . toNativeType ( ) ,
632630 null ,
633631 loadExpr
634632 ) ;
@@ -654,7 +652,8 @@ export class Compiler extends DiagnosticEmitter {
654652 }
655653 module . addFunction (
656654 name ,
657- this . ensureFunctionType ( [ type ] , Type . void , usizeType ) ,
655+ createType ( [ usizeType . toNativeType ( ) , type . toNativeType ( ) ] ) ,
656+ NativeType . None ,
658657 null ,
659658 module . store (
660659 type . byteSize ,
@@ -759,6 +758,7 @@ export class Compiler extends DiagnosticEmitter {
759758
760759 // compile top-level statements within the file's start function
761760 var startFunction = file . startFunction ;
761+ var startSignature = startFunction . signature ;
762762 var previousBody = this . currentBody ;
763763 var startFunctionBody = new Array < ExpressionRef > ( ) ;
764764 this . currentBody = startFunctionBody ;
@@ -781,9 +781,11 @@ export class Compiler extends DiagnosticEmitter {
781781 let numLocals = locals . length ;
782782 let varTypes = new Array < NativeType > ( numLocals ) ;
783783 for ( let i = 0 ; i < numLocals ; ++ i ) varTypes [ i ] = locals [ i ] . type . toNativeType ( ) ;
784+
784785 module . addFunction (
785786 startFunction . internalName ,
786- this . ensureFunctionType ( startFunction . signature . parameterTypes , startFunction . signature . returnType ) ,
787+ startSignature . nativeParams ,
788+ startSignature . nativeResults ,
787789 varTypes ,
788790 startFunctionBody . length > 1
789791 ? module . block ( null , startFunctionBody )
@@ -1112,51 +1114,6 @@ export class Compiler extends DiagnosticEmitter {
11121114 return instance ;
11131115 }
11141116
1115- /** Either reuses or creates the function type matching the specified signature. */
1116- ensureFunctionType (
1117- parameterTypes : Type [ ] | null ,
1118- returnType : Type ,
1119- thisType : Type | null = null
1120- ) : FunctionTypeRef {
1121- var numParameters = parameterTypes ? parameterTypes . length : 0 ;
1122- var paramTypes : NativeType [ ] ;
1123- var index = 0 ;
1124- if ( thisType ) {
1125- paramTypes = new Array ( 1 + numParameters ) ;
1126- paramTypes [ 0 ] = thisType . toNativeType ( ) ;
1127- index = 1 ;
1128- } else {
1129- paramTypes = new Array ( numParameters ) ;
1130- }
1131- if ( parameterTypes ) {
1132- for ( let i = 0 ; i < numParameters ; ++ i , ++ index ) {
1133- paramTypes [ index ] = parameterTypes [ i ] . toNativeType ( ) ;
1134- }
1135- }
1136- var resultType = returnType . toNativeType ( ) ;
1137- var module = this . module ;
1138- var typeRef = module . getFunctionTypeBySignature ( resultType , paramTypes ) ;
1139- if ( ! typeRef ) {
1140- let name = Signature . makeSignatureString ( parameterTypes , returnType , thisType ) ;
1141- typeRef = module . addFunctionType ( name , resultType , paramTypes ) ;
1142- }
1143- return typeRef ;
1144- }
1145-
1146- /** Either reuses or creates the event type matching the specified name. */
1147- ensureEventType (
1148- name : string ,
1149- parameterTypes : Type [ ] | null
1150- ) : EventRef {
1151- var events = this . events ;
1152- if ( events . has ( name ) ) return events . get ( name ) ! ;
1153- var module = this . module ;
1154- var funcType = this . ensureFunctionType ( parameterTypes , Type . void ) ;
1155- var eventType = module . addEvent ( name , 0 , funcType ) ;
1156- events . set ( name , eventType ) ;
1157- return eventType ;
1158- }
1159-
11601117 /** Compiles the body of a function within the specified flow. */
11611118 compileFunctionBody (
11621119 /** Function to compile. */
@@ -1274,7 +1231,6 @@ export class Compiler extends DiagnosticEmitter {
12741231 var signature = instance . signature ;
12751232 var bodyNode = instance . prototype . bodyNode ;
12761233
1277- var typeRef = this . ensureFunctionType ( signature . parameterTypes , signature . returnType , signature . thisType ) ;
12781234 var funcRef : FunctionRef ;
12791235
12801236 // concrete function
@@ -1336,7 +1292,8 @@ export class Compiler extends DiagnosticEmitter {
13361292 // create the function
13371293 funcRef = module . addFunction (
13381294 instance . internalName ,
1339- typeRef ,
1295+ signature . nativeParams ,
1296+ signature . nativeResults ,
13401297 typesToNativeTypes ( instance . additionalLocals ) ,
13411298 flatten ( module , stmts , instance . signature . returnType . toNativeType ( ) )
13421299 ) ;
@@ -1358,7 +1315,8 @@ export class Compiler extends DiagnosticEmitter {
13581315 instance . internalName ,
13591316 mangleImportName_moduleName ,
13601317 mangleImportName_elementName ,
1361- typeRef
1318+ signature . nativeParams ,
1319+ signature . nativeResults
13621320 ) ;
13631321 funcRef = module . getFunction ( instance . internalName ) ;
13641322 }
@@ -6404,11 +6362,8 @@ export class Compiler extends DiagnosticEmitter {
64046362
64056363 var funcRef = module . addFunction (
64066364 trampoline . internalName ,
6407- this . ensureFunctionType (
6408- trampolineSignature . parameterTypes ,
6409- returnType ,
6410- thisType
6411- ) ,
6365+ trampolineSignature . nativeParams ,
6366+ trampolineSignature . nativeResults ,
64126367 typesToNativeTypes ( trampoline . additionalLocals ) ,
64136368 module . block ( null , stmts , returnType . toNativeType ( ) )
64146369 ) ;
@@ -6435,7 +6390,8 @@ export class Compiler extends DiagnosticEmitter {
64356390 if ( ! this . argcSet ) {
64366391 let module = this . module ;
64376392 this . argcSet = module . addFunction ( BuiltinSymbols . setargc ,
6438- this . ensureFunctionType ( [ Type . u32 ] , Type . void ) ,
6393+ NativeType . I32 ,
6394+ NativeType . None ,
64396395 null ,
64406396 module . global_set ( this . ensureArgcVar ( ) ,
64416397 module . local_get ( 0 , NativeType . I32 )
@@ -6906,7 +6862,6 @@ export class Compiler extends DiagnosticEmitter {
69066862 }
69076863 assert ( numOperands >= minOperands ) ;
69086864
6909- this . ensureFunctionType ( signature . parameterTypes , signature . returnType , signature . thisType ) ;
69106865 var module = this . module ;
69116866
69126867 // fill up omitted arguments with zeroes
@@ -6931,7 +6886,8 @@ export class Compiler extends DiagnosticEmitter {
69316886 ? module . unary ( UnaryOp . WrapI64 , indexArg )
69326887 : indexArg ,
69336888 operands ,
6934- signature . toSignatureString ( )
6889+ signature . nativeParams ,
6890+ signature . nativeResults
69356891 )
69366892 ] , returnType . toNativeType ( ) ) ;
69376893 this . currentType = returnType ;
@@ -7951,15 +7907,14 @@ export class Compiler extends DiagnosticEmitter {
79517907 this . currentFlow = previousFlow ;
79527908
79537909 // make the function
7954- var typeRef = this . ensureFunctionType ( signature . parameterTypes , signature . returnType , signature . thisType ) ;
79557910 var locals = instance . localsByIndex ;
79567911 var varTypes = new Array < NativeType > ( ) ; // of temp. vars added while compiling initializers
79577912 var numOperands = 1 + signature . parameterTypes . length ;
79587913 var numLocals = locals . length ;
79597914 if ( numLocals > numOperands ) {
79607915 for ( let i = numOperands ; i < numLocals ; ++ i ) varTypes . push ( locals [ i ] . type . toNativeType ( ) ) ;
79617916 }
7962- var funcRef = module . addFunction ( instance . internalName , typeRef , varTypes , body ) ;
7917+ var funcRef = module . addFunction ( instance . internalName , signature . nativeParams , signature . nativeResults , varTypes , body ) ;
79637918 instance . finalize ( module , funcRef ) ;
79647919 return instance ;
79657920 }
0 commit comments