forked from lihaoyi/Metascala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Obj.scala
159 lines (129 loc) · 4.07 KB
/
Obj.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package metascala.rt
import collection.mutable
import metascala._
import metascala.imm
import scala.Some
object Obj{
val headerSize = 2
def allocate(cls: rt.Cls, initMembers: (String, Ref)*)(implicit registrar: Registrar): Obj = {
implicit val vm = registrar.vm
val address = vm.heap.allocate(headerSize + cls.fieldList.length)
// println("Allocating " + cls.name + " at " + address)
vm.heap(address()) = -cls.index
vm.heap(address() + 1) = cls.fieldList.length
val obj = new Obj(address)
for ((s, v) <- initMembers){
obj(s) = v()
}
obj
}
def unapply(x: Val)(implicit vm: VM) = new Obj(x)
}
class Obj(val address: Ref)
(implicit vm: VM) {
/**
* Layout
* ------
* 0 Class Index
* 1 Length
* 2 Field0
* 3 Field1
* ...
* N FieldN-2
*/
import vm._
def cls: rt.Cls = vm.ClsTable.clsIndex(-vm.heap(address()).toInt)
object members extends mutable.Seq[Val]{
def apply(n: Int): Val = {
vm.heap(address() + n + Obj.headerSize)
}
def update(n: Int, v: Val): Unit = {
vm.heap(address() + n + Obj.headerSize) = v
}
def length = cls.fieldList.length
def iterator: Iterator[Val] = new Iterator[Val]{
var index = 0
def hasNext = index < members.length
def next() = {
val x = vm.heap(address() + index + Obj.headerSize)
index += 1
x
}
}
}
def tpe = cls.tpe
def heapSize = cls.heapSize
def apply(name: String): Val = {
members(tpe.fieldList.lastIndexWhere(_.name == name))
}
def update(name: String, value: Val) = {
val index = tpe.fieldList.lastIndexWhere(_.name == name)
members(index + 1 - tpe.fieldList(index).desc.size) = value
}
def view = address + " " + vm.heap.memory.slice(address(), address() + heapSize).toList
override def toString = {
s"vrt.Obj(${cls.name})"
}
}
object Arr{
val headerSize = 2
/**
* Allocates and returns an array of the specified type, with `n` elements.
* This is multiplied with the size of the type being allocated when
* calculating the total amount of memory benig allocated
*/
def allocate(t: imm.Type, n: scala.Int)(implicit registrar: Registrar): Arr = {
rt.Arr.allocate(t, Array.fill[Int](n * t.size)(0).map(x => new ManualRef(x): Ref))
}
def allocate(innerType: imm.Type, backing: Array[Ref])(implicit registrar: Registrar): Arr = {
implicit val vm = registrar.vm
val address = vm.heap.allocate(Arr.headerSize + backing.length)
// println("Allocating Array[" + innerType.name + "] at " + address)
vm.heap(address()) = vm.arrayTypeCache.length
vm.arrayTypeCache.append(innerType)
vm.heap(address() + 1) = backing.length / innerType.size
backing.map(_()).copyToArray(vm.heap.memory, address() + Arr.headerSize)
new Arr(address)
}
def unapply(x: Int)(implicit vm: VM): Option[Arr] = Some(new Arr(x))
}
/**
* Can be treated as a mutable sequence of Ints; this representation is
* completely unaware of differently sized contents.
*/
class Arr(val address: Ref)(implicit vm: VM) extends mutable.Seq[Int]{
/**
* Layout
* ------
* 0 Class Index
* 1 Length
* 2 Field0
* 3 Field1
* ...
* N FieldN-2
*/
/**
* The type the array contains
*/
def innerType = vm.arrayTypeCache(vm.heap(address()))
/**
* The type of the entire array
*/
def tpe = imm.Type.Arr(innerType)
/**
* Length of the array, in Ints or Longs, from the POV of interpreted code
*/
def arrayLength = vm.heap(address() + 1)
/**
* Total size of the array on the heap
*/
def heapSize = Arr.headerSize + length
/**
* Length of the read-writable part of the array, in Ints.
*/
def length = vm.heap(address() + 1) * innerType.size
def apply(index: scala.Int) = vm.heap(address() + index + Arr.headerSize)
def update(index: scala.Int, value: Val) = vm.heap(address() + index + Arr.headerSize) = value
override def toString = ""+vm.heap.memory.slice(address(), address() + heapSize).toList
def iterator: Iterator[Int] = vm.heap.memory.view(address() + Arr.headerSize, address() + heapSize).iterator
}