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

Commit 7ee443b

Browse files
authored
Merge pull request #110 from sjrd/remove-defaults
Generate defaults from IR types instead of from Wasm types.
2 parents 2eecaeb + 20ac533 commit 7ee443b

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

Diff for: wasm/src/main/scala/ir2wasm/HelperFunctions.scala

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import wasm.wasm4s.WasmInstr._
1515

1616
import EmbeddedConstants._
1717
import TypeTransformer._
18-
import wasm4s.Defaults
1918

2019
object HelperFunctions {
2120

@@ -1878,7 +1877,7 @@ object HelperFunctions {
18781877
assert(clazz.hasDirectInstances)
18791878

18801879
val structName = WasmTypeName.WasmStructTypeName.forClass(className)
1881-
val fctx = WasmFunctionContext(
1880+
implicit val fctx = WasmFunctionContext(
18821881
Names.WasmFunctionName.newDefault(className),
18831882
Nil,
18841883
List(WasmRefType(structName))
@@ -1895,8 +1894,7 @@ object HelperFunctions {
18951894
instrs += REF_NULL(WasmHeapType(WasmArrayTypeName.itables))
18961895

18971896
classInfo.allFieldDefs.foreach { f =>
1898-
val ty = transformType(f.ftpe)
1899-
instrs += Defaults.defaultValue(ty)
1897+
WasmExpressionBuilder.generateIRBody(IRTypes.zeroOf(f.ftpe)(clazz.pos), f.ftpe)
19001898
}
19011899
instrs += STRUCT_NEW(structName)
19021900

Diff for: wasm/src/main/scala/ir2wasm/WasmBuilder.scala

+17-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import org.scalajs.linker.interface.unstable.RuntimeClassNameMapperImpl
1818
import org.scalajs.linker.standard.{CoreSpec, LinkedClass, LinkedTopLevelExport}
1919

2020
import collection.mutable
21-
import java.awt.Window.Type
22-
import _root_.wasm4s.Defaults
2321

2422
import EmbeddedConstants._
2523

@@ -67,11 +65,10 @@ class WasmBuilder(coreSpec: CoreSpec) {
6765
field @ IRTrees.FieldDef(flags, name, _, ftpe) <- clazz.fields
6866
if flags.namespace.isStatic
6967
} {
70-
val typ = transformType(ftpe)
7168
val global = WasmGlobal(
7269
WasmGlobalName.forStaticField(name.name),
73-
typ,
74-
WasmExpr(List(Defaults.defaultValue(typ))),
70+
transformType(ftpe),
71+
WasmExpr(List(genStaticVarInit(ftpe))),
7572
isMutable = true
7673
)
7774
ctx.addGlobal(global)
@@ -96,6 +93,21 @@ class WasmBuilder(coreSpec: CoreSpec) {
9693
}
9794
}
9895

96+
private def genStaticVarInit(tpe: IRTypes.Type)(implicit ctx: WasmContext): WasmInstr = {
97+
import IRTypes._
98+
99+
tpe match {
100+
case BooleanType | CharType | ByteType | ShortType | IntType => I32_CONST(0)
101+
case LongType => I64_CONST(0L)
102+
case FloatType => F32_CONST(0.0f)
103+
case DoubleType => F64_CONST(0.0)
104+
case AnyType | ClassType(_) | ArrayType(_) | NullType => REF_NULL(WasmHeapType.None)
105+
106+
case NoType | NothingType | StringType | UndefType | _: RecordType =>
107+
throw new AssertionError(s"Unexpected type for static variable: ${tpe.show()}")
108+
}
109+
}
110+
99111
def genArrayClasses()(implicit ctx: WasmContext): Unit = {
100112
import WasmTypeName.WasmStructTypeName
101113

Diff for: wasm/src/main/scala/ir2wasm/WasmExpressionBuilder.scala

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import wasm4s.WasmInstr._
1616
import org.scalajs.ir.Types.ClassType
1717
import org.scalajs.ir.ClassKind
1818
import org.scalajs.ir.Position
19-
import _root_.wasm4s.Defaults
2019

2120
import EmbeddedConstants._
2221

Diff for: wasm/src/main/scala/wasm4s/Defaults.scala

-19
This file was deleted.

0 commit comments

Comments
 (0)