Skip to content

Commit d0a4309

Browse files
author
Guannan Wei
committed
further refactor
1 parent 9d70d49 commit d0a4309

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
lines changed

src/main/scala/Memory.scala

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,66 @@ case class RTMemoryType(min: Int, max: Option[Int])
88
case class RTMemory(var memType: RTMemoryType, data: ArrayBuffer[Byte]) {
99
def size: Int = (data.size / RTMemory.pageSize).toInt
1010

11-
def grow(delta: Int): Option[RTMemoryException] = {
11+
def grow(delta: Int): Option[RTMemoryException] =
1212
val oldSize = memType.min
1313
val newSize = oldSize + delta
14-
if (newSize < oldSize) {
14+
if (newSize < oldSize)
1515
Some(RTMemoryException("SizeOverflow"))
16-
} else {
16+
else
1717
memType = RTMemoryType(newSize, memType.max)
1818
data ++= Array.fill[Byte](delta * RTMemory.pageSize.toInt)(0)
1919
None
20-
}
21-
}
2220

23-
def loadByte(a: Long): Byte = {
21+
def loadByte(a: Long): Byte =
2422
if (a >= data.size) throw new RTMemoryException("Bounds")
2523
data(a.toInt)
26-
}
2724

28-
def storeByte(a: Long, b: Byte): Unit = {
25+
def storeByte(a: Long, b: Byte): Unit =
2926
if (a >= data.size) throw new RTMemoryException("Bounds")
3027
data(a.toInt) = b
31-
}
3228

33-
def loadn(a: Long, o: Int, n: Int): Long = {
29+
def loadn(a: Long, o: Int, n: Int): Long =
3430
assert(n > 0 && n <= 8)
3531
var result: Long = 0
36-
for (i <- (n - 1) to 0 by -1) { // Little-endian: start from least significant byte
32+
for (i <- (n - 1) to 0 by -1) // Little-endian: start from least significant byte
3733
result = (result << 8) | (loadByte(a + i) & 0xff)
38-
}
3934
result
40-
}
4135

42-
def storen(a: Long, o: Int, n: Int, x: Long): Unit = {
36+
def storen(a: Long, o: Int, n: Int, x: Long): Unit =
4337
assert(n > 0 && n <= 8)
4438
var temp = x
45-
for (i <- 0 until n) {
39+
for (i <- 0 until n)
4640
storeByte(a + i, (temp & 0xff).toByte) // Little-endian: store least significant byte first
4741
temp = temp >> 8
48-
}
49-
}
5042

5143
// TODO: store/load different types
52-
def loadInt(addr: Long): Int = {
44+
def loadInt(addr: Long): Int =
5345
val result: Long = loadn(addr, 0, 4)
5446
result.toInt
55-
}
5647

57-
def storeInt(addr: Long, num: Int): Unit = {
48+
def storeInt(addr: Long, num: Int): Unit =
5849
storen(addr, 0, 4, num)
59-
}
6050

61-
def fill(offset: Long, size: Long, value: Byte): Unit = {
51+
def fill(offset: Long, size: Long, value: Byte): Unit =
6252
// TODO: instead of using storeByte and loadByte, check
6353
// bounds up front so we can avoid the checks in load/storeByte
64-
for (i <- offset until offset + size) {
54+
for (i <- offset until offset + size)
6555
storeByte(i, value)
66-
}
67-
}
6856

69-
def copy(src: Long, dst: Long, size: Long): Unit = {
57+
def copy(src: Long, dst: Long, size: Long): Unit =
7058
// TODO: instead of using storeByte and loadByte, check
7159
// bounds up front so we can avoid the checks in load/storeByte
72-
if (src < dst) {
73-
for (i <- size - 1 to 0 by -1) {
60+
if (src < dst)
61+
for (i <- size - 1 to 0 by -1)
7462
storeByte(dst + i, loadByte(src + i))
75-
}
76-
} else {
77-
for (i <- 0 until size.toInt) {
63+
else
64+
for (i <- 0 until size.toInt)
7865
storeByte(dst + i, loadByte(src + i))
79-
}
80-
}
81-
}
8266
}
8367

84-
object RTMemory {
68+
object RTMemory:
8569
val pageSize = 65536 // https://www.w3.org/TR/wasm-core-2/exec/runtime.html#memory-instances
8670
def apply(): RTMemory = this.apply(1024)
8771
def apply(size: Int): RTMemory = this.apply(size, None)
88-
def apply(size: Int, maxSize: Option[Int]): RTMemory = {
72+
def apply(size: Int, maxSize: Option[Int]): RTMemory =
8973
new RTMemory(RTMemoryType(size, maxSize), ArrayBuffer.fill[Byte](size * pageSize.toInt)(0))
90-
}
91-
}

src/main/scala/MiniWasm.scala

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ case class Evaluator(module: ModuleInstance) {
2626
val newStack = stack.drop(ty.inps.size)
2727
val frameLocals = args ++ locals.map(zero(_))
2828
val newFrame = Frame(ArrayBuffer(frameLocals: _*))
29-
if (isTail)
30-
// when tail call, share the continuation for returning with the callee
31-
eval(body, List(), newFrame, kont, List(kont))
32-
else
33-
val restK: Cont[Ans] = (retStack) =>
34-
eval(rest, retStack.take(ty.out.size) ++ newStack, frame, kont, trail)
35-
// We make a new trail by `restK`, since function creates a new block to escape
36-
// (more or less like `return`)
37-
eval(body, List(), newFrame, restK, List(restK))
29+
val restK: Cont[Ans] = (retStack) =>
30+
eval(rest, retStack.take(ty.out.size) ++ newStack, frame, kont, trail)
31+
eval(body, List(), newFrame, restK, List(restK))
3832
case Import("console", "log", _) =>
3933
val I32V(v) :: newStack = stack
4034
println(v)
@@ -166,9 +160,7 @@ case class Evaluator(module: ModuleInstance) {
166160
trail(goto)(newStack)
167161
case Return => trail.last(stack)
168162
case Call(f) => evalCall(rest, stack, frame, kont, trail, f, false)
169-
case _ =>
170-
println(inst)
171-
throw new Exception(s"instruction $inst not implemented")
163+
case _ => throw new Exception(s"instruction $inst not implemented")
172164

173165
// If `main` is given, then we use that function as the entry point of the program;
174166
// otherwise, we look up the top-level `start` instruction to locate the entry point.

0 commit comments

Comments
 (0)