forked from mozilla/pluotsorbet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.ts
298 lines (280 loc) · 8.13 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
module J2ME {
import assert = Debug.assert;
var writer = new IndentingWriter();
export enum Kind {
Boolean,
Byte,
Short,
Char,
Int,
Float,
Long,
Double,
Reference,
Void,
Illegal,
Store
}
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 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 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 kindCharacter(kind: Kind): string {
switch (kind) {
case Kind.Boolean:
return 'Z';
case Kind.Byte:
return 'B';
case Kind.Short:
return 'S';
case Kind.Char:
return 'C';
case Kind.Int:
return 'I';
case Kind.Float:
return 'F';
case Kind.Long:
return 'J';
case Kind.Double:
return 'D';
case Kind.Reference:
return 'R';
case Kind.Void:
return 'V';
}
}
export function getKindCheck(kind: Kind): (x: any) => boolean {
switch (kind) {
case Kind.Boolean:
return (x) => x === 0 || x === 1;
case Kind.Byte:
return (x) => (x | 0) === x && x >= Constants.BYTE_MIN && x <= Constants.BYTE_MAX;
case Kind.Short:
return (x) => (x | 0) === x && x >= Constants.SHORT_MIN && x <= Constants.SHORT_MAX;
case Kind.Char:
return (x) => (x | 0) === x && x >= Constants.CHAR_MIN && x <= Constants.CHAR_MAX;
case Kind.Int:
return (x) => (x | 0) === x;
case Kind.Float:
return (x) => isNaN(x) || Math.fround(x) === x;
case Kind.Long:
return (x) => x instanceof Long.constructor;
case Kind.Double:
return (x) => isNaN(x) || (+x) === x;
case Kind.Reference:
return (x) => x === null || x instanceof Object;
case Kind.Void:
return (x) => typeof x === "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;
}
}