Skip to content

Commit ffd3e3c

Browse files
committed
Address comment in the emitters.
1 parent 9510202 commit ffd3e3c

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/FunctionEmitter.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,11 +2352,14 @@ private[emitter] class FunctionEmitter(sjsGen: SJSGen) {
23522352
case ApplyTypedClosure(_, fun, args) =>
23532353
val newFun = transformExprNoChar(checkNotNull(fun))
23542354
val newArgs = fun.tpe match {
2355-
case ClosureType(paramTypes, resultType, _) =>
2355+
case ClosureType(paramTypes, _, _) =>
23562356
for ((arg, paramType) <- args.zip(paramTypes)) yield
23572357
transformExpr(arg, paramType)
2358-
case _ =>
2358+
case NothingType | NullType =>
23592359
args.map(transformExpr(_, preserveChar = true))
2360+
case _ =>
2361+
throw new AssertionError(
2362+
s"Unexpected type for the fun of ApplyTypedClosure: ${fun.tpe}")
23602363
}
23612364
js.Apply.makeProtected(newFun, newArgs)
23622365

linker/shared/src/main/scala/org/scalajs/linker/backend/wasmemitter/FunctionEmitter.scala

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,21 @@ object FunctionEmitter {
7474
functionID: wanme.FunctionID,
7575
originalName: OriginalName,
7676
funTypeID: wanme.TypeID,
77-
enclosingClassName: Option[ClassName],
78-
captureParamDefs: Option[List[ParamDef]],
79-
receiverType: Option[watpe.Type],
77+
captureParamDefs: List[ParamDef],
8078
paramDefs: List[ParamDef],
81-
restParam: Option[ParamDef],
8279
body: Tree,
8380
resultType: Type
8481
)(implicit ctx: WasmContext, pos: Position): Unit = {
8582
val emitter = prepareEmitter(
8683
functionID,
8784
originalName,
88-
enclosingClassName,
89-
captureParamDefs,
85+
enclosingClassName = None,
86+
Some(captureParamDefs),
9087
captureDataAsRefStruct = true,
9188
preSuperVarDefs = None,
9289
hasNewTarget = false,
93-
receiverType,
94-
paramDefs ::: restParam.toList,
90+
receiverType = None,
91+
paramDefs,
9592
transformResultType(resultType)
9693
)
9794
emitter.fb.setFunctionType(funTypeID)
@@ -1309,11 +1306,10 @@ private class FunctionEmitter private (
13091306
NothingType
13101307

13111308
case closureType @ ClosureType(paramTypes, resultType, _) =>
1312-
genTreeAuto(tree.fun)
1313-
13141309
val (funTypeID, typedClosureTypeID) = ctx.genTypedClosureStructType(closureType)
13151310
val funLocal = addSyntheticLocal(watpe.RefType(typedClosureTypeID))
13161311

1312+
genTreeAuto(tree.fun)
13171313
genAsNonNullOrNPEFor(tree.fun)
13181314
fb += wa.LocalTee(funLocal)
13191315
fb += wa.StructGet(typedClosureTypeID, genFieldID.typedClosure.data)
@@ -3156,11 +3152,8 @@ private class FunctionEmitter private (
31563152
closureFuncID,
31573153
closureFuncOrigName,
31583154
funTypeID,
3159-
enclosingClassName = None,
3160-
Some(tree.captureParams),
3161-
receiverType = None,
3155+
tree.captureParams,
31623156
tree.params,
3163-
restParam = None,
31643157
tree.body,
31653158
tree.resultType
31663159
)

linker/shared/src/main/scala/org/scalajs/linker/backend/wasmemitter/TypeTransformer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ object TypeTransformer {
9595
watpe.RefType(nullable, genTypeID.forArrayClass(arrayTypeRef))
9696

9797
case tpe @ ClosureType(_, _, nullable) =>
98-
watpe.RefType(nullable, ctx.genTypedClosureStructType(tpe)._2)
98+
val (_, typedClosureTypeID) = ctx.genTypedClosureStructType(tpe)
99+
watpe.RefType(nullable, typedClosureTypeID)
99100

100101
case RecordType(fields) =>
101102
throw new AssertionError(s"Unexpected record type $tpe")

linker/shared/src/main/scala/org/scalajs/linker/backend/wasmemitter/WasmContext.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,29 @@ final class WasmContext(
181181
)
182182
}
183183

184+
/** Generates the struct type for a `ClosureType`.
185+
*
186+
* @return
187+
* `(funTypeID, structTypeID)`, where `funTypeID` is the function type of
188+
* the `ref.func`s, and `structTypeID` is the struct type that contains
189+
* the capture data and the `ref.func` (i.e., the actual Wasm type of
190+
* values of the given `ClosureType`).
191+
*/
184192
def genTypedClosureStructType(tpe0: ClosureType): (wanme.TypeID, wanme.TypeID) = {
185193
// Normalize to the non-nullable variant
186194
val tpe = tpe0.toNonNullable
187195

188196
typedClosureTypes.getOrElseUpdate(tpe, {
189197
implicit val ctx = this
190198

199+
val tpeNameString = tpe.show()
200+
191201
val funType = watpe.FunctionType(
192202
watpe.RefType.struct :: tpe.paramTypes.map(TypeTransformer.transformParamType(_)),
193203
TypeTransformer.transformResultType(tpe.resultType)
194204
)
195205
val funTypeID = genTypeID.forClosureFunType(tpe)
196-
mainRecType.addSubType(funTypeID, OriginalName("fun" + tpe.show()), funType)
206+
mainRecType.addSubType(funTypeID, OriginalName("fun" + tpeNameString), funType)
197207

198208
val fields: List[watpe.StructField] = List(
199209
watpe.StructField(
@@ -212,7 +222,7 @@ final class WasmContext(
212222

213223
val structTypeID = genTypeID.forClosureType(tpe)
214224
val structType = watpe.StructType(fields)
215-
mainRecType.addSubType(structTypeID, OriginalName(tpe.show()), structType)
225+
mainRecType.addSubType(structTypeID, OriginalName(tpeNameString), structType)
216226

217227
(funTypeID, structTypeID)
218228
})

0 commit comments

Comments
 (0)