-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypes.js
335 lines (310 loc) · 8.63 KB
/
types.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// types.js
//
// Kopiluwak. Copyright (c) 2020 Ben Zotto
//
//
// JTYPE constant definitions.
//
// Reference types
const JTYPE_NULL = 0;
const JTYPE_CLASS = 1;
const JTYPE_ARRAY = 2;
const JTYPE_INTERFACE = 3;
// Primitive types
const JTYPE_BYTE = 4
const JTYPE_SHORT = 5;
const JTYPE_INT = 6;
const JTYPE_LONG = 7;
const JTYPE_CHAR = 8;
const JTYPE_FLOAT = 9;
const JTYPE_DOUBLE = 10;
const JTYPE_BOOLEAN = 11;
const JTYPE_RETURNADDR = 12;
//
// Describes a valid Java-land type. Can describe a primitive or reference type, and
// optionally has a "name" which names a class or interface, or a "subtype" (also a JType
// instance) which describes the type contained within an array type.
//
// Can be initialized with the numeric type for a primitive. Can be initialized with a
// valid descriptor for primitive, array, or reference types. Void is not an actual type, so
// the descriptor "V" is not valid here, and there is no descriptor for the returnAddress type.
//
function JType(typeOrDescriptor) {
this.type = JTYPE_NULL; // a JTYPE_* value
this.name = null; // the name of a class or interface
this.dimensions = 0; // number of dimensions of an array
this.componentType = null; // the type contained within an array. (Another JType instance.)
//
// The only way to mutate this object is by flagging it from a class to an interface. This is
// because there's no way to construct an interface as such, the descriptor is identical to a
// class descriptor. So the caller has to know, and can signal it after creating the type.
//
this.setIsInterface = function() {
// Only allow this if currently a class type.
if (this.type == JTYPE_CLASS) {
this.type = JTYPE_INTERFACE;
}
}
//
// Public predicates and accessors.
//
this.isNull = function() {
return this.type == JTYPE_NULL;
}
this.isPrimitiveType = function() {
return this.type >= 4 && this.type <= 12;
}
this.isReferenceType = function() {
return this.type >= 0 && this.type <= 3;
}
this.isClass = function() {
return this.type == JTYPE_CLASS;
}
this.className = function() {
return this.name;
}
this.isInterface = function() {
return this.type == JTYPE_INTERFACE;
}
this.interfaceName = function() {
return this.name;
}
this.isArray = function() {
return this.type == JTYPE_ARRAY;
}
this.arrayDimensions = function() {
return this.dimensions;
}
this.arrayComponentType = function() {
return this.componentType;
}
this.isOrdinaryClass = function() {
return !this.isArray() && !this.isInterface();
}
this.isIntegralType = function() {
return this.type == JTYPE_BYTE || this.type == JTYPE_SHORT || this.type == JTYPE_INT || this.type == JTYPE_LONG;
}
this.isFloatingType = function() {
return this.type == JTYPE_FLOAT || this.type == JTYPE_DOUBLE;
}
this.isByte = function() {
return this.type == JTYPE_BYTE;
}
this.isShort = function() {
return this.type == JTYPE_SHORT;
}
this.isInt = function() {
return this.type == JTYPE_INT;
}
this.isLong = function() {
return this.type == JTYPE_LONG;
}
this.isChar = function() {
return this.type == JTYPE_CHAR;
}
this.isFloat = function() {
return this.type == JTYPE_FLOAT;
}
this.isDouble = function() {
return this.type == JTYPE_DOUBLE;
}
this.isBoolean = function() {
return this.type == JTYPE_BOOLEAN;
}
this.isReturnAddress = function() {
return this.type == JTYPE_RETURNADDR;
}
this.isCategory1ComputationalType = function() {
return !this.isCategory2ComputationalType(); // ;-)
}
this.isCategory2ComputationalType = function() {
return (this.type == JTYPE_LONG || this.type == JTYPE_DOUBLE);
}
this.descriptorString = function() {
switch (this.type) {
case JTYPE_CLASS:
case JTYPE_INTERFACE:
return "L" + this.name + ";";
case JTYPE_ARRAY:
{
let str = "";
for (let i = 0; i < this.dimensions; i++) {
str = str + "[";
}
return str + this.componentType.descriptorString();
}
case JTYPE_BYTE:
return "B";
case JTYPE_SHORT:
return "S";
case JTYPE_INT:
return "I";
case JTYPE_LONG:
return "J";
case JTYPE_CHAR:
return "C";
case JTYPE_FLOAT:
return "F";
case JTYPE_DOUBLE:
return "D";
case JTYPE_BOOLEAN:
return "Z";
case JTYPE_NULL:
case JTYPE_RETURNADDR:
// We can produce a descriptor string for all types except return address,
// which doesn't map to a Java language type and doesn't show up in descriptors. Null is a synthetic
// placeholder, not a real type, and thus is not meaningful in descriptors.
return null;
}
}
this.isIdenticalTo = function(other) {
if (other == null) {
return false;
}
if (this.type != other.type || this.name != other.name || this.dimensions != other.dimensions) {
return false;
}
if (!!this.componentType != !!other.componentType) {
return false;
}
if (this.componentType && !this.componentType.isIdenticalTo(other.componentType)) {
return false;
}
return true;
}
//
// Construction
//
if (typeof typeOrDescriptor == 'number') {
let type = typeOrDescriptor;
if (type != JTYPE_NULL && (type < 4 || type > 12)) {
KLLogError("JType: can't create with numeric type " + type);
type = JTYPE_NULL;
}
this.type = type;
} else if (typeof typeOrDescriptor == 'string') {
let fieldType = typeOrDescriptor;
// See if there are array dimensions to be had.
this.dimensions = 0;
let idx = 0;
while (fieldType[idx] == '[') {
this.dimensions++;
idx++;
}
// If this is an array, then mark this object as an array and recusively create the component
// type with the remainder of the string.
if (this.dimensions > 0) {
this.type = JTYPE_ARRAY;
this.componentType = new JType(fieldType.substring(idx));
} else {
let baseOrObjectType = fieldType;
if (baseOrObjectType[0] == "L") {
// This can signal either a class or interface, the descriptor syntax does not disambiguate so the caller
// will have to flag this as an interface after the construction if that is known.
this.type = JTYPE_CLASS;
this.name = baseOrObjectType.substring(1, baseOrObjectType.length-1);
} else {
let baseType = baseOrObjectType;
if (baseType == "B") {
this.type = JTYPE_BYTE;
} else if (baseType == "S") {
this.type = JTYPE_SHORT;
} else if (baseType == "I") {
this.type = JTYPE_INT;
} else if (baseType == "J") {
this.type = JTYPE_LONG;
} else if (baseType == "C") {
this.type = JTYPE_CHAR;
} else if (baseType == "F") {
this.type = JTYPE_FLOAT;
} else if (baseType == "D") {
this.type = JTYPE_DOUBLE;
} else if (baseType == "Z") {
this.type = JTYPE_BOOLEAN;
}
}
}
}
}
function JTypeFromJVMArrayType(atype) {
switch (atype) {
case T_BOOLEAN:
return new JType(JTYPE_BOOLEAN);
case T_CHAR:
return new JType(JTYPE_CHAR);
case T_FLOAT:
return new JType(JTYPE_FLOAT);
case T_DOUBLE:
return new JTyoe(JTYPE_DOUBLE);
case T_BYTE:
return new JType(JTYPE_BYTE);
case T_SHORT:
return new JType(JTYPE_SHORT);
case T_INT:
return new JType(JTYPE_INT);
case T_LONG:
return new JType(JTYPE_LONG);
default:
debugger;
return new JType(JTYPE_INT);
}
}
//
// Parses and describes a method argument and return types with instances of type objects.
//
function KLMethodDescriptor(descriptorString) {
if (descriptorString[0] != '(') {
KLLogError("Cannot create KLMethodDescriptor with non-method desc " + descriptorString + ", using (void)void");
descriptorString = "()V";
}
this.descriptor = (' ' + descriptorString).slice(1);
this.argumentTypes = [];
this.return = null;
//
// Accessors
//
this.descriptorString = function() {
return this.descriptor;
}
this.argumentCount = function() {
return this.argumentTypes.length;
}
this.argumentTypeAtIndex = function(index) {
return this.argumentTypes[index];
}
this.returnsVoid = function() {
return this.return == null;
}
this.returnType = function() {
return this.return;
}
//
// Construction
//
let idx = 1;
while (descriptorString[idx] != ')') {
let start = idx;
let end = start;
while (descriptorString[end] == '[') {
end++;
}
if (descriptorString[end] == 'L') {
while (descriptorString[end] != ';') {
end++;
}
}
end += 1;
let thisArgDesc = descriptorString.substring(start, end);
let thisArgType = new JType(thisArgDesc);
this.argumentTypes.push(thisArgType);
idx = end;
}
// Everything from here to the end of the method desc is the return type.
// Void return is specified with "V" but that's not a real type, so if that's
// what we see, leave the return type null.
let returnDescriptor = descriptorString.substring(idx+1);
if (returnDescriptor != "V") {
this.return = new JType(returnDescriptor);
}
}