-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathCls.scala
175 lines (145 loc) · 4.15 KB
/
Cls.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package metascala
package rt
import collection.mutable
import metascala.{VM, imm}
import metascala.imm.{Sig, Access, Type}
import metascala.opcodes.{Conversion, Insn}
import metascala.imm.Type.Prim.I
import org.objectweb.asm.tree.ClassNode
/**
* A handle to a readable and writable value.
*/
class Var(var x: Val){
final def apply() = x
final def update(y: Val){
x = y
}
}
object Cls{
def apply(cn: ClassNode, index: Int)(implicit vm: VM) = {
import imm.NullSafe._
val fields = cn.fields.safeSeq.map(imm.Field.read)
val superType = cn.superName.safeOpt.map(Type.Cls.read)
new Cls(
tpe = imm.Type.Cls.read(cn.name),
superType = superType,
sourceFile = cn.sourceFile.safeOpt,
interfaces = cn.interfaces.safeSeq.map(Type.Cls.read),
accessFlags = cn.access,
methods =
cn.methods
.safeSeq
.zipWithIndex
.map{case (mn, i) =>
new rt.Method.Cls(
index,
i,
Sig(mn.name, imm.Desc.read(mn.desc)),
mn.access,
() => Conversion.ssa(cn.name, mn)
)
},
fieldList =
superType.toSeq.flatMap(_.cls.fieldList) ++
fields.filter(!_.static).flatMap{x =>
Seq.fill(x.desc.size)(x)
},
staticList =
fields.filter(_.static).flatMap{x =>
Seq.fill(x.desc.size)(x)
},
outerCls = cn.outerClass.safeOpt.map(Type.Cls.read),
index
)
}
}
/**
* The runtime mutable and VM-specific data of a Java Class
*/
class Cls(val tpe: imm.Type.Cls,
val superType: Option[imm.Type.Cls],
val sourceFile: Option[String],
val interfaces: Seq[imm.Type.Cls],
val accessFlags: Int,
val methods: Seq[rt.Method.Cls],
val fieldList: Seq[imm.Field],
val staticList: Seq[imm.Field],
val outerCls: Option[imm.Type.Cls],
val index: Int)
(implicit vm: VM){
import vm._
var initialized = false
def checkInitialized()(implicit vm: VM): Unit = {
if (!initialized){
initialized = true
vm.resolveDirectRef(tpe, Sig("<clinit>", imm.Desc.read("()V")))
.foreach(threads(0).invoke(_, Nil))
superType.foreach{ cls =>
vm.ClsTable(cls).checkInitialized()
}
}
}
val isInterface = (accessFlags & Access.Interface) != 0
val statics = vm.alloc(implicit i =>
rt.Arr.allocate(I, staticList.length)
)
def method(name: String, desc: imm.Desc): Option[rt.Method] = {
clsAncestry.flatMap(_.methods)
.find(m => m.sig.name == name && m.sig.desc == desc)
}
lazy val size = fieldList.length
def name = tpe.name
/**
* All classes that this class inherits from
*/
lazy val clsAncestry: List[imm.Type.Cls] = {
superType match{
case None => List(tpe)
case Some(superT) => tpe :: superT.cls.clsAncestry
}
}
/**
* All classes and interfaces that this class inherits from
*/
lazy val typeAncestry: Set[imm.Type.Cls] = {
Set(tpe) ++
superType.toSeq.flatMap(_.cls.typeAncestry) ++
interfaces.flatMap(_.cls.typeAncestry)
}
/**
* The virtual function dispatch table
*/
lazy val vTable: Seq[rt.Method] = {
val oldMethods =
mutable.ArrayBuffer(
superType
.toArray
.flatMap(_.vTable): _*
)
methods.filter(!_.static)
.foreach{ m =>
val index = oldMethods.indexWhere{ mRef => mRef.sig == m.sig }
val native = vm.natives.trapped.find{case rt.Method.Native(clsName, sig, func) =>
(name == clsName) && sig == m.sig
}
val update =
if (index == -1) oldMethods.append(_: Method)
else oldMethods.update(index, _: Method)
native match {
case None => update(m)
case Some(native) => update(native)
}
}
oldMethods
}
/**
* A hash map of the virtual function table, used for quick lookup
* by method signature
*/
lazy val vTableMap = vTable.map(m => m.sig -> m).toMap
override def toString() = {
s"Cls($index, ${tpe.name})"
}
def shortName = shorten(tpe.name)
def heapSize = fieldList.length + rt.Obj.headerSize
}