@@ -26,7 +26,6 @@ import {
26
26
NativeType ,
27
27
FunctionRef ,
28
28
ExpressionId ,
29
- FunctionTypeRef ,
30
29
GlobalRef ,
31
30
EventRef ,
32
31
FeatureFlags ,
@@ -46,7 +45,8 @@ import {
46
45
needsExplicitUnreachable ,
47
46
getLocalSetValue ,
48
47
getGlobalGetName ,
49
- isGlobalMutable
48
+ isGlobalMutable ,
49
+ createType
50
50
} from "./module" ;
51
51
52
52
import {
@@ -395,11 +395,8 @@ export class Compiler extends DiagnosticEmitter {
395
395
}
396
396
let funcRef = module . addFunction (
397
397
startFunctionInstance . internalName ,
398
- this . ensureFunctionType (
399
- signature . parameterTypes ,
400
- signature . returnType ,
401
- signature . thisType
402
- ) ,
398
+ signature . nativeParams ,
399
+ signature . nativeResults ,
403
400
typesToNativeTypes ( startFunctionInstance . additionalLocals ) ,
404
401
module . block ( null , startFunctionBody )
405
402
) ;
@@ -456,7 +453,7 @@ export class Compiler extends DiagnosticEmitter {
456
453
// set up function table
457
454
var functionTable = this . functionTable ;
458
455
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 ( ) ) ;
460
457
461
458
// import table if requested (default table is named '0' by Binaryen)
462
459
if ( options . importTable ) module . addTableImport ( "0" , "env" , "table" ) ;
@@ -628,7 +625,8 @@ export class Compiler extends DiagnosticEmitter {
628
625
if ( type . isManaged ) loadExpr = this . makeRetain ( loadExpr ) ;
629
626
module . addFunction (
630
627
name ,
631
- this . ensureFunctionType ( null , type , usizeType ) ,
628
+ usizeType . toNativeType ( ) ,
629
+ type . toNativeType ( ) ,
632
630
null ,
633
631
loadExpr
634
632
) ;
@@ -654,7 +652,8 @@ export class Compiler extends DiagnosticEmitter {
654
652
}
655
653
module . addFunction (
656
654
name ,
657
- this . ensureFunctionType ( [ type ] , Type . void , usizeType ) ,
655
+ createType ( [ usizeType . toNativeType ( ) , type . toNativeType ( ) ] ) ,
656
+ NativeType . None ,
658
657
null ,
659
658
module . store (
660
659
type . byteSize ,
@@ -759,6 +758,7 @@ export class Compiler extends DiagnosticEmitter {
759
758
760
759
// compile top-level statements within the file's start function
761
760
var startFunction = file . startFunction ;
761
+ var startSignature = startFunction . signature ;
762
762
var previousBody = this . currentBody ;
763
763
var startFunctionBody = new Array < ExpressionRef > ( ) ;
764
764
this . currentBody = startFunctionBody ;
@@ -781,9 +781,11 @@ export class Compiler extends DiagnosticEmitter {
781
781
let numLocals = locals . length ;
782
782
let varTypes = new Array < NativeType > ( numLocals ) ;
783
783
for ( let i = 0 ; i < numLocals ; ++ i ) varTypes [ i ] = locals [ i ] . type . toNativeType ( ) ;
784
+
784
785
module . addFunction (
785
786
startFunction . internalName ,
786
- this . ensureFunctionType ( startFunction . signature . parameterTypes , startFunction . signature . returnType ) ,
787
+ startSignature . nativeParams ,
788
+ startSignature . nativeResults ,
787
789
varTypes ,
788
790
startFunctionBody . length > 1
789
791
? module . block ( null , startFunctionBody )
@@ -1112,51 +1114,6 @@ export class Compiler extends DiagnosticEmitter {
1112
1114
return instance ;
1113
1115
}
1114
1116
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
-
1160
1117
/** Compiles the body of a function within the specified flow. */
1161
1118
compileFunctionBody (
1162
1119
/** Function to compile. */
@@ -1274,7 +1231,6 @@ export class Compiler extends DiagnosticEmitter {
1274
1231
var signature = instance . signature ;
1275
1232
var bodyNode = instance . prototype . bodyNode ;
1276
1233
1277
- var typeRef = this . ensureFunctionType ( signature . parameterTypes , signature . returnType , signature . thisType ) ;
1278
1234
var funcRef : FunctionRef ;
1279
1235
1280
1236
// concrete function
@@ -1336,7 +1292,8 @@ export class Compiler extends DiagnosticEmitter {
1336
1292
// create the function
1337
1293
funcRef = module . addFunction (
1338
1294
instance . internalName ,
1339
- typeRef ,
1295
+ signature . nativeParams ,
1296
+ signature . nativeResults ,
1340
1297
typesToNativeTypes ( instance . additionalLocals ) ,
1341
1298
flatten ( module , stmts , instance . signature . returnType . toNativeType ( ) )
1342
1299
) ;
@@ -1358,7 +1315,8 @@ export class Compiler extends DiagnosticEmitter {
1358
1315
instance . internalName ,
1359
1316
mangleImportName_moduleName ,
1360
1317
mangleImportName_elementName ,
1361
- typeRef
1318
+ signature . nativeParams ,
1319
+ signature . nativeResults
1362
1320
) ;
1363
1321
funcRef = module . getFunction ( instance . internalName ) ;
1364
1322
}
@@ -6404,11 +6362,8 @@ export class Compiler extends DiagnosticEmitter {
6404
6362
6405
6363
var funcRef = module . addFunction (
6406
6364
trampoline . internalName ,
6407
- this . ensureFunctionType (
6408
- trampolineSignature . parameterTypes ,
6409
- returnType ,
6410
- thisType
6411
- ) ,
6365
+ trampolineSignature . nativeParams ,
6366
+ trampolineSignature . nativeResults ,
6412
6367
typesToNativeTypes ( trampoline . additionalLocals ) ,
6413
6368
module . block ( null , stmts , returnType . toNativeType ( ) )
6414
6369
) ;
@@ -6435,7 +6390,8 @@ export class Compiler extends DiagnosticEmitter {
6435
6390
if ( ! this . argcSet ) {
6436
6391
let module = this . module ;
6437
6392
this . argcSet = module . addFunction ( BuiltinSymbols . setargc ,
6438
- this . ensureFunctionType ( [ Type . u32 ] , Type . void ) ,
6393
+ NativeType . I32 ,
6394
+ NativeType . None ,
6439
6395
null ,
6440
6396
module . global_set ( this . ensureArgcVar ( ) ,
6441
6397
module . local_get ( 0 , NativeType . I32 )
@@ -6906,7 +6862,6 @@ export class Compiler extends DiagnosticEmitter {
6906
6862
}
6907
6863
assert ( numOperands >= minOperands ) ;
6908
6864
6909
- this . ensureFunctionType ( signature . parameterTypes , signature . returnType , signature . thisType ) ;
6910
6865
var module = this . module ;
6911
6866
6912
6867
// fill up omitted arguments with zeroes
@@ -6931,7 +6886,8 @@ export class Compiler extends DiagnosticEmitter {
6931
6886
? module . unary ( UnaryOp . WrapI64 , indexArg )
6932
6887
: indexArg ,
6933
6888
operands ,
6934
- signature . toSignatureString ( )
6889
+ signature . nativeParams ,
6890
+ signature . nativeResults
6935
6891
)
6936
6892
] , returnType . toNativeType ( ) ) ;
6937
6893
this . currentType = returnType ;
@@ -7951,15 +7907,14 @@ export class Compiler extends DiagnosticEmitter {
7951
7907
this . currentFlow = previousFlow ;
7952
7908
7953
7909
// make the function
7954
- var typeRef = this . ensureFunctionType ( signature . parameterTypes , signature . returnType , signature . thisType ) ;
7955
7910
var locals = instance . localsByIndex ;
7956
7911
var varTypes = new Array < NativeType > ( ) ; // of temp. vars added while compiling initializers
7957
7912
var numOperands = 1 + signature . parameterTypes . length ;
7958
7913
var numLocals = locals . length ;
7959
7914
if ( numLocals > numOperands ) {
7960
7915
for ( let i = numOperands ; i < numLocals ; ++ i ) varTypes . push ( locals [ i ] . type . toNativeType ( ) ) ;
7961
7916
}
7962
- var funcRef = module . addFunction ( instance . internalName , typeRef , varTypes , body ) ;
7917
+ var funcRef = module . addFunction ( instance . internalName , signature . nativeParams , signature . nativeResults , varTypes , body ) ;
7963
7918
instance . finalize ( module , funcRef ) ;
7964
7919
return instance ;
7965
7920
}
0 commit comments