This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
types.ts
318 lines (299 loc) · 8.9 KB
/
types.ts
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
module J2ME {
import assert = Debug.assert;
import Bytecodes = Bytecode.Bytecodes;
var writer = new IndentingWriter();
export const enum Kind {
Boolean,
Byte,
Short,
Char,
Int,
Float,
Long,
Double,
Reference,
Void,
Illegal,
High,
Store
}
export function getKindName(kind: Kind): string {
return (<any>J2ME).Kind[kind];
}
export function isTwoSlot(kind: Kind) {
return kind === Kind.Long || kind === Kind.Double;
}
export var valueKinds = [
Kind.Boolean,
Kind.Char,
Kind.Float,
Kind.Double,
Kind.Byte,
Kind.Short,
Kind.Int,
Kind.Long
];
export function returnKind(op: Bytecodes): Kind {
switch (op) {
case Bytecodes.LRETURN:
return Kind.Long;
case Bytecodes.DRETURN:
return Kind.Double;
case Bytecodes.IRETURN:
return Kind.Int;
case Bytecodes.FRETURN:
return Kind.Float;
case Bytecodes.ARETURN:
return Kind.Reference;
case Bytecodes.RETURN:
return Kind.Void;
}
}
export function stackKind(kind: Kind): Kind {
switch (kind) {
case Kind.Boolean: return Kind.Int;
case Kind.Byte: return Kind.Int;
case Kind.Short: return Kind.Int;
case Kind.Char: return Kind.Int;
case Kind.Int: return Kind.Int;
case Kind.Float: return Kind.Float;
case Kind.Long: return Kind.Long;
case Kind.Double: return Kind.Double;
case Kind.Reference: return Kind.Reference;
default: throw Debug.unexpected("Unknown stack kind: " + kind);
}
}
export function kindCharacterToKind(kindCharacter: string): Kind {
switch (kindCharacter[0]) {
case 'Z': return Kind.Boolean;
case 'B': return Kind.Byte;
case 'S': return Kind.Short;
case 'C': return Kind.Char;
case 'I': return Kind.Int;
case 'F': return Kind.Float;
case 'J': return Kind.Long;
case 'D': return Kind.Double;
case 'V': return Kind.Void;
case '[': // Fallthrough
case 'L': return Kind.Reference;
default: throw Debug.unexpected("Unknown kind character: " + kindCharacter);
}
}
export function arrayTypeCodeToKind(typeCode): Kind {
switch (typeCode) {
case 4: return Kind.Boolean;
case 5: return Kind.Char;
case 6: return Kind.Float;
case 7: return Kind.Double;
case 8: return Kind.Byte;
case 9: return Kind.Short;
case 10: return Kind.Int;
case 11: return Kind.Long;
default: throw Debug.unexpected("Unknown array type code: " + typeCode);
}
}
export function kindSize(kind: Kind): number {
if (isTwoSlot(kind)) {
return 8;
}
return 4;
}
export function getKindCheck(kind: Kind): (l: any, h: number) => boolean {
switch (kind) {
case Kind.Boolean:
return (l, h) => l === 0 || l === 1;
case Kind.Byte:
return (l, h) => (l | 0) === l && l >= Constants.BYTE_MIN && l <= Constants.BYTE_MAX;
case Kind.Short:
return (l, h) => (l | 0) === l && l >= Constants.SHORT_MIN && l <= Constants.SHORT_MAX;
case Kind.Char:
return (l, h) => (l | 0) === l && l >= Constants.CHAR_MIN && l <= Constants.CHAR_MAX;
case Kind.Int:
return (l, h) => (l | 0) === l;
case Kind.Float:
return (l, h) => (l | 0) === l;
case Kind.Long:
return (l, h) => ((l | 0) === l) && ((h | 0) === h);
case Kind.Double:
return (l, h) => ((l | 0) === l) && ((h | 0) === h);
case Kind.Reference:
return (l, h) => l === null || l instanceof Object;
case Kind.Void:
return (l, h) => typeof l === "undefined";
default:
throw Debug.unexpected("Unknown kind: " + kind);
}
}
export function getSignatureKind(signature: Uint8Array): Kind {
switch (signature[0]) {
case UTF8Chars.Z:
return Kind.Boolean;
case UTF8Chars.B:
return Kind.Byte;
case UTF8Chars.S:
return Kind.Short;
case UTF8Chars.C:
return Kind.Char;
case UTF8Chars.I:
return Kind.Int;
case UTF8Chars.F:
return Kind.Float;
case UTF8Chars.J:
return Kind.Long;
case UTF8Chars.D:
return Kind.Double;
case UTF8Chars.OpenBracket:
case UTF8Chars.L:
return Kind.Reference;
case UTF8Chars.V:
return Kind.Void;
}
}
/**
* MethodDescriptor:
* ( ParameterDescriptor* ) ReturnDescriptor
* ParameterDescriptor:
* FieldType
* ReturnDescriptor:
* FieldType
* VoidDescriptor
* VoidDescriptor:
* V
* FieldDescriptor:
* FieldType
* FieldType:
* BaseType
* ObjectType
* ArrayType
* BaseType:
* B
* C
* D
* F
* I
* J
* S
* Z
* ObjectType:
* L ClassName ;
* ArrayType:
* [ ComponentType
* ComponentType:
* FieldType
*/
// Global state for signature parsing, kind of hackish but fast.
var globalNextIndex = 0;
var descriptorKinds = [];
/**
* Returns an array of kinds that appear in a method signature. The first element is always the
* return kind. The returned array is shared, so you if you need a copy of it, you'll need to
* clone it.
*
* The parsing algorithm needs some global state to keep track of the current position in the
* descriptor, namely |globalNextIndex| which always points to the next index in the descriptor
* after a token has been consumed.
*/
export function parseMethodDescriptorKinds(value: Uint8Array, startIndex: number): Kind [] {
globalNextIndex = 0;
if ((startIndex > value.length - 3) || value[startIndex] !== UTF8Chars.OpenParenthesis) {
assert(false, "Invalid method signature.");
}
descriptorKinds.length = 0;
descriptorKinds.push(Kind.Void); // placeholder until the return type is parsed
var i = startIndex + 1;
while (value[i] !== UTF8Chars.CloseParenthesis) {
var kind = parseTypeDescriptorKind(value, i);
descriptorKinds.push(kind);
i = globalNextIndex;
if (i >= value.length) {
assert(false, "Invalid method signature.");
}
}
i++;
var kind = parseTypeDescriptorKind(value, i);
if (globalNextIndex !== value.length) {
assert(false, "Invalid method signature.");
}
// Plug in the return type
descriptorKinds[0] = kind;
return descriptorKinds;
}
function parseTypeDescriptorKind(value: Uint8Array, startIndex: number): Kind {
globalNextIndex = startIndex + 1;
switch (value[startIndex]) {
case UTF8Chars.Z:
return Kind.Boolean;
case UTF8Chars.B:
return Kind.Byte;
case UTF8Chars.C:
return Kind.Char;
case UTF8Chars.D:
return Kind.Double;
case UTF8Chars.F:
return Kind.Float;
case UTF8Chars.I:
return Kind.Int;
case UTF8Chars.J:
return Kind.Long;
case UTF8Chars.S:
return Kind.Short;
case UTF8Chars.V:
return Kind.Void;
case UTF8Chars.L: {
// parse a slashified Java class name
var endIndex = parseClassNameKind(value, startIndex + 1, UTF8Chars.Slash);
if (endIndex > startIndex + 1 && endIndex < value.length && value[endIndex] === UTF8Chars.Semicolon) {
globalNextIndex = endIndex + 1;
return Kind.Reference;
}
Debug.unexpected("Invalid signature.");
}
case UTF8Chars.OpenBracket: {
// compute the number of dimensions
var index = startIndex;
while (index < value.length && value[index] === UTF8Chars.OpenBracket) {
index++;
}
var dimensions = index - startIndex;
if (dimensions > 255) {
Debug.unexpected("Array with more than 255 dimensions.");
}
var component = parseTypeDescriptorKind(value, index);
return Kind.Reference;
}
default:
Debug.unexpected("Unexpected type descriptor prefix: " + value[startIndex]);
}
}
function parseClassNameKind(value: Uint8Array, index: number, separator: number): number {
var position = index;
var length = value.length;
while (position < length) {
var nextch = value[position];
if (nextch === UTF8Chars.Dot || nextch === UTF8Chars.Slash) {
if (separator !== nextch) {
return position;
}
} else if (nextch === UTF8Chars.Semicolon || nextch === UTF8Chars.OpenBracket) {
return position;
}
position++;
}
return position;
}
export function signatureHasTwoSlotArguments(signatureKinds: Kind []): boolean {
for (var i = 1; i < signatureKinds.length; i++) {
if (isTwoSlot(signatureKinds[i])) {
return true;
}
}
return false;
}
export function signatureArgumentSlotCount(signatureKinds: Kind []): number {
var count = 0;
for (var i = 1; i < signatureKinds.length; i++) {
count += isTwoSlot(signatureKinds[i]) ? 2 : 1;
}
return count;
}
}