#4669 @ html.java.net: Provide generic conversion function and when catching something, use it to convert to appropriate Java exception
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 = readUShortArg(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 + readShortArg(byteCodes, i);
851 emitIf(out, "if (@1 == 0) ",
852 smapper.popI(), i, indx, topMostLabel);
857 int indx = i + readShortArg(byteCodes, i);
858 emitIf(out, "if (@1 != 0) ",
859 smapper.popI(), i, indx, topMostLabel);
864 int indx = i + readShortArg(byteCodes, i);
865 emitIf(out, "if (@1 < 0) ",
866 smapper.popI(), i, indx, topMostLabel);
871 int indx = i + readShortArg(byteCodes, i);
872 emitIf(out, "if (@1 <= 0) ",
873 smapper.popI(), i, indx, topMostLabel);
878 int indx = i + readShortArg(byteCodes, i);
879 emitIf(out, "if (@1 > 0) ",
880 smapper.popI(), i, indx, topMostLabel);
885 int indx = i + readShortArg(byteCodes, i);
886 emitIf(out, "if (@1 >= 0) ",
887 smapper.popI(), i, indx, topMostLabel);
891 case opc_ifnonnull: {
892 int indx = i + readShortArg(byteCodes, i);
893 emitIf(out, "if (@1 !== null) ",
894 smapper.popA(), i, indx, topMostLabel);
899 int indx = i + readShortArg(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 + readShortArg(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 = readUShortArg(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 = readUShortArg(byteCodes, i);
968 generateANewArray(type, smapper);
971 case opc_multianewarray: {
972 int type = readUShortArg(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(readShortArg(byteCodes, i)));
1191 case opc_getfield: {
1192 int indx = readUShortArg(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 = readUShortArg(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 = readUShortArg(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 = readUShortArg(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 = readUShortArg(byteCodes, i);
1242 generateCheckcast(indx, smapper);
1246 case opc_instanceof: {
1247 int indx = readUShortArg(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 + readShortArg(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 readInt4(byte[] byteCodes, int offset) {
1308 final int d = byteCodes[offset + 0] << 24;
1309 final int c = byteCodes[offset + 1] << 16;
1310 final int b = byteCodes[offset + 2] << 8;
1311 final int a = byteCodes[offset + 3];
1312 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1314 private static int readUByte(byte[] byteCodes, int offset) {
1315 return byteCodes[offset] & 0xff;
1318 private static int readUShort(byte[] byteCodes, int offset) {
1319 return ((byteCodes[offset] & 0xff) << 8)
1320 | (byteCodes[offset + 1] & 0xff);
1322 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1323 return readUShort(byteCodes, offsetInstruction + 1);
1326 private static int readShort(byte[] byteCodes, int offset) {
1327 int signed = byteCodes[offset];
1328 byte b0 = (byte)signed;
1329 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1331 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1332 return readShort(byteCodes, offsetInstruction + 1);
1335 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1337 Boolean count = null;
1338 boolean array = false;
1340 int firstPos = sig.length();
1341 while (i < descriptor.length()) {
1342 char ch = descriptor.charAt(i++);
1363 if (ch == 'J' || ch == 'D') {
1369 sig.insert(firstPos, ch);
1371 returnType[0] = '[';
1372 sig.insert(firstPos, "_3");
1381 returnType[0] = 'V';
1382 sig.insert(firstPos, 'V');
1385 int next = descriptor.indexOf(';', i);
1386 String realSig = mangleSig(descriptor, i - 1, next + 1);
1391 sig.append(realSig);
1394 sig.insert(firstPos, realSig);
1396 sig.insert(firstPos, "_3");
1398 returnType[0] = 'L';
1407 throw new IllegalStateException("Invalid char: " + ch);
1412 static String mangleSig(String sig) {
1413 return mangleSig(sig, 0, sig.length());
1416 private static String mangleSig(String txt, int first, int last) {
1417 StringBuilder sb = new StringBuilder();
1418 for (int i = first; i < last; i++) {
1419 final char ch = txt.charAt(i);
1421 case '/': sb.append('_'); break;
1422 case '_': sb.append("_1"); break;
1423 case ';': sb.append("_2"); break;
1424 case '[': sb.append("_3"); break;
1425 default: sb.append(ch); break;
1428 return sb.toString();
1431 private static String findMethodName(MethodData m, StringBuilder cnt) {
1432 StringBuilder name = new StringBuilder();
1433 if ("<init>".equals(m.getName())) { // NOI18N
1434 name.append("cons"); // NOI18N
1435 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1436 name.append("class"); // NOI18N
1438 name.append(m.getName());
1441 countArgs(m.getInternalSig(), new char[1], name, cnt);
1442 return name.toString();
1445 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1446 StringBuilder name = new StringBuilder();
1447 String descr = mi[2];//mi.getDescriptor();
1449 if ("<init>".equals(nm)) { // NOI18N
1450 name.append("cons"); // NOI18N
1454 countArgs(descr, returnType, name, cnt);
1455 return name.toString();
1458 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1459 throws IOException {
1460 int methodIndex = readUShortArg(byteCodes, i);
1461 String[] mi = jc.getFieldInfoName(methodIndex);
1462 char[] returnType = { 'V' };
1463 StringBuilder cnt = new StringBuilder();
1464 String mn = findMethodName(mi, cnt, returnType);
1466 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1467 final Variable[] vars = new Variable[numArguments];
1469 for (int j = numArguments - 1; j >= 0; --j) {
1470 vars[j] = mapper.pop();
1473 if (returnType[0] != 'V') {
1475 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1479 final String in = mi[0];
1480 out.append(accessClass(in.replace('/', '_')));
1481 out.append("(false).");
1482 if (mn.startsWith("cons_")) {
1483 out.append("constructor.");
1489 out.append(".call(");
1491 if (numArguments > 0) {
1492 out.append(vars[0]);
1493 for (int j = 1; j < numArguments; ++j) {
1495 out.append(vars[j]);
1503 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1504 throws IOException {
1505 int methodIndex = readUShortArg(byteCodes, i);
1506 String[] mi = jc.getFieldInfoName(methodIndex);
1507 char[] returnType = { 'V' };
1508 StringBuilder cnt = new StringBuilder();
1509 String mn = findMethodName(mi, cnt, returnType);
1511 final int numArguments = cnt.length() + 1;
1512 final Variable[] vars = new Variable[numArguments];
1514 for (int j = numArguments - 1; j >= 0; --j) {
1515 vars[j] = mapper.pop();
1518 if (returnType[0] != 'V') {
1520 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1524 out.append(vars[0]).append('.');
1528 for (int j = 1; j < numArguments; ++j) {
1530 out.append(vars[j]);
1538 private void addReference(String cn) throws IOException {
1539 if (requireReference(cn)) {
1540 debug(" /* needs " + cn + " */");
1544 private void outType(String d, StringBuilder out) {
1546 while (d.charAt(0) == '[') {
1550 if (d.charAt(0) == 'L') {
1551 assert d.charAt(d.length() - 1) == ';';
1552 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1558 private String encodeConstant(int entryIndex) throws IOException {
1559 String[] classRef = { null };
1560 String s = jc.stringValue(entryIndex, classRef);
1561 if (classRef[0] != null) {
1562 if (classRef[0].startsWith("[")) {
1563 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1565 addReference(classRef[0]);
1566 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1572 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1573 byte[] arr = m.findAnnotationData(true);
1577 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1578 class P extends AnnotationParser {
1584 String[] args = new String[30];
1588 protected void visitAttr(String type, String attr, String at, String value) {
1589 if (type.equals(jvmType)) {
1590 if ("body".equals(attr)) {
1592 } else if ("args".equals(attr)) {
1593 args[cnt++] = value;
1595 throw new IllegalArgumentException(attr);
1602 if (p.body == null) {
1605 StringBuilder cnt = new StringBuilder();
1606 final String mn = findMethodName(m, cnt);
1607 out.append(destObject).append(".").append(mn);
1608 out.append(" = function(");
1611 for (int i = 0; i < cnt.length(); i++) {
1613 space = outputArg(out, p.args, index);
1616 out.append(") {").append("\n");
1618 out.append("\n}\n");
1621 private static String className(ClassData jc) {
1622 //return jc.getName().getInternalName().replace('/', '_');
1623 return jc.getClassName().replace('/', '_');
1626 private static String[] findAnnotation(
1627 byte[] arr, ClassData cd, final String className,
1628 final String... attrNames
1629 ) throws IOException {
1633 final String[] values = new String[attrNames.length];
1634 final boolean[] found = { false };
1635 final String jvmType = "L" + className.replace('.', '/') + ";";
1636 AnnotationParser ap = new AnnotationParser(false, true) {
1638 protected void visitAttr(String type, String attr, String at, String value) {
1639 if (type.equals(jvmType)) {
1641 for (int i = 0; i < attrNames.length; i++) {
1642 if (attrNames[i].equals(attr)) {
1651 return found[0] ? values : null;
1654 private CharSequence initField(FieldData v) {
1655 final String is = v.getInternalSig();
1656 if (is.length() == 1) {
1657 switch (is.charAt(0)) {
1663 case 'I': return " = 0;";
1665 case 'D': return " = 0.0;";
1667 throw new IllegalStateException(is);
1673 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1674 AnnotationParser ap = new AnnotationParser(true, false) {
1675 int[] cnt = new int[32];
1679 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1680 final String slashType = attrType.substring(1, attrType.length() - 1);
1681 requireReference(slashType);
1683 if (cnt[depth]++ > 0) {
1687 out.append('"').append(attrType).append("\" : ");
1694 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1695 out.append("\n}\n");
1700 protected void visitValueStart(String attrName, char type) throws IOException {
1701 if (cnt[depth]++ > 0) {
1705 if (attrName != null) {
1706 out.append(attrName).append(" : ");
1714 protected void visitValueEnd(String attrName, char type) throws IOException {
1722 protected void visitAttr(String type, String attr, String attrType, String value)
1723 throws IOException {
1724 if (attr == null && value == null) {
1731 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1732 throws IOException {
1733 final String slashType = attrType.substring(1, attrType.length() - 1);
1734 requireReference(slashType);
1736 out.append(accessClass(slashType.replace('/', '_')))
1737 .append("(false).constructor.").append(value);
1743 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1744 final String name = args[indx];
1748 if (name.contains(",")) {
1749 throw new IOException("Wrong parameter with ',': " + name);
1755 private static void emit(final Appendable out,
1756 final String format,
1757 final CharSequence... params) throws IOException {
1758 final int length = format.length();
1761 int paramOffset = format.indexOf('@');
1762 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1763 final char paramChar = format.charAt(paramOffset + 1);
1764 if ((paramChar >= '1') && (paramChar <= '9')) {
1765 final int paramIndex = paramChar - '0' - 1;
1767 out.append(format, processed, paramOffset);
1768 out.append(params[paramIndex]);
1771 processed = paramOffset + 1;
1774 paramOffset = format.indexOf('@', paramOffset + 1);
1777 out.append(format, processed, length);
1780 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1781 out.append("} catch (e) {\n");
1783 for (TrapData e : traps) {
1787 if (e.catch_cpx != 0) { //not finally
1788 final String classInternalName = jc.getClassName(e.catch_cpx);
1789 addReference(classInternalName);
1790 out.append("e = vm.java_lang_Throwable(false).bck2BrwsrCnvrt(e);");
1791 out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
1792 out.append("var stA0 = e;");
1793 goTo(out, current, e.handler_pc, topMostLabel);
1796 finallyPC = e.handler_pc;
1799 if (finallyPC == -1) {
1800 out.append("throw e;");
1802 out.append("var stA0 = e;");
1803 goTo(out, current, finallyPC, topMostLabel);
1808 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1811 out.append("{ gt = 0; continue X_" + to + "; }");
1813 out.append("{ gt = " + to + "; continue X_0; }");
1816 out.append("{ gt = " + to + "; break IF; }");
1820 private static void emitIf(
1821 Appendable out, String pattern, Variable param,
1822 int current, int to, int canBack
1823 ) throws IOException {
1824 emit(out, pattern, param);
1825 goTo(out, current, to, canBack);
1828 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1831 case 4: jvmType = "[Z"; break;
1832 case 5: jvmType = "[C"; break;
1833 case 6: jvmType = "[F"; break;
1834 case 7: jvmType = "[D"; break;
1835 case 8: jvmType = "[B"; break;
1836 case 9: jvmType = "[S"; break;
1837 case 10: jvmType = "[I"; break;
1838 case 11: jvmType = "[J"; break;
1839 default: throw new IllegalStateException("Array type: " + atype);
1841 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1842 smapper.popI(), smapper.pushA(), jvmType);
1845 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1846 String typeName = jc.getClassName(type);
1847 if (typeName.startsWith("[")) {
1848 typeName = "[" + typeName;
1850 typeName = "[L" + typeName + ";";
1852 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1853 smapper.popI(), smapper.pushA(), typeName);
1856 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1857 String typeName = jc.getClassName(type);
1858 int dim = readUByte(byteCodes, ++i);
1859 StringBuilder dims = new StringBuilder();
1861 for (int d = 0; d < dim; d++) {
1863 dims.insert(1, ",");
1865 dims.insert(1, smapper.popI());
1868 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1869 dims.toString(), smapper.pushA(), typeName);
1873 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1874 int table = i / 4 * 4 + 4;
1875 int dflt = i + readInt4(byteCodes, table);
1877 int low = readInt4(byteCodes, table);
1879 int high = readInt4(byteCodes, table);
1881 out.append("switch (").append(smapper.popI()).append(") {\n");
1882 while (low <= high) {
1883 int offset = i + readInt4(byteCodes, table);
1885 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1888 out.append(" default: ");
1889 goTo(out, i, dflt, topMostLabel);
1895 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1896 int table = i / 4 * 4 + 4;
1897 int dflt = i + readInt4(byteCodes, table);
1899 int n = readInt4(byteCodes, table);
1901 out.append("switch (").append(smapper.popI()).append(") {\n");
1903 int cnstnt = readInt4(byteCodes, table);
1905 int offset = i + readInt4(byteCodes, table);
1907 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1909 out.append(" default: ");
1910 goTo(out, i, dflt, topMostLabel);
1916 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1917 final String type = jc.getClassName(indx);
1918 if (!type.startsWith("[")) {
1919 emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
1920 smapper.popA(), smapper.pushI(),
1921 type.replace('/', '_'));
1923 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1924 smapper.popA(), smapper.pushI(),
1930 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1931 final String type = jc.getClassName(indx);
1932 if (!type.startsWith("[")) {
1934 "if (@1 !== null && !@1.$instOf_@2) throw vm.java_lang_ClassCastException(true);",
1935 smapper.getA(0), type.replace('/', '_'));
1937 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);",
1938 smapper.getA(0), type
1943 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1944 for (int j = prev; j <= i; j++) {
1946 final int cc = readUByte(byteCodes, j);
1947 out.append(Integer.toString(cc));