2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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 org.apidesign.bck2brwsr.core.JavaScriptBody;
23 import static org.apidesign.vm4brwsr.ByteCodeParser.*;
24 import org.apidesign.vm4brwsr.ByteCodeParser.AnnotationParser;
25 import org.apidesign.vm4brwsr.ByteCodeParser.BootMethodData;
26 import org.apidesign.vm4brwsr.ByteCodeParser.CPX2;
27 import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
28 import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
29 import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
30 import org.apidesign.vm4brwsr.ByteCodeParser.StackMapIterator;
31 import org.apidesign.vm4brwsr.ByteCodeParser.TrapData;
32 import org.apidesign.vm4brwsr.ByteCodeParser.TrapDataIterator;
34 /** Translator of the code inside class files to JavaScript.
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 abstract class ByteCodeToJavaScript implements Appendable {
40 private final Appendable out;
41 private final StringArray classRefs = new StringArray();
42 private boolean outChanged;
43 private boolean callbacks;
44 private final NumberOperations numbers = new NumberOperations();
46 protected ByteCodeToJavaScript(Appendable out) {
51 public final Appendable append(CharSequence csq) throws IOException {
58 public final Appendable append(CharSequence csq, int start, int end) throws IOException {
59 out.append(csq, start, end);
65 public final Appendable append(char c) throws IOException {
71 /* Collects additional required resources.
73 * @param internalClassName classes that were referenced and should be loaded in order the
74 * generated JavaScript code works properly. The names are in internal
75 * JVM form so String is <code>java/lang/String</code>.
77 protected abstract boolean requireReference(String internalClassName);
80 * @param resourcePath name of resources to read
82 protected abstract void requireScript(String resourcePath) throws IOException;
84 protected abstract void requireResource(String resourcePath) throws IOException;
86 /** Allows subclasses to redefine what field a function representing a
87 * class gets assigned. By default it returns the suggested name followed
88 * by <code>" = "</code>;
90 * @param className suggested name of the class
92 /* protected */ String assignClass(String className) {
93 return className + " = ";
95 /* protected */ String accessClass(String classOperation) {
96 return classOperation;
99 final String accessClassFalse(String classOperation) {
100 if (mangleClassName(jc.getClassName()).equals(classOperation)) {
103 classRefs.addIfMissing(classOperation);
104 return "(refs_" + classOperation + " || (refs_" + classOperation + " = " + accessClass(classOperation) + "(false)))";
107 protected FieldData findField(String[] fieldInfoName) throws IOException {
111 protected String accessField(String object, FieldData data, String[] fieldInfoName)
113 String mangledName = "_" + fieldInfoName[1];
114 return object + "." + mangledName;
117 protected String accessStaticMethod(
120 String[] fieldInfoName) throws IOException {
121 return object + "." + mangledName;
124 protected String accessVirtualMethod(
127 String[] fieldInfoName,
129 ) throws IOException {
130 return object + "." + mangledName + '(';
133 protected void declareClass(ClassData classData, String mangledName)
138 protected void declaredField(FieldData fieldData,
140 String mangledName) throws IOException {
143 protected void declaredMethod(MethodData methodData,
145 String mangledName) throws IOException {
148 /** Prints out a debug message.
150 * @param msg the message
151 * @return true if the message has been printed
152 * @throws IOException
154 boolean debug(String msg) throws IOException {
160 * Converts a given class file to a JavaScript version.
162 * @param classFile input stream with code of the .class file
163 * @return the initialization code for this class, if any. Otherwise <code>null</code>
165 * @throws IOException if something goes wrong during read or write or translating
168 public String compile(InputStream classFile) throws IOException {
169 return compile(new ClassData(classFile));
172 protected String compile(ClassData classData) throws IOException {
174 final String cn = this.jc.getClassName();
176 return compileImpl(cn);
177 } catch (IOException ex) {
178 throw new IOException("Cannot compile " + cn + ":", ex);
182 private String compileImpl(final String cn) throws IOException {
183 this.numbers.reset();
184 this.callbacks = cn.endsWith("/$JsCallbacks$");
185 if (jc.getMajor_version() < 50 && !cn.endsWith("/package-info")) {
186 throw new IOException("Can't compile " + cn + ". Class file version " + jc.getMajor_version() + "."
187 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
190 byte[] arrData = jc.findAnnotationData(true);
192 String[] arr = findAnnotation(arrData, jc,
193 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
194 "resource", "processByteCode"
197 if (!arr[0].isEmpty()) {
198 requireScript(arr[0]);
200 if ("0".equals(arr[1])) {
205 final String jsResource;
207 String[] arr = findAnnotation(arrData, jc,
208 "net.java.html.js.JavaScriptResource",
212 if (arr[0].startsWith("/")) {
215 int last = cn.lastIndexOf('/');
216 jsResource = cn.substring(0, last + 1).replace('.', '/') + arr[0];
222 String[] proto = findAnnotation(arrData, jc,
223 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
224 "container", "prototype"
226 StringArray toInitilize = new StringArray();
227 final String className = className(jc);
229 append("function ").append(className).append("() {");
231 append("\n var CLS = ").append(className).append(';');
232 append("\n if (!CLS.$class) {");
234 String sc = jc.getSuperClassName(); // with _
235 append("\n var pp = ").
236 append(accessClass(mangleClassName(sc))).append("(true);");
237 append("\n var p = CLS.prototype = pp;");
238 append("\n var c = p;");
239 append("\n var sprcls = pp.constructor.$class;");
241 append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
242 if (proto[0] == null) {
245 append("\n var c = ").append(proto[0]).append(";");
246 append("\n var sprcls = null;");
248 for (FieldData v : jc.getFields()) {
250 if ((v.access & ACC_FINAL) != 0 && v.hasConstantValue()) {
251 if (v.getInternalSig().length() == 1 || v.getInternalSig().equals("Ljava/lang/String;")) {
255 append("\n CLS['fld_").append(v.getName()).append("']").append(initField(v));
256 append("\n m = c._").append(v.getName()).append(" = function (v) {")
257 .append(" if (arguments.length == 1) CLS['fld_").append(v.getName())
258 .append("'] = v; return CLS['fld_").
259 append(v.getName()).append("']; };");
261 append("\n m = c._").append(v.getName()).append(" = function (v) {")
262 .append(" if (arguments.length == 1) this['fld_").
263 append(className).append('_').append(v.getName())
264 .append("'] = v; return this['fld_").
265 append(className).append('_').append(v.getName())
269 declaredField(v, "c", "_" + v.getName());
271 for (MethodData m : jc.getMethods()) {
272 byte[] onlyArr = m.findAnnotationData(true);
273 if (javaScriptOnly(onlyArr)) continue;
279 mn = generateStaticMethod(destObject, m, toInitilize);
281 if (m.isConstructor()) {
283 mn = generateInstanceMethod(destObject, m);
286 mn = generateInstanceMethod(destObject, m);
289 declaredMethod(m, destObject, mn);
290 byte[] runAnno = m.findAnnotationData(false);
291 if (runAnno != null) {
292 append("\n m.anno = {");
293 AnnotationParser ap = new GenerateAnno(true, false);
294 ap.parse(runAnno, jc);
297 append("\n m.access = " + m.getAccess()).append(";");
298 append("\n m.cls = CLS;");
300 append(numbers.generate());
301 append("\n c.constructor = CLS;");
302 append("\n function ").append(className).append("fillInstOf(x) {");
303 String instOfName = "$instOf_" + className;
304 append("\n Object.defineProperty(x, '").append(instOfName).append("', { value : true });");
305 if (jc.isInterface()) {
306 for (MethodData m : jc.getMethods()) {
307 if ((m.getAccess() & ACC_ABSTRACT) == 0
308 && (m.getAccess() & ACC_STATIC) == 0
309 && (m.getAccess() & ACC_PRIVATE) == 0) {
310 final String mn = findMethodName(m, new StringBuilder());
311 append("\n if (!x['").append(mn).append("']) Object.defineProperty(x, '").append(mn).append("', { value : c['").append(mn).append("']});");
315 for (String superInterface : jc.getSuperInterfaces()) {
316 String intrfc = mangleClassName(superInterface);
317 append("\n vm.").append(intrfc).append("(false)['fillInstOf'](x);");
318 requireReference(superInterface);
321 append("\n if (!c.hasOwnProperty('fillInstOf')) Object.defineProperty(c, 'fillInstOf', { value: ").append(className).append("fillInstOf });");
322 append("\n ").append(className).append("fillInstOf(c);");
323 // obfuscationDelegate.exportJSProperty(this, "c", instOfName);
324 append("\n CLS.$class = 'temp';");
325 append("\n CLS.$class = ");
326 append(accessClass("java_lang_Class")).append("(true);");
327 append("\n CLS.$class.jvmName = '").append(cn).append("';");
328 append("\n CLS.$class.superclass = sprcls;");
329 append("\n CLS.$class.interfaces = function() { return [");
331 boolean first = true;
332 for (String intrfc : jc.getSuperInterfaces()) {
336 requireReference(intrfc);
337 String mangledIface = mangleClassName(intrfc);
339 append(accessClass(mangledIface)).append("(false).constructor.$class");
344 int flags = jc.getAccessFlags();
345 if (jc.hasEnclosingMethod()) {
348 append("\n CLS.$class.access = ").append(flags+";");
349 append("\n CLS.$class.cnstr = CLS;");
350 byte[] classAnno = jc.findAnnotationData(false);
351 if (classAnno != null) {
352 append("\n CLS.$class.anno = {");
353 AnnotationParser ap = new GenerateAnno(true, false);
354 ap.parse(classAnno, jc);
357 for (String init : toInitilize.toArray()) {
358 append("\n ").append(init).append("();");
360 for (String ref : classRefs.toArray()) {
361 append("\n var refs_").append(ref).append(";");
365 if (jsResource != null) {
366 requireResource(jsResource);
370 append("\n if (arguments.length === 0) {");
371 append("\n if (!(this instanceof CLS)) {");
372 append("\n return new CLS();");
374 for (FieldData v : jc.getFields()) {
375 byte[] onlyArr = v.findAnnotationData(true);
376 if (javaScriptOnly(onlyArr)) continue;
378 append("\n this['fld_").
379 append(className).append('_').
380 append(v.getName()).append("']").append(initField(v));
383 append("\n return this;");
385 append("\n return arguments[0] ? new CLS() : CLS.prototype;");
388 append("\n").append(assignClass(className));
389 declareClass(jc, className);
392 // StringBuilder sb = new StringBuilder();
393 // for (String init : toInitilize.toArray()) {
394 // sb.append("\n").append(init).append("();");
399 private boolean javaScriptOnly(byte[] anno) throws IOException {
400 String[] only = findAnnotation(anno, jc,
401 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
405 if (only[0] != null && only[1] != null) {
406 append("\n p.").append(only[0]).append(" = ")
407 .append(only[1]).append(";");
409 if (ExportedSymbols.isMarkedAsExported(anno, jc)) {
410 append("\n p['").append(only[0]).append("'] = p.")
411 .append(only[0]).append(";");
417 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
418 String jsb = javaScriptBody(destObject, m, true);
422 final String mn = findMethodName(m, new StringBuilder());
423 boolean defineProp = generateMethod(destObject, mn, m);
424 if (mn.equals("class__V")) {
426 toInitilize.add(accessClassFalse(className(jc)) + "['" + mn + "']");
428 toInitilize.add(accessClassFalse(className(jc)) + "." + mn);
434 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
435 String jsb = javaScriptBody(destObject, m, false);
439 final String mn = findMethodName(m, new StringBuilder());
440 generateMethod(destObject, mn, m);
444 private boolean generateMethod(String destObject, String name, MethodData m)
446 final StackMapIterator stackMapIterator = m.createStackMapIterator();
447 TrapDataIterator trap = m.getTrapDataIterator();
448 final LocalsMapper lmapper =
449 new LocalsMapper(stackMapIterator.getArguments());
452 "java/lang/Object".equals(jc.getClassName()) ||
453 "java/lang/reflect/Array".equals(jc.getClassName());
456 append("Object.defineProperty(").append(destObject).
457 append(", '").append(name).append("', { configurable: true, writable: true, value: m = function(");
459 append("m = ").append(destObject).append(".").append(name).append(" = function(");
461 lmapper.outputArguments(this, m.isStatic());
462 append(") {").append("\n");
464 final byte[] byteCodes = m.getCode();
465 if (byteCodes == null) {
466 byte[] defaultAttr = m.getDefaultAttribute();
467 if (defaultAttr != null) {
469 AnnotationParser ap = new GenerateAnno(true, false);
470 ap.parseDefault(defaultAttr, jc);
473 if (debug(" throw 'no code found for ")) {
475 .append(jc.getClassName()).append('.')
476 .append(m.getName()).append("';\n");
487 final StackMapper smapper = new StackMapper();
490 append(" var ").append(" lcA0 = this;\n");
494 TrapData[] previousTrap = null;
495 boolean wide = false;
497 if (stackMapIterator.isEmpty()) {
503 append("\n var gt = 0;\n");
507 int topMostLabel = 0;
508 for (int i = 0; i < byteCodes.length; i++) {
511 stackMapIterator.advanceTo(i);
512 boolean changeInCatch = trap.advanceTo(i);
513 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
514 if (previousTrap != null) {
515 generateCatch(previousTrap, i, topMostLabel);
519 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
524 if (openBraces > 64) {
525 for (int c = 0; c < 64; c++) {
532 lastStackFrame = stackMapIterator.getFrameIndex();
533 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
534 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
535 append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
537 changeInCatch = true;
539 debug(" /* " + i + " */ ");
541 if (changeInCatch && trap.useTry()) {
543 previousTrap = trap.current();
545 final int c = readUByte(byteCodes, i);
548 smapper.assign(this, VarType.REFERENCE, lmapper.getA(0));
551 smapper.assign(this, VarType.INTEGER, lmapper.getI(0));
554 smapper.assign(this, VarType.LONG, lmapper.getL(0));
557 smapper.assign(this, VarType.FLOAT, lmapper.getF(0));
560 smapper.assign(this, VarType.DOUBLE, lmapper.getD(0));
563 smapper.assign(this, VarType.REFERENCE, lmapper.getA(1));
566 smapper.assign(this, VarType.INTEGER, lmapper.getI(1));
569 smapper.assign(this, VarType.LONG, lmapper.getL(1));
572 smapper.assign(this, VarType.FLOAT, lmapper.getF(1));
575 smapper.assign(this, VarType.DOUBLE, lmapper.getD(1));
578 smapper.assign(this, VarType.REFERENCE, lmapper.getA(2));
581 smapper.assign(this, VarType.INTEGER, lmapper.getI(2));
584 smapper.assign(this, VarType.LONG, lmapper.getL(2));
587 smapper.assign(this, VarType.FLOAT, lmapper.getF(2));
590 smapper.assign(this, VarType.DOUBLE, lmapper.getD(2));
593 smapper.assign(this, VarType.REFERENCE, lmapper.getA(3));
596 smapper.assign(this, VarType.INTEGER, lmapper.getI(3));
599 smapper.assign(this, VarType.LONG, lmapper.getL(3));
602 smapper.assign(this, VarType.FLOAT, lmapper.getF(3));
605 smapper.assign(this, VarType.DOUBLE, lmapper.getD(3));
609 final int indx = wide ? readUShort(byteCodes, i++)
610 : readUByte(byteCodes, i);
612 smapper.assign(this, VarType.INTEGER, lmapper.getI(indx));
617 final int indx = wide ? readUShort(byteCodes, i++)
618 : readUByte(byteCodes, i);
620 smapper.assign(this, VarType.LONG, lmapper.getL(indx));
625 final int indx = wide ? readUShort(byteCodes, i++)
626 : readUByte(byteCodes, i);
628 smapper.assign(this, VarType.FLOAT, lmapper.getF(indx));
633 final int indx = wide ? readUShort(byteCodes, i++)
634 : readUByte(byteCodes, i);
636 smapper.assign(this, VarType.DOUBLE, lmapper.getD(indx));
641 final int indx = wide ? readUShort(byteCodes, i++)
642 : readUByte(byteCodes, i);
644 smapper.assign(this, VarType.REFERENCE, lmapper.getA(indx));
649 final int indx = wide ? readUShort(byteCodes, i++)
650 : readUByte(byteCodes, i);
652 emit(smapper, this, "var @1 = @2;",
653 lmapper.setI(indx), smapper.popI());
658 final int indx = wide ? readUShort(byteCodes, i++)
659 : readUByte(byteCodes, i);
661 emit(smapper, this, "var @1 = @2;",
662 lmapper.setL(indx), smapper.popL());
667 final int indx = wide ? readUShort(byteCodes, i++)
668 : readUByte(byteCodes, i);
670 emit(smapper, this, "var @1 = @2;",
671 lmapper.setF(indx), smapper.popF());
676 final int indx = wide ? readUShort(byteCodes, i++)
677 : readUByte(byteCodes, i);
679 emit(smapper, this, "var @1 = @2;",
680 lmapper.setD(indx), smapper.popD());
685 final int indx = wide ? readUShort(byteCodes, i++)
686 : readUByte(byteCodes, i);
688 emit(smapper, this, "var @1 = @2;",
689 lmapper.setA(indx), smapper.popA());
693 emit(smapper, this, "var @1 = @2;", lmapper.setA(0), smapper.popA());
696 emit(smapper, this, "var @1 = @2;", lmapper.setI(0), smapper.popI());
699 emit(smapper, this, "var @1 = @2;", lmapper.setL(0), smapper.popL());
702 emit(smapper, this, "var @1 = @2;", lmapper.setF(0), smapper.popF());
705 emit(smapper, this, "var @1 = @2;", lmapper.setD(0), smapper.popD());
708 emit(smapper, this, "var @1 = @2;", lmapper.setA(1), smapper.popA());
711 emit(smapper, this, "var @1 = @2;", lmapper.setI(1), smapper.popI());
714 emit(smapper, this, "var @1 = @2;", lmapper.setL(1), smapper.popL());
717 emit(smapper, this, "var @1 = @2;", lmapper.setF(1), smapper.popF());
720 emit(smapper, this, "var @1 = @2;", lmapper.setD(1), smapper.popD());
723 emit(smapper, this, "var @1 = @2;", lmapper.setA(2), smapper.popA());
726 emit(smapper, this, "var @1 = @2;", lmapper.setI(2), smapper.popI());
729 emit(smapper, this, "var @1 = @2;", lmapper.setL(2), smapper.popL());
732 emit(smapper, this, "var @1 = @2;", lmapper.setF(2), smapper.popF());
735 emit(smapper, this, "var @1 = @2;", lmapper.setD(2), smapper.popD());
738 emit(smapper, this, "var @1 = @2;", lmapper.setA(3), smapper.popA());
741 emit(smapper, this, "var @1 = @2;", lmapper.setI(3), smapper.popI());
744 emit(smapper, this, "var @1 = @2;", lmapper.setL(3), smapper.popL());
747 emit(smapper, this, "var @1 = @2;", lmapper.setF(3), smapper.popF());
750 emit(smapper, this, "var @1 = @2;", lmapper.setD(3), smapper.popD());
753 smapper.replace(this, VarType.INTEGER, "(((@1) + (@2)) | 0)", smapper.getI(1), smapper.popI());
756 smapper.replace(this, VarType.LONG, numbers.add64(), smapper.getL(1), smapper.popL());
759 smapper.replace(this, VarType.FLOAT, "(@1 + @2)", smapper.getF(1), smapper.popF());
762 smapper.replace(this, VarType.DOUBLE, "(@1 + @2)", smapper.getD(1), smapper.popD());
765 smapper.replace(this, VarType.INTEGER, "(((@1) - (@2)) | 0)", smapper.getI(1), smapper.popI());
768 smapper.replace(this, VarType.LONG, numbers.sub64(), smapper.getL(1), smapper.popL());
771 smapper.replace(this, VarType.FLOAT, "(@1 - @2)", smapper.getF(1), smapper.popF());
774 smapper.replace(this, VarType.DOUBLE, "(@1 - @2)", smapper.getD(1), smapper.popD());
777 smapper.replace(this, VarType.INTEGER, numbers.mul32(), smapper.getI(1), smapper.popI());
780 smapper.replace(this, VarType.LONG, numbers.mul64(), smapper.getL(1), smapper.popL());
783 smapper.replace(this, VarType.FLOAT, "(@1 * @2)", smapper.getF(1), smapper.popF());
786 smapper.replace(this, VarType.DOUBLE, "(@1 * @2)", smapper.getD(1), smapper.popD());
789 smapper.replace(this, VarType.INTEGER, numbers.div32(),
790 smapper.getI(1), smapper.popI());
793 smapper.replace(this, VarType.LONG, numbers.div64(),
794 smapper.getL(1), smapper.popL());
797 smapper.replace(this, VarType.FLOAT, "(@1 / @2)", smapper.getF(1), smapper.popF());
800 smapper.replace(this, VarType.DOUBLE, "(@1 / @2)", smapper.getD(1), smapper.popD());
803 smapper.replace(this, VarType.INTEGER, numbers.mod32(),
804 smapper.getI(1), smapper.popI());
807 smapper.replace(this, VarType.LONG, numbers.mod64(),
808 smapper.getL(1), smapper.popL());
811 smapper.replace(this, VarType.FLOAT, "(@1 % @2)", smapper.getF(1), smapper.popF());
814 smapper.replace(this, VarType.DOUBLE, "(@1 % @2)", smapper.getD(1), smapper.popD());
817 smapper.replace(this, VarType.INTEGER, "(@1 & @2)", smapper.getI(1), smapper.popI());
820 smapper.replace(this, VarType.LONG, numbers.and64(), smapper.getL(1), smapper.popL());
823 smapper.replace(this, VarType.INTEGER, "(@1 | @2)", smapper.getI(1), smapper.popI());
826 smapper.replace(this, VarType.LONG, numbers.or64(), smapper.getL(1), smapper.popL());
829 smapper.replace(this, VarType.INTEGER, "(@1 ^ @2)", smapper.getI(1), smapper.popI());
832 smapper.replace(this, VarType.LONG, numbers.xor64(), smapper.getL(1), smapper.popL());
835 smapper.replace(this, VarType.INTEGER, "(-(@1) | 0)", smapper.getI(0));
838 smapper.replace(this, VarType.LONG, numbers.neg64(), smapper.getL(0));
841 smapper.replace(this, VarType.FLOAT, "(-@1)", smapper.getF(0));
844 smapper.replace(this, VarType.DOUBLE, "(-@1)", smapper.getD(0));
847 smapper.replace(this, VarType.INTEGER, "(@1 << @2)", smapper.getI(1), smapper.popI());
850 smapper.replace(this, VarType.LONG, numbers.shl64(), smapper.getL(1), smapper.popI());
853 smapper.replace(this, VarType.INTEGER, "(@1 >> @2)", smapper.getI(1), smapper.popI());
856 smapper.replace(this, VarType.LONG, numbers.shr64(), smapper.getL(1), smapper.popI());
859 smapper.replace(this, VarType.INTEGER, "(@1 >>> @2)", smapper.getI(1), smapper.popI());
862 smapper.replace(this, VarType.LONG, numbers.ushr64(), smapper.getL(1), smapper.popI());
866 final int varIndx = wide ? readUShort(byteCodes, i++)
867 : readUByte(byteCodes, i);
869 final int incrBy = wide ? readShort(byteCodes, i++)
873 emit(smapper, this, "@1++;", lmapper.getI(varIndx));
875 emit(smapper, this, "@1 += @2;",
876 lmapper.getI(varIndx),
877 Integer.toString(incrBy));
882 emit(smapper, this, "return;");
885 emit(smapper, this, "return @1;", smapper.popI());
888 emit(smapper, this, "return @1;", smapper.popL());
891 emit(smapper, this, "return @1;", smapper.popF());
894 emit(smapper, this, "return @1;", smapper.popD());
897 emit(smapper, this, "return @1;", smapper.popA());
900 smapper.replace(this, VarType.LONG, "@1", smapper.getI(0));
903 smapper.replace(this, VarType.FLOAT, "@1", smapper.getI(0));
906 smapper.replace(this, VarType.DOUBLE, "@1", smapper.getI(0));
909 smapper.replace(this, VarType.INTEGER, "((@1) | 0)", smapper.getL(0));
913 smapper.replace(this, VarType.FLOAT, "(@1).toFP()", smapper.getL(0));
916 smapper.replace(this, VarType.DOUBLE, "(@1).toFP()", smapper.getL(0));
919 smapper.replace(this, VarType.DOUBLE, "@1",
923 smapper.replace(this, VarType.FLOAT, "@1",
927 smapper.replace(this, VarType.INTEGER, "((@1) | 0)",
931 smapper.replace(this, VarType.LONG, "(@1).toLong()",
935 smapper.replace(this, VarType.INTEGER, "((@1)| 0)",
939 smapper.replace(this, VarType.LONG, "(@1).toLong()", smapper.getD(0));
942 smapper.replace(this, VarType.INTEGER, "(((@1) << 24) >> 24)", smapper.getI(0));
946 smapper.replace(this, VarType.INTEGER, "(((@1) << 16) >> 16)", smapper.getI(0));
948 case opc_aconst_null:
949 smapper.assign(this, VarType.REFERENCE, "null");
952 smapper.assign(this, VarType.INTEGER, "-1");
955 smapper.assign(this, VarType.INTEGER, "0");
958 smapper.assign(this, VarType.DOUBLE, "0");
961 smapper.assign(this, VarType.LONG, "0");
964 smapper.assign(this, VarType.FLOAT, "0");
967 smapper.assign(this, VarType.INTEGER, "1");
970 smapper.assign(this, VarType.LONG, "1");
973 smapper.assign(this, VarType.FLOAT, "1");
976 smapper.assign(this, VarType.DOUBLE, "1");
979 smapper.assign(this, VarType.INTEGER, "2");
982 smapper.assign(this, VarType.FLOAT, "2");
985 smapper.assign(this, VarType.INTEGER, "3");
988 smapper.assign(this, VarType.INTEGER, "4");
991 smapper.assign(this, VarType.INTEGER, "5");
994 int indx = readUByte(byteCodes, ++i);
995 String v = encodeConstant(indx);
996 int type = VarType.fromConstantType(jc.getTag(indx));
997 smapper.assign(this, type, v);
1002 int indx = readUShortArg(byteCodes, i);
1004 String v = encodeConstant(indx);
1005 int type = VarType.fromConstantType(jc.getTag(indx));
1006 if (type == VarType.LONG) {
1007 final Long lv = new Long(v);
1008 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
1009 final int hi = (int)(lv.longValue() >> 32);
1011 smapper.assign(this, VarType.LONG, "0x" + Integer.toHexString(low));
1013 smapper.assign(this, VarType.LONG,
1014 "0x" + Integer.toHexString(hi) + ".next32(0x" +
1015 Integer.toHexString(low) + ")"
1019 smapper.assign(this, type, v);
1024 smapper.replace(this, VarType.INTEGER, numbers.compare64(), smapper.popL(), smapper.getL(0));
1028 emit(smapper, this, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
1029 smapper.popF(), smapper.popF(), smapper.pushI());
1033 emit(smapper, this, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
1034 smapper.popD(), smapper.popD(), smapper.pushI());
1037 i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
1038 "===", topMostLabel);
1041 i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
1042 "!==", topMostLabel);
1045 i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1046 "==", topMostLabel);
1049 int indx = i + readShortArg(byteCodes, i);
1050 emitIf(smapper, this, "if ((@1) == 0) ",
1051 smapper.popI(), i, indx, topMostLabel);
1056 int indx = i + readShortArg(byteCodes, i);
1057 emitIf(smapper, this, "if ((@1) != 0) ",
1058 smapper.popI(), i, indx, topMostLabel);
1063 int indx = i + readShortArg(byteCodes, i);
1064 emitIf(smapper, this, "if ((@1) < 0) ",
1065 smapper.popI(), i, indx, topMostLabel);
1070 int indx = i + readShortArg(byteCodes, i);
1071 emitIf(smapper, this, "if ((@1) <= 0) ",
1072 smapper.popI(), i, indx, topMostLabel);
1077 int indx = i + readShortArg(byteCodes, i);
1078 emitIf(smapper, this, "if ((@1) > 0) ",
1079 smapper.popI(), i, indx, topMostLabel);
1084 int indx = i + readShortArg(byteCodes, i);
1085 emitIf(smapper, this, "if ((@1) >= 0) ",
1086 smapper.popI(), i, indx, topMostLabel);
1090 case opc_ifnonnull: {
1091 int indx = i + readShortArg(byteCodes, i);
1092 emitIf(smapper, this, "if ((@1) !== null) ",
1093 smapper.popA(), i, indx, topMostLabel);
1098 int indx = i + readShortArg(byteCodes, i);
1099 emitIf(smapper, this, "if ((@1) === null) ",
1100 smapper.popA(), i, indx, topMostLabel);
1105 i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1106 "!=", topMostLabel);
1109 i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1113 i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1114 "<=", topMostLabel);
1117 i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1121 i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1122 ">=", topMostLabel);
1125 smapper.flush(this);
1126 int indx = i + readShortArg(byteCodes, i);
1127 goTo(this, i, indx, topMostLabel);
1131 case opc_lookupswitch: {
1132 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
1135 case opc_tableswitch: {
1136 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
1139 case opc_invokeinterface: {
1140 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
1143 case opc_invokevirtual:
1144 i = invokeVirtualMethod(byteCodes, i, smapper);
1146 case opc_invokespecial:
1147 i = invokeStaticMethod(byteCodes, i, smapper, false);
1149 case opc_invokestatic:
1150 i = invokeStaticMethod(byteCodes, i, smapper, true);
1152 case opc_invokedynamic: {
1153 int indx = readUShortArg(byteCodes, i);
1154 println("invoke dynamic: " + indx);
1155 ByteCodeParser.CPX2 c2 = jc.getCpoolEntry(indx);
1156 BootMethodData bm = jc.getBootMethod(c2.cpx1);
1157 CPX2 methodHandle = jc.getCpoolEntry(bm.method);
1158 println(" type: " + methodHandle.cpx1);
1159 String[] mi = jc.getFieldInfoName(methodHandle.cpx2);
1160 String mcn = mangleClassName(mi[0]);
1161 char[] returnType = {'V'};
1162 StringBuilder cnt = new StringBuilder();
1163 String mn = findMethodName(mi, cnt, returnType);
1164 StringBuilder sb = new StringBuilder();
1165 sb.append("We don't handle invokedynamic, need to preprocess ahead-of-time:\n");
1166 sb.append(" mi[0]: ").append(mi[0]).append("\n");
1167 sb.append(" mi[1]: ").append(mi[1]).append("\n");
1168 sb.append(" mi[2]: ").append(mi[2]).append("\n");
1169 sb.append(" mn : ").append(mn).append("\n");
1170 sb.append(" name and type: ").append(jc.stringValue(c2.cpx2, true)).append("\n");
1171 throw new IOException(sb.toString());
1173 CPX2 nameAndType = jc.getCpoolEntry(c2.cpx2);
1174 String type = jc.StringValue(nameAndType.cpx2);
1175 String object = accessClass(mcn) + "(false)";
1176 if (mn.startsWith("cons_")) {
1177 object += ".constructor";
1179 append("var metHan = ");
1180 append(accessStaticMethod(object, mn, mi));
1182 String lookup = accessClass("java_lang_invoke_MethodHandles") + "(false).findFor__Ljava_lang_invoke_MethodHandles$Lookup_2Ljava_lang_Class_2(CLS.$class)";
1184 append(", '").append(mi[1]).append("', ");
1185 String methodType = accessClass("java_lang_invoke_MethodType") + "(false).fromMethodDescriptorString__Ljava_lang_invoke_MethodType_2Ljava_lang_String_2Ljava_lang_ClassLoader_2(";
1186 append(methodType).append("'").append(type).append("', null)");
1187 // if (numArguments > 0) {
1189 // for (int j = 1; j < numArguments; ++j) {
1195 emit(smapper, this, "throw 'Invoke dynamic: ' + @1 + ': ' + metHan;", "" + indx);
1201 int indx = readUShortArg(byteCodes, i);
1202 String ci = jc.getClassName(indx);
1203 if (DirectlLibraries.isScriptLibrary(ci)) {
1204 int last = ci.lastIndexOf('/');
1205 emit(smapper, this, "var @1 = new @2",
1206 smapper.pushA(), ci.substring(last + 1)
1211 emit(smapper, this, "var @1 = new @2;",
1212 smapper.pushA(), accessClass(mangleClassName(ci)));
1219 int atype = readUByte(byteCodes, ++i);
1220 generateNewArray(atype, smapper);
1222 case opc_anewarray: {
1223 int type = readUShortArg(byteCodes, i);
1225 generateANewArray(type, smapper);
1228 case opc_multianewarray: {
1229 int type = readUShortArg(byteCodes, i);
1231 i = generateMultiANewArray(type, byteCodes, i, smapper);
1234 case opc_arraylength:
1235 smapper.replace(this, VarType.INTEGER, "(@1).length", smapper.getA(0));
1238 emit(smapper, this, "Array.at(@3, @2, @1);",
1239 smapper.popL(), smapper.popI(), smapper.popA());
1242 emit(smapper, this, "Array.at(@3, @2, @1);",
1243 smapper.popF(), smapper.popI(), smapper.popA());
1246 emit(smapper, this, "Array.at(@3, @2, @1);",
1247 smapper.popD(), smapper.popI(), smapper.popA());
1250 emit(smapper, this, "Array.at(@3, @2, @1);",
1251 smapper.popA(), smapper.popI(), smapper.popA());
1257 emit(smapper, this, "Array.at(@3, @2, @1);",
1258 smapper.popI(), smapper.popI(), smapper.popA());
1261 smapper.replace(this, VarType.LONG, "(@2[@1] || Array.at(@2, @1))",
1262 smapper.popI(), smapper.getA(0));
1265 smapper.replace(this, VarType.FLOAT, "(@2[@1] || Array.at(@2, @1))",
1266 smapper.popI(), smapper.getA(0));
1269 smapper.replace(this, VarType.DOUBLE, "(@2[@1] || Array.at(@2, @1))",
1270 smapper.popI(), smapper.getA(0));
1273 smapper.replace(this, VarType.REFERENCE, "(@2[@1] || Array.at(@2, @1))",
1274 smapper.popI(), smapper.getA(0));
1280 smapper.replace(this, VarType.INTEGER, "(@2[@1] || Array.at(@2, @1))",
1281 smapper.popI(), smapper.getA(0));
1289 final Variable v = smapper.get(0);
1290 if (smapper.isDirty()) {
1291 emit(smapper, this, "var @1 = @2;", smapper.pushT(v.getType()), v);
1293 smapper.assign(this, v.getType(), v);
1298 final Variable vi1 = smapper.get(0);
1300 if (vi1.isCategory2()) {
1301 emit(smapper, this, "var @1 = @2;",
1302 smapper.pushT(vi1.getType()), vi1);
1304 final Variable vi2 = smapper.get(1);
1305 emit(smapper, this, "var @1 = @2, @3 = @4;",
1306 smapper.pushT(vi2.getType()), vi2,
1307 smapper.pushT(vi1.getType()), vi1);
1312 final Variable vi1 = smapper.pop(this);
1313 final Variable vi2 = smapper.pop(this);
1314 final Variable vo3 = smapper.pushT(vi1.getType());
1315 final Variable vo2 = smapper.pushT(vi2.getType());
1316 final Variable vo1 = smapper.pushT(vi1.getType());
1318 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
1319 vo1, vi1, vo2, vi2, vo3, vo1);
1323 final Variable vi1 = smapper.pop(this);
1324 final Variable vi2 = smapper.pop(this);
1326 if (vi1.isCategory2()) {
1327 final Variable vo3 = smapper.pushT(vi1.getType());
1328 final Variable vo2 = smapper.pushT(vi2.getType());
1329 final Variable vo1 = smapper.pushT(vi1.getType());
1331 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
1332 vo1, vi1, vo2, vi2, vo3, vo1);
1334 final Variable vi3 = smapper.pop(this);
1335 final Variable vo5 = smapper.pushT(vi2.getType());
1336 final Variable vo4 = smapper.pushT(vi1.getType());
1337 final Variable vo3 = smapper.pushT(vi3.getType());
1338 final Variable vo2 = smapper.pushT(vi2.getType());
1339 final Variable vo1 = smapper.pushT(vi1.getType());
1341 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
1342 vo1, vi1, vo2, vi2, vo3, vi3);
1343 emit(smapper, this, " @1 = @2, @3 = @4;",
1344 vo4, vo1, vo5, vo2);
1349 final Variable vi1 = smapper.pop(this);
1350 final Variable vi2 = smapper.pop(this);
1352 if (vi2.isCategory2()) {
1353 final Variable vo3 = smapper.pushT(vi1.getType());
1354 final Variable vo2 = smapper.pushT(vi2.getType());
1355 final Variable vo1 = smapper.pushT(vi1.getType());
1357 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
1358 vo1, vi1, vo2, vi2, vo3, vo1);
1360 final Variable vi3 = smapper.pop(this);
1361 final Variable vo4 = smapper.pushT(vi1.getType());
1362 final Variable vo3 = smapper.pushT(vi3.getType());
1363 final Variable vo2 = smapper.pushT(vi2.getType());
1364 final Variable vo1 = smapper.pushT(vi1.getType());
1366 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1367 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1372 final Variable vi1 = smapper.pop(this);
1373 final Variable vi2 = smapper.pop(this);
1375 if (vi1.isCategory2()) {
1376 if (vi2.isCategory2()) {
1377 final Variable vo3 = smapper.pushT(vi1.getType());
1378 final Variable vo2 = smapper.pushT(vi2.getType());
1379 final Variable vo1 = smapper.pushT(vi1.getType());
1381 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
1382 vo1, vi1, vo2, vi2, vo3, vo1);
1384 final Variable vi3 = smapper.pop(this);
1385 final Variable vo4 = smapper.pushT(vi1.getType());
1386 final Variable vo3 = smapper.pushT(vi3.getType());
1387 final Variable vo2 = smapper.pushT(vi2.getType());
1388 final Variable vo1 = smapper.pushT(vi1.getType());
1390 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1391 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1394 final Variable vi3 = smapper.pop(this);
1396 if (vi3.isCategory2()) {
1397 final Variable vo5 = smapper.pushT(vi2.getType());
1398 final Variable vo4 = smapper.pushT(vi1.getType());
1399 final Variable vo3 = smapper.pushT(vi3.getType());
1400 final Variable vo2 = smapper.pushT(vi2.getType());
1401 final Variable vo1 = smapper.pushT(vi1.getType());
1403 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
1404 vo1, vi1, vo2, vi2, vo3, vi3);
1405 emit(smapper, this, " @1 = @2, @3 = @4;",
1406 vo4, vo1, vo5, vo2);
1408 final Variable vi4 = smapper.pop(this);
1409 final Variable vo6 = smapper.pushT(vi2.getType());
1410 final Variable vo5 = smapper.pushT(vi1.getType());
1411 final Variable vo4 = smapper.pushT(vi4.getType());
1412 final Variable vo3 = smapper.pushT(vi3.getType());
1413 final Variable vo2 = smapper.pushT(vi2.getType());
1414 final Variable vo1 = smapper.pushT(vi1.getType());
1416 emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1417 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1418 emit(smapper, this, " @1 = @2, @3 = @4;",
1419 vo5, vo1, vo6, vo2);
1425 final Variable vi1 = smapper.get(0);
1426 final Variable vi2 = smapper.get(1);
1428 if (vi1.getType() == vi2.getType()) {
1429 final Variable tmp = smapper.pushT(vi1.getType());
1431 emit(smapper, this, "var @1 = @2, @2 = @3, @3 = @1;",
1436 smapper.pushT(vi1.getType());
1437 smapper.pushT(vi2.getType());
1442 smapper.assign(this, VarType.INTEGER,
1443 "(" + Integer.toString(byteCodes[++i]) + ")");
1446 smapper.assign(this, VarType.INTEGER,
1447 "(" + Integer.toString(readShortArg(byteCodes, i)) + ")"
1451 case opc_getfield: {
1452 int indx = readUShortArg(byteCodes, i);
1453 String[] fi = jc.getFieldInfoName(indx);
1454 final int type = VarType.fromFieldType(fi[2].charAt(0));
1455 FieldData field = findField(fi);
1456 if (field == null) {
1457 final String mangleClass = mangleClassName(fi[0]);
1458 final String mangleClassAccess = accessClassFalse(mangleClass);
1459 smapper.replace(this, type, "@2.call(@1)",
1461 accessField(mangleClassAccess, null, fi)
1464 final String fieldOwner = mangleClassName(field.cls.getClassName());
1465 smapper.replace(this, type, "@1@2",
1467 accessField(fieldOwner, field, fi)
1471 addReference(fi[0]);
1474 case opc_putfield: {
1475 int indx = readUShortArg(byteCodes, i);
1476 String[] fi = jc.getFieldInfoName(indx);
1477 final int type = VarType.fromFieldType(fi[2].charAt(0));
1478 FieldData field = findField(fi);
1479 if (field == null) {
1480 final String mangleClass = mangleClassName(fi[0]);
1481 final String mangleClassAccess = accessClassFalse(mangleClass);
1482 emit(smapper, this, "@3.call(@2, @1);",
1485 accessField(mangleClassAccess, null, fi)
1488 final String fieldOwner = mangleClassName(field.cls.getClassName());
1489 emit(smapper, this, "@2@3 = @1;",
1492 accessField(fieldOwner, field, fi)
1496 addReference(fi[0]);
1499 case opc_getstatic: {
1500 int indx = readUShortArg(byteCodes, i);
1501 String[] fi = jc.getFieldInfoName(indx);
1502 final int type = VarType.fromFieldType(fi[2].charAt(0));
1503 if (DirectlLibraries.isScriptLibrary(fi[0]) && fi[0].endsWith("/Exports")) {
1504 smapper.assign(this, type, fi[1]);
1506 String ac = accessClassFalse(mangleClassName(fi[0]));
1507 FieldData field = findField(fi);
1508 String af = accessField(ac, field, fi);
1509 smapper.assign(this, type, af + "()");
1510 addReference(fi[0]);
1515 case opc_putstatic: {
1516 int indx = readUShortArg(byteCodes, i);
1517 String[] fi = jc.getFieldInfoName(indx);
1518 final int type = VarType.fromFieldType(fi[2].charAt(0));
1519 emit(smapper, this, "@1._@2(@3);",
1520 accessClassFalse(mangleClassName(fi[0])), fi[1],
1521 smapper.popT(type));
1523 addReference(fi[0]);
1526 case opc_checkcast: {
1527 int indx = readUShortArg(byteCodes, i);
1528 generateCheckcast(indx, smapper);
1532 case opc_instanceof: {
1533 int indx = readUShortArg(byteCodes, i);
1534 generateInstanceOf(indx, smapper);
1539 final CharSequence v = smapper.popA();
1542 emit(smapper, this, "{ var @1 = @2; throw @2; }",
1543 smapper.pushA(), v);
1547 case opc_monitorenter: {
1548 debug("/* monitor enter */");
1553 case opc_monitorexit: {
1554 debug("/* monitor exit */");
1565 emit(smapper, this, "throw 'unknown bytecode @1';",
1566 Integer.toString(c));
1570 generateByteCodeComment(prev, i, byteCodes);
1576 if (previousTrap != null) {
1577 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1582 while (openBraces-- > 0) {
1593 private int generateIf(StackMapper mapper, byte[] byteCodes,
1594 int i, final CharSequence v2, final CharSequence v1,
1595 final String test, int topMostLabel
1596 ) throws IOException {
1598 int indx = i + readShortArg(byteCodes, i);
1599 append("if ((").append(v1)
1600 .append(") ").append(test).append(" (")
1601 .append(v2).append(")) ");
1602 goTo(this, i, indx, topMostLabel);
1606 private int readInt4(byte[] byteCodes, int offset) {
1607 final int d = byteCodes[offset + 0] << 24;
1608 final int c = byteCodes[offset + 1] << 16;
1609 final int b = byteCodes[offset + 2] << 8;
1610 final int a = byteCodes[offset + 3];
1611 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1613 private static int readUByte(byte[] byteCodes, int offset) {
1614 return byteCodes[offset] & 0xff;
1617 private static int readUShort(byte[] byteCodes, int offset) {
1618 return ((byteCodes[offset] & 0xff) << 8)
1619 | (byteCodes[offset + 1] & 0xff);
1621 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1622 return readUShort(byteCodes, offsetInstruction + 1);
1625 private static int readShort(byte[] byteCodes, int offset) {
1626 int signed = byteCodes[offset];
1627 byte b0 = (byte)signed;
1628 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1630 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1631 return readShort(byteCodes, offsetInstruction + 1);
1634 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1636 Boolean count = null;
1637 boolean array = false;
1639 int firstPos = sig.length();
1640 while (i < descriptor.length()) {
1641 char ch = descriptor.charAt(i++);
1662 if (ch == 'J' || ch == 'D') {
1668 sig.insert(firstPos, ch);
1670 returnType[0] = '[';
1671 sig.insert(firstPos, "_3");
1680 returnType[0] = 'V';
1681 sig.insert(firstPos, 'V');
1684 int next = descriptor.indexOf(';', i);
1685 String realSig = mangleSig(descriptor, i - 1, next + 1);
1690 sig.append(realSig);
1693 sig.insert(firstPos, realSig);
1695 sig.insert(firstPos, "_3");
1697 returnType[0] = 'L';
1706 throw new IllegalStateException("Invalid char: " + ch);
1711 static String mangleSig(String sig) {
1712 return mangleSig(sig, 0, sig.length());
1715 private static String mangleMethodName(String name) {
1716 StringBuilder sb = new StringBuilder(name.length() * 2);
1717 int last = name.length();
1718 for (int i = 0; i < last; i++) {
1719 final char ch = name.charAt(i);
1721 case '_': sb.append("_1"); break;
1722 default: sb.append(ch); break;
1725 return sb.toString();
1727 private static String mangleSig(String txt, int first, int last) {
1728 StringBuilder sb = new StringBuilder((last - first) * 2);
1729 for (int i = first; i < last; i++) {
1730 final char ch = txt.charAt(i);
1732 case '/': sb.append('_'); break;
1733 case '_': sb.append("_1"); break;
1734 case ';': sb.append("_2"); break;
1735 case '[': sb.append("_3"); break;
1737 if (Character.isJavaIdentifierPart(ch)) {
1741 String hex = Integer.toHexString(ch).toLowerCase();
1742 for (int m = hex.length(); m < 4; m++) {
1750 return sb.toString();
1753 private static String mangleClassName(String name) {
1754 return mangleSig(name);
1757 private static String findMethodName(MethodData m, StringBuilder cnt) {
1758 StringBuilder name = new StringBuilder();
1759 if ("<init>".equals(m.getName())) { // NOI18N
1760 name.append("cons"); // NOI18N
1761 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1762 name.append("class"); // NOI18N
1764 name.append(mangleMethodName(m.getName()));
1767 countArgs(m.getInternalSig(), new char[1], name, cnt);
1768 return name.toString();
1771 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1772 StringBuilder name = new StringBuilder();
1773 String descr = mi[2];//mi.getDescriptor();
1775 if ("<init>".equals(nm)) { // NOI18N
1776 name.append("cons"); // NOI18N
1778 name.append(mangleMethodName(nm));
1780 countArgs(descr, returnType, name, cnt);
1781 return name.toString();
1784 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1785 throws IOException {
1786 int methodIndex = readUShortArg(byteCodes, i);
1787 String[] mi = jc.getFieldInfoName(methodIndex);
1788 char[] returnType = { 'V' };
1789 StringBuilder cnt = new StringBuilder();
1790 String mn = findMethodName(mi, cnt, returnType);
1792 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1793 final CharSequence[] vars = new CharSequence[numArguments];
1795 for (int j = numArguments - 1; j >= 0; --j) {
1796 vars[j] = mapper.popValue();
1800 "newUpdater__Ljava_util_concurrent_atomic_AtomicIntegerFieldUpdater_2Ljava_lang_Class_2Ljava_lang_String_2".equals(mn)
1801 && "java/util/concurrent/atomic/AtomicIntegerFieldUpdater".equals(mi[0])
1803 "newUpdater__Ljava_util_concurrent_atomic_AtomicLongFieldUpdater_2Ljava_lang_Class_2Ljava_lang_String_2".equals(mn)
1804 && "java/util/concurrent/atomic/AtomicLongFieldUpdater".equals(mi[0])
1806 if (vars[1] instanceof String) {
1807 String field = vars[1].toString();
1808 if (field.length() > 2 && field.charAt(0) == '"' && field.charAt(field.length() - 1) == '"') {
1809 vars[1] = "c._" + field.substring(1, field.length() - 1);
1814 "newUpdater__Ljava_util_concurrent_atomic_AtomicReferenceFieldUpdater_2Ljava_lang_Class_2Ljava_lang_Class_2Ljava_lang_String_2".equals(mn)
1815 && "java/util/concurrent/atomic/AtomicReferenceFieldUpdater".equals(mi[0])
1817 if (vars[1] instanceof String) {
1818 String field = vars[2].toString();
1819 if (field.length() > 2 && field.charAt(0) == '"' && field.charAt(field.length() - 1) == '"') {
1820 vars[2] = "c._" + field.substring(1, field.length() - 1);
1825 if (returnType[0] != 'V') {
1828 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1832 final String in = mi[0];
1834 final boolean direct = DirectlLibraries.isScriptLibrary(in);
1835 boolean skipThis = false;
1837 in.equals("org/apidesign/html/boot/spi/Fn") ||
1838 in.equals("org/netbeans/html/boot/spi/Fn")
1840 mcn = "java_lang_Class";
1841 } else if (direct) {
1842 if (in.endsWith("/Exports")) {
1846 } else if (mn.startsWith("cons_")) {
1851 mcn = mangleClassName(in);
1854 mcn = mangleClassName(in);
1857 String object = accessClassFalse(mcn);
1858 if (mn.startsWith("cons_")) {
1859 object += ".constructor";
1861 append(accessStaticMethod(object, mn, mi));
1869 if (numArguments > 0) {
1877 for (int j = 1; j < numArguments; ++j) {
1887 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1888 throws IOException {
1889 int methodIndex = readUShortArg(byteCodes, i);
1890 String[] mi = jc.getFieldInfoName(methodIndex);
1891 char[] returnType = { 'V' };
1892 StringBuilder cnt = new StringBuilder();
1893 String mn = findMethodName(mi, cnt, returnType);
1895 final int numArguments = cnt.length() + 1;
1896 final CharSequence[] vars = new CharSequence[numArguments];
1898 for (int j = numArguments - 1; j >= 0; --j) {
1899 vars[j] = mapper.popValue();
1902 if (returnType[0] != 'V') {
1905 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1910 if (DirectlLibraries.isScriptLibrary(mi[0])) {
1912 "$get__Ljava_lang_Object_2Ljava_lang_String_2".equals(mn) ||
1913 "$get__Ljava_lang_Object_2D".equals(mn)
1915 append(vars[0].toString())
1922 "$set__VLjava_lang_String_2Ljava_lang_Object_2".equals(mn) ||
1923 "$set__VDLjava_lang_Object_2".equals(mn)
1925 append(vars[0].toString())
1933 append(vars[0].toString()).append("['").append(mi[1]).append("'](");
1935 append(accessVirtualMethod(vars[0].toString(), mn, mi, numArguments));
1938 for (int j = 1; j < numArguments; ++j) {
1947 private void addReference(String cn) throws IOException {
1948 if (requireReference(cn)) {
1949 debug(" /* needs " + cn + " */");
1953 private void outType(String d, StringBuilder out) {
1955 while (d.charAt(0) == '[') {
1959 if (d.charAt(0) == 'L') {
1960 assert d.charAt(d.length() - 1) == ';';
1961 out.append(mangleClassName(d).substring(0, d.length() - 1));
1967 private String encodeConstant(int entryIndex) throws IOException {
1968 String[] classRef = { null };
1969 String s = jc.stringValue(entryIndex, classRef);
1970 if (classRef[0] != null) {
1971 if (classRef[0].startsWith("[")) {
1972 s = accessClass("java_lang_Class") + "(false)['forName__Ljava_lang_Class_2Ljava_lang_String_2']('" + classRef[0] + "')";
1974 addReference(classRef[0]);
1975 s = accessClassFalse(mangleClassName(s)) + ".constructor.$class";
1981 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1982 byte[] arr = m.findAnnotationData(true);
1986 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1987 final String htmlType = "Lnet/java/html/js/JavaScriptBody;";
1988 class P extends AnnotationParser {
1994 String[] args = new String[30];
2000 protected void visitAttr(String type, String attr, String at, String value) {
2001 if (type.equals(jvmType)) {
2002 if ("body".equals(attr)) {
2004 } else if ("args".equals(attr)) {
2005 args[cnt++] = value;
2007 throw new IllegalArgumentException(attr);
2010 if (type.equals(htmlType)) {
2012 if ("body".equals(attr)) {
2014 } else if ("args".equals(attr)) {
2015 args[cnt++] = value;
2016 } else if ("javacall".equals(attr)) {
2017 javacall = "1".equals(value);
2018 } else if ("wait4js".equals(attr)) {
2019 // ignore, we always invoke synchronously
2021 throw new IllegalArgumentException(attr);
2028 if (p.body == null) {
2031 StringBuilder cnt = new StringBuilder();
2032 final String mn = findMethodName(m, cnt);
2033 append("m = ").append(destObject).append(".").append(mn);
2034 append(" = function(");
2037 StringBuilder toValue = new StringBuilder();
2038 for (int i = 0; i < cnt.length(); i++) {
2040 space = outputArg(this, p.args, index);
2041 if (p.html4j && space.length() > 0) {
2042 toValue.append("\n ").append(p.args[index]).append(" = ")
2043 .append(accessClass("java_lang_Class")).append("(false).toJS(").
2044 append(p.args[index]).append(");");
2048 append(") {").append("\n");
2049 append(toValue.toString());
2051 int lastSlash = jc.getClassName().lastIndexOf('/');
2052 final String pkg = jc.getClassName().substring(0, lastSlash);
2053 append(mangleCallbacks(pkg, p.body));
2054 requireReference(pkg + "/$JsCallbacks$");
2062 private CharSequence mangleCallbacks(String pkgName, String body) {
2063 StringBuilder sb = new StringBuilder();
2066 int next = body.indexOf(".@", pos);
2068 sb.append(body.substring(pos));
2069 body = sb.toString();
2074 if (!Character.isJavaIdentifierPart(body.charAt(--ident))) {
2079 String refId = body.substring(ident, next);
2081 sb.append(body.substring(pos, ident));
2083 int sigBeg = body.indexOf('(', next);
2084 int sigEnd = body.indexOf(')', sigBeg);
2085 int colon4 = body.indexOf("::", next);
2086 if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
2087 throw new IllegalStateException("Malformed body " + body);
2089 String fqn = body.substring(next + 2, colon4);
2090 String method = body.substring(colon4 + 2, sigBeg);
2091 String params = body.substring(sigBeg, sigEnd + 1);
2093 int paramBeg = body.indexOf('(', sigEnd + 1);
2094 int paramEnd = closingParenthesis(body, paramBeg);
2096 sb.append(accessClass("java_lang_Class")).append("(false).toJS(");
2097 sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM().");
2098 sb.append(mangleJsCallbacks(fqn, method, params, false));
2099 sb.append("(").append(refId);
2100 if (body.charAt(paramBeg + 1) != ')') {
2103 sb.append(body.substring(paramBeg + 1, paramEnd));
2110 int next = body.indexOf("@", pos);
2115 sb.append(body.substring(pos));
2119 sb = new StringBuilder();
2122 sb.append(body.substring(pos, next));
2124 int sigBeg = body.indexOf('(', next);
2125 int sigEnd = body.indexOf(')', sigBeg);
2126 int colon4 = body.indexOf("::", next);
2127 if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
2128 throw new IllegalStateException("Malformed body " + body);
2130 String fqn = body.substring(next + 1, colon4);
2131 String method = body.substring(colon4 + 2, sigBeg);
2132 String params = body.substring(sigBeg, sigEnd + 1);
2134 int paramBeg = body.indexOf('(', sigEnd + 1);
2135 int paramEnd = closingParenthesis(body, paramBeg);
2137 sb.append(accessClass("java_lang_Class")).append("(false).toJS(");
2138 sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM().");
2139 sb.append(mangleJsCallbacks(fqn, method, params, true));
2141 sb.append(body.substring(paramBeg + 1, paramEnd));
2147 static String mangleJsCallbacks(String fqn, String method, String params, boolean isStatic) {
2148 if (params.startsWith("(")) {
2149 params = params.substring(1);
2151 if (params.endsWith(")")) {
2152 params = params.substring(0, params.length() - 1);
2154 StringBuilder sb = new StringBuilder();
2155 final String fqnu = fqn.replace('.', '_');
2156 final String rfqn = mangleClassName(fqnu);
2157 final String rm = mangleMethodName(method);
2160 StringBuilder pb = new StringBuilder();
2161 int len = params.length();
2163 while (indx < len) {
2164 char ch = params.charAt(indx);
2165 if (ch == '[' || ch == 'L') {
2166 int column = params.indexOf(';', indx) + 1;
2167 if (column > indx) {
2168 String real = params.substring(indx, column);
2169 if ("Ljava/lang/String;".equals(real)) {
2170 pb.append("Ljava/lang/String;");
2175 pb.append("Ljava/lang/Object;");
2182 srp = mangleSig(pb.toString());
2184 final String rp = mangleSig(params);
2185 final String mrp = mangleMethodName(rp);
2186 sb.append(rfqn).append("$").append(rm).
2187 append('$').append(mrp).append("__Ljava_lang_Object_2");
2189 sb.append('L').append(fqnu).append("_2");
2192 return sb.toString();
2195 private static String className(ClassData jc) {
2196 //return jc.getName().getInternalName().replace('/', '_');
2197 return mangleClassName(jc.getClassName());
2200 private static String[] findAnnotation(
2201 byte[] arr, ClassData cd, final String className,
2202 final String... attrNames
2203 ) throws IOException {
2207 final String[] values = new String[attrNames.length];
2208 final boolean[] found = { false };
2209 final String jvmType = "L" + className.replace('.', '/') + ";";
2210 AnnotationParser ap = new AnnotationParser(false, true) {
2212 protected void visitAttr(String type, String attr, String at, String value) {
2213 if (type.equals(jvmType)) {
2215 for (int i = 0; i < attrNames.length; i++) {
2216 if (attrNames[i].equals(attr)) {
2225 return found[0] ? values : null;
2228 private CharSequence initField(FieldData v) {
2229 final String is = v.getInternalSig();
2230 if (is.length() == 1) {
2231 switch (is.charAt(0)) {
2237 case 'I': return " = 0;";
2239 case 'D': return " = 0.0;";
2241 throw new IllegalStateException(is);
2247 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
2248 final String name = args[indx];
2252 if (name.contains(",")) {
2253 throw new IOException("Wrong parameter with ',': " + name);
2259 final void emitNoFlush(
2261 final String format, final CharSequence... params
2262 ) throws IOException {
2263 emitImpl(this, format, params);
2265 static final void emit(
2267 final Appendable out,
2268 final String format, final CharSequence... params
2269 ) throws IOException {
2271 emitImpl(out, format, params);
2273 static void emitImpl(final Appendable out,
2274 final String format,
2275 final CharSequence... params) throws IOException {
2276 final int length = format.length();
2279 int paramOffset = format.indexOf('@');
2280 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
2281 final char paramChar = format.charAt(paramOffset + 1);
2282 if ((paramChar >= '1') && (paramChar <= '9')) {
2283 final int paramIndex = paramChar - '0' - 1;
2285 out.append(format, processed, paramOffset);
2286 out.append(params[paramIndex]);
2289 processed = paramOffset + 1;
2292 paramOffset = format.indexOf('@', paramOffset + 1);
2295 out.append(format, processed, length);
2298 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
2299 append("} catch (e) {\n");
2301 for (TrapData e : traps) {
2305 if (e.catch_cpx != 0) { //not finally
2306 final String classInternalName = jc.getClassName(e.catch_cpx);
2307 addReference(classInternalName);
2308 append("e = vm.java_lang_Class(false).bck2BrwsrThrwrbl(e);");
2309 append("if (e['$instOf_" + mangleClassName(classInternalName) + "']) {");
2310 append("var stA0 = e;");
2311 goTo(this, current, e.handler_pc, topMostLabel);
2314 finallyPC = e.handler_pc;
2317 if (finallyPC == -1) {
2320 append("var stA0 = e;");
2321 goTo(this, current, finallyPC, topMostLabel);
2326 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
2329 out.append("{ gt = 0; continue X_" + to + "; }");
2331 out.append("{ gt = " + to + "; continue X_0; }");
2334 out.append("{ gt = " + to + "; break IF; }");
2338 private static void emitIf(
2340 Appendable out, String pattern,
2342 int current, int to, int canBack
2343 ) throws IOException {
2345 emitImpl(out, pattern, param);
2346 goTo(out, current, to, canBack);
2349 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
2352 case 4: jvmType = "[Z"; break;
2353 case 5: jvmType = "[C"; break;
2354 case 6: jvmType = "[F"; break;
2355 case 7: jvmType = "[D"; break;
2356 case 8: jvmType = "[B"; break;
2357 case 9: jvmType = "[S"; break;
2358 case 10: jvmType = "[I"; break;
2359 case 11: jvmType = "[J"; break;
2360 default: throw new IllegalStateException("Array type: " + atype);
2363 "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](true, '@3', null, @1);",
2364 smapper.popI(), smapper.pushA(), jvmType);
2367 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
2368 String typeName = jc.getClassName(type);
2369 String ref = "null";
2370 if (typeName.startsWith("[")) {
2371 typeName = "'[" + typeName + "'";
2373 ref = "vm." + mangleClassName(typeName);
2374 typeName = "'[L" + typeName + ";'";
2377 "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](false, @3, @4, @1);",
2378 smapper.popI(), smapper.pushA(), typeName, ref);
2381 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
2382 String typeName = jc.getClassName(type);
2383 int dim = readUByte(byteCodes, ++i);
2384 StringBuilder dims = new StringBuilder();
2386 for (int d = 0; d < dim; d++) {
2388 dims.insert(1, ",");
2390 dims.insert(1, smapper.popI());
2394 if (typeName.charAt(dim) == 'L') {
2395 fn = "vm." + mangleClassName(typeName.substring(dim + 1, typeName.length() - 1));
2398 "var @2 = Array.prototype['multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3ILjava_lang_Object_2']('@3', @1, @4);",
2399 dims.toString(), smapper.pushA(), typeName, fn
2404 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
2405 int table = i / 4 * 4 + 4;
2406 int dflt = i + readInt4(byteCodes, table);
2408 int low = readInt4(byteCodes, table);
2410 int high = readInt4(byteCodes, table);
2412 final CharSequence swVar = smapper.popValue();
2413 smapper.flush(this);
2414 append("switch (").append(swVar).append(") {\n");
2415 while (low <= high) {
2416 int offset = i + readInt4(byteCodes, table);
2418 append(" case " + low).append(":"); goTo(this, i, offset, topMostLabel); append('\n');
2421 append(" default: ");
2422 goTo(this, i, dflt, topMostLabel);
2428 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
2429 int table = i / 4 * 4 + 4;
2430 int dflt = i + readInt4(byteCodes, table);
2432 int n = readInt4(byteCodes, table);
2434 final CharSequence swVar = smapper.popValue();
2435 smapper.flush(this);
2436 append("switch (").append(swVar).append(") {\n");
2438 int cnstnt = readInt4(byteCodes, table);
2440 int offset = i + readInt4(byteCodes, table);
2442 append(" case " + cnstnt).append(": "); goTo(this, i, offset, topMostLabel); append('\n');
2444 append(" default: ");
2445 goTo(this, i, dflt, topMostLabel);
2451 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
2452 String type = jc.getClassName(indx);
2453 if (DirectlLibraries.isScriptLibrary(type)) {
2455 "var @2 = @1 !== null && @1 !== undefined;",
2456 smapper.popA(), smapper.pushI(),
2457 mangleClassName(type));
2460 if (!type.startsWith("[")) {
2462 "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
2463 smapper.popA(), smapper.pushI(),
2464 mangleClassName(type));
2467 while (type.charAt(cnt) == '[') {
2470 if (type.charAt(cnt) == 'L') {
2471 String component = type.substring(cnt + 1, type.length() - 1);
2472 requireReference(component);
2473 type = "vm." + mangleClassName(component);
2475 "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @4, @3);",
2476 smapper.popA(), smapper.pushI(),
2481 "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@3');",
2482 smapper.popA(), smapper.pushI(), type
2488 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
2489 String type = jc.getClassName(indx);
2490 if (DirectlLibraries.isScriptLibrary(type)) {
2491 emitNoFlush(smapper,
2493 smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type), type.replace('/', '.'));
2496 if (!type.startsWith("[")) {
2497 emitNoFlush(smapper,
2498 "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx(@1, '@3');",
2499 smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type), type.replace('/', '.'));
2502 while (type.charAt(cnt) == '[') {
2505 if (type.charAt(cnt) == 'L') {
2506 String component = type.substring(cnt + 1, type.length() - 1);
2507 requireReference(component);
2508 type = "vm." + mangleClassName(component);
2509 emitNoFlush(smapper,
2510 "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @3, @2)) vm.java_lang_Class(false).castEx(@1, '');",
2511 smapper.getT(0, VarType.REFERENCE, false), type, "" + cnt
2514 emitNoFlush(smapper,
2515 "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@2')) vm.java_lang_Class(false).castEx(@1, '');",
2516 smapper.getT(0, VarType.REFERENCE, false), type
2522 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
2523 for (int j = prev; j <= i; j++) {
2525 final int cc = readUByte(byteCodes, j);
2526 append(Integer.toString(cc));
2530 @JavaScriptBody(args = "msg", body = "")
2531 private static void println(String msg) {
2532 System.err.println(msg);
2535 private static int closingParenthesis(String body, int at) {
2538 switch (body.charAt(at++)) {
2539 case '(': cnt++; break;
2540 case ')': cnt--; break;
2548 private class GenerateAnno extends AnnotationParser {
2549 public GenerateAnno(boolean textual, boolean iterateArray) {
2550 super(textual, iterateArray);
2552 int[] cnt = new int[32];
2556 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
2557 final String slashType = attrType.substring(1, attrType.length() - 1);
2558 requireReference(slashType);
2560 if (cnt[depth]++ > 0) {
2564 append('"').append(attrType).append("\" : ");
2571 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
2577 protected void visitValueStart(String attrName, char type) throws IOException {
2578 if (cnt[depth]++ > 0) {
2582 if (attrName != null) {
2583 append('"').append(attrName).append("\" : ");
2591 protected void visitValueEnd(String attrName, char type) throws IOException {
2599 protected void visitAttr(String type, String attr, String attrType, String value)
2600 throws IOException {
2601 if (attr == null && value == null) {
2608 protected void visitEnumAttr(String type, String attr, String attrType, String value)
2609 throws IOException {
2610 final String slashType = attrType.substring(1, attrType.length() - 1);
2611 requireReference(slashType);
2613 final String cn = mangleClassName(slashType);
2614 append(accessClassFalse(cn))
2615 .append("['valueOf__L").
2617 append("_2Ljava_lang_String_2']('").
2623 protected void visitClassAttr(String annoType, String attr, String className) throws IOException {
2624 final String slashType = className.substring(1, className.length() - 1);
2625 requireReference(slashType);
2627 final String cn = mangleClassName(slashType);
2628 append(accessClassFalse(cn)).append(".constructor.$class");