For debugging purposes it is much better to give class constructor functions fully qualified names. Obfuscation will optimize them away later anyway.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import static org.apidesign.vm4brwsr.ByteCodeParser.*;
24 /** Translator of the code inside class files to JavaScript.
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 abstract class ByteCodeToJavaScript {
31 final ObfuscationDelegate obfuscationDelegate;
33 protected ByteCodeToJavaScript(Appendable out) {
34 this(out, ObfuscationDelegate.NULL);
37 protected ByteCodeToJavaScript(
38 Appendable out, ObfuscationDelegate obfuscationDelegate) {
40 this.obfuscationDelegate = obfuscationDelegate;
43 /* Collects additional required resources.
45 * @param internalClassName classes that were referenced and should be loaded in order the
46 * generated JavaScript code works properly. The names are in internal
47 * JVM form so String is <code>java/lang/String</code>.
49 protected abstract boolean requireReference(String internalClassName);
52 * @param resourcePath name of resources to read
54 protected abstract void requireScript(String resourcePath) throws IOException;
56 /** Allows subclasses to redefine what field a function representing a
57 * class gets assigned. By default it returns the suggested name followed
58 * by <code>" = "</code>;
60 * @param className suggested name of the class
62 /* protected */ String assignClass(String className) {
63 return className + " = ";
65 /* protected */ String accessClass(String classOperation) {
66 return classOperation;
69 abstract String getVMObject();
71 /** Prints out a debug message.
73 * @param msg the message
74 * @return true if the message has been printed
77 boolean debug(String msg) throws IOException {
83 * Converts a given class file to a JavaScript version.
85 * @param classFile input stream with code of the .class file
86 * @return the initialization code for this class, if any. Otherwise <code>null</code>
88 * @throws IOException if something goes wrong during read or write or translating
91 public String compile(InputStream classFile) throws IOException {
92 this.jc = new ClassData(classFile);
93 if (jc.getMajor_version() < 50) {
94 throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
95 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
98 byte[] arrData = jc.findAnnotationData(true);
99 String[] arr = findAnnotation(arrData, jc,
100 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
101 "resource", "processByteCode"
104 if (!arr[0].isEmpty()) {
105 requireScript(arr[0]);
107 if ("0".equals(arr[1])) {
111 String[] proto = findAnnotation(arrData, jc,
112 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
113 "container", "prototype"
115 StringArray toInitilize = new StringArray();
116 final String className = className(jc);
117 out.append("\n\n").append(assignClass(className));
118 out.append("function ").append(className).append("() {");
119 out.append("\n var CLS = ").append(className).append(';');
120 out.append("\n if (!CLS.$class) {");
122 String sc = jc.getSuperClassName(); // with _
123 out.append("\n var pp = ").
124 append(accessClass(sc.replace('/', '_'))).append("(true);");
125 out.append("\n var p = CLS.prototype = pp;");
126 out.append("\n var c = p;");
127 out.append("\n var sprcls = pp.constructor.$class;");
129 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
130 if (proto[0] == null) {
133 out.append("\n var c = ").append(proto[0]).append(";");
134 out.append("\n var sprcls = null;");
136 for (FieldData v : jc.getFields()) {
138 out.append("\n CLS.").append(v.getName()).append(initField(v));
139 out.append("\n c._").append(v.getName()).append(" = function (v) {")
140 .append(" if (arguments.length == 1) CLS.").append(v.getName())
141 .append(" = v; return CLS.").
142 append(v.getName()).append("; };");
144 out.append("\n c._").append(v.getName()).append(" = function (v) {")
145 .append(" if (arguments.length == 1) this.fld_").
146 append(className).append('_').append(v.getName())
147 .append(" = v; return this.fld_").
148 append(className).append('_').append(v.getName())
152 obfuscationDelegate.exportField(out, "c", "_" + v.getName(), v);
154 for (MethodData m : jc.getMethods()) {
155 byte[] onlyArr = m.findAnnotationData(true);
156 String[] only = findAnnotation(onlyArr, jc,
157 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
161 if (only[0] != null && only[1] != null) {
162 out.append("\n p.").append(only[0]).append(" = ")
163 .append(only[1]).append(";");
172 mn = generateStaticMethod(destObject, m, toInitilize);
174 if (m.isConstructor()) {
176 mn = generateInstanceMethod(destObject, m);
179 mn = generateInstanceMethod(destObject, m);
182 obfuscationDelegate.exportMethod(out, destObject, mn, m);
183 byte[] runAnno = m.findAnnotationData(false);
184 if (runAnno != null) {
185 out.append("\n ").append(destObject).append(".").append(mn).append(".anno = {");
186 generateAnno(jc, out, runAnno);
189 out.append("\n ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
190 out.append("\n ").append(destObject).append(".").append(mn).append(".cls = CLS;");
192 out.append("\n c.constructor = CLS;");
193 String instOfName = "$instOf_" + className;
194 out.append("\n c.").append(instOfName).append(" = true;");
195 obfuscationDelegate.exportJSProperty(out, "c", instOfName);
196 for (String superInterface : jc.getSuperInterfaces()) {
197 instOfName = "$instOf_" + superInterface.replace('/', '_');
198 out.append("\n c.").append(instOfName).append(" = true;");
199 obfuscationDelegate.exportJSProperty(out, "c", instOfName);
201 out.append("\n CLS.$class = 'temp';");
202 out.append("\n CLS.$class = ");
203 out.append(accessClass("java_lang_Class(true);"));
204 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
205 out.append("\n CLS.$class.superclass = sprcls;");
206 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
207 out.append("\n CLS.$class.cnstr = CLS;");
208 byte[] classAnno = jc.findAnnotationData(false);
209 if (classAnno != null) {
210 out.append("\n CLS.$class.anno = {");
211 generateAnno(jc, out, classAnno);
214 for (String init : toInitilize.toArray()) {
215 out.append("\n ").append(init).append("();");
218 out.append("\n if (arguments.length === 0) {");
219 out.append("\n if (!(this instanceof CLS)) {");
220 out.append("\n return new CLS();");
222 for (FieldData v : jc.getFields()) {
223 byte[] onlyArr = v.findAnnotationData(true);
224 String[] only = findAnnotation(onlyArr, jc,
225 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
229 if (only[0] != null && only[1] != null) {
230 out.append("\n p.").append(only[0]).append(" = ")
231 .append(only[1]).append(";");
236 out.append("\n this.fld_").
237 append(className).append('_').
238 append(v.getName()).append(initField(v));
241 out.append("\n return this;");
243 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
246 obfuscationDelegate.exportClass(out, getVMObject(), className, jc);
248 // StringBuilder sb = new StringBuilder();
249 // for (String init : toInitilize.toArray()) {
250 // sb.append("\n").append(init).append("();");
254 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
255 String jsb = javaScriptBody(destObject, m, true);
259 final String mn = findMethodName(m, new StringBuilder());
260 if (mn.equals("class__V")) {
261 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
263 generateMethod(destObject, mn, m);
267 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
268 String jsb = javaScriptBody(destObject, m, false);
272 final String mn = findMethodName(m, new StringBuilder());
273 generateMethod(destObject, mn, m);
277 private void generateMethod(String destObject, String name, MethodData m)
279 final StackMapIterator stackMapIterator = m.createStackMapIterator();
280 TrapDataIterator trap = m.getTrapDataIterator();
281 final LocalsMapper lmapper =
282 new LocalsMapper(stackMapIterator.getArguments());
284 out.append(destObject).append(".").append(name).append(" = function(");
285 lmapper.outputArguments(out, m.isStatic());
286 out.append(") {").append("\n");
288 final byte[] byteCodes = m.getCode();
289 if (byteCodes == null) {
290 out.append(" throw 'no code found for ")
291 .append(jc.getClassName()).append('.')
292 .append(m.getName()).append("';\n");
297 final StackMapper smapper = new StackMapper();
300 out.append(" var ").append(" lcA0 = this;\n");
303 int lastStackFrame = -1;
304 TrapData[] previousTrap = null;
305 boolean wide = false;
307 out.append("\n var gt = 0;\n");
309 int topMostLabel = 0;
310 for (int i = 0; i < byteCodes.length; i++) {
312 stackMapIterator.advanceTo(i);
313 boolean changeInCatch = trap.advanceTo(i);
314 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
315 if (previousTrap != null) {
316 generateCatch(previousTrap, i, topMostLabel);
320 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
324 if (openBraces > 64) {
325 for (int c = 0; c < 64; c++) {
326 out.append("break;}\n");
332 lastStackFrame = stackMapIterator.getFrameIndex();
333 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
334 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
335 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
337 changeInCatch = true;
339 debug(" /* " + i + " */ ");
341 if (changeInCatch && trap.useTry()) {
343 previousTrap = trap.current();
345 final int c = readUByte(byteCodes, i);
348 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
351 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
354 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
357 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
360 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
363 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
366 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
369 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
372 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
375 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
378 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
381 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
384 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
387 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
390 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
393 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
396 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
399 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
402 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
405 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
409 final int indx = wide ? readUShort(byteCodes, i++)
410 : readUByte(byteCodes, i);
412 emit(out, "var @1 = @2;",
413 smapper.pushI(), lmapper.getI(indx));
418 final int indx = wide ? readUShort(byteCodes, i++)
419 : readUByte(byteCodes, i);
421 emit(out, "var @1 = @2;",
422 smapper.pushL(), lmapper.getL(indx));
427 final int indx = wide ? readUShort(byteCodes, i++)
428 : readUByte(byteCodes, i);
430 emit(out, "var @1 = @2;",
431 smapper.pushF(), lmapper.getF(indx));
436 final int indx = wide ? readUShort(byteCodes, i++)
437 : readUByte(byteCodes, i);
439 emit(out, "var @1 = @2;",
440 smapper.pushD(), lmapper.getD(indx));
445 final int indx = wide ? readUShort(byteCodes, i++)
446 : readUByte(byteCodes, i);
448 emit(out, "var @1 = @2;",
449 smapper.pushA(), lmapper.getA(indx));
454 final int indx = wide ? readUShort(byteCodes, i++)
455 : readUByte(byteCodes, i);
457 emit(out, "var @1 = @2;",
458 lmapper.setI(indx), smapper.popI());
463 final int indx = wide ? readUShort(byteCodes, i++)
464 : readUByte(byteCodes, i);
466 emit(out, "var @1 = @2;",
467 lmapper.setL(indx), smapper.popL());
472 final int indx = wide ? readUShort(byteCodes, i++)
473 : readUByte(byteCodes, i);
475 emit(out, "var @1 = @2;",
476 lmapper.setF(indx), smapper.popF());
481 final int indx = wide ? readUShort(byteCodes, i++)
482 : readUByte(byteCodes, i);
484 emit(out, "var @1 = @2;",
485 lmapper.setD(indx), smapper.popD());
490 final int indx = wide ? readUShort(byteCodes, i++)
491 : readUByte(byteCodes, i);
493 emit(out, "var @1 = @2;",
494 lmapper.setA(indx), smapper.popA());
498 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
501 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
504 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
507 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
510 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
513 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
516 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
519 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
522 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
525 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
528 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
531 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
534 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
537 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
540 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
543 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
546 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
549 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
552 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
555 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
558 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
561 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
564 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
567 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
570 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
573 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
576 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
579 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
582 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
585 emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
588 emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
591 emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
594 emit(out, "@1 = @1.div32(@2);",
595 smapper.getI(1), smapper.popI());
598 emit(out, "@1 = @1.div64(@2);",
599 smapper.getL(1), smapper.popL());
602 emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
605 emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
608 emit(out, "@1 = @1.mod32(@2);",
609 smapper.getI(1), smapper.popI());
612 emit(out, "@1 = @1.mod64(@2);",
613 smapper.getL(1), smapper.popL());
616 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
619 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
622 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
625 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
628 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
631 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
634 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
637 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
640 emit(out, "@1 = @1.neg32();", smapper.getI(0));
643 emit(out, "@1 = @1.neg64();", smapper.getL(0));
646 emit(out, "@1 = -@1;", smapper.getF(0));
649 emit(out, "@1 = -@1;", smapper.getD(0));
652 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
655 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
658 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
661 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
664 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
667 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
671 final int varIndx = wide ? readUShort(byteCodes, i++)
672 : readUByte(byteCodes, i);
674 final int incrBy = wide ? readShort(byteCodes, i++)
678 emit(out, "@1++;", lmapper.getI(varIndx));
680 emit(out, "@1 += @2;",
681 lmapper.getI(varIndx),
682 Integer.toString(incrBy));
687 emit(out, "return;");
690 emit(out, "return @1;", smapper.popI());
693 emit(out, "return @1;", smapper.popL());
696 emit(out, "return @1;", smapper.popF());
699 emit(out, "return @1;", smapper.popD());
702 emit(out, "return @1;", smapper.popA());
705 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
708 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
711 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
714 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
718 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
721 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
724 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
727 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
730 emit(out, "var @2 = @1.toInt32();",
731 smapper.popF(), smapper.pushI());
734 emit(out, "var @2 = @1.toLong();",
735 smapper.popF(), smapper.pushL());
738 emit(out, "var @2 = @1.toInt32();",
739 smapper.popD(), smapper.pushI());
742 emit(out, "var @2 = @1.toLong();",
743 smapper.popD(), smapper.pushL());
746 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
749 out.append("{ /* number conversion */ }");
752 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
754 case opc_aconst_null:
755 emit(out, "var @1 = null;", smapper.pushA());
758 emit(out, "var @1 = -1;", smapper.pushI());
761 emit(out, "var @1 = 0;", smapper.pushI());
764 emit(out, "var @1 = 0;", smapper.pushD());
767 emit(out, "var @1 = 0;", smapper.pushL());
770 emit(out, "var @1 = 0;", smapper.pushF());
773 emit(out, "var @1 = 1;", smapper.pushI());
776 emit(out, "var @1 = 1;", smapper.pushL());
779 emit(out, "var @1 = 1;", smapper.pushF());
782 emit(out, "var @1 = 1;", smapper.pushD());
785 emit(out, "var @1 = 2;", smapper.pushI());
788 emit(out, "var @1 = 2;", smapper.pushF());
791 emit(out, "var @1 = 3;", smapper.pushI());
794 emit(out, "var @1 = 4;", smapper.pushI());
797 emit(out, "var @1 = 5;", smapper.pushI());
800 int indx = readUByte(byteCodes, ++i);
801 String v = encodeConstant(indx);
802 int type = VarType.fromConstantType(jc.getTag(indx));
803 emit(out, "var @1 = @2;", smapper.pushT(type), v);
808 int indx = readIntArg(byteCodes, i);
810 String v = encodeConstant(indx);
811 int type = VarType.fromConstantType(jc.getTag(indx));
812 if (type == VarType.LONG) {
813 final Long lv = new Long(v);
814 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
815 final int hi = (int)(lv.longValue() >> 32);
816 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
817 Integer.toHexString(low), Integer.toHexString(hi));
819 emit(out, "var @1 = @2;", smapper.pushT(type), v);
824 emit(out, "var @3 = @2.compare64(@1);",
825 smapper.popL(), smapper.popL(), smapper.pushI());
829 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
830 smapper.popF(), smapper.popF(), smapper.pushI());
834 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
835 smapper.popD(), smapper.popD(), smapper.pushI());
838 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
839 "===", topMostLabel);
842 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
843 "!==", topMostLabel);
846 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
850 int indx = i + readIntArg(byteCodes, i);
851 emitIf(out, "if (@1 == 0) ",
852 smapper.popI(), i, indx, topMostLabel);
857 int indx = i + readIntArg(byteCodes, i);
858 emitIf(out, "if (@1 != 0) ",
859 smapper.popI(), i, indx, topMostLabel);
864 int indx = i + readIntArg(byteCodes, i);
865 emitIf(out, "if (@1 < 0) ",
866 smapper.popI(), i, indx, topMostLabel);
871 int indx = i + readIntArg(byteCodes, i);
872 emitIf(out, "if (@1 <= 0) ",
873 smapper.popI(), i, indx, topMostLabel);
878 int indx = i + readIntArg(byteCodes, i);
879 emitIf(out, "if (@1 > 0) ",
880 smapper.popI(), i, indx, topMostLabel);
885 int indx = i + readIntArg(byteCodes, i);
886 emitIf(out, "if (@1 >= 0) ",
887 smapper.popI(), i, indx, topMostLabel);
891 case opc_ifnonnull: {
892 int indx = i + readIntArg(byteCodes, i);
893 emitIf(out, "if (@1 !== null) ",
894 smapper.popA(), i, indx, topMostLabel);
899 int indx = i + readIntArg(byteCodes, i);
900 emitIf(out, "if (@1 === null) ",
901 smapper.popA(), i, indx, topMostLabel);
906 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
910 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
914 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
918 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
922 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
926 int indx = i + readIntArg(byteCodes, i);
927 goTo(out, i, indx, topMostLabel);
931 case opc_lookupswitch: {
932 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
935 case opc_tableswitch: {
936 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
939 case opc_invokeinterface: {
940 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
943 case opc_invokevirtual:
944 i = invokeVirtualMethod(byteCodes, i, smapper);
946 case opc_invokespecial:
947 i = invokeStaticMethod(byteCodes, i, smapper, false);
949 case opc_invokestatic:
950 i = invokeStaticMethod(byteCodes, i, smapper, true);
953 int indx = readIntArg(byteCodes, i);
954 String ci = jc.getClassName(indx);
955 emit(out, "var @1 = new @2;",
956 smapper.pushA(), accessClass(ci.replace('/', '_')));
962 int atype = readUByte(byteCodes, ++i);
963 generateNewArray(atype, smapper);
965 case opc_anewarray: {
966 int type = readIntArg(byteCodes, i);
968 generateANewArray(type, smapper);
971 case opc_multianewarray: {
972 int type = readIntArg(byteCodes, i);
974 i = generateMultiANewArray(type, byteCodes, i, smapper);
977 case opc_arraylength:
978 emit(out, "var @2 = @1.length;",
979 smapper.popA(), smapper.pushI());
982 emit(out, "@3.at(@2, @1);",
983 smapper.popL(), smapper.popI(), smapper.popA());
986 emit(out, "@3.at(@2, @1);",
987 smapper.popF(), smapper.popI(), smapper.popA());
990 emit(out, "@3.at(@2, @1);",
991 smapper.popD(), smapper.popI(), smapper.popA());
994 emit(out, "@3.at(@2, @1);",
995 smapper.popA(), smapper.popI(), smapper.popA());
1001 emit(out, "@3.at(@2, @1);",
1002 smapper.popI(), smapper.popI(), smapper.popA());
1005 emit(out, "var @3 = @2.at(@1);",
1006 smapper.popI(), smapper.popA(), smapper.pushL());
1009 emit(out, "var @3 = @2.at(@1);",
1010 smapper.popI(), smapper.popA(), smapper.pushF());
1013 emit(out, "var @3 = @2.at(@1);",
1014 smapper.popI(), smapper.popA(), smapper.pushD());
1017 emit(out, "var @3 = @2.at(@1);",
1018 smapper.popI(), smapper.popA(), smapper.pushA());
1024 emit(out, "var @3 = @2.at(@1);",
1025 smapper.popI(), smapper.popA(), smapper.pushI());
1033 final Variable v = smapper.get(0);
1034 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1038 final Variable vi1 = smapper.get(0);
1040 if (vi1.isCategory2()) {
1041 emit(out, "var @1 = @2;",
1042 smapper.pushT(vi1.getType()), vi1);
1044 final Variable vi2 = smapper.get(1);
1045 emit(out, "var @1 = @2, @3 = @4;",
1046 smapper.pushT(vi2.getType()), vi2,
1047 smapper.pushT(vi1.getType()), vi1);
1052 final Variable vi1 = smapper.pop();
1053 final Variable vi2 = smapper.pop();
1054 final Variable vo3 = smapper.pushT(vi1.getType());
1055 final Variable vo2 = smapper.pushT(vi2.getType());
1056 final Variable vo1 = smapper.pushT(vi1.getType());
1058 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1059 vo1, vi1, vo2, vi2, vo3, vo1);
1063 final Variable vi1 = smapper.pop();
1064 final Variable vi2 = smapper.pop();
1066 if (vi1.isCategory2()) {
1067 final Variable vo3 = smapper.pushT(vi1.getType());
1068 final Variable vo2 = smapper.pushT(vi2.getType());
1069 final Variable vo1 = smapper.pushT(vi1.getType());
1071 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1072 vo1, vi1, vo2, vi2, vo3, vo1);
1074 final Variable vi3 = smapper.pop();
1075 final Variable vo5 = smapper.pushT(vi2.getType());
1076 final Variable vo4 = smapper.pushT(vi1.getType());
1077 final Variable vo3 = smapper.pushT(vi3.getType());
1078 final Variable vo2 = smapper.pushT(vi2.getType());
1079 final Variable vo1 = smapper.pushT(vi1.getType());
1081 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1082 vo1, vi1, vo2, vi2, vo3, vi3);
1083 emit(out, " @1 = @2, @3 = @4;",
1084 vo4, vo1, vo5, vo2);
1089 final Variable vi1 = smapper.pop();
1090 final Variable vi2 = smapper.pop();
1092 if (vi2.isCategory2()) {
1093 final Variable vo3 = smapper.pushT(vi1.getType());
1094 final Variable vo2 = smapper.pushT(vi2.getType());
1095 final Variable vo1 = smapper.pushT(vi1.getType());
1097 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1098 vo1, vi1, vo2, vi2, vo3, vo1);
1100 final Variable vi3 = smapper.pop();
1101 final Variable vo4 = smapper.pushT(vi1.getType());
1102 final Variable vo3 = smapper.pushT(vi3.getType());
1103 final Variable vo2 = smapper.pushT(vi2.getType());
1104 final Variable vo1 = smapper.pushT(vi1.getType());
1106 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1107 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1112 final Variable vi1 = smapper.pop();
1113 final Variable vi2 = smapper.pop();
1115 if (vi1.isCategory2()) {
1116 if (vi2.isCategory2()) {
1117 final Variable vo3 = smapper.pushT(vi1.getType());
1118 final Variable vo2 = smapper.pushT(vi2.getType());
1119 final Variable vo1 = smapper.pushT(vi1.getType());
1121 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1122 vo1, vi1, vo2, vi2, vo3, vo1);
1124 final Variable vi3 = smapper.pop();
1125 final Variable vo4 = smapper.pushT(vi1.getType());
1126 final Variable vo3 = smapper.pushT(vi3.getType());
1127 final Variable vo2 = smapper.pushT(vi2.getType());
1128 final Variable vo1 = smapper.pushT(vi1.getType());
1130 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1131 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1134 final Variable vi3 = smapper.pop();
1136 if (vi3.isCategory2()) {
1137 final Variable vo5 = smapper.pushT(vi2.getType());
1138 final Variable vo4 = smapper.pushT(vi1.getType());
1139 final Variable vo3 = smapper.pushT(vi3.getType());
1140 final Variable vo2 = smapper.pushT(vi2.getType());
1141 final Variable vo1 = smapper.pushT(vi1.getType());
1143 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1144 vo1, vi1, vo2, vi2, vo3, vi3);
1145 emit(out, " @1 = @2, @3 = @4;",
1146 vo4, vo1, vo5, vo2);
1148 final Variable vi4 = smapper.pop();
1149 final Variable vo6 = smapper.pushT(vi2.getType());
1150 final Variable vo5 = smapper.pushT(vi1.getType());
1151 final Variable vo4 = smapper.pushT(vi4.getType());
1152 final Variable vo3 = smapper.pushT(vi3.getType());
1153 final Variable vo2 = smapper.pushT(vi2.getType());
1154 final Variable vo1 = smapper.pushT(vi1.getType());
1156 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1157 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1158 emit(out, " @1 = @2, @3 = @4;",
1159 vo5, vo1, vo6, vo2);
1165 final Variable vi1 = smapper.get(0);
1166 final Variable vi2 = smapper.get(1);
1168 if (vi1.getType() == vi2.getType()) {
1169 final Variable tmp = smapper.pushT(vi1.getType());
1171 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1176 smapper.pushT(vi1.getType());
1177 smapper.pushT(vi2.getType());
1182 emit(out, "var @1 = @2;",
1183 smapper.pushI(), Integer.toString(byteCodes[++i]));
1186 emit(out, "var @1 = @2;",
1188 Integer.toString(readIntArg(byteCodes, i)));
1191 case opc_getfield: {
1192 int indx = readIntArg(byteCodes, i);
1193 String[] fi = jc.getFieldInfoName(indx);
1194 final int type = VarType.fromFieldType(fi[2].charAt(0));
1195 final String mangleClass = mangleSig(fi[0]);
1196 final String mangleClassAccess = accessClass(mangleClass);
1197 emit(out, "var @2 = @4(false)._@3.call(@1);",
1199 smapper.pushT(type), fi[1], mangleClassAccess
1204 case opc_putfield: {
1205 int indx = readIntArg(byteCodes, i);
1206 String[] fi = jc.getFieldInfoName(indx);
1207 final int type = VarType.fromFieldType(fi[2].charAt(0));
1208 final String mangleClass = mangleSig(fi[0]);
1209 final String mangleClassAccess = accessClass(mangleClass);
1210 emit(out, "@4(false)._@3.call(@2, @1);",
1212 smapper.popA(), fi[1],
1218 case opc_getstatic: {
1219 int indx = readIntArg(byteCodes, i);
1220 String[] fi = jc.getFieldInfoName(indx);
1221 final int type = VarType.fromFieldType(fi[2].charAt(0));
1222 emit(out, "var @1 = @2(false)._@3();",
1223 smapper.pushT(type),
1224 accessClass(fi[0].replace('/', '_')), fi[1]);
1226 addReference(fi[0]);
1229 case opc_putstatic: {
1230 int indx = readIntArg(byteCodes, i);
1231 String[] fi = jc.getFieldInfoName(indx);
1232 final int type = VarType.fromFieldType(fi[2].charAt(0));
1233 emit(out, "@1(false)._@2(@3);",
1234 accessClass(fi[0].replace('/', '_')), fi[1],
1235 smapper.popT(type));
1237 addReference(fi[0]);
1240 case opc_checkcast: {
1241 int indx = readIntArg(byteCodes, i);
1242 generateCheckcast(indx, smapper);
1246 case opc_instanceof: {
1247 int indx = readIntArg(byteCodes, i);
1248 generateInstanceOf(indx, smapper);
1253 final Variable v = smapper.popA();
1256 emit(out, "{ var @1 = @2; throw @2; }",
1257 smapper.pushA(), v);
1261 case opc_monitorenter: {
1262 out.append("/* monitor enter */");
1267 case opc_monitorexit: {
1268 out.append("/* monitor exit */");
1279 emit(out, "throw 'unknown bytecode @1';",
1280 Integer.toString(c));
1284 generateByteCodeComment(prev, i, byteCodes);
1288 if (previousTrap != null) {
1289 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1291 out.append("\n }\n");
1292 while (openBraces-- > 0) {
1298 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1299 int indx = i + readIntArg(byteCodes, i);
1300 out.append("if (").append(v1)
1301 .append(' ').append(test).append(' ')
1302 .append(v2).append(") ");
1303 goTo(out, i, indx, topMostLabel);
1307 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
1308 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
1309 final int indxLo = byteCodes[offsetInstruction + 2];
1310 return (indxHi & 0xffffff00) | (indxLo & 0xff);
1312 private int readInt4(byte[] byteCodes, int offset) {
1313 final int d = byteCodes[offset + 0] << 24;
1314 final int c = byteCodes[offset + 1] << 16;
1315 final int b = byteCodes[offset + 2] << 8;
1316 final int a = byteCodes[offset + 3];
1317 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1319 private int readUByte(byte[] byteCodes, int offset) {
1320 return byteCodes[offset] & 0xff;
1323 private int readUShort(byte[] byteCodes, int offset) {
1324 return ((byteCodes[offset] & 0xff) << 8)
1325 | (byteCodes[offset + 1] & 0xff);
1328 private int readShort(byte[] byteCodes, int offset) {
1329 return (byteCodes[offset] << 8)
1330 | (byteCodes[offset + 1] & 0xff);
1333 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1335 Boolean count = null;
1336 boolean array = false;
1338 int firstPos = sig.length();
1339 while (i < descriptor.length()) {
1340 char ch = descriptor.charAt(i++);
1361 if (ch == 'J' || ch == 'D') {
1367 sig.insert(firstPos, ch);
1369 returnType[0] = '[';
1370 sig.insert(firstPos, "_3");
1379 returnType[0] = 'V';
1380 sig.insert(firstPos, 'V');
1383 int next = descriptor.indexOf(';', i);
1384 String realSig = mangleSig(descriptor, i - 1, next + 1);
1389 sig.append(realSig);
1392 sig.insert(firstPos, realSig);
1394 sig.insert(firstPos, "_3");
1396 returnType[0] = 'L';
1405 throw new IllegalStateException("Invalid char: " + ch);
1410 static String mangleSig(String sig) {
1411 return mangleSig(sig, 0, sig.length());
1414 private static String mangleSig(String txt, int first, int last) {
1415 StringBuilder sb = new StringBuilder();
1416 for (int i = first; i < last; i++) {
1417 final char ch = txt.charAt(i);
1419 case '/': sb.append('_'); break;
1420 case '_': sb.append("_1"); break;
1421 case ';': sb.append("_2"); break;
1422 case '[': sb.append("_3"); break;
1423 default: sb.append(ch); break;
1426 return sb.toString();
1429 private static String findMethodName(MethodData m, StringBuilder cnt) {
1430 StringBuilder name = new StringBuilder();
1431 if ("<init>".equals(m.getName())) { // NOI18N
1432 name.append("cons"); // NOI18N
1433 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1434 name.append("class"); // NOI18N
1436 name.append(m.getName());
1439 countArgs(m.getInternalSig(), new char[1], name, cnt);
1440 return name.toString();
1443 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1444 StringBuilder name = new StringBuilder();
1445 String descr = mi[2];//mi.getDescriptor();
1447 if ("<init>".equals(nm)) { // NOI18N
1448 name.append("cons"); // NOI18N
1452 countArgs(descr, returnType, name, cnt);
1453 return name.toString();
1456 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1457 throws IOException {
1458 int methodIndex = readIntArg(byteCodes, i);
1459 String[] mi = jc.getFieldInfoName(methodIndex);
1460 char[] returnType = { 'V' };
1461 StringBuilder cnt = new StringBuilder();
1462 String mn = findMethodName(mi, cnt, returnType);
1464 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1465 final Variable[] vars = new Variable[numArguments];
1467 for (int j = numArguments - 1; j >= 0; --j) {
1468 vars[j] = mapper.pop();
1471 if (returnType[0] != 'V') {
1473 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1477 final String in = mi[0];
1478 out.append(accessClass(in.replace('/', '_')));
1479 out.append("(false).");
1480 if (mn.startsWith("cons_")) {
1481 out.append("constructor.");
1487 out.append(".call(");
1489 if (numArguments > 0) {
1490 out.append(vars[0]);
1491 for (int j = 1; j < numArguments; ++j) {
1493 out.append(vars[j]);
1501 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1502 throws IOException {
1503 int methodIndex = readIntArg(byteCodes, i);
1504 String[] mi = jc.getFieldInfoName(methodIndex);
1505 char[] returnType = { 'V' };
1506 StringBuilder cnt = new StringBuilder();
1507 String mn = findMethodName(mi, cnt, returnType);
1509 final int numArguments = cnt.length() + 1;
1510 final Variable[] vars = new Variable[numArguments];
1512 for (int j = numArguments - 1; j >= 0; --j) {
1513 vars[j] = mapper.pop();
1516 if (returnType[0] != 'V') {
1518 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1522 out.append(vars[0]).append('.');
1526 for (int j = 1; j < numArguments; ++j) {
1528 out.append(vars[j]);
1536 private void addReference(String cn) throws IOException {
1537 if (requireReference(cn)) {
1538 debug(" /* needs " + cn + " */");
1542 private void outType(String d, StringBuilder out) {
1544 while (d.charAt(0) == '[') {
1548 if (d.charAt(0) == 'L') {
1549 assert d.charAt(d.length() - 1) == ';';
1550 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1556 private String encodeConstant(int entryIndex) throws IOException {
1557 String[] classRef = { null };
1558 String s = jc.stringValue(entryIndex, classRef);
1559 if (classRef[0] != null) {
1560 if (classRef[0].startsWith("[")) {
1561 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1563 addReference(classRef[0]);
1564 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1570 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1571 byte[] arr = m.findAnnotationData(true);
1575 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1576 class P extends AnnotationParser {
1582 String[] args = new String[30];
1586 protected void visitAttr(String type, String attr, String at, String value) {
1587 if (type.equals(jvmType)) {
1588 if ("body".equals(attr)) {
1590 } else if ("args".equals(attr)) {
1591 args[cnt++] = value;
1593 throw new IllegalArgumentException(attr);
1600 if (p.body == null) {
1603 StringBuilder cnt = new StringBuilder();
1604 final String mn = findMethodName(m, cnt);
1605 out.append(destObject).append(".").append(mn);
1606 out.append(" = function(");
1609 for (int i = 0; i < cnt.length(); i++) {
1611 space = outputArg(out, p.args, index);
1614 out.append(") {").append("\n");
1616 out.append("\n}\n");
1619 private static String className(ClassData jc) {
1620 //return jc.getName().getInternalName().replace('/', '_');
1621 return jc.getClassName().replace('/', '_');
1624 private static String[] findAnnotation(
1625 byte[] arr, ClassData cd, final String className,
1626 final String... attrNames
1627 ) throws IOException {
1631 final String[] values = new String[attrNames.length];
1632 final boolean[] found = { false };
1633 final String jvmType = "L" + className.replace('.', '/') + ";";
1634 AnnotationParser ap = new AnnotationParser(false, true) {
1636 protected void visitAttr(String type, String attr, String at, String value) {
1637 if (type.equals(jvmType)) {
1639 for (int i = 0; i < attrNames.length; i++) {
1640 if (attrNames[i].equals(attr)) {
1649 return found[0] ? values : null;
1652 private CharSequence initField(FieldData v) {
1653 final String is = v.getInternalSig();
1654 if (is.length() == 1) {
1655 switch (is.charAt(0)) {
1661 case 'I': return " = 0;";
1663 case 'D': return " = 0.0;";
1665 throw new IllegalStateException(is);
1671 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1672 AnnotationParser ap = new AnnotationParser(true, false) {
1673 int[] cnt = new int[32];
1677 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1678 final String slashType = attrType.substring(1, attrType.length() - 1);
1679 requireReference(slashType);
1681 if (cnt[depth]++ > 0) {
1685 out.append('"').append(attrType).append("\" : ");
1692 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1693 out.append("\n}\n");
1698 protected void visitValueStart(String attrName, char type) throws IOException {
1699 if (cnt[depth]++ > 0) {
1703 if (attrName != null) {
1704 out.append(attrName).append(" : ");
1712 protected void visitValueEnd(String attrName, char type) throws IOException {
1720 protected void visitAttr(String type, String attr, String attrType, String value)
1721 throws IOException {
1722 if (attr == null && value == null) {
1729 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1730 throws IOException {
1731 final String slashType = attrType.substring(1, attrType.length() - 1);
1732 requireReference(slashType);
1734 out.append(accessClass(slashType.replace('/', '_')))
1735 .append("(false).constructor.").append(value);
1741 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1742 final String name = args[indx];
1746 if (name.contains(",")) {
1747 throw new IOException("Wrong parameter with ',': " + name);
1753 private static void emit(final Appendable out,
1754 final String format,
1755 final CharSequence... params) throws IOException {
1756 final int length = format.length();
1759 int paramOffset = format.indexOf('@');
1760 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1761 final char paramChar = format.charAt(paramOffset + 1);
1762 if ((paramChar >= '1') && (paramChar <= '9')) {
1763 final int paramIndex = paramChar - '0' - 1;
1765 out.append(format, processed, paramOffset);
1766 out.append(params[paramIndex]);
1769 processed = paramOffset + 1;
1772 paramOffset = format.indexOf('@', paramOffset + 1);
1775 out.append(format, processed, length);
1778 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1779 out.append("} catch (e) {\n");
1781 for (TrapData e : traps) {
1785 if (e.catch_cpx != 0) { //not finally
1786 final String classInternalName = jc.getClassName(e.catch_cpx);
1787 addReference(classInternalName);
1788 if ("java/lang/Throwable".equals(classInternalName)) {
1789 out.append("if (e.$instOf_java_lang_Throwable) {");
1790 out.append(" var stA0 = e;");
1791 out.append("} else {");
1792 out.append(" var stA0 = vm.java_lang_Throwable(true);");
1793 out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
1795 goTo(out, current, e.handler_pc, topMostLabel);
1797 out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
1798 out.append("var stA0 = e;");
1799 goTo(out, current, e.handler_pc, topMostLabel);
1803 finallyPC = e.handler_pc;
1806 if (finallyPC == -1) {
1807 out.append("throw e;");
1809 out.append("var stA0 = e;");
1810 goTo(out, current, finallyPC, topMostLabel);
1815 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1818 out.append("{ gt = 0; continue X_" + to + "; }");
1820 out.append("{ gt = " + to + "; continue X_0; }");
1823 out.append("{ gt = " + to + "; break IF; }");
1827 private static void emitIf(
1828 Appendable out, String pattern, Variable param,
1829 int current, int to, int canBack
1830 ) throws IOException {
1831 emit(out, pattern, param);
1832 goTo(out, current, to, canBack);
1835 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1838 case 4: jvmType = "[Z"; break;
1839 case 5: jvmType = "[C"; break;
1840 case 6: jvmType = "[F"; break;
1841 case 7: jvmType = "[D"; break;
1842 case 8: jvmType = "[B"; break;
1843 case 9: jvmType = "[S"; break;
1844 case 10: jvmType = "[I"; break;
1845 case 11: jvmType = "[J"; break;
1846 default: throw new IllegalStateException("Array type: " + atype);
1848 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1849 smapper.popI(), smapper.pushA(), jvmType);
1852 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1853 String typeName = jc.getClassName(type);
1854 if (typeName.startsWith("[")) {
1855 typeName = "[" + typeName;
1857 typeName = "[L" + typeName + ";";
1859 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1860 smapper.popI(), smapper.pushA(), typeName);
1863 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1864 String typeName = jc.getClassName(type);
1865 int dim = readUByte(byteCodes, ++i);
1866 StringBuilder dims = new StringBuilder();
1868 for (int d = 0; d < dim; d++) {
1870 dims.insert(1, ",");
1872 dims.insert(1, smapper.popI());
1875 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1876 dims.toString(), smapper.pushA(), typeName);
1880 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1881 int table = i / 4 * 4 + 4;
1882 int dflt = i + readInt4(byteCodes, table);
1884 int low = readInt4(byteCodes, table);
1886 int high = readInt4(byteCodes, table);
1888 out.append("switch (").append(smapper.popI()).append(") {\n");
1889 while (low <= high) {
1890 int offset = i + readInt4(byteCodes, table);
1892 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1895 out.append(" default: ");
1896 goTo(out, i, dflt, topMostLabel);
1902 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1903 int table = i / 4 * 4 + 4;
1904 int dflt = i + readInt4(byteCodes, table);
1906 int n = readInt4(byteCodes, table);
1908 out.append("switch (").append(smapper.popI()).append(") {\n");
1910 int cnstnt = readInt4(byteCodes, table);
1912 int offset = i + readInt4(byteCodes, table);
1914 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1916 out.append(" default: ");
1917 goTo(out, i, dflt, topMostLabel);
1923 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1924 final String type = jc.getClassName(indx);
1925 if (!type.startsWith("[")) {
1926 emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
1927 smapper.popA(), smapper.pushI(),
1928 type.replace('/', '_'));
1930 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1931 smapper.popA(), smapper.pushI(),
1937 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1938 final String type = jc.getClassName(indx);
1939 if (!type.startsWith("[")) {
1941 "if (@1 !== null && !@1.$instOf_@2) throw {};",
1942 smapper.getA(0), type.replace('/', '_'));
1944 emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
1945 smapper.getA(0), type
1950 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1951 for (int j = prev; j <= i; j++) {
1953 final int cc = readUByte(byteCodes, j);
1954 out.append(Integer.toString(cc));