@@ -8,84 +8,66 @@ case class RTMemoryType(min: Int, max: Option[Int])
88case 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- }
0 commit comments