Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Commit b953150

Browse files
authored
Merge pull request #126 from sjrd/simplify-wasm-context
Simplify WasmContext.
2 parents d457fe6 + fb0eff1 commit b953150

File tree

8 files changed

+141
-155
lines changed

8 files changed

+141
-155
lines changed

Diff for: wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/ClassEmitter.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
143143
)
144144
val xParam = fb.addParam("x", WasmRefType.anyref)
145145
fb.setResultType(WasmInt32)
146-
fb.setFunctionType(ctx.isJSClassInstanceFuncTypeName)
146+
fb.setFunctionType(genTypeName.isJSClassInstanceFuncType)
147147

148148
val instrs = fb
149149

@@ -373,7 +373,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
373373
case None => genTypeName.typeData
374374
case Some(s) => genTypeName.forVTable(s)
375375
}
376-
val structType = WasmStructType(ctx.typeDataStructFields ::: vtableFields)
376+
val structType = WasmStructType(CoreWasmLib.typeDataStructFields ::: vtableFields)
377377
val subType = WasmSubType(typeName, isFinal = false, Some(superType), structType)
378378
ctx.mainRecType.addSubType(subType)
379379
typeName
@@ -533,7 +533,7 @@ class ClassEmitter(coreSpec: CoreSpec) {
533533
)
534534
val fromParam = fb.addParam("from", WasmRefType(genTypeName.ObjectStruct))
535535
fb.setResultType(WasmRefType(genTypeName.ObjectStruct))
536-
fb.setFunctionType(ctx.cloneFunctionTypeName)
536+
fb.setFunctionType(genTypeName.cloneFunctionType)
537537

538538
val instrs = fb
539539

Diff for: wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/CoreWasmLib.scala

+55-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,42 @@ object CoreWasmLib {
2222

2323
private implicit val noPos: Position = Position.NoPosition
2424

25+
/** Fields of the `typeData` struct definition.
26+
*
27+
* They are accessible as a public list because they must be repeated in every vtable type
28+
* definition.
29+
*
30+
* @see
31+
* [[VarGen.genFieldName.typeData]], which contains documentation of what is in each field.
32+
*/
33+
val typeDataStructFields: List[WasmStructField] = {
34+
import genFieldName.typeData._
35+
import WasmRefType.nullable
36+
List(
37+
WasmStructField(nameOffset, WasmInt32, isMutable = false),
38+
WasmStructField(nameSize, WasmInt32, isMutable = false),
39+
WasmStructField(nameStringIndex, WasmInt32, isMutable = false),
40+
WasmStructField(kind, WasmInt32, isMutable = false),
41+
WasmStructField(specialInstanceTypes, WasmInt32, isMutable = false),
42+
WasmStructField(strictAncestors, nullable(genTypeName.typeDataArray), isMutable = false),
43+
WasmStructField(componentType, nullable(genTypeName.typeData), isMutable = false),
44+
WasmStructField(name, WasmRefType.anyref, isMutable = true),
45+
WasmStructField(classOfValue, nullable(genTypeName.ClassStruct), isMutable = true),
46+
WasmStructField(arrayOf, nullable(genTypeName.ObjectVTable), isMutable = true),
47+
WasmStructField(cloneFunction, nullable(genTypeName.cloneFunctionType), isMutable = false),
48+
WasmStructField(
49+
isJSClassInstance,
50+
nullable(genTypeName.isJSClassInstanceFuncType),
51+
isMutable = false
52+
),
53+
WasmStructField(
54+
reflectiveProxies,
55+
WasmRefType(genTypeName.reflectiveProxies),
56+
isMutable = false
57+
)
58+
)
59+
}
60+
2561
/** Generates definitions that must come *before* the code generated for regular classes.
2662
*
2763
* This notably includes the `typeData` definitions, since the vtable of `jl.Object` is a subtype
@@ -66,6 +102,19 @@ object CoreWasmLib {
66102
}
67103

68104
private def genCoreTypesInRecType()(implicit ctx: WasmContext): Unit = {
105+
ctx.mainRecType.addSubType(
106+
genTypeName.cloneFunctionType,
107+
WasmFunctionType(
108+
List(WasmRefType(genTypeName.ObjectStruct)),
109+
List(WasmRefType(genTypeName.ObjectStruct))
110+
)
111+
)
112+
113+
ctx.mainRecType.addSubType(
114+
genTypeName.isJSClassInstanceFuncType,
115+
WasmFunctionType(List(WasmRefType.anyref), List(WasmInt32))
116+
)
117+
69118
ctx.mainRecType.addSubType(
70119
genTypeName.typeDataArray,
71120
WasmArrayType(WasmFieldType(WasmRefType(genTypeName.typeData), isMutable = false))
@@ -84,7 +133,7 @@ object CoreWasmLib {
84133
genTypeName.typeData,
85134
isFinal = false,
86135
None,
87-
WasmStructType(ctx.typeDataStructFields)
136+
WasmStructType(typeDataStructFields)
88137
)
89138
)
90139

@@ -148,7 +197,7 @@ object CoreWasmLib {
148197

149198
private def genTags()(implicit ctx: WasmContext): Unit = {
150199
val exceptionSig = WasmFunctionSignature(List(WasmRefType.externref), Nil)
151-
val typeName = ctx.addFunctionType(exceptionSig)
200+
val typeName = ctx.moduleBuilder.signatureToTypeName(exceptionSig)
152201
ctx.moduleBuilder.addImport(
153202
WasmImport(
154203
"__scalaJSHelpers",
@@ -287,7 +336,7 @@ object CoreWasmLib {
287336
results: List[WasmType]
288337
): Unit = {
289338
val sig = WasmFunctionSignature(params, results)
290-
val typeName = ctx.addFunctionType(sig)
339+
val typeName = ctx.moduleBuilder.signatureToTypeName(sig)
291340
ctx.moduleBuilder.addImport(
292341
WasmImport("__scalaJSHelpers", name.name, WasmImportDesc.Func(name, typeName))
293342
)
@@ -932,7 +981,7 @@ object CoreWasmLib {
932981
instrs += REF_NULL(WasmHeapType.None) // arrayOf
933982

934983
// clone
935-
instrs.switch(WasmRefType(ctx.cloneFunctionTypeName)) { () =>
984+
instrs.switch(WasmRefType(genTypeName.cloneFunctionType)) { () =>
936985
instrs += LOCAL_GET(typeDataParam)
937986
instrs += STRUCT_GET(genTypeName.typeData, genFieldIdx.typeData.kindIdx)
938987
}(
@@ -1104,7 +1153,7 @@ object CoreWasmLib {
11041153
instrs += BR_ON_NULL(isJSClassInstanceIsNull)
11051154

11061155
// Call the function
1107-
instrs += CALL_REF(ctx.isJSClassInstanceFuncTypeName)
1156+
instrs += CALL_REF(genTypeName.isJSClassInstanceFuncType)
11081157
instrs += RETURN
11091158
}
11101159
instrs += DROP // drop `value` which was left on the stack
@@ -2073,7 +2122,7 @@ object CoreWasmLib {
20732122
val fb = newFunctionBuilder(genFunctionName.clone(baseRef))
20742123
val fromParam = fb.addParam("from", WasmRefType(genTypeName.ObjectStruct))
20752124
fb.setResultType(WasmRefType(genTypeName.ObjectStruct))
2076-
fb.setFunctionType(ctx.cloneFunctionTypeName)
2125+
fb.setFunctionType(genTypeName.cloneFunctionType)
20772126

20782127
val instrs = fb
20792128

Diff for: wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/FunctionEmitter.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ object FunctionEmitter {
4747
restParam: Option[IRTrees.ParamDef],
4848
body: IRTrees.Tree,
4949
resultType: IRTypes.Type
50-
)(implicit ctx: TypeDefinableWasmContext, pos: Position): Unit = {
50+
)(implicit ctx: WasmContext, pos: Position): Unit = {
5151
val emitter = prepareEmitter(
5252
functionName,
5353
enclosingClassName,
@@ -69,7 +69,7 @@ object FunctionEmitter {
6969
enclosingClassName: IRNames.ClassName,
7070
jsClassCaptures: List[IRTrees.ParamDef],
7171
ctor: IRTrees.JSConstructorDef
72-
)(implicit ctx: TypeDefinableWasmContext): Unit = {
72+
)(implicit ctx: WasmContext): Unit = {
7373
implicit val pos = ctor.pos
7474

7575
val allCtorParams = ctor.args ::: ctor.restParam.toList
@@ -148,7 +148,7 @@ object FunctionEmitter {
148148
receiverTyp: Option[Types.WasmType],
149149
paramDefs: List[IRTrees.ParamDef],
150150
resultTypes: List[Types.WasmType]
151-
)(implicit ctx: TypeDefinableWasmContext, pos: Position): FunctionEmitter = {
151+
)(implicit ctx: WasmContext, pos: Position): FunctionEmitter = {
152152
val fb = new FunctionBuilder(ctx.moduleBuilder, functionName, pos)
153153

154154
def addCaptureLikeParamListAndMakeEnv(
@@ -250,7 +250,7 @@ object FunctionEmitter {
250250
}
251251

252252
private class FunctionEmitter private (
253-
ctx: TypeDefinableWasmContext,
253+
ctx: WasmContext,
254254
val fb: FunctionBuilder,
255255
enclosingClassName: Option[IRNames.ClassName],
256256
_newTargetStorage: Option[FunctionEmitter.VarStorage.Local],
@@ -2668,7 +2668,7 @@ private class FunctionEmitter private (
26682668
genFieldIdx.typeData.cloneFunctionIdx
26692669
)
26702670
// cloneFunction: (ref j.l.Object) -> ref j.l.Object
2671-
instrs += CALL_REF(ctx.cloneFunctionTypeName)
2671+
instrs += CALL_REF(genTypeName.cloneFunctionType)
26722672

26732673
t.tpe match {
26742674
case ClassType(className) =>

Diff for: wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/SWasmGen.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ object SWasmGen {
5050
}
5151

5252
def genLoadJSConstructor(fb: FunctionBuilder, className: ClassName)(implicit
53-
ctx: TypeDefinableWasmContext
53+
ctx: WasmContext
5454
): Unit = {
5555
val info = ctx.getClassInfo(className)
5656

@@ -65,7 +65,7 @@ object SWasmGen {
6565
}
6666

6767
def genLoadJSFromSpec(fb: FunctionBuilder, loadSpec: JSNativeLoadSpec)(implicit
68-
ctx: TypeDefinableWasmContext
68+
ctx: WasmContext
6969
): Unit = {
7070
def genFollowPath(path: List[String]): Unit = {
7171
for (prop <- path) {

Diff for: wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/SpecialNames.scala

+3
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ object SpecialNames {
2323
val JSExceptionField = FieldName(JSExceptionClass, SimpleFieldName("exception"))
2424

2525
val hashCodeMethodName = MethodName("hashCode", Nil, IntRef)
26+
27+
/** A unique simple method name to map all method *signatures* into `MethodName`s. */
28+
val normalizedSimpleMethodName = SimpleMethodName("m")
2629
}

Diff for: wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/TypeTransformer.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object TypeTransformer {
2424
*/
2525
def transformResultType(
2626
t: IRTypes.Type
27-
)(implicit ctx: ReadOnlyWasmContext): List[Types.WasmType] =
27+
)(implicit ctx: WasmContext): List[Types.WasmType] =
2828
t match {
2929
case IRTypes.NoType => Nil
3030
case IRTypes.NothingType => Nil
@@ -36,7 +36,7 @@ object TypeTransformer {
3636
* This method cannot be used for `void` and `nothing`, since they have no corresponding Wasm
3737
* value type.
3838
*/
39-
def transformType(t: IRTypes.Type)(implicit ctx: ReadOnlyWasmContext): Types.WasmType =
39+
def transformType(t: IRTypes.Type)(implicit ctx: WasmContext): Types.WasmType =
4040
t match {
4141
case IRTypes.AnyType => Types.WasmRefType.anyref
4242

@@ -51,7 +51,7 @@ object TypeTransformer {
5151

5252
def transformClassType(
5353
className: IRNames.ClassName
54-
)(implicit ctx: ReadOnlyWasmContext): Types.WasmRefType = {
54+
)(implicit ctx: WasmContext): Types.WasmRefType = {
5555
val info = ctx.getClassInfo(className)
5656
if (info.isAncestorOfHijackedClass)
5757
Types.WasmRefType.anyref

Diff for: wasm/src/main/scala/org/scalajs/linker/backend/wasmemitter/VarGen.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,11 @@ object VarGen {
500500

501501
def forFunction(idx: Int): WasmTypeName = WasmTypeName(s"f.$idx")
502502

503-
def forRecFunction(idx: Int): WasmTypeName = WasmTypeName(s"recf.$idx")
503+
val cloneFunctionType = WasmTypeName("cloneFuncType")
504+
val isJSClassInstanceFuncType = WasmTypeName("isJSClassInstanceFuncType")
505+
506+
def forTableFunctionType(methodName: MethodName): WasmTypeName =
507+
WasmTypeName("m." + methodName.nameString)
504508
}
505509

506510
object genTagName {

0 commit comments

Comments
 (0)