@@ -149,45 +149,46 @@ class ClassEmitter(coreSpec: CoreSpec) {
149
149
150
150
implicit val noPos : Position = Position .NoPosition
151
151
152
- def build (loadJSClass : (WasmFunctionContext ) => Unit ): WasmFunctionName = {
153
- implicit val fctx = WasmFunctionContext (
152
+ def build (loadJSClass : (FunctionBuilder ) => Unit ): WasmFunctionName = {
153
+ val fb = new FunctionBuilder (
154
+ ctx.moduleBuilder,
154
155
genFunctionName.isJSClassInstance(clazz.className),
155
- List (" x" -> WasmRefType .anyref),
156
- List (WasmInt32 )
156
+ noPos
157
157
)
158
+ val xParam = fb.addParam(" x" , WasmRefType .anyref)
159
+ fb.setResultType(WasmInt32 )
160
+ fb.setFunctionType(ctx.isJSClassInstanceFuncTypeName)
158
161
159
- val List (xParam) = fctx.paramIndices
160
-
161
- import fctx .instrs
162
+ val instrs = fb
162
163
163
164
if (clazz.kind == ClassKind .JSClass && ! clazz.hasInstances) {
164
165
/* We need to constant-fold the instance test, to avoid trying to
165
166
* call $loadJSClass.className, since it will not exist at all.
166
167
*/
167
- fctx. instrs += I32_CONST (0 ) // false
168
+ instrs += I32_CONST (0 ) // false
168
169
} else {
169
170
instrs += LOCAL_GET (xParam)
170
- loadJSClass(fctx )
171
+ loadJSClass(fb )
171
172
instrs += CALL (genFunctionName.jsBinaryOps(IRTrees .JSBinaryOp .instanceof))
172
173
instrs += CALL (genFunctionName.unbox(IRTypes .BooleanRef ))
173
174
}
174
175
175
- val func = fctx.buildAndAddToContext(ctx.isJSClassInstanceFuncTypeName )
176
+ val func = fb.buildAndAddToModule( )
176
177
func.name
177
178
}
178
179
179
180
clazz.kind match {
180
181
case ClassKind .NativeJSClass =>
181
182
clazz.jsNativeLoadSpec.map { jsNativeLoadSpec =>
182
- build { fctx =>
183
- WasmExpressionBuilder .genLoadJSNativeLoadSpec(fctx , jsNativeLoadSpec)
183
+ build { fb =>
184
+ WasmExpressionBuilder .genLoadJSNativeLoadSpec(fb , jsNativeLoadSpec)
184
185
}
185
186
}
186
187
187
188
case ClassKind .JSClass =>
188
189
if (clazz.jsClassCaptures.isEmpty) {
189
- val funcName = build { fctx =>
190
- fctx.instrs += CALL (genFunctionName.loadJSClass(clazz.className))
190
+ val funcName = build { fb =>
191
+ fb += CALL (genFunctionName.loadJSClass(clazz.className))
191
192
}
192
193
Some (funcName)
193
194
} else {
@@ -430,17 +431,18 @@ class ClassEmitter(coreSpec: CoreSpec) {
430
431
431
432
val classInfo = ctx.getClassInfo(clazz.className)
432
433
433
- val fctx = WasmFunctionContext (
434
+ val fb = new FunctionBuilder (
435
+ ctx.moduleBuilder,
434
436
genFunctionName.instanceTest(clazz.name.name),
435
- List (" expr" -> WasmRefType .anyref),
436
- List (WasmInt32 )
437
+ pos
437
438
)
438
- val List (exprParam) = fctx.paramIndices
439
+ val exprParam = fb.addParam(" expr" , WasmRefType .anyref)
440
+ fb.setResultType(WasmInt32 )
439
441
440
- import fctx . instrs
442
+ val instrs = fb
441
443
442
- val itables = fctx .addLocal(" itables" , WasmRefType .nullable(genTypeName.itables))
443
- val exprNonNullLocal = fctx .addLocal(" exprNonNull" , WasmRefType .any)
444
+ val itables = fb .addLocal(" itables" , WasmRefType .nullable(genTypeName.itables))
445
+ val exprNonNullLocal = fb .addLocal(" exprNonNull" , WasmRefType .any)
444
446
445
447
val itableIdx = ctx.getItableIdx(classInfo)
446
448
instrs.block(WasmRefType .anyref) { testFail =>
@@ -511,7 +513,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
511
513
instrs += I32_CONST (0 ) // false
512
514
}
513
515
514
- fctx.buildAndAddToContext ()
516
+ fb.buildAndAddToModule ()
515
517
}
516
518
517
519
private def genNewDefaultFunc (clazz : LinkedClass )(implicit ctx : WasmContext ): Unit = {
@@ -522,13 +524,14 @@ class ClassEmitter(coreSpec: CoreSpec) {
522
524
assert(clazz.hasDirectInstances)
523
525
524
526
val structName = genTypeName.forClass(className)
525
- implicit val fctx = WasmFunctionContext (
527
+ val fb = new FunctionBuilder (
528
+ ctx.moduleBuilder,
526
529
genFunctionName.newDefault(className),
527
- Nil ,
528
- List (WasmRefType (structName))
530
+ pos
529
531
)
532
+ fb.setResultType(WasmRefType (structName))
530
533
531
- import fctx . instrs
534
+ val instrs = fb
532
535
533
536
instrs += GLOBAL_GET (genGlobalName.forVTable(className))
534
537
@@ -543,7 +546,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
543
546
}
544
547
instrs += STRUCT_NEW (structName)
545
548
546
- fctx.buildAndAddToContext ()
549
+ fb.buildAndAddToModule ()
547
550
}
548
551
549
552
/** Generate clone function for the given class, if it is concrete and implements the Cloneable
@@ -557,19 +560,21 @@ class ClassEmitter(coreSpec: CoreSpec) {
557
560
val className = clazz.className
558
561
val info = ctx.getClassInfo(className)
559
562
560
- val fctx = WasmFunctionContext (
563
+ val fb = new FunctionBuilder (
564
+ ctx.moduleBuilder,
561
565
genFunctionName.clone(className),
562
- List (" from" -> WasmRefType (genTypeName.ObjectStruct )),
563
- List (WasmRefType (genTypeName.ObjectStruct ))
566
+ pos
564
567
)
565
- val List (fromParam) = fctx.paramIndices
568
+ val fromParam = fb.addParam(" from" , WasmRefType (genTypeName.ObjectStruct ))
569
+ fb.setResultType(WasmRefType (genTypeName.ObjectStruct ))
570
+ fb.setFunctionType(ctx.cloneFunctionTypeName)
566
571
567
- import fctx . instrs
572
+ val instrs = fb
568
573
569
574
val heapType = WasmHeapType (genTypeName.forClass(className))
570
575
571
- val from = fctx.addSyntheticLocal( WasmRefType .nullable(heapType))
572
- val result = fctx.addSyntheticLocal( WasmRefType .nullable(heapType))
576
+ val from = fb.addLocal( " fromTyped " , WasmRefType .nullable(heapType))
577
+ val result = fb.addLocal( " result " , WasmRefType .nullable(heapType))
573
578
574
579
instrs += LOCAL_GET (fromParam)
575
580
instrs += REF_CAST (WasmRefType (heapType))
@@ -587,7 +592,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
587
592
instrs += LOCAL_GET (result)
588
593
instrs += REF_AS_NOT_NULL
589
594
590
- fctx.buildAndAddToContext(ctx.cloneFunctionTypeName )
595
+ fb.buildAndAddToModule( )
591
596
}
592
597
593
598
private def genLoadModuleFunc (clazz : LinkedClass )(implicit ctx : WasmContext ): Unit = {
@@ -608,15 +613,16 @@ class ClassEmitter(coreSpec: CoreSpec) {
608
613
609
614
val resultTyp = WasmRefType (typeName)
610
615
611
- implicit val fctx = WasmFunctionContext (
616
+ val fb = new FunctionBuilder (
617
+ ctx.moduleBuilder,
612
618
genFunctionName.loadModule(clazz.className),
613
- Nil ,
614
- List (resultTyp)
619
+ pos
615
620
)
621
+ fb.setResultType(resultTyp)
616
622
617
- val instanceLocal = fctx .addLocal(" instance" , resultTyp)
623
+ val instanceLocal = fb .addLocal(" instance" , resultTyp)
618
624
619
- import fctx . instrs
625
+ val instrs = fb
620
626
621
627
instrs.block(resultTyp) { nonNullLabel =>
622
628
// load global, return if not null
@@ -636,7 +642,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
636
642
instrs += LOCAL_GET (instanceLocal)
637
643
}
638
644
639
- fctx.buildAndAddToContext ()
645
+ fb.buildAndAddToModule ()
640
646
}
641
647
642
648
/** Generate global instance of the class itable. Their init value will be an array of null refs
@@ -1073,15 +1079,14 @@ class ClassEmitter(coreSpec: CoreSpec) {
1073
1079
)
1074
1080
ctx.addGlobal(cachedJSClassGlobal)
1075
1081
1076
- val fctx = WasmFunctionContext (
1077
- Some (clazz.className) ,
1082
+ val fb = new FunctionBuilder (
1083
+ ctx.moduleBuilder ,
1078
1084
genFunctionName.loadJSClass(clazz.className),
1079
- None ,
1080
- Nil ,
1081
- List (WasmRefType .any)
1085
+ pos
1082
1086
)
1087
+ fb.setResultType(WasmRefType .any)
1083
1088
1084
- import fctx . instrs
1089
+ val instrs = fb
1085
1090
1086
1091
instrs.block(WasmRefType .any) { doneLabel =>
1087
1092
// Load cached JS class, return if non-null
@@ -1091,7 +1096,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
1091
1096
instrs += CALL (genFunctionName.createJSClassOf(clazz.className))
1092
1097
}
1093
1098
1094
- fctx.buildAndAddToContext ()
1099
+ fb.buildAndAddToModule ()
1095
1100
}
1096
1101
1097
1102
private def genLoadJSModuleFunction (clazz : LinkedClass )(implicit ctx : WasmContext ): Unit = {
@@ -1109,13 +1114,14 @@ class ClassEmitter(coreSpec: CoreSpec) {
1109
1114
)
1110
1115
)
1111
1116
1112
- val fctx = WasmFunctionContext (
1117
+ val fb = new FunctionBuilder (
1118
+ ctx.moduleBuilder,
1113
1119
genFunctionName.loadModule(className),
1114
- Nil ,
1115
- List (WasmRefType .anyref)
1120
+ pos
1116
1121
)
1122
+ fb.setResultType(WasmRefType .anyref)
1117
1123
1118
- import fctx . instrs
1124
+ val instrs = fb
1119
1125
1120
1126
instrs.block(WasmRefType .anyref) { doneLabel =>
1121
1127
// Load cached instance; return if non-null
@@ -1132,7 +1138,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
1132
1138
instrs += GLOBAL_GET (cacheGlobalName)
1133
1139
}
1134
1140
1135
- fctx.buildAndAddToContext ()
1141
+ fb.buildAndAddToModule ()
1136
1142
}
1137
1143
1138
1144
private def transformTopLevelMethodExportDef (
@@ -1246,20 +1252,22 @@ class ClassEmitter(coreSpec: CoreSpec) {
1246
1252
* at least one table.
1247
1253
*/
1248
1254
1249
- implicit val fctx = WasmFunctionContext (
1250
- Some (className) ,
1255
+ val fb = new FunctionBuilder (
1256
+ ctx.moduleBuilder ,
1251
1257
genFunctionName.forTableEntry(className, methodName),
1252
- Some (WasmRefType .any),
1253
- method.args,
1254
- method.resultType
1258
+ pos
1255
1259
)
1260
+ val receiverParam = fb.addParam(" <this>" , WasmRefType .any)
1261
+ val argParams = method.args.map { arg =>
1262
+ fb.addParam(arg.name.name.nameString, TypeTransformer .transformType(arg.ptpe))
1263
+ }
1264
+ fb.setResultTypes(TypeTransformer .transformResultType(method.resultType))
1265
+ fb.setFunctionType(ctx.tableFunctionType(methodName))
1256
1266
1257
- import fctx .instrs
1258
-
1259
- val receiverLocal :: paramLocals = fctx.paramIndices: @ unchecked
1267
+ val instrs = fb
1260
1268
1261
1269
// Load and cast down the receiver
1262
- instrs += LOCAL_GET (receiverLocal )
1270
+ instrs += LOCAL_GET (receiverParam )
1263
1271
receiverTyp match {
1264
1272
case Some (Types .WasmRefType (_, WasmHeapType .Any )) =>
1265
1273
() // no cast necessary
@@ -1270,13 +1278,13 @@ class ClassEmitter(coreSpec: CoreSpec) {
1270
1278
}
1271
1279
1272
1280
// Load the other parameters
1273
- for (paramLocal <- paramLocals )
1274
- instrs += LOCAL_GET (paramLocal )
1281
+ for (argParam <- argParams )
1282
+ instrs += LOCAL_GET (argParam )
1275
1283
1276
1284
// Call the statically resolved method
1277
1285
instrs += RETURN_CALL (functionName)
1278
1286
1279
- fctx.buildAndAddToContext(useFunctionTypeInMainRecType = true )
1287
+ fb.buildAndAddToModule( )
1280
1288
}
1281
1289
}
1282
1290
0 commit comments