forked from mbebenita/j2me.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
classinfo.js
156 lines (134 loc) · 5.07 KB
/
classinfo.js
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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
'use strict';
var FieldInfo = (function() {
var idgen = 0;
return function(classInfo, access_flags, name, signature) {
this.classInfo = classInfo;
this.access_flags = access_flags;
this.name = name;
this.signature = signature;
this.id = idgen++;
}
})();
FieldInfo.prototype.get = function(obj) {
var value = obj[this.id];
if (typeof value === "undefined") {
value = util.defaultValue(this.signature);
}
return value;
}
FieldInfo.prototype.set = function(obj, value) {
obj[this.id] = value;
}
FieldInfo.prototype.toString = function() {
return "[field " + this.name + "]";
}
var ClassInfo = function(classBytes) {
var classImage = getClassImage(classBytes, this);
var cp = classImage.constant_pool;
this.className = cp[cp[classImage.this_class].name_index].bytes;
this.superClassName = classImage.super_class ? cp[cp[classImage.super_class].name_index].bytes : null;
this.access_flags = classImage.access_flags;
this.constant_pool = cp;
this.constructor = function () {
}
this.constructor.prototype.class = this;
this.constructor.prototype.toString = function() {
return "[instance " + this.class.className + "]";
}
var self = this;
this.interfaces = [];
classImage.interfaces.forEach(function(i) {
var int = CLASSES.loadClass(cp[cp[i].name_index].bytes);
self.interfaces.push(int);
self.interfaces = self.interfaces.concat(int.interfaces);
});
this.fields = [];
classImage.fields.forEach(function(f) {
var field = new FieldInfo(self, f.access_flags, cp[f.name_index].bytes, cp[f.descriptor_index].bytes);
f.attributes.forEach(function(attribute) {
if (cp[attribute.attribute_name_index].bytes === "ConstantValue")
field.constantValue = new DataView(attribute.info).getUint16(0, false);
});
self.fields.push(field);
});
this.methods = [];
classImage.methods.forEach(function(m) {
var method = {};
method.classInfo = self;
method.access_flags = m.access_flags;
method.name = cp[m.name_index].bytes;
method.signature = cp[m.signature_index].bytes;
method.attributes = m.attributes;
method.attributes.forEach(function(a) {
if (a.info.type === ATTRIBUTE_TYPES.Code) {
method.code = new Uint8Array(a.info.code);
method.exception_table = a.info.exception_table;
method.max_locals = a.info.max_locals;
}
});
self.methods.push(method);
});
var classes = this.classes = [];
classImage.attributes.forEach(function(a) {
if (a.info.type === ATTRIBUTE_TYPES.InnerClasses) {
a.info.classes.forEach(function(c) {
classes.push(cp[cp[c.inner_class_info_index].name_index].bytes);
if (c.outer_class_info_index)
classes.push(cp[cp[c.outer_class_info_index].name_index].bytes);
});
}
});
}
ClassInfo.prototype.implementsInterface = function(iface) {
var classInfo = this;
do {
var interfaces = classInfo.interfaces;
for (var n = 0; n < interfaces.length; ++n) {
if (interfaces[n] === iface)
return true;
}
classInfo = classInfo.superClass;
} while (classInfo);
return false;
}
ClassInfo.prototype.isAssignableTo = function(toClass) {
if (this === toClass || toClass === ClassInfo.java_lang_Object)
return true;
if (ACCESS_FLAGS.isInterface(toClass.access_flags) && this.implementsInterface(toClass))
return true;
if (this.elementClass && toClass.elementClass)
return this.elementClass.isAssignableTo(toClass.elementClass);
return this.superClass ? this.superClass.isAssignableTo(toClass) : false;
}
ClassInfo.prototype.getClassObject = function(ctx) {
var className = this.className;
var classObjects = ctx.runtime.classObjects;
var classObject = classObjects[className];
if (!classObject) {
classObject = ctx.newObject(CLASSES.java_lang_Class);
classObject.vmClass = this;
classObjects[className] = classObject;
}
return classObject;
}
ClassInfo.prototype.getField = function(name, signature, isStatic) {
return CLASSES.getField(this, name, signature, isStatic);
}
ClassInfo.prototype.toString = function() {
return "[class " + this.className + "]";
}
var ArrayClass = function(className, elementClass) {
this.className = className;
this.superClassName = "java/lang/Object";
this.access_flags = 0;
this.elementClass = elementClass;
}
ArrayClass.prototype.methods = [];
ArrayClass.prototype.isArrayClass = true;
ArrayClass.prototype.implementsInterface = function(iface) {
return false;
}
ArrayClass.prototype.isAssignableTo = ClassInfo.prototype.isAssignableTo;
ArrayClass.prototype.getClassObject = ClassInfo.prototype.getClassObject;