forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantPool.java
More file actions
418 lines (377 loc) · 12.6 KB
/
ConstantPool.java
File metadata and controls
418 lines (377 loc) · 12.6 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.classfile;
import org.mozilla.javascript.ObjToIntMap;
import org.mozilla.javascript.UintMap;
final class ConstantPool
{
ConstantPool(ClassFileWriter cfw)
{
this.cfw = cfw;
itsTopIndex = 1; // the zero'th entry is reserved
itsPool = new byte[ConstantPoolSize];
itsTop = 0;
}
private static final int ConstantPoolSize = 256;
static final byte
CONSTANT_Class = 7,
CONSTANT_Fieldref = 9,
CONSTANT_Methodref = 10,
CONSTANT_InterfaceMethodref = 11,
CONSTANT_String = 8,
CONSTANT_Integer = 3,
CONSTANT_Float = 4,
CONSTANT_Long = 5,
CONSTANT_Double = 6,
CONSTANT_NameAndType = 12,
CONSTANT_Utf8 = 1,
CONSTANT_MethodType = 16,
CONSTANT_MethodHandle = 15,
CONSTANT_InvokeDynamic = 18;
int write(byte[] data, int offset)
{
offset = ClassFileWriter.putInt16((short)itsTopIndex, data, offset);
System.arraycopy(itsPool, 0, data, offset, itsTop);
offset += itsTop;
return offset;
}
int getWriteSize()
{
return 2 + itsTop;
}
int addConstant(int k)
{
ensure(5);
itsPool[itsTop++] = CONSTANT_Integer;
itsTop = ClassFileWriter.putInt32(k, itsPool, itsTop);
itsPoolTypes.put(itsTopIndex, CONSTANT_Integer);
return (short)(itsTopIndex++);
}
int addConstant(long k)
{
ensure(9);
itsPool[itsTop++] = CONSTANT_Long;
itsTop = ClassFileWriter.putInt64(k, itsPool, itsTop);
int index = itsTopIndex;
itsTopIndex += 2;
itsPoolTypes.put(index, CONSTANT_Long);
return index;
}
int addConstant(float k)
{
ensure(5);
itsPool[itsTop++] = CONSTANT_Float;
int bits = Float.floatToIntBits(k);
itsTop = ClassFileWriter.putInt32(bits, itsPool, itsTop);
itsPoolTypes.put(itsTopIndex, CONSTANT_Float);
return itsTopIndex++;
}
int addConstant(double k)
{
ensure(9);
itsPool[itsTop++] = CONSTANT_Double;
long bits = Double.doubleToLongBits(k);
itsTop = ClassFileWriter.putInt64(bits, itsPool, itsTop);
int index = itsTopIndex;
itsTopIndex += 2;
itsPoolTypes.put(index, CONSTANT_Double);
return index;
}
int addConstant(String k)
{
int utf8Index = 0xFFFF & addUtf8(k);
int theIndex = itsStringConstHash.getInt(utf8Index, -1);
if (theIndex == -1) {
theIndex = itsTopIndex++;
ensure(3);
itsPool[itsTop++] = CONSTANT_String;
itsTop = ClassFileWriter.putInt16(utf8Index, itsPool, itsTop);
itsStringConstHash.put(utf8Index, theIndex);
}
itsPoolTypes.put(theIndex, CONSTANT_String);
return theIndex;
}
int addConstant(Object value) {
if (value instanceof Integer || value instanceof Byte
|| value instanceof Short) {
return addConstant(((Number) value).intValue());
} else if (value instanceof Character) {
return addConstant(((Character) value).charValue());
} else if (value instanceof Boolean) {
return addConstant(((Boolean) value).booleanValue() ? 1 : 0);
} else if (value instanceof Float) {
return addConstant(((Float) value).floatValue());
} else if (value instanceof Long) {
return addConstant(((Long) value).longValue());
} else if (value instanceof Double) {
return addConstant(((Double) value).doubleValue());
} else if (value instanceof String) {
return addConstant((String) value);
//} else if (value instanceof ClassFileWriter.MethodType) {
// return addMethodType((ClassFileWriter.MethodType) value);
} else if (value instanceof ClassFileWriter.MHandle) {
return addMethodHandle((ClassFileWriter.MHandle) value);
} else {
throw new IllegalArgumentException("value " + value);
}
}
boolean isUnderUtfEncodingLimit(String s)
{
int strLen = s.length();
if (strLen * 3 <= MAX_UTF_ENCODING_SIZE) {
return true;
} else if (strLen > MAX_UTF_ENCODING_SIZE) {
return false;
}
return strLen == getUtfEncodingLimit(s, 0, strLen);
}
/**
* Get maximum i such that <code>start <= i <= end</code> and
* <code>s.substring(start, i)</code> fits JVM UTF string encoding limit.
*/
int getUtfEncodingLimit(String s, int start, int end)
{
if ((end - start) * 3 <= MAX_UTF_ENCODING_SIZE) {
return end;
}
int limit = MAX_UTF_ENCODING_SIZE;
for (int i = start; i != end; i++) {
int c = s.charAt(i);
if (0 != c && c <= 0x7F) {
--limit;
} else if (c < 0x7FF) {
limit -= 2;
} else {
limit -= 3;
}
if (limit < 0) {
return i;
}
}
return end;
}
short addUtf8(String k)
{
int theIndex = itsUtf8Hash.get(k, -1);
if (theIndex == -1) {
int strLen = k.length();
boolean tooBigString;
if (strLen > MAX_UTF_ENCODING_SIZE) {
tooBigString = true;
} else {
tooBigString = false;
// Ask for worst case scenario buffer when each char takes 3
// bytes
ensure(1 + 2 + strLen * 3);
int top = itsTop;
itsPool[top++] = CONSTANT_Utf8;
top += 2; // skip length
char[] chars = cfw.getCharBuffer(strLen);
k.getChars(0, strLen, chars, 0);
for (int i = 0; i != strLen; i++) {
int c = chars[i];
if (c != 0 && c <= 0x7F) {
itsPool[top++] = (byte)c;
} else if (c > 0x7FF) {
itsPool[top++] = (byte)(0xE0 | (c >> 12));
itsPool[top++] = (byte)(0x80 | ((c >> 6) & 0x3F));
itsPool[top++] = (byte)(0x80 | (c & 0x3F));
} else {
itsPool[top++] = (byte)(0xC0 | (c >> 6));
itsPool[top++] = (byte)(0x80 | (c & 0x3F));
}
}
int utfLen = top - (itsTop + 1 + 2);
if (utfLen > MAX_UTF_ENCODING_SIZE) {
tooBigString = true;
} else {
// Write back length
itsPool[itsTop + 1] = (byte)(utfLen >>> 8);
itsPool[itsTop + 2] = (byte)utfLen;
itsTop = top;
theIndex = itsTopIndex++;
itsUtf8Hash.put(k, theIndex);
}
}
if (tooBigString) {
throw new IllegalArgumentException("Too big string");
}
}
setConstantData(theIndex, k);
itsPoolTypes.put(theIndex, CONSTANT_Utf8);
return (short)theIndex;
}
private short addNameAndType(String name, String type)
{
short nameIndex = addUtf8(name);
short typeIndex = addUtf8(type);
ensure(5);
itsPool[itsTop++] = CONSTANT_NameAndType;
itsTop = ClassFileWriter.putInt16(nameIndex, itsPool, itsTop);
itsTop = ClassFileWriter.putInt16(typeIndex, itsPool, itsTop);
itsPoolTypes.put(itsTopIndex, CONSTANT_NameAndType);
return (short)(itsTopIndex++);
}
short addClass(String className)
{
int theIndex = itsClassHash.get(className, -1);
if (theIndex == -1) {
String slashed = className;
if (className.indexOf('.') > 0) {
slashed = ClassFileWriter.getSlashedForm(className);
theIndex = itsClassHash.get(slashed, -1);
if (theIndex != -1) {
itsClassHash.put(className, theIndex);
}
}
if (theIndex == -1) {
int utf8Index = addUtf8(slashed);
ensure(3);
itsPool[itsTop++] = CONSTANT_Class;
itsTop = ClassFileWriter.putInt16(utf8Index, itsPool, itsTop);
theIndex = itsTopIndex++;
itsClassHash.put(slashed, theIndex);
if (!className.equals(slashed)) {
itsClassHash.put(className, theIndex);
}
}
}
setConstantData(theIndex, className);
itsPoolTypes.put(theIndex, CONSTANT_Class);
return (short)theIndex;
}
short addFieldRef(String className, String fieldName, String fieldType)
{
FieldOrMethodRef ref = new FieldOrMethodRef(className, fieldName,
fieldType);
int theIndex = itsFieldRefHash.get(ref, -1);
if (theIndex == -1) {
short ntIndex = addNameAndType(fieldName, fieldType);
short classIndex = addClass(className);
ensure(5);
itsPool[itsTop++] = CONSTANT_Fieldref;
itsTop = ClassFileWriter.putInt16(classIndex, itsPool, itsTop);
itsTop = ClassFileWriter.putInt16(ntIndex, itsPool, itsTop);
theIndex = itsTopIndex++;
itsFieldRefHash.put(ref, theIndex);
}
setConstantData(theIndex, ref);
itsPoolTypes.put(theIndex, CONSTANT_Fieldref);
return (short)theIndex;
}
short addMethodRef(String className, String methodName,
String methodType)
{
FieldOrMethodRef ref = new FieldOrMethodRef(className, methodName,
methodType);
int theIndex = itsMethodRefHash.get(ref, -1);
if (theIndex == -1) {
short ntIndex = addNameAndType(methodName, methodType);
short classIndex = addClass(className);
ensure(5);
itsPool[itsTop++] = CONSTANT_Methodref;
itsTop = ClassFileWriter.putInt16(classIndex, itsPool, itsTop);
itsTop = ClassFileWriter.putInt16(ntIndex, itsPool, itsTop);
theIndex = itsTopIndex++;
itsMethodRefHash.put(ref, theIndex);
}
setConstantData(theIndex, ref);
itsPoolTypes.put(theIndex, CONSTANT_Methodref);
return (short)theIndex;
}
short addInterfaceMethodRef(String className,
String methodName, String methodType)
{
short ntIndex = addNameAndType(methodName, methodType);
short classIndex = addClass(className);
ensure(5);
itsPool[itsTop++] = CONSTANT_InterfaceMethodref;
itsTop = ClassFileWriter.putInt16(classIndex, itsPool, itsTop);
itsTop = ClassFileWriter.putInt16(ntIndex, itsPool, itsTop);
FieldOrMethodRef r = new FieldOrMethodRef(className, methodName,
methodType);
setConstantData(itsTopIndex, r);
itsPoolTypes.put(itsTopIndex, CONSTANT_InterfaceMethodref);
return (short)(itsTopIndex++);
}
short addInvokeDynamic(String methodName, String methodType, int bootstrapIndex)
{
ConstantEntry entry = new ConstantEntry(CONSTANT_InvokeDynamic,
bootstrapIndex, methodName, methodType);
int theIndex = itsConstantHash.get(entry, -1);
if (theIndex == -1) {
short nameTypeIndex = addNameAndType(methodName, methodType);
ensure(5);
itsPool[itsTop++] = CONSTANT_InvokeDynamic;
itsTop = ClassFileWriter.putInt16(bootstrapIndex, itsPool, itsTop);
itsTop = ClassFileWriter.putInt16(nameTypeIndex, itsPool, itsTop);
theIndex = itsTopIndex++;
itsConstantHash.put(entry, theIndex);
setConstantData(theIndex, methodType);
itsPoolTypes.put(theIndex, CONSTANT_InvokeDynamic);
}
return (short)(theIndex);
}
short addMethodHandle(ClassFileWriter.MHandle mh)
{
int theIndex = itsConstantHash.get(mh, -1);
if (theIndex == -1) {
short ref;
if (mh.tag <= ByteCode.MH_PUTSTATIC) {
ref = addFieldRef(mh.owner, mh.name, mh.desc);
} else if (mh.tag == ByteCode.MH_INVOKEINTERFACE) {
ref = addInterfaceMethodRef(mh.owner, mh.name, mh.desc);
} else {
ref = addMethodRef(mh.owner, mh.name, mh.desc);
}
ensure(4);
itsPool[itsTop++] = CONSTANT_MethodHandle;
itsPool[itsTop++] = mh.tag;
itsTop = ClassFileWriter.putInt16(ref, itsPool, itsTop);
theIndex = itsTopIndex++;
itsConstantHash.put(mh, theIndex);
itsPoolTypes.put(theIndex, CONSTANT_MethodHandle);
}
return (short)(theIndex);
}
Object getConstantData(int index)
{
return itsConstantData.getObject(index);
}
void setConstantData(int index, Object data)
{
itsConstantData.put(index, data);
}
byte getConstantType(int index)
{
return (byte) itsPoolTypes.getInt(index, 0);
}
private void ensure(int howMuch)
{
if (itsTop + howMuch > itsPool.length) {
int newCapacity = itsPool.length * 2;
if (itsTop + howMuch > newCapacity) {
newCapacity = itsTop + howMuch;
}
byte[] tmp = new byte[newCapacity];
System.arraycopy(itsPool, 0, tmp, 0, itsTop);
itsPool = tmp;
}
}
private ClassFileWriter cfw;
private static final int MAX_UTF_ENCODING_SIZE = 65535;
private UintMap itsStringConstHash = new UintMap();
private ObjToIntMap itsUtf8Hash = new ObjToIntMap();
private ObjToIntMap itsFieldRefHash = new ObjToIntMap();
private ObjToIntMap itsMethodRefHash = new ObjToIntMap();
private ObjToIntMap itsClassHash = new ObjToIntMap();
private ObjToIntMap itsConstantHash = new ObjToIntMap();
private int itsTop;
private int itsTopIndex;
private UintMap itsConstantData = new UintMap();
private UintMap itsPoolTypes = new UintMap();
private byte itsPool[];
}