@@ -8,84 +8,66 @@ case class RTMemoryType(min: Int, max: Option[Int])
8
8
case class RTMemory (var memType : RTMemoryType , data : ArrayBuffer [Byte ]) {
9
9
def size : Int = (data.size / RTMemory .pageSize).toInt
10
10
11
- def grow (delta : Int ): Option [RTMemoryException ] = {
11
+ def grow (delta : Int ): Option [RTMemoryException ] =
12
12
val oldSize = memType.min
13
13
val newSize = oldSize + delta
14
- if (newSize < oldSize) {
14
+ if (newSize < oldSize)
15
15
Some (RTMemoryException (" SizeOverflow" ))
16
- } else {
16
+ else
17
17
memType = RTMemoryType (newSize, memType.max)
18
18
data ++= Array .fill[Byte ](delta * RTMemory .pageSize.toInt)(0 )
19
19
None
20
- }
21
- }
22
20
23
- def loadByte (a : Long ): Byte = {
21
+ def loadByte (a : Long ): Byte =
24
22
if (a >= data.size) throw new RTMemoryException (" Bounds" )
25
23
data(a.toInt)
26
- }
27
24
28
- def storeByte (a : Long , b : Byte ): Unit = {
25
+ def storeByte (a : Long , b : Byte ): Unit =
29
26
if (a >= data.size) throw new RTMemoryException (" Bounds" )
30
27
data(a.toInt) = b
31
- }
32
28
33
- def loadn (a : Long , o : Int , n : Int ): Long = {
29
+ def loadn (a : Long , o : Int , n : Int ): Long =
34
30
assert(n > 0 && n <= 8 )
35
31
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
37
33
result = (result << 8 ) | (loadByte(a + i) & 0xff )
38
- }
39
34
result
40
- }
41
35
42
- def storen (a : Long , o : Int , n : Int , x : Long ): Unit = {
36
+ def storen (a : Long , o : Int , n : Int , x : Long ): Unit =
43
37
assert(n > 0 && n <= 8 )
44
38
var temp = x
45
- for (i <- 0 until n) {
39
+ for (i <- 0 until n)
46
40
storeByte(a + i, (temp & 0xff ).toByte) // Little-endian: store least significant byte first
47
41
temp = temp >> 8
48
- }
49
- }
50
42
51
43
// TODO: store/load different types
52
- def loadInt (addr : Long ): Int = {
44
+ def loadInt (addr : Long ): Int =
53
45
val result : Long = loadn(addr, 0 , 4 )
54
46
result.toInt
55
- }
56
47
57
- def storeInt (addr : Long , num : Int ): Unit = {
48
+ def storeInt (addr : Long , num : Int ): Unit =
58
49
storen(addr, 0 , 4 , num)
59
- }
60
50
61
- def fill (offset : Long , size : Long , value : Byte ): Unit = {
51
+ def fill (offset : Long , size : Long , value : Byte ): Unit =
62
52
// TODO: instead of using storeByte and loadByte, check
63
53
// 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)
65
55
storeByte(i, value)
66
- }
67
- }
68
56
69
- def copy (src : Long , dst : Long , size : Long ): Unit = {
57
+ def copy (src : Long , dst : Long , size : Long ): Unit =
70
58
// TODO: instead of using storeByte and loadByte, check
71
59
// 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 )
74
62
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)
78
65
storeByte(dst + i, loadByte(src + i))
79
- }
80
- }
81
- }
82
66
}
83
67
84
- object RTMemory {
68
+ object RTMemory :
85
69
val pageSize = 65536 // https://www.w3.org/TR/wasm-core-2/exec/runtime.html#memory-instances
86
70
def apply (): RTMemory = this .apply(1024 )
87
71
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 =
89
73
new RTMemory (RTMemoryType (size, maxSize), ArrayBuffer .fill[Byte ](size * pageSize.toInt)(0 ))
90
- }
91
- }
0 commit comments