Skip to content

Commit 5d326c6

Browse files
committed
Move BlockType to WasmInstr.
An alternative would be to move it to Types, but in the Wasm specification, it is part of the Instructions section, so it probably makes more sense in `WasmInstr`.
1 parent 9dd074c commit 5d326c6

File tree

8 files changed

+10
-34
lines changed

8 files changed

+10
-34
lines changed

wasm/src/main/scala/converters/WasmBinaryWriter.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.io.ByteArrayOutputStream
99
import wasm.wasm4s._
1010
import wasm.wasm4s.Names._
1111
import wasm.wasm4s.Types._
12-
import wasm.wasm4s.WasmInstr.END
12+
import wasm.wasm4s.WasmInstr.{BlockType, END}
1313

1414
final class WasmBinaryWriter(module: WasmModule, emitDebugInfo: Boolean) {
1515
import WasmBinaryWriter._
@@ -448,9 +448,7 @@ final class WasmBinaryWriter(module: WasmModule, emitDebugInfo: Boolean) {
448448
}
449449
}
450450

451-
private def writeBlockType(buf: Buffer, blockType: WasmImmediate.BlockType): Unit = {
452-
import WasmImmediate._
453-
451+
private def writeBlockType(buf: Buffer, blockType: BlockType): Unit = {
454452
blockType match {
455453
case BlockType.ValueType(None) => buf.byte(0x40)
456454
case BlockType.ValueType(Some(typ)) => writeType(buf, typ)

wasm/src/main/scala/converters/WasmTextWriter.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ class WasmTextWriter {
303303
else v.toString()
304304
}
305305

306-
private def writeBlockType(blockType: WasmImmediate.BlockType)(implicit b: WatBuilder): Unit = {
306+
private def writeBlockType(blockType: BlockType)(implicit b: WatBuilder): Unit = {
307307
blockType match {
308-
case WasmImmediate.BlockType.FunctionType(name) =>
308+
case BlockType.FunctionType(name) =>
309309
b.appendElement(s"(type ${name.show})")
310-
case WasmImmediate.BlockType.ValueType(optTy) =>
310+
case BlockType.ValueType(optTy) =>
311311
for (ty <- optTy)
312312
b.sameLineList("result", writeType(ty))
313313
}

wasm/src/main/scala/ir2wasm/HelperFunctions.scala

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ object HelperFunctions {
3737

3838
private def genStringLiteral()(implicit ctx: WasmContext): Unit = {
3939
import WasmTypeName.WasmArrayTypeName
40-
import WasmImmediate._
40+
4141
val fctx = WasmFunctionContext(
4242
WasmFunctionName.stringLiteral,
4343
List("offset" -> WasmInt32, "size" -> WasmInt32, "stringIndex" -> WasmInt32),
@@ -74,7 +74,6 @@ object HelperFunctions {
7474

7575
/** `createStringFromData: (ref array u16) -> (ref any)` (representing a `string`). */
7676
private def genCreateStringFromData()(implicit ctx: WasmContext): Unit = {
77-
import WasmImmediate._
7877
import WasmTypeName.WasmArrayTypeName
7978

8079
val dataType = WasmRefType(WasmArrayTypeName.i16Array)
@@ -152,7 +151,6 @@ object HelperFunctions {
152151
* [[https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Class.html#getName()]]
153152
*/
154153
private def genTypeDataName()(implicit ctx: WasmContext): Unit = {
155-
import WasmImmediate._
156154
import WasmTypeName._
157155

158156
val typeDataType = WasmRefType(WasmStructType.typeData.name)
@@ -291,7 +289,6 @@ object HelperFunctions {
291289
* with the non-null case as a fast-path.
292290
*/
293291
private def genCreateClassOf()(implicit ctx: WasmContext): Unit = {
294-
import WasmImmediate._
295292
import WasmTypeName.WasmStructTypeName
296293

297294
val typeDataType = WasmRefType(WasmStructType.typeData.name)
@@ -412,7 +409,6 @@ object HelperFunctions {
412409
* performance-sensitive.
413410
*/
414411
private def genGetClassOf()(implicit ctx: WasmContext): Unit = {
415-
import WasmImmediate._
416412
import WasmTypeName.WasmStructTypeName
417413

418414
val typeDataType = WasmRefType(WasmStructType.typeData.name)
@@ -446,7 +442,6 @@ object HelperFunctions {
446442
* must be be strictly positive.
447443
*/
448444
private def genArrayTypeData()(implicit ctx: WasmContext): Unit = {
449-
import WasmImmediate._
450445
import WasmTypeName._
451446

452447
val typeDataType = WasmRefType(WasmStructType.typeData.name)
@@ -549,7 +544,6 @@ object HelperFunctions {
549544
* [[https://lampwww.epfl.ch/~doeraene/sjsir-semantics/#sec-sjsir-createclassdataof]].
550545
*/
551546
private def genIsInstance()(implicit ctx: WasmContext): Unit = {
552-
import WasmImmediate._
553547
import WasmTypeName.WasmStructTypeName
554548
import WasmFieldIdx.typeData._
555549

@@ -719,8 +713,6 @@ object HelperFunctions {
719713
* This is the underlying func for the `isAssignableFrom()` closure inside class data objects.
720714
*/
721715
private def genIsAssignableFromExternal()(implicit ctx: WasmContext): Unit = {
722-
import WasmImmediate._
723-
724716
val typeDataType = WasmRefType(WasmStructType.typeData.name)
725717

726718
val fctx = WasmFunctionContext(
@@ -753,7 +745,6 @@ object HelperFunctions {
753745
* Specified by `java.lang.Class.isAssignableFrom(Class)`.
754746
*/
755747
private def genIsAssignableFrom()(implicit ctx: WasmContext): Unit = {
756-
import WasmImmediate._
757748
import WasmTypeName._
758749
import WasmFieldIdx.typeData._
759750

@@ -913,7 +904,6 @@ object HelperFunctions {
913904
* This is the underlying func for the `getComponentType()` closure inside class data objects.
914905
*/
915906
private def genGetComponentType()(implicit ctx: WasmContext): Unit = {
916-
import WasmImmediate._
917907
import WasmTypeName.WasmStructTypeName
918908

919909
val typeDataType = WasmRefType(WasmStructType.typeData.name)
@@ -952,7 +942,6 @@ object HelperFunctions {
952942
* This is the underlying func for the `newArrayOfThisClass()` closure inside class data objects.
953943
*/
954944
private def genNewArrayOfThisClass()(implicit ctx: WasmContext): Unit = {
955-
import WasmImmediate._
956945
import WasmTypeName.WasmStructTypeName
957946

958947
val typeDataType = WasmRefType(WasmStructType.typeData.name)
@@ -1033,7 +1022,6 @@ object HelperFunctions {
10331022
* [[https://www.scala-js.org/doc/semantics.html#getclass]].
10341023
*/
10351024
private def genAnyGetClass()(implicit ctx: WasmContext): Unit = {
1036-
import WasmImmediate._
10371025
import WasmTypeName.WasmStructTypeName
10381026

10391027
val typeDataType = WasmRefType(WasmStructType.typeData.name)
@@ -1185,7 +1173,6 @@ object HelperFunctions {
11851173
* lengths, lengthIndex - 1)`.
11861174
*/
11871175
def genNewArrayObject()(implicit ctx: WasmContext): Unit = {
1188-
import WasmImmediate._
11891176
import WasmTypeName._
11901177
import WasmFieldIdx.typeData._
11911178

@@ -1374,7 +1361,6 @@ object HelperFunctions {
13741361
* See https://github.com/tanishiking/scala-wasm/issues/27#issuecomment-2008252049
13751362
*/
13761363
def genInstanceTest(clazz: LinkedClass)(implicit ctx: WasmContext): Unit = {
1377-
import WasmImmediate._
13781364
assert(clazz.kind == ClassKind.Interface)
13791365

13801366
val fctx = WasmFunctionContext(
@@ -1429,7 +1415,6 @@ object HelperFunctions {
14291415
* called on the class instance.
14301416
*/
14311417
def genCloneFunction(clazz: LinkedClass)(implicit ctx: WasmContext): Unit = {
1432-
import WasmImmediate._
14331418
val info = ctx.getClassInfo(clazz.name.name)
14341419
if (info.ancestors.contains(IRNames.CloneableClass) && !info.isAbstract) {
14351420
val heapType =

wasm/src/main/scala/ir2wasm/WasmBuilder.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import wasm4s.WasmContext._
66
import wasm4s.Names._
77
import wasm4s.Types._
88
import wasm4s.WasmInstr._
9-
import wasm4s.WasmImmediate._
109
import TypeTransformer._
1110

1211
import org.scalajs.ir.{Trees => IRTrees}
@@ -410,7 +409,7 @@ class WasmBuilder {
410409
// global.get $module_name
411410
GLOBAL_GET(globalInstanceName), // [rt]
412411
REF_IS_NULL, // [rt] -> [i32] (bool)
413-
IF(WasmImmediate.BlockType.ValueType(None)),
412+
IF(BlockType.ValueType()),
414413
CALL(WasmFunctionName.newDefault(clazz.name.name)),
415414
GLOBAL_SET(globalInstanceName),
416415
GLOBAL_GET(globalInstanceName),

wasm/src/main/scala/ir2wasm/WasmExpressionBuilder.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import wasm4s._
1313
import wasm4s.Names._
1414
import wasm4s.Names.WasmTypeName._
1515
import wasm4s.WasmInstr._
16-
import wasm4s.WasmImmediate._
1716
import org.scalajs.ir.Types.ClassType
1817
import org.scalajs.ir.ClassKind
1918
import org.scalajs.ir.Position

wasm/src/main/scala/wasm4s/Defaults.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package wasm4s
22

33
import wasm.wasm4s.Types._
4-
import wasm.wasm4s.WasmImmediate._
54
import wasm.wasm4s.WasmInstr
65
import wasm.wasm4s.WasmInstr._
76

wasm/src/main/scala/wasm4s/Instructions.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import Names.WasmTypeName._
88
sealed abstract class WasmInstr(val mnemonic: String, val opcode: Int)
99

1010
object WasmInstr {
11-
import WasmImmediate._
12-
1311
// Semantic categories of instructions
1412

1513
/** A stack-polymorphic instruction. */
@@ -365,9 +363,8 @@ object WasmInstr {
365363
case class CatchAll(l: WasmLabelName) extends CatchClause("catch_all", 0x02, None, l)
366364
case class CatchAllRef(l: WasmLabelName) extends CatchClause("catch_all_ref", 0x03, None, l)
367365
}
368-
}
369366

370-
object WasmImmediate {
367+
// Block types
371368

372369
/** A structured instruction can consume input and produce output on the operand stack according
373370
* to its annotated block type. It is given either as a type index that refers to a suitable
@@ -376,10 +373,10 @@ object WasmImmediate {
376373
* @see
377374
* https://webassembly.github.io/spec/core/syntax/instructions.html#control-instructions
378375
*/
379-
abstract sealed trait BlockType
376+
sealed abstract class BlockType
380377
object BlockType {
381378
case class FunctionType(ty: WasmFunctionTypeName) extends BlockType
382-
case class ValueType private (ty: Option[WasmType]) extends BlockType
379+
case class ValueType(ty: Option[WasmType]) extends BlockType
383380
object ValueType {
384381
def apply(ty: WasmType): ValueType = ValueType(Some(ty))
385382
def apply(): ValueType = ValueType(None)

wasm/src/main/scala/wasm4s/WasmFunctionContext.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import org.scalajs.ir.{Trees => IRTrees}
88

99
import wasm.wasm4s.Names._
1010
import wasm.wasm4s.Types.WasmType
11-
import wasm.wasm4s.WasmImmediate.BlockType
1211
import wasm.wasm4s.WasmInstr._
1312

1413
import wasm.ir2wasm.TypeTransformer

0 commit comments

Comments
 (0)