forked from jruby/jruby
/
InheritedCacheCompiler.java
614 lines (545 loc) · 27.7 KB
/
InheritedCacheCompiler.java
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.compiler.impl;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jcodings.Encoding;
import org.jruby.Ruby;
import org.jruby.RubyEncoding;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyModule;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.ast.NodeType;
import org.jruby.ast.executable.AbstractScript;
import org.jruby.ast.executable.RuntimeCache;
import org.jruby.compiler.ASTInspector;
import org.jruby.compiler.CacheCompiler;
import org.jruby.compiler.CompilerCallback;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.BlockBody;
import org.jruby.runtime.CallSite;
import org.jruby.runtime.CallType;
import org.jruby.runtime.CompiledBlockCallback;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.encoding.EncodingService;
import org.jruby.util.ByteList;
import org.objectweb.asm.Label;
import static org.jruby.util.CodegenUtils.*;
/**
*
* @author headius
*/
public class InheritedCacheCompiler implements CacheCompiler {
protected StandardASMCompiler scriptCompiler;
int scopeCount = 0;
int callSiteCount = 0;
List<String> callSiteList = new ArrayList<String>();
List<CallType> callTypeList = new ArrayList<CallType>();
Map<String, Integer> stringIndices = new HashMap<String, Integer>();
Map<String, Integer> encodingIndices = new HashMap<String, Integer>();
Map<String, Integer> stringEncodings = new HashMap<String, Integer>();
Map<String, Integer> symbolIndices = new HashMap<String, Integer>();
Map<Long, Integer> fixnumIndices = new HashMap<Long, Integer>();
Map<Double, Integer> floatIndices = new HashMap<Double, Integer>();
int inheritedSymbolCount = 0;
int inheritedStringCount = 0;
int inheritedEncodingCount = 0;
int inheritedRegexpCount = 0;
int inheritedBigIntegerCount = 0;
int inheritedVariableReaderCount = 0;
int inheritedVariableWriterCount = 0;
int inheritedFixnumCount = 0;
int inheritedFloatCount = 0;
int inheritedConstantCount = 0;
int inheritedBlockBodyCount = 0;
int inheritedBlockCallbackCount = 0;
int inheritedMethodCount = 0;
boolean runtimeCacheInited = false;
public InheritedCacheCompiler(StandardASMCompiler scriptCompiler) {
this.scriptCompiler = scriptCompiler;
}
public void cacheStaticScope(BaseBodyCompiler method, StaticScope scope) {
String scopeString = RuntimeHelpers.encodeScope(scope);
// retrieve scope from scopes array
method.loadThis();
method.loadThreadContext();
method.method.ldc(scopeString);
if (scopeCount < AbstractScript.NUMBERED_SCOPE_COUNT) {
// use numbered access method
method.method.invokevirtual(scriptCompiler.getClassname(), "getScope" + scopeCount, sig(StaticScope.class, ThreadContext.class, String.class));
} else {
method.method.pushInt(scopeCount);
method.method.invokevirtual(scriptCompiler.getClassname(), "getScope", sig(StaticScope.class, ThreadContext.class, String.class, int.class));
}
scopeCount++;
}
public void cacheCallSite(BaseBodyCompiler method, String name, CallType callType) {
// retrieve call site from sites array
method.loadThis();
if (callSiteCount < AbstractScript.NUMBERED_CALLSITE_COUNT) {
// use numbered access method
method.method.invokevirtual(scriptCompiler.getClassname(), "getCallSite" + callSiteCount, sig(CallSite.class));
} else {
method.method.pushInt(callSiteCount);
method.method.invokevirtual(scriptCompiler.getClassname(), "getCallSite", sig(CallSite.class, int.class));
}
// add name to call site list
callSiteList.add(name);
callTypeList.add(callType);
callSiteCount++;
}
public void cacheSymbol(BaseBodyCompiler method, String symbol) {
Integer index = symbolIndices.get(symbol);
if (index == null) {
index = Integer.valueOf(inheritedSymbolCount++);
symbolIndices.put(symbol, index);
}
method.loadThis();
method.loadThreadContext();
if (index < AbstractScript.NUMBERED_SYMBOL_COUNT) {
method.method.ldc(symbol);
method.method.invokevirtual(scriptCompiler.getClassname(), "getSymbol" + index, sig(RubySymbol.class, ThreadContext.class, String.class));
} else {
method.method.ldc(index.intValue());
method.method.ldc(symbol);
method.method.invokevirtual(scriptCompiler.getClassname(), "getSymbol", sig(RubySymbol.class, ThreadContext.class, int.class, String.class));
}
}
public void cacheRegexp(BaseBodyCompiler method, ByteList pattern, int options) {
method.loadThis();
method.loadThreadContext();
int index = inheritedRegexpCount++;
if (index < AbstractScript.NUMBERED_REGEXP_COUNT) {
cacheByteList(method, pattern);
method.method.ldc(options);
method.method.invokevirtual(scriptCompiler.getClassname(), "getRegexp" + index, sig(RubyRegexp.class, ThreadContext.class, ByteList.class, int.class));
} else {
method.method.pushInt(index);
cacheByteList(method, pattern);
method.method.ldc(options);
method.method.invokevirtual(scriptCompiler.getClassname(), "getRegexp", sig(RubyRegexp.class, ThreadContext.class, int.class, ByteList.class, int.class));
}
}
public void cacheDRegexp(BaseBodyCompiler method, CompilerCallback createStringCallback, int options) {
int index = inheritedRegexpCount++;
Label alreadyCompiled = new Label();
method.loadThis();
method.method.getfield(scriptCompiler.getClassname(), "runtimeCache", ci(RuntimeCache.class));
method.method.pushInt(index);
method.method.invokevirtual(p(RuntimeCache.class), "getRegexp", sig(RubyRegexp.class, int.class));
method.method.dup();
method.ifNotNull(alreadyCompiled);
method.method.pop();
method.loadThis();
method.method.getfield(scriptCompiler.getClassname(), "runtimeCache", ci(RuntimeCache.class));
method.method.pushInt(index);
createStringCallback.call(method);
method.method.ldc(options);
method.method.invokevirtual(p(RuntimeCache.class), "cacheRegexp", sig(RubyRegexp.class, int.class, RubyString.class, int.class));
method.method.label(alreadyCompiled);
}
public void cacheFixnum(BaseBodyCompiler method, long value) {
if (value <= 5 && value >= -1) {
method.loadRuntime();
switch ((int)value) {
case -1:
method.method.invokestatic(p(RubyFixnum.class), "minus_one", sig(RubyFixnum.class, Ruby.class));
break;
case 0:
method.method.invokestatic(p(RubyFixnum.class), "zero", sig(RubyFixnum.class, Ruby.class));
break;
case 1:
method.method.invokestatic(p(RubyFixnum.class), "one", sig(RubyFixnum.class, Ruby.class));
break;
case 2:
method.method.invokestatic(p(RubyFixnum.class), "two", sig(RubyFixnum.class, Ruby.class));
break;
case 3:
method.method.invokestatic(p(RubyFixnum.class), "three", sig(RubyFixnum.class, Ruby.class));
break;
case 4:
method.method.invokestatic(p(RubyFixnum.class), "four", sig(RubyFixnum.class, Ruby.class));
break;
case 5:
method.method.invokestatic(p(RubyFixnum.class), "five", sig(RubyFixnum.class, Ruby.class));
break;
default:
throw new RuntimeException("wtf?");
}
} else {
Integer index = fixnumIndices.get(value);
if (index == null) {
index = Integer.valueOf(inheritedFixnumCount++);
fixnumIndices.put(value, index);
}
method.loadThis();
method.loadThreadContext();
if (value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE) {
if (index < AbstractScript.NUMBERED_FIXNUM_COUNT) {
method.method.pushInt((int)value);
method.method.invokevirtual(scriptCompiler.getClassname(), "getFixnum" + index, sig(RubyFixnum.class, ThreadContext.class, int.class));
} else {
method.method.pushInt(index.intValue());
method.method.pushInt((int)value);
method.method.invokevirtual(scriptCompiler.getClassname(), "getFixnum", sig(RubyFixnum.class, ThreadContext.class, int.class, int.class));
}
} else {
method.method.pushInt(index.intValue());
method.method.ldc(value);
method.method.invokevirtual(scriptCompiler.getClassname(), "getFixnum", sig(RubyFixnum.class, ThreadContext.class, int.class, long.class));
}
}
}
public void cacheFloat(BaseBodyCompiler method, double value) {
Integer index = Integer.valueOf(inheritedFloatCount++);
floatIndices.put(value, index);
method.loadThis();
method.loadThreadContext();
if (index < AbstractScript.NUMBERED_FLOAT_COUNT) {
method.method.ldc(value);
method.method.invokevirtual(scriptCompiler.getClassname(), "getFloat" + index, sig(RubyFloat.class, ThreadContext.class, double.class));
} else {
method.method.pushInt(index.intValue());
method.method.ldc(value);
method.method.invokevirtual(scriptCompiler.getClassname(), "getFloat", sig(RubyFloat.class, ThreadContext.class, int.class, double.class));
}
}
public void cacheConstant(BaseBodyCompiler method, String constantName) {
method.loadThis();
method.loadThreadContext();
method.method.ldc(constantName);
if (inheritedConstantCount < AbstractScript.NUMBERED_CONSTANT_COUNT) {
method.method.invokevirtual(scriptCompiler.getClassname(), "getConstant" + inheritedConstantCount, sig(IRubyObject.class, ThreadContext.class, String.class));
} else {
method.method.pushInt(inheritedConstantCount);
method.method.invokevirtual(scriptCompiler.getClassname(), "getConstant", sig(IRubyObject.class, ThreadContext.class, String.class, int.class));
}
inheritedConstantCount++;
}
public void cacheConstantFrom(BaseBodyCompiler method, String constantName) {
// module is on top of stack
method.loadThis();
method.method.swap();
method.loadThreadContext();
method.method.ldc(constantName);
if (inheritedConstantCount < AbstractScript.NUMBERED_CONSTANT_COUNT) {
method.method.invokevirtual(scriptCompiler.getClassname(), "getConstantFrom" + inheritedConstantCount, sig(IRubyObject.class, RubyModule.class, ThreadContext.class, String.class));
} else {
method.method.pushInt(inheritedConstantCount);
method.method.invokevirtual(scriptCompiler.getClassname(), "getConstantFrom", sig(IRubyObject.class, RubyModule.class, ThreadContext.class, String.class, int.class));
}
inheritedConstantCount++;
}
public void cacheString(BaseBodyCompiler method, ByteList contents, int codeRange) {
String asString = RuntimeHelpers.rawBytesToString(contents.bytes());
Integer index = stringIndices.get(asString);
if (index == null) {
index = Integer.valueOf(inheritedStringCount++);
stringIndices.put(asString, index);
stringEncodings.put(asString, cacheEncoding(contents.getEncoding()));
}
method.loadThis();
method.loadThreadContext();
if (index < AbstractScript.NUMBERED_STRING_COUNT) {
method.method.pushInt(codeRange);
method.method.invokevirtual(scriptCompiler.getClassname(), "getString" + index, sig(RubyString.class, ThreadContext.class, int.class));
} else {
method.method.pushInt(index.intValue());
method.method.pushInt(codeRange);
method.method.invokevirtual(scriptCompiler.getClassname(), "getString", sig(RubyString.class, ThreadContext.class, int.class, int.class));
}
}
public void cacheByteList(BaseBodyCompiler method, ByteList contents) {
String asString = RuntimeHelpers.rawBytesToString(contents.bytes());
Integer index = stringIndices.get(asString);
if (index == null) {
index = Integer.valueOf(inheritedStringCount++);
stringIndices.put(asString, index);
stringEncodings.put(asString, cacheEncoding(contents.getEncoding()));
}
method.loadThis();
if (index < AbstractScript.NUMBERED_STRING_COUNT) {
method.method.invokevirtual(scriptCompiler.getClassname(), "getByteList" + index, sig(ByteList.class));
} else {
method.method.pushInt(index.intValue());
method.method.invokevirtual(scriptCompiler.getClassname(), "getByteList", sig(ByteList.class, int.class));
}
}
public void cacheEncoding(BaseBodyCompiler method, Encoding encoding) {
// split into three methods since ByteList depends on two parts in different places
int encodingIndex = cacheEncoding(encoding);
loadEncoding(method.method, encodingIndex);
createRubyEncoding(method);
}
private int cacheEncoding(Encoding encoding) {
String encodingName = new String(encoding.getName());
Integer index = encodingIndices.get(encodingName);
if (index == null) {
index = Integer.valueOf(inheritedEncodingCount++);
encodingIndices.put(encodingName, index);
}
return index;
}
private void loadEncoding(SkinnyMethodAdapter method, int encodingIndex) {
method.aload(0);
if (encodingIndex < AbstractScript.NUMBERED_ENCODING_COUNT) {
method.invokevirtual(scriptCompiler.getClassname(), "getEncoding" + encodingIndex, sig(Encoding.class));
} else {
method.pushInt(encodingIndex);
method.invokevirtual(scriptCompiler.getClassname(), "getEncoding", sig(Encoding.class, int.class));
}
}
private void createRubyEncoding(BaseBodyCompiler method) {
method.loadRuntime();
method.invokeRuby("getEncodingService", sig(EncodingService.class));
method.method.swap();
method.method.invokevirtual(p(EncodingService.class), "getEncoding", sig(RubyEncoding.class, Encoding.class));
}
public void cacheBigInteger(BaseBodyCompiler method, BigInteger bigint) {
method.loadThis();
int index = inheritedBigIntegerCount++;
if (index < AbstractScript.NUMBERED_BIGINTEGER_COUNT) {
method.method.ldc(bigint.toString(16));
method.method.invokevirtual(scriptCompiler.getClassname(), "getBigInteger" + index, sig(BigInteger.class, String.class));
} else {
method.method.pushInt(index);
method.method.ldc(bigint.toString(16));
method.method.invokevirtual(scriptCompiler.getClassname(), "getBigInteger", sig(BigInteger.class, int.class, String.class));
}
}
public void cachedGetVariable(BaseBodyCompiler method, String name) {
method.loadThis();
method.loadThreadContext();
int index = inheritedVariableReaderCount++;
if (index < AbstractScript.NUMBERED_VARIABLEREADER_COUNT) {
method.method.ldc(name);
method.loadSelf();
method.method.invokevirtual(scriptCompiler.getClassname(), "getVariable" + index, sig(IRubyObject.class, ThreadContext.class, String.class, IRubyObject.class));
} else {
method.method.pushInt(index);
method.method.ldc(name);
method.loadSelf();
method.method.invokevirtual(scriptCompiler.getClassname(), "getVariable", sig(IRubyObject.class, ThreadContext.class, int.class, String.class, IRubyObject.class));
}
}
public void cachedSetVariable(BaseBodyCompiler method, String name, CompilerCallback valueCallback) {
method.loadThis();
int index = inheritedVariableWriterCount++;
if (index < AbstractScript.NUMBERED_VARIABLEWRITER_COUNT) {
method.method.ldc(name);
method.loadSelf();
valueCallback.call(method);
method.method.invokevirtual(scriptCompiler.getClassname(), "setVariable" + index, sig(IRubyObject.class, String.class, IRubyObject.class, IRubyObject.class));
} else {
method.method.pushInt(index);
method.method.ldc(name);
method.loadSelf();
valueCallback.call(method);
method.method.invokevirtual(scriptCompiler.getClassname(), "setVariable", sig(IRubyObject.class, int.class, String.class, IRubyObject.class, IRubyObject.class));
}
}
public void cacheClosure(BaseBodyCompiler method, String closureMethod, int arity, StaticScope scope, String file, int line, boolean hasMultipleArgsHead, NodeType argsNodeId, ASTInspector inspector) {
String descriptor = RuntimeHelpers.buildBlockDescriptor(
closureMethod,
arity,
scope,
file,
line,
hasMultipleArgsHead,
argsNodeId,
inspector);
method.loadThis();
method.loadThreadContext();
if (inheritedBlockBodyCount < AbstractScript.NUMBERED_BLOCKBODY_COUNT) {
method.method.ldc(descriptor);
method.method.invokevirtual(scriptCompiler.getClassname(), "getBlockBody" + inheritedBlockBodyCount, sig(BlockBody.class, ThreadContext.class, String.class));
} else {
method.method.pushInt(inheritedBlockBodyCount);
method.method.ldc(descriptor);
method.method.invokevirtual(scriptCompiler.getClassname(), "getBlockBody", sig(BlockBody.class, ThreadContext.class, int.class, String.class));
}
inheritedBlockBodyCount++;
}
public void cacheClosure19(BaseBodyCompiler method, String closureMethod, int arity, StaticScope scope, String file, int line, boolean hasMultipleArgsHead, NodeType argsNodeId, String parameterList, ASTInspector inspector) {
String descriptor = RuntimeHelpers.buildBlockDescriptor19(
closureMethod,
arity,
scope,
file,
line,
hasMultipleArgsHead,
argsNodeId,
parameterList,
inspector);
method.loadThis();
method.loadThreadContext();
if (inheritedBlockBodyCount < AbstractScript.NUMBERED_BLOCKBODY_COUNT) {
method.method.ldc(descriptor);
method.method.invokevirtual(scriptCompiler.getClassname(), "getBlockBody19" + inheritedBlockBodyCount, sig(BlockBody.class, ThreadContext.class, String.class));
} else {
method.method.pushInt(inheritedBlockBodyCount);
method.method.ldc(descriptor);
method.method.invokevirtual(scriptCompiler.getClassname(), "getBlockBody19", sig(BlockBody.class, ThreadContext.class, int.class, String.class));
}
inheritedBlockBodyCount++;
}
public void cacheSpecialClosure(BaseBodyCompiler method, String closureMethod) {
method.loadThis();
if (inheritedBlockCallbackCount < AbstractScript.NUMBERED_BLOCKCALLBACK_COUNT) {
method.method.ldc(closureMethod);
method.method.invokevirtual(scriptCompiler.getClassname(), "getBlockCallback" + inheritedBlockCallbackCount, sig(CompiledBlockCallback.class, String.class));
} else {
method.method.pushInt(inheritedBlockCallbackCount);
method.method.ldc(closureMethod);
method.method.invokevirtual(scriptCompiler.getClassname(), "getBlockCallback", sig(CompiledBlockCallback.class, int.class, String.class));
}
inheritedBlockCallbackCount++;
}
public void cacheMethod(BaseBodyCompiler method, String methodName) {
method.loadThis();
method.loadThreadContext();
method.loadSelf();
if (inheritedMethodCount < AbstractScript.NUMBERED_METHOD_COUNT) {
method.method.ldc(methodName);
method.method.invokevirtual(scriptCompiler.getClassname(), "getMethod" + inheritedMethodCount, sig(DynamicMethod.class, ThreadContext.class, IRubyObject.class, String.class));
} else {
method.method.pushInt(inheritedMethodCount);
method.method.ldc(methodName);
method.method.invokevirtual(scriptCompiler.getClassname(), "getMethod", sig(DynamicMethod.class, ThreadContext.class, IRubyObject.class, int.class, String.class));
}
inheritedMethodCount++;
}
public void cacheMethod(BaseBodyCompiler method, String methodName, int receiverLocal) {
method.loadThis();
method.loadThreadContext();
method.method.aload(receiverLocal);
if (inheritedMethodCount < AbstractScript.NUMBERED_METHOD_COUNT) {
method.method.ldc(methodName);
method.method.invokevirtual(scriptCompiler.getClassname(), "getMethod" + inheritedMethodCount, sig(DynamicMethod.class, ThreadContext.class, IRubyObject.class, String.class));
} else {
method.method.pushInt(inheritedMethodCount);
method.method.ldc(methodName);
method.method.invokevirtual(scriptCompiler.getClassname(), "getMethod", sig(DynamicMethod.class, ThreadContext.class, IRubyObject.class, int.class, String.class));
}
inheritedMethodCount++;
}
public void finish() {
SkinnyMethodAdapter initMethod = scriptCompiler.getInitMethod();
// generate call sites portion of descriptor
int callSiteListSize = callSiteList.size();
int otherCount = scopeCount
+ inheritedSymbolCount
+ inheritedFixnumCount
+ inheritedFloatCount
+ inheritedConstantCount
+ inheritedRegexpCount
+ inheritedBigIntegerCount
+ inheritedVariableReaderCount
+ inheritedVariableWriterCount
+ inheritedBlockBodyCount
+ inheritedBlockCallbackCount
+ inheritedMethodCount
+ inheritedStringCount
+ inheritedEncodingCount;
if (callSiteListSize + otherCount != 0) {
ensureRuntimeCacheInited(initMethod);
StringBuffer descriptor = new StringBuffer(callSiteListSize * 5 + 12); // rough guess of total size
for (int i = 0; i < callSiteListSize; i++) {
String name = callSiteList.get(i);
CallType callType = callTypeList.get(i);
if (i > 0) descriptor.append('\uFFFF');
if (callType.equals(CallType.NORMAL)) {
descriptor.append(name).append("\uFFFFN");
} else if (callType.equals(CallType.FUNCTIONAL)) {
descriptor.append(name).append("\uFFFFF");
} else if (callType.equals(CallType.VARIABLE)) {
descriptor.append(name).append("\uFFFFV");
} else if (callType.equals(CallType.SUPER)) {
descriptor.append("super").append("\uFFFFS");
}
}
// generate "others" part of descriptor
descriptor.append('\uFFFF');
descriptor.append((char)scopeCount);
descriptor.append((char)inheritedSymbolCount);
descriptor.append((char)inheritedFixnumCount);
descriptor.append((char)inheritedFloatCount);
descriptor.append((char)inheritedConstantCount);
descriptor.append((char)inheritedRegexpCount);
descriptor.append((char)inheritedBigIntegerCount);
descriptor.append((char)inheritedVariableReaderCount);
descriptor.append((char)inheritedVariableWriterCount);
descriptor.append((char)inheritedBlockBodyCount);
descriptor.append((char)inheritedBlockCallbackCount);
descriptor.append((char)inheritedMethodCount);
descriptor.append((char)inheritedStringCount);
descriptor.append((char)inheritedEncodingCount);
// init from descriptor
initMethod.aload(0);
initMethod.ldc(descriptor.toString());
initMethod.invokevirtual(p(AbstractScript.class), "initFromDescriptor", sig(void.class, String.class));
if (inheritedEncodingCount > 0) {
// init all encodings
for (Map.Entry<String, Integer> entry : encodingIndices.entrySet()) {
initMethod.aload(0);
initMethod.ldc(entry.getValue());
initMethod.ldc(entry.getKey());
initMethod.invokevirtual(p(AbstractScript.class), "setEncoding", sig(void.class, int.class, String.class));
}
}
if (inheritedStringCount > 0) {
// init all strings
for (Map.Entry<String, Integer> entry : stringIndices.entrySet()) {
initMethod.aload(0);
initMethod.ldc(entry.getValue());
initMethod.ldc(entry.getKey());
loadEncoding(initMethod, stringEncodings.get(entry.getKey()));
initMethod.invokevirtual(p(AbstractScript.class), "setByteList", sig(void.class, int.class, String.class, Encoding.class));
}
}
}
}
private void ensureRuntimeCacheInited(SkinnyMethodAdapter initMethod) {
if (!runtimeCacheInited) {
initMethod.aload(0);
initMethod.newobj(p(RuntimeCache.class));
initMethod.dup();
initMethod.invokespecial(p(RuntimeCache.class), "<init>", sig(void.class));
initMethod.putfield(p(AbstractScript.class), "runtimeCache", ci(RuntimeCache.class));
runtimeCacheInited = true;
}
}
}