rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 13 Sep 2014 18:14:55 +0200
branchjdk8
changeset 1686 63ca617c8101
parent 1673 2d3d0b72af04
child 1688 e709c530c227
permissions -rw-r--r--
Only define defender methods if they are not already defined
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    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 
    25 /** Translator of the code inside class files to JavaScript.
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 abstract class ByteCodeToJavaScript implements Appendable {
    30     private ClassData jc;
    31     private final Appendable out;
    32     private boolean outChanged;
    33     private boolean callbacks;
    34 
    35     protected ByteCodeToJavaScript(Appendable out) {
    36         this.out = out;
    37     }
    38     
    39     @Override
    40     public final Appendable append(CharSequence csq) throws IOException {
    41         out.append(csq);
    42         outChanged = true;
    43         return this;
    44     }
    45 
    46     @Override
    47     public final Appendable append(CharSequence csq, int start, int end) throws IOException {
    48         out.append(csq, start, end);
    49         outChanged = true;
    50         return this;
    51     }
    52 
    53     @Override
    54     public final Appendable append(char c) throws IOException {
    55         out.append(c);
    56         outChanged = true;
    57         return this;
    58     }
    59     
    60     /* Collects additional required resources.
    61      * 
    62      * @param internalClassName classes that were referenced and should be loaded in order the
    63      *   generated JavaScript code works properly. The names are in internal 
    64      *   JVM form so String is <code>java/lang/String</code>. 
    65      */
    66     protected abstract boolean requireReference(String internalClassName);
    67     
    68     /*
    69      * @param resourcePath name of resources to read
    70      */
    71     protected abstract void requireScript(String resourcePath) throws IOException;
    72     
    73     /** Allows subclasses to redefine what field a function representing a
    74      * class gets assigned. By default it returns the suggested name followed
    75      * by <code>" = "</code>;
    76      * 
    77      * @param className suggested name of the class
    78      */
    79     /* protected */ String assignClass(String className) {
    80         return className + " = ";
    81     }
    82     /* protected */ String accessClass(String classOperation) {
    83         return classOperation;
    84     }
    85 
    86     protected String accessField(String object, String mangledName,
    87                                  String[] fieldInfoName) throws IOException {
    88         return object + "." + mangledName;
    89     }
    90 
    91     protected String accessStaticMethod(
    92                              String object,
    93                              String mangledName,
    94                              String[] fieldInfoName) throws IOException {
    95         return object + "." + mangledName;
    96     }
    97 
    98     protected String accessVirtualMethod(
    99             String object, 
   100             String mangledName, 
   101             String[] fieldInfoName, 
   102             int params
   103     ) throws IOException {
   104         return object + "." + mangledName + '(';
   105     }
   106 
   107     protected void declaredClass(ClassData classData, String mangledName)
   108             throws IOException {
   109     }
   110 
   111     protected void declaredField(FieldData fieldData,
   112                                  String destObject,
   113                                  String mangledName) throws IOException {
   114     }
   115 
   116     protected void declaredMethod(MethodData methodData,
   117                                   String destObject,
   118                                   String mangledName) throws IOException {
   119     }
   120 
   121     /** Prints out a debug message. 
   122      * 
   123      * @param msg the message
   124      * @return true if the message has been printed
   125      * @throws IOException 
   126      */
   127     boolean debug(String msg) throws IOException {
   128         append(msg);
   129         return true;
   130     }
   131 
   132     /**
   133      * Converts a given class file to a JavaScript version.
   134      *
   135      * @param classFile input stream with code of the .class file
   136      * @return the initialization code for this class, if any. Otherwise <code>null</code>
   137      * 
   138      * @throws IOException if something goes wrong during read or write or translating
   139      */
   140     
   141     public String compile(InputStream classFile) throws IOException {
   142         return compile(new ClassData(classFile));
   143     }
   144 
   145     protected String compile(ClassData classData) throws IOException {
   146         this.jc = classData;
   147         this.callbacks = this.jc.getClassName().endsWith("/$JsCallbacks$");
   148         if (jc.getMajor_version() < 50) {
   149             throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
   150                 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
   151             );
   152         }
   153         byte[] arrData = jc.findAnnotationData(true);
   154         {
   155             String[] arr = findAnnotation(arrData, jc, 
   156                 "org.apidesign.bck2brwsr.core.ExtraJavaScript", 
   157                 "resource", "processByteCode"
   158             );
   159             if (arr != null) {
   160                 if (!arr[0].isEmpty()) {
   161                     requireScript(arr[0]);
   162                 }
   163                 if ("0".equals(arr[1])) {
   164                     return null;
   165                 }
   166             }
   167         }
   168         {
   169             String[] arr = findAnnotation(arrData, jc, 
   170                 "net.java.html.js.JavaScriptResource", 
   171                 "value"
   172             );
   173             if (arr != null) {
   174                 if (arr[0].startsWith("/")) {
   175                     requireScript(arr[0]);
   176                 } else {
   177                     int last = jc.getClassName().lastIndexOf('/');
   178                     requireScript(
   179                         jc.getClassName().substring(0, last + 1).replace('.', '/') + arr[0]
   180                     );
   181                 }
   182             }
   183         }
   184         String[] proto = findAnnotation(arrData, jc, 
   185             "org.apidesign.bck2brwsr.core.JavaScriptPrototype", 
   186             "container", "prototype"
   187         );
   188         StringArray toInitilize = new StringArray();
   189         final String className = className(jc);
   190         append("\n\n").append(assignClass(className));
   191         append("function ").append(className).append("() {");
   192         append("\n  var CLS = ").append(className).append(';');
   193         append("\n  if (!CLS.$class) {");
   194         if (proto == null) {
   195             String sc = jc.getSuperClassName(); // with _
   196             append("\n    var pp = ").
   197                 append(accessClass(mangleClassName(sc))).append("(true);");
   198             append("\n    var p = CLS.prototype = pp;");
   199             append("\n    var c = p;");
   200             append("\n    var sprcls = pp.constructor.$class;");
   201         } else {
   202             append("\n    var p = CLS.prototype = ").append(proto[1]).append(";");
   203             if (proto[0] == null) {
   204                 proto[0] = "p";
   205             }
   206             append("\n    var c = ").append(proto[0]).append(";");
   207             append("\n    var sprcls = null;");
   208         }
   209         for (FieldData v : jc.getFields()) {
   210             if (v.isStatic()) {
   211                 if ((v.access & ACC_FINAL) != 0 && v.hasConstantValue()) {
   212                     if (v.getInternalSig().length() == 1 || v.getInternalSig().equals("Ljava/lang/String;")) {
   213                         continue;
   214                     }
   215                 }
   216                 append("\n  CLS.fld_").append(v.getName()).append(initField(v));
   217                 append("\n  c._").append(v.getName()).append(" = function (v) {")
   218                    .append("  if (arguments.length == 1) CLS.fld_").append(v.getName())
   219                    .append(" = v; return CLS.fld_").
   220                     append(v.getName()).append("; };");
   221             } else {
   222                 append("\n  c._").append(v.getName()).append(" = function (v) {")
   223                    .append("  if (arguments.length == 1) this.fld_").
   224                     append(className).append('_').append(v.getName())
   225                    .append(" = v; return this.fld_").
   226                     append(className).append('_').append(v.getName())
   227                    .append("; };");
   228             }
   229 
   230             declaredField(v, "c", "_" + v.getName());
   231         }
   232         for (MethodData m : jc.getMethods()) {
   233             byte[] onlyArr = m.findAnnotationData(true);
   234             if (javaScriptOnly(onlyArr)) continue;
   235             String destObject;
   236             String mn;
   237             append("\n    ");
   238             if (m.isStatic()) {
   239                 destObject = "c";
   240                 mn = generateStaticMethod(destObject, m, toInitilize);
   241             } else {
   242                 if (m.isConstructor()) {
   243                     destObject = "CLS";
   244                     mn = generateInstanceMethod(destObject, m);
   245                 } else {
   246                     destObject = "c";
   247                     mn = generateInstanceMethod(destObject, m);
   248                 }
   249             }
   250             declaredMethod(m, destObject, mn);
   251             byte[] runAnno = m.findAnnotationData(false);
   252             if (runAnno != null) {
   253                 append("\n    ").append(destObject).append(".").append(mn).append(".anno = {");
   254                 generateAnno(jc, runAnno);
   255                 append("\n    };");
   256             }
   257             append("\n    ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
   258             append("\n    ").append(destObject).append(".").append(mn).append(".cls = CLS;");
   259         }
   260         append("\n    c.constructor = CLS;");
   261         append("\n    function ").append(className).append("fillInstOf(x) {");
   262         String instOfName = "$instOf_" + className;
   263         append("\n        Object.defineProperty(x, '").append(instOfName).append("', { value : true });");
   264         if (jc.isInterface()) {
   265             for (MethodData m : jc.getMethods()) {
   266                 if ((m.getAccess() & ACC_ABSTRACT) == 0
   267                         && (m.getAccess() & ACC_STATIC) == 0
   268                         && (m.getAccess() & ACC_PRIVATE) == 0) {
   269                     final String mn = findMethodName(m, new StringBuilder());
   270                     append("\n        try {");
   271                     append("\n          if (!x['").append(mn).append("']) Object.defineProperty(x, '").append(mn).append("', { value : c['").append(mn).append("']});");
   272                     append("\n        } catch (ignore) {");
   273                     append("\n        }");
   274                 }
   275             }
   276         }
   277         for (String superInterface : jc.getSuperInterfaces()) {
   278             String intrfc = superInterface.replace('/', '_');
   279             append("\n      vm.").append(intrfc).append("(false)['fillInstOf'](x);");
   280             requireReference(superInterface);
   281         }
   282         append("\n    }");
   283         append("\n    try {");
   284         append("\n      Object.defineProperty(c, 'fillInstOf', { value: ").append(className).append("fillInstOf });");
   285         append("\n      ").append(className).append("fillInstOf(c);");
   286         append("\n    } catch (ignore) {");
   287         append("\n    }");
   288 //        obfuscationDelegate.exportJSProperty(this, "c", instOfName);
   289         append("\n    CLS.$class = 'temp';");
   290         append("\n    CLS.$class = ");
   291         append(accessClass("java_lang_Class(true);"));
   292         append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
   293         append("\n    CLS.$class.superclass = sprcls;");
   294         append("\n    CLS.$class.access = ").append(jc.getAccessFlags()+";");
   295         append("\n    CLS.$class.cnstr = CLS;");
   296         byte[] classAnno = jc.findAnnotationData(false);
   297         if (classAnno != null) {
   298             append("\n    CLS.$class.anno = {");
   299             generateAnno(jc, classAnno);
   300             append("\n    };");
   301         }
   302         for (String init : toInitilize.toArray()) {
   303             append("\n    ").append(init).append("();");
   304         }
   305         append("\n  }");
   306         append("\n  if (arguments.length === 0) {");
   307         append("\n    if (!(this instanceof CLS)) {");
   308         append("\n      return new CLS();");
   309         append("\n    }");
   310         for (FieldData v : jc.getFields()) {
   311             byte[] onlyArr = v.findAnnotationData(true);
   312             if (javaScriptOnly(onlyArr)) continue;
   313             if (!v.isStatic()) {
   314                 append("\n    this.fld_").
   315                     append(className).append('_').
   316                     append(v.getName()).append(initField(v));
   317             }
   318         }
   319         append("\n    return this;");
   320         append("\n  }");
   321         append("\n  return arguments[0] ? new CLS() : CLS.prototype;");
   322         append("\n};");
   323 
   324         declaredClass(jc, className);
   325 
   326 //        StringBuilder sb = new StringBuilder();
   327 //        for (String init : toInitilize.toArray()) {
   328 //            sb.append("\n").append(init).append("();");
   329 //        }
   330         return "";
   331     }
   332 
   333     private boolean javaScriptOnly(byte[] anno) throws IOException {
   334         String[] only = findAnnotation(anno, jc,
   335             "org.apidesign.bck2brwsr.core.JavaScriptOnly",
   336             "name", "value"
   337         );
   338         if (only != null) {
   339             if (only[0] != null && only[1] != null) {
   340                 append("\n    p.").append(only[0]).append(" = ")
   341                     .append(only[1]).append(";");
   342             }
   343             if (ExportedSymbols.isMarkedAsExported(anno, jc)) {
   344                 append("\n    p['").append(only[0]).append("'] = p.")
   345                     .append(only[0]).append(";");
   346             }
   347             return true;
   348         }
   349         return false;
   350     }
   351     private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
   352         String jsb = javaScriptBody(destObject, m, true);
   353         if (jsb != null) {
   354             return jsb;
   355         }
   356         final String mn = findMethodName(m, new StringBuilder());
   357         if (mn.equals("class__V")) {
   358             toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
   359         }
   360         generateMethod(destObject, mn, m);
   361         return mn;
   362     }
   363 
   364     private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
   365         String jsb = javaScriptBody(destObject, m, false);
   366         if (jsb != null) {
   367             return jsb;
   368         }
   369         final String mn = findMethodName(m, new StringBuilder());
   370         generateMethod(destObject, mn, m);
   371         return mn;
   372     }
   373 
   374     private void generateMethod(String destObject, String name, MethodData m)
   375             throws IOException {
   376         final StackMapIterator stackMapIterator = m.createStackMapIterator();
   377         TrapDataIterator trap = m.getTrapDataIterator();
   378         final LocalsMapper lmapper =
   379                 new LocalsMapper(stackMapIterator.getArguments());
   380 
   381         boolean obj = "java/lang/Object".equals(jc.getClassName());
   382         
   383         if (obj) {
   384             append("Object.defineProperty(").append(destObject).
   385             append(", '").append(name).append("', { configurable: true, writable: true, value: function(");
   386         } else {
   387             append(destObject).append(".").append(name).append(" = function(");
   388         }
   389         lmapper.outputArguments(this, m.isStatic());
   390         append(") {").append("\n");
   391 
   392         final byte[] byteCodes = m.getCode();
   393         if (byteCodes == null) {
   394             append("  throw 'no code found for ")
   395                .append(jc.getClassName()).append('.')
   396                .append(m.getName()).append("';\n");
   397             if (obj) {
   398                 append("}");
   399             }
   400             append("};");
   401             return;
   402         }
   403 
   404         final StackMapper smapper = new StackMapper();
   405 
   406         if (!m.isStatic()) {
   407             append("  var ").append(" lcA0 = this;\n");
   408         }
   409 
   410         int lastStackFrame;
   411         TrapData[] previousTrap = null;
   412         boolean wide = false;
   413         boolean didBranches;
   414         if (stackMapIterator.isEmpty()) {
   415             didBranches = false;
   416             lastStackFrame = 0;
   417         } else {
   418             didBranches = true;
   419             lastStackFrame = -1;
   420             append("\n  var gt = 0;\n");
   421         }
   422         
   423         int openBraces = 0;
   424         int topMostLabel = 0;
   425         for (int i = 0; i < byteCodes.length; i++) {
   426             int prev = i;
   427             outChanged = false;
   428             stackMapIterator.advanceTo(i);
   429             boolean changeInCatch = trap.advanceTo(i);
   430             if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
   431                 if (previousTrap != null) {
   432                     generateCatch(previousTrap, i, topMostLabel);
   433                     previousTrap = null;
   434                 }
   435             }
   436             if (lastStackFrame != stackMapIterator.getFrameIndex()) {
   437                 smapper.flush(this);
   438                 if (i != 0) {
   439                     append("    }\n");
   440                 }
   441                 if (openBraces > 64) {
   442                     for (int c = 0; c < 64; c++) {
   443                         append("break;}\n");
   444                     }
   445                     openBraces = 1;
   446                     topMostLabel = i;
   447                 }
   448                 
   449                 lastStackFrame = stackMapIterator.getFrameIndex();
   450                 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
   451                 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
   452                 append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
   453                 openBraces++;
   454                 changeInCatch = true;
   455             } else {
   456                 debug("    /* " + i + " */ ");
   457             }
   458             if (changeInCatch && trap.useTry()) {
   459                 append("try {");
   460                 previousTrap = trap.current();
   461             }
   462             final int c = readUByte(byteCodes, i);
   463             switch (c) {
   464                 case opc_aload_0:
   465                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(0));
   466                     break;
   467                 case opc_iload_0:
   468                     smapper.assign(this, VarType.INTEGER, lmapper.getI(0));
   469                     break;
   470                 case opc_lload_0:
   471                     smapper.assign(this, VarType.LONG, lmapper.getL(0));
   472                     break;
   473                 case opc_fload_0:
   474                     smapper.assign(this, VarType.FLOAT, lmapper.getF(0));
   475                     break;
   476                 case opc_dload_0:
   477                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(0));
   478                     break;
   479                 case opc_aload_1:
   480                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(1));
   481                     break;
   482                 case opc_iload_1:
   483                     smapper.assign(this, VarType.INTEGER, lmapper.getI(1));
   484                     break;
   485                 case opc_lload_1:
   486                     smapper.assign(this, VarType.LONG, lmapper.getL(1));
   487                     break;
   488                 case opc_fload_1:
   489                     smapper.assign(this, VarType.FLOAT, lmapper.getF(1));
   490                     break;
   491                 case opc_dload_1:
   492                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(1));
   493                     break;
   494                 case opc_aload_2:
   495                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(2));
   496                     break;
   497                 case opc_iload_2:
   498                     smapper.assign(this, VarType.INTEGER, lmapper.getI(2));
   499                     break;
   500                 case opc_lload_2:
   501                     smapper.assign(this, VarType.LONG, lmapper.getL(2));
   502                     break;
   503                 case opc_fload_2:
   504                     smapper.assign(this, VarType.FLOAT, lmapper.getF(2));
   505                     break;
   506                 case opc_dload_2:
   507                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(2));
   508                     break;
   509                 case opc_aload_3:
   510                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(3));
   511                     break;
   512                 case opc_iload_3:
   513                     smapper.assign(this, VarType.INTEGER, lmapper.getI(3));
   514                     break;
   515                 case opc_lload_3:
   516                     smapper.assign(this, VarType.LONG, lmapper.getL(3));
   517                     break;
   518                 case opc_fload_3:
   519                     smapper.assign(this, VarType.FLOAT, lmapper.getF(3));
   520                     break;
   521                 case opc_dload_3:
   522                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(3));
   523                     break;
   524                 case opc_iload: {
   525                     ++i;
   526                     final int indx = wide ? readUShort(byteCodes, i++)
   527                                           : readUByte(byteCodes, i);
   528                     wide = false;
   529                     smapper.assign(this, VarType.INTEGER, lmapper.getI(indx));
   530                     break;
   531                 }
   532                 case opc_lload: {
   533                     ++i;
   534                     final int indx = wide ? readUShort(byteCodes, i++)
   535                                           : readUByte(byteCodes, i);
   536                     wide = false;
   537                     smapper.assign(this, VarType.LONG, lmapper.getL(indx));
   538                     break;
   539                 }
   540                 case opc_fload: {
   541                     ++i;
   542                     final int indx = wide ? readUShort(byteCodes, i++)
   543                                           : readUByte(byteCodes, i);
   544                     wide = false;
   545                     smapper.assign(this, VarType.FLOAT, lmapper.getF(indx));
   546                     break;
   547                 }
   548                 case opc_dload: {
   549                     ++i;
   550                     final int indx = wide ? readUShort(byteCodes, i++)
   551                                           : readUByte(byteCodes, i);
   552                     wide = false;
   553                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(indx));
   554                     break;
   555                 }
   556                 case opc_aload: {
   557                     ++i;
   558                     final int indx = wide ? readUShort(byteCodes, i++)
   559                                           : readUByte(byteCodes, i);
   560                     wide = false;
   561                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(indx));
   562                     break;
   563                 }
   564                 case opc_istore: {
   565                     ++i;
   566                     final int indx = wide ? readUShort(byteCodes, i++)
   567                                           : readUByte(byteCodes, i);
   568                     wide = false;
   569                     emit(smapper, this, "var @1 = @2;",
   570                          lmapper.setI(indx), smapper.popI());
   571                     break;
   572                 }
   573                 case opc_lstore: {
   574                     ++i;
   575                     final int indx = wide ? readUShort(byteCodes, i++)
   576                                           : readUByte(byteCodes, i);
   577                     wide = false;
   578                     emit(smapper, this, "var @1 = @2;",
   579                          lmapper.setL(indx), smapper.popL());
   580                     break;
   581                 }
   582                 case opc_fstore: {
   583                     ++i;
   584                     final int indx = wide ? readUShort(byteCodes, i++)
   585                                           : readUByte(byteCodes, i);
   586                     wide = false;
   587                     emit(smapper, this, "var @1 = @2;",
   588                          lmapper.setF(indx), smapper.popF());
   589                     break;
   590                 }
   591                 case opc_dstore: {
   592                     ++i;
   593                     final int indx = wide ? readUShort(byteCodes, i++)
   594                                           : readUByte(byteCodes, i);
   595                     wide = false;
   596                     emit(smapper, this, "var @1 = @2;",
   597                          lmapper.setD(indx), smapper.popD());
   598                     break;
   599                 }
   600                 case opc_astore: {
   601                     ++i;
   602                     final int indx = wide ? readUShort(byteCodes, i++)
   603                                           : readUByte(byteCodes, i);
   604                     wide = false;
   605                     emit(smapper, this, "var @1 = @2;",
   606                          lmapper.setA(indx), smapper.popA());
   607                     break;
   608                 }
   609                 case opc_astore_0:
   610                     emit(smapper, this, "var @1 = @2;", lmapper.setA(0), smapper.popA());
   611                     break;
   612                 case opc_istore_0:
   613                     emit(smapper, this, "var @1 = @2;", lmapper.setI(0), smapper.popI());
   614                     break;
   615                 case opc_lstore_0:
   616                     emit(smapper, this, "var @1 = @2;", lmapper.setL(0), smapper.popL());
   617                     break;
   618                 case opc_fstore_0:
   619                     emit(smapper, this, "var @1 = @2;", lmapper.setF(0), smapper.popF());
   620                     break;
   621                 case opc_dstore_0:
   622                     emit(smapper, this, "var @1 = @2;", lmapper.setD(0), smapper.popD());
   623                     break;
   624                 case opc_astore_1:
   625                     emit(smapper, this, "var @1 = @2;", lmapper.setA(1), smapper.popA());
   626                     break;
   627                 case opc_istore_1:
   628                     emit(smapper, this, "var @1 = @2;", lmapper.setI(1), smapper.popI());
   629                     break;
   630                 case opc_lstore_1:
   631                     emit(smapper, this, "var @1 = @2;", lmapper.setL(1), smapper.popL());
   632                     break;
   633                 case opc_fstore_1:
   634                     emit(smapper, this, "var @1 = @2;", lmapper.setF(1), smapper.popF());
   635                     break;
   636                 case opc_dstore_1:
   637                     emit(smapper, this, "var @1 = @2;", lmapper.setD(1), smapper.popD());
   638                     break;
   639                 case opc_astore_2:
   640                     emit(smapper, this, "var @1 = @2;", lmapper.setA(2), smapper.popA());
   641                     break;
   642                 case opc_istore_2:
   643                     emit(smapper, this, "var @1 = @2;", lmapper.setI(2), smapper.popI());
   644                     break;
   645                 case opc_lstore_2:
   646                     emit(smapper, this, "var @1 = @2;", lmapper.setL(2), smapper.popL());
   647                     break;
   648                 case opc_fstore_2:
   649                     emit(smapper, this, "var @1 = @2;", lmapper.setF(2), smapper.popF());
   650                     break;
   651                 case opc_dstore_2:
   652                     emit(smapper, this, "var @1 = @2;", lmapper.setD(2), smapper.popD());
   653                     break;
   654                 case opc_astore_3:
   655                     emit(smapper, this, "var @1 = @2;", lmapper.setA(3), smapper.popA());
   656                     break;
   657                 case opc_istore_3:
   658                     emit(smapper, this, "var @1 = @2;", lmapper.setI(3), smapper.popI());
   659                     break;
   660                 case opc_lstore_3:
   661                     emit(smapper, this, "var @1 = @2;", lmapper.setL(3), smapper.popL());
   662                     break;
   663                 case opc_fstore_3:
   664                     emit(smapper, this, "var @1 = @2;", lmapper.setF(3), smapper.popF());
   665                     break;
   666                 case opc_dstore_3:
   667                     emit(smapper, this, "var @1 = @2;", lmapper.setD(3), smapper.popD());
   668                     break;
   669                 case opc_iadd:
   670                     smapper.replace(this, VarType.INTEGER, "(@1).add32(@2)", smapper.getI(1), smapper.popI());
   671                     break;
   672                 case opc_ladd:
   673                     smapper.replace(this, VarType.LONG, "(@1).add64(@2)", smapper.getL(1), smapper.popL());
   674                     break;
   675                 case opc_fadd:
   676                     smapper.replace(this, VarType.FLOAT, "(@1 + @2)", smapper.getF(1), smapper.popF());
   677                     break;
   678                 case opc_dadd:
   679                     smapper.replace(this, VarType.DOUBLE, "(@1 + @2)", smapper.getD(1), smapper.popD());
   680                     break;
   681                 case opc_isub:
   682                     smapper.replace(this, VarType.INTEGER, "(@1).sub32(@2)", smapper.getI(1), smapper.popI());
   683                     break;
   684                 case opc_lsub:
   685                     smapper.replace(this, VarType.LONG, "(@1).sub64(@2)", smapper.getL(1), smapper.popL());
   686                     break;
   687                 case opc_fsub:
   688                     smapper.replace(this, VarType.FLOAT, "(@1 - @2)", smapper.getF(1), smapper.popF());
   689                     break;
   690                 case opc_dsub:
   691                     smapper.replace(this, VarType.DOUBLE, "(@1 - @2)", smapper.getD(1), smapper.popD());
   692                     break;
   693                 case opc_imul:
   694                     smapper.replace(this, VarType.INTEGER, "(@1).mul32(@2)", smapper.getI(1), smapper.popI());
   695                     break;
   696                 case opc_lmul:
   697                     smapper.replace(this, VarType.LONG, "(@1).mul64(@2)", smapper.getL(1), smapper.popL());
   698                     break;
   699                 case opc_fmul:
   700                     smapper.replace(this, VarType.FLOAT, "(@1 * @2)", smapper.getF(1), smapper.popF());
   701                     break;
   702                 case opc_dmul:
   703                     smapper.replace(this, VarType.DOUBLE, "(@1 * @2)", smapper.getD(1), smapper.popD());
   704                     break;
   705                 case opc_idiv:
   706                     smapper.replace(this, VarType.INTEGER, "(@1).div32(@2)",
   707                          smapper.getI(1), smapper.popI());
   708                     break;
   709                 case opc_ldiv:
   710                     smapper.replace(this, VarType.LONG, "(@1).div64(@2)",
   711                          smapper.getL(1), smapper.popL());
   712                     break;
   713                 case opc_fdiv:
   714                     smapper.replace(this, VarType.FLOAT, "(@1 / @2)", smapper.getF(1), smapper.popF());
   715                     break;
   716                 case opc_ddiv:
   717                     smapper.replace(this, VarType.DOUBLE, "(@1 / @2)", smapper.getD(1), smapper.popD());
   718                     break;
   719                 case opc_irem:
   720                     smapper.replace(this, VarType.INTEGER, "(@1).mod32(@2)",
   721                          smapper.getI(1), smapper.popI());
   722                     break;
   723                 case opc_lrem:
   724                     smapper.replace(this, VarType.LONG, "(@1).mod64(@2)",
   725                          smapper.getL(1), smapper.popL());
   726                     break;
   727                 case opc_frem:
   728                     smapper.replace(this, VarType.FLOAT, "(@1 % @2)", smapper.getF(1), smapper.popF());
   729                     break;
   730                 case opc_drem:
   731                     smapper.replace(this, VarType.DOUBLE, "(@1 % @2)", smapper.getD(1), smapper.popD());
   732                     break;
   733                 case opc_iand:
   734                     smapper.replace(this, VarType.INTEGER, "(@1 & @2)", smapper.getI(1), smapper.popI());
   735                     break;
   736                 case opc_land:
   737                     smapper.replace(this, VarType.LONG, "(@1).and64(@2)", smapper.getL(1), smapper.popL());
   738                     break;
   739                 case opc_ior:
   740                     smapper.replace(this, VarType.INTEGER, "(@1 | @2)", smapper.getI(1), smapper.popI());
   741                     break;
   742                 case opc_lor:
   743                     smapper.replace(this, VarType.LONG, "(@1).or64(@2)", smapper.getL(1), smapper.popL());
   744                     break;
   745                 case opc_ixor:
   746                     smapper.replace(this, VarType.INTEGER, "(@1 ^ @2)", smapper.getI(1), smapper.popI());
   747                     break;
   748                 case opc_lxor:
   749                     smapper.replace(this, VarType.LONG, "(@1).xor64(@2)", smapper.getL(1), smapper.popL());
   750                     break;
   751                 case opc_ineg:
   752                     smapper.replace(this, VarType.INTEGER, "(@1).neg32()", smapper.getI(0));
   753                     break;
   754                 case opc_lneg:
   755                     smapper.replace(this, VarType.LONG, "(@1).neg64()", smapper.getL(0));
   756                     break;
   757                 case opc_fneg:
   758                     smapper.replace(this, VarType.FLOAT, "(-@1)", smapper.getF(0));
   759                     break;
   760                 case opc_dneg:
   761                     smapper.replace(this, VarType.DOUBLE, "(-@1)", smapper.getD(0));
   762                     break;
   763                 case opc_ishl:
   764                     smapper.replace(this, VarType.INTEGER, "(@1 << @2)", smapper.getI(1), smapper.popI());
   765                     break;
   766                 case opc_lshl:
   767                     smapper.replace(this, VarType.LONG, "(@1).shl64(@2)", smapper.getL(1), smapper.popI());
   768                     break;
   769                 case opc_ishr:
   770                     smapper.replace(this, VarType.INTEGER, "(@1 >> @2)", smapper.getI(1), smapper.popI());
   771                     break;
   772                 case opc_lshr:
   773                     smapper.replace(this, VarType.LONG, "(@1).shr64(@2)", smapper.getL(1), smapper.popI());
   774                     break;
   775                 case opc_iushr:
   776                     smapper.replace(this, VarType.INTEGER, "(@1 >>> @2)", smapper.getI(1), smapper.popI());
   777                     break;
   778                 case opc_lushr:
   779                     smapper.replace(this, VarType.LONG, "(@1).ushr64(@2)", smapper.getL(1), smapper.popI());
   780                     break;
   781                 case opc_iinc: {
   782                     ++i;
   783                     final int varIndx = wide ? readUShort(byteCodes, i++)
   784                                              : readUByte(byteCodes, i);
   785                     ++i;
   786                     final int incrBy = wide ? readShort(byteCodes, i++)
   787                                             : byteCodes[i];
   788                     wide = false;
   789                     if (incrBy == 1) {
   790                         emit(smapper, this, "@1++;", lmapper.getI(varIndx));
   791                     } else {
   792                         emit(smapper, this, "@1 += @2;",
   793                              lmapper.getI(varIndx),
   794                              Integer.toString(incrBy));
   795                     }
   796                     break;
   797                 }
   798                 case opc_return:
   799                     emit(smapper, this, "return;");
   800                     break;
   801                 case opc_ireturn:
   802                     emit(smapper, this, "return @1;", smapper.popI());
   803                     break;
   804                 case opc_lreturn:
   805                     emit(smapper, this, "return @1;", smapper.popL());
   806                     break;
   807                 case opc_freturn:
   808                     emit(smapper, this, "return @1;", smapper.popF());
   809                     break;
   810                 case opc_dreturn:
   811                     emit(smapper, this, "return @1;", smapper.popD());
   812                     break;
   813                 case opc_areturn:
   814                     emit(smapper, this, "return @1;", smapper.popA());
   815                     break;
   816                 case opc_i2l:
   817                     smapper.replace(this, VarType.LONG, "@1", smapper.getI(0));
   818                     break;
   819                 case opc_i2f:
   820                     smapper.replace(this, VarType.FLOAT, "@1", smapper.getI(0));
   821                     break;
   822                 case opc_i2d:
   823                     smapper.replace(this, VarType.DOUBLE, "@1", smapper.getI(0));
   824                     break;
   825                 case opc_l2i:
   826                     smapper.replace(this, VarType.INTEGER, "(@1).toInt32()", smapper.getL(0));
   827                     break;
   828                     // max int check?
   829                 case opc_l2f:
   830                     smapper.replace(this, VarType.FLOAT, "(@1).toFP()", smapper.getL(0));
   831                     break;
   832                 case opc_l2d:
   833                     smapper.replace(this, VarType.DOUBLE, "(@1).toFP()", smapper.getL(0));
   834                     break;
   835                 case opc_f2d:
   836                     smapper.replace(this, VarType.DOUBLE, "@1",
   837                          smapper.getF(0));
   838                     break;
   839                 case opc_d2f:
   840                     smapper.replace(this, VarType.FLOAT, "@1",
   841                          smapper.getD(0));
   842                     break;
   843                 case opc_f2i:
   844                     smapper.replace(this, VarType.INTEGER, "(@1).toInt32()",
   845                          smapper.getF(0));
   846                     break;
   847                 case opc_f2l:
   848                     smapper.replace(this, VarType.LONG, "(@1).toLong()",
   849                          smapper.getF(0));
   850                     break;
   851                 case opc_d2i:
   852                     smapper.replace(this, VarType.INTEGER, "(@1).toInt32()",
   853                          smapper.getD(0));
   854                     break;
   855                 case opc_d2l:
   856                     smapper.replace(this, VarType.LONG, "(@1).toLong()", smapper.getD(0));
   857                     break;
   858                 case opc_i2b:
   859                     smapper.replace(this, VarType.INTEGER, "(@1).toInt8()", smapper.getI(0));
   860                     break;
   861                 case opc_i2c:
   862                 case opc_i2s:
   863                     smapper.replace(this, VarType.INTEGER, "(@1).toInt16()", smapper.getI(0));
   864                     break;
   865                 case opc_aconst_null:
   866                     smapper.assign(this, VarType.REFERENCE, "null");
   867                     break;
   868                 case opc_iconst_m1:
   869                     smapper.assign(this, VarType.INTEGER, "-1");
   870                     break;
   871                 case opc_iconst_0:
   872                     smapper.assign(this, VarType.INTEGER, "0");
   873                     break;
   874                 case opc_dconst_0:
   875                     smapper.assign(this, VarType.DOUBLE, "0");
   876                     break;
   877                 case opc_lconst_0:
   878                     smapper.assign(this, VarType.LONG, "0");
   879                     break;
   880                 case opc_fconst_0:
   881                     smapper.assign(this, VarType.FLOAT, "0");
   882                     break;
   883                 case opc_iconst_1:
   884                     smapper.assign(this, VarType.INTEGER, "1");
   885                     break;
   886                 case opc_lconst_1:
   887                     smapper.assign(this, VarType.LONG, "1");
   888                     break;
   889                 case opc_fconst_1:
   890                     smapper.assign(this, VarType.FLOAT, "1");
   891                     break;
   892                 case opc_dconst_1:
   893                     smapper.assign(this, VarType.DOUBLE, "1");
   894                     break;
   895                 case opc_iconst_2:
   896                     smapper.assign(this, VarType.INTEGER, "2");
   897                     break;
   898                 case opc_fconst_2:
   899                     smapper.assign(this, VarType.FLOAT, "2");
   900                     break;
   901                 case opc_iconst_3:
   902                     smapper.assign(this, VarType.INTEGER, "3");
   903                     break;
   904                 case opc_iconst_4:
   905                     smapper.assign(this, VarType.INTEGER, "4");
   906                     break;
   907                 case opc_iconst_5:
   908                     smapper.assign(this, VarType.INTEGER, "5");
   909                     break;
   910                 case opc_ldc: {
   911                     int indx = readUByte(byteCodes, ++i);
   912                     String v = encodeConstant(indx);
   913                     int type = VarType.fromConstantType(jc.getTag(indx));
   914                     smapper.assign(this, type, v);
   915                     break;
   916                 }
   917                 case opc_ldc_w:
   918                 case opc_ldc2_w: {
   919                     int indx = readUShortArg(byteCodes, i);
   920                     i += 2;
   921                     String v = encodeConstant(indx);
   922                     int type = VarType.fromConstantType(jc.getTag(indx));
   923                     if (type == VarType.LONG) {
   924                         final Long lv = new Long(v);
   925                         final int low = (int)(lv.longValue() & 0xFFFFFFFF);
   926                         final int hi = (int)(lv.longValue() >> 32);
   927                         if (hi == 0) {
   928                             smapper.assign(this, VarType.LONG, "0x" + Integer.toHexString(low));
   929                         } else {
   930                             smapper.assign(this, VarType.LONG,
   931                                 "0x" + Integer.toHexString(hi) + ".next32(0x" + 
   932                                     Integer.toHexString(low) + ")"
   933                             );
   934                         }
   935                     } else {
   936                         smapper.assign(this, type, v);
   937                     }
   938                     break;
   939                 }
   940                 case opc_lcmp:
   941                     smapper.replace(this, VarType.INTEGER, "(@2).compare64(@1)", smapper.popL(), smapper.getL(0));
   942                     break;
   943                 case opc_fcmpl:
   944                 case opc_fcmpg:
   945                     smapper.replace(this, VarType.INTEGER, "(@2).compare(@1)", smapper.popF(), smapper.getF(0));
   946                     break;
   947                 case opc_dcmpl:
   948                 case opc_dcmpg:
   949                     smapper.replace(this, VarType.INTEGER, "(@2).compare(@1)", smapper.popD(), smapper.getD(0));
   950                     break;
   951                 case opc_if_acmpeq:
   952                     i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
   953                                    "===", topMostLabel);
   954                     break;
   955                 case opc_if_acmpne:
   956                     i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
   957                                    "!==", topMostLabel);
   958                     break;
   959                 case opc_if_icmpeq:
   960                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
   961                                    "==", topMostLabel);
   962                     break;
   963                 case opc_ifeq: {
   964                     int indx = i + readShortArg(byteCodes, i);
   965                     emitIf(smapper, this, "if ((@1) == 0) ",
   966                          smapper.popI(), i, indx, topMostLabel);
   967                     i += 2;
   968                     break;
   969                 }
   970                 case opc_ifne: {
   971                     int indx = i + readShortArg(byteCodes, i);
   972                     emitIf(smapper, this, "if ((@1) != 0) ",
   973                          smapper.popI(), i, indx, topMostLabel);
   974                     i += 2;
   975                     break;
   976                 }
   977                 case opc_iflt: {
   978                     int indx = i + readShortArg(byteCodes, i);
   979                     emitIf(smapper, this, "if ((@1) < 0) ",
   980                          smapper.popI(), i, indx, topMostLabel);
   981                     i += 2;
   982                     break;
   983                 }
   984                 case opc_ifle: {
   985                     int indx = i + readShortArg(byteCodes, i);
   986                     emitIf(smapper, this, "if ((@1) <= 0) ",
   987                          smapper.popI(), i, indx, topMostLabel);
   988                     i += 2;
   989                     break;
   990                 }
   991                 case opc_ifgt: {
   992                     int indx = i + readShortArg(byteCodes, i);
   993                     emitIf(smapper, this, "if ((@1) > 0) ",
   994                          smapper.popI(), i, indx, topMostLabel);
   995                     i += 2;
   996                     break;
   997                 }
   998                 case opc_ifge: {
   999                     int indx = i + readShortArg(byteCodes, i);
  1000                     emitIf(smapper, this, "if ((@1) >= 0) ",
  1001                          smapper.popI(), i, indx, topMostLabel);
  1002                     i += 2;
  1003                     break;
  1004                 }
  1005                 case opc_ifnonnull: {
  1006                     int indx = i + readShortArg(byteCodes, i);
  1007                     emitIf(smapper, this, "if ((@1) !== null) ",
  1008                          smapper.popA(), i, indx, topMostLabel);
  1009                     i += 2;
  1010                     break;
  1011                 }
  1012                 case opc_ifnull: {
  1013                     int indx = i + readShortArg(byteCodes, i);
  1014                     emitIf(smapper, this, "if ((@1) === null) ",
  1015                          smapper.popA(), i, indx, topMostLabel);
  1016                     i += 2;
  1017                     break;
  1018                 }
  1019                 case opc_if_icmpne:
  1020                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1021                                    "!=", topMostLabel);
  1022                     break;
  1023                 case opc_if_icmplt:
  1024                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1025                                    "<", topMostLabel);
  1026                     break;
  1027                 case opc_if_icmple:
  1028                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1029                                    "<=", topMostLabel);
  1030                     break;
  1031                 case opc_if_icmpgt:
  1032                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1033                                    ">", topMostLabel);
  1034                     break;
  1035                 case opc_if_icmpge:
  1036                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1037                                    ">=", topMostLabel);
  1038                     break;
  1039                 case opc_goto: {
  1040                     smapper.flush(this);
  1041                     int indx = i + readShortArg(byteCodes, i);
  1042                     goTo(this, i, indx, topMostLabel);
  1043                     i += 2;
  1044                     break;
  1045                 }
  1046                 case opc_lookupswitch: {
  1047                     i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
  1048                     break;
  1049                 }
  1050                 case opc_tableswitch: {
  1051                     i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
  1052                     break;
  1053                 }
  1054                 case opc_invokeinterface: {
  1055                     i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
  1056                     break;
  1057                 }
  1058                 case opc_invokevirtual:
  1059                     i = invokeVirtualMethod(byteCodes, i, smapper);
  1060                     break;
  1061                 case opc_invokespecial:
  1062                     i = invokeStaticMethod(byteCodes, i, smapper, false);
  1063                     break;
  1064                 case opc_invokestatic:
  1065                     i = invokeStaticMethod(byteCodes, i, smapper, true);
  1066                     break;
  1067                 case opc_invokedynamic: {
  1068                     int indx = readUShortArg(byteCodes, i);
  1069                     println("invoke dynamic: " + indx);
  1070                     ByteCodeParser.CPX2 c2 = jc.getCpoolEntry(indx);
  1071                     BootMethodData bm = jc.getBootMethod(c2.cpx1);
  1072                     CPX2 methodHandle = jc.getCpoolEntry(bm.method);
  1073                     println("  type: " + methodHandle.cpx1);
  1074                     String[] mi = jc.getFieldInfoName(methodHandle.cpx2);
  1075                     String mcn = mangleClassName(mi[0]);
  1076                     char[] returnType = {'V'};
  1077                     StringBuilder cnt = new StringBuilder();
  1078                     String mn = findMethodName(mi, cnt, returnType);
  1079                     println("  mi[0]: " + mi[0]);
  1080                     println("  mi[1]: " + mi[1]);
  1081                     println("  mi[2]: " + mi[2]);
  1082                     println("  mn   : " + mn);
  1083                     println("  name and type: " + jc.stringValue(c2.cpx2, true));
  1084                     CPX2 nameAndType = jc.getCpoolEntry(c2.cpx2);
  1085                     String type = jc.StringValue(nameAndType.cpx2);
  1086                     String object = accessClass(mcn) + "(false)";
  1087                     if (mn.startsWith("cons_")) {
  1088                         object += ".constructor";
  1089                     }
  1090                     append("var metHan = ");
  1091                     append(accessStaticMethod(object, mn, mi));
  1092                     append('(');
  1093                     String lookup = accessClass("java_lang_invoke_MethodHandles") + "(false).findFor__Ljava_lang_invoke_MethodHandles$Lookup_2Ljava_lang_Class_2(CLS.$class)";
  1094                     append(lookup);
  1095                     append(", '").append(mi[1]).append("', ");
  1096                     String methodType = accessClass("java_lang_invoke_MethodType") + "(false).fromMethodDescriptorString__Ljava_lang_invoke_MethodType_2Ljava_lang_String_2Ljava_lang_ClassLoader_2(";
  1097                     append(methodType).append("'").append(type).append("', null)");
  1098 //                    if (numArguments > 0) {
  1099 //                        append(vars[0]);
  1100 //                        for (int j = 1; j < numArguments; ++j) {
  1101 //                            append(", ");
  1102 //                            append(vars[j]);
  1103 //                        }
  1104 //                    }
  1105                     append(");");
  1106                     emit(smapper, this, "throw 'Invoke dynamic: ' + @1 + ': ' + metHan;", "" + indx);
  1107                     i += 4;
  1108                     break;
  1109                 }
  1110                 case opc_new: {
  1111                     int indx = readUShortArg(byteCodes, i);
  1112                     String ci = jc.getClassName(indx);
  1113                     emit(smapper, this, "var @1 = new @2;",
  1114                          smapper.pushA(), accessClass(mangleClassName(ci)));
  1115                     addReference(ci);
  1116                     i += 2;
  1117                     break;
  1118                 }
  1119                 case opc_newarray:
  1120                     int atype = readUByte(byteCodes, ++i);
  1121                     generateNewArray(atype, smapper);
  1122                     break;
  1123                 case opc_anewarray: {
  1124                     int type = readUShortArg(byteCodes, i);
  1125                     i += 2;
  1126                     generateANewArray(type, smapper);
  1127                     break;
  1128                 }
  1129                 case opc_multianewarray: {
  1130                     int type = readUShortArg(byteCodes, i);
  1131                     i += 2;
  1132                     i = generateMultiANewArray(type, byteCodes, i, smapper);
  1133                     break;
  1134                 }
  1135                 case opc_arraylength:
  1136                     smapper.replace(this, VarType.INTEGER, "(@1).length", smapper.getA(0));
  1137                     break;
  1138                 case opc_lastore:
  1139                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1140                          smapper.popL(), smapper.popI(), smapper.popA());
  1141                     break;
  1142                 case opc_fastore:
  1143                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1144                          smapper.popF(), smapper.popI(), smapper.popA());
  1145                     break;
  1146                 case opc_dastore:
  1147                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1148                          smapper.popD(), smapper.popI(), smapper.popA());
  1149                     break;
  1150                 case opc_aastore:
  1151                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1152                          smapper.popA(), smapper.popI(), smapper.popA());
  1153                     break;
  1154                 case opc_iastore:
  1155                 case opc_bastore:
  1156                 case opc_castore:
  1157                 case opc_sastore:
  1158                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1159                          smapper.popI(), smapper.popI(), smapper.popA());
  1160                     break;
  1161                 case opc_laload:
  1162                     smapper.replace(this, VarType.LONG, "Array.at(@2, @1)",
  1163                          smapper.popI(), smapper.getA(0));
  1164                     break;
  1165                 case opc_faload:
  1166                     smapper.replace(this, VarType.FLOAT, "Array.at(@2, @1)",
  1167                          smapper.popI(), smapper.getA(0));
  1168                     break;
  1169                 case opc_daload:
  1170                     smapper.replace(this, VarType.DOUBLE, "Array.at(@2, @1)",
  1171                          smapper.popI(), smapper.getA(0));
  1172                     break;
  1173                 case opc_aaload:
  1174                     smapper.replace(this, VarType.REFERENCE, "Array.at(@2, @1)",
  1175                          smapper.popI(), smapper.getA(0));
  1176                     break;
  1177                 case opc_iaload:
  1178                 case opc_baload:
  1179                 case opc_caload:
  1180                 case opc_saload:
  1181                     smapper.replace(this, VarType.INTEGER, "Array.at(@2, @1)",
  1182                          smapper.popI(), smapper.getA(0));
  1183                     break;
  1184                 case opc_pop:
  1185                 case opc_pop2:
  1186                     smapper.pop(1);
  1187                     debug("/* pop */");
  1188                     break;
  1189                 case opc_dup: {
  1190                     final Variable v = smapper.get(0);
  1191                     if (smapper.isDirty()) {
  1192                         emit(smapper, this, "var @1 = @2;", smapper.pushT(v.getType()), v);
  1193                     } else {
  1194                         smapper.assign(this, v.getType(), v);
  1195                     }   
  1196                     break;
  1197                 }
  1198                 case opc_dup2: {
  1199                     final Variable vi1 = smapper.get(0);
  1200 
  1201                     if (vi1.isCategory2()) {
  1202                         emit(smapper, this, "var @1 = @2;",
  1203                              smapper.pushT(vi1.getType()), vi1);
  1204                     } else {
  1205                         final Variable vi2 = smapper.get(1);
  1206                         emit(smapper, this, "var @1 = @2, @3 = @4;",
  1207                              smapper.pushT(vi2.getType()), vi2,
  1208                              smapper.pushT(vi1.getType()), vi1);
  1209                     }
  1210                     break;
  1211                 }
  1212                 case opc_dup_x1: {
  1213                     final Variable vi1 = smapper.pop(this);
  1214                     final Variable vi2 = smapper.pop(this);
  1215                     final Variable vo3 = smapper.pushT(vi1.getType());
  1216                     final Variable vo2 = smapper.pushT(vi2.getType());
  1217                     final Variable vo1 = smapper.pushT(vi1.getType());
  1218 
  1219                     emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1220                          vo1, vi1, vo2, vi2, vo3, vo1);
  1221                     break;
  1222                 }
  1223                 case opc_dup2_x1: {
  1224                     final Variable vi1 = smapper.pop(this);
  1225                     final Variable vi2 = smapper.pop(this);
  1226 
  1227                     if (vi1.isCategory2()) {
  1228                         final Variable vo3 = smapper.pushT(vi1.getType());
  1229                         final Variable vo2 = smapper.pushT(vi2.getType());
  1230                         final Variable vo1 = smapper.pushT(vi1.getType());
  1231 
  1232                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1233                              vo1, vi1, vo2, vi2, vo3, vo1);
  1234                     } else {
  1235                         final Variable vi3 = smapper.pop(this);
  1236                         final Variable vo5 = smapper.pushT(vi2.getType());
  1237                         final Variable vo4 = smapper.pushT(vi1.getType());
  1238                         final Variable vo3 = smapper.pushT(vi3.getType());
  1239                         final Variable vo2 = smapper.pushT(vi2.getType());
  1240                         final Variable vo1 = smapper.pushT(vi1.getType());
  1241 
  1242                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
  1243                              vo1, vi1, vo2, vi2, vo3, vi3);
  1244                         emit(smapper, this, " @1 = @2, @3 = @4;",
  1245                              vo4, vo1, vo5, vo2);
  1246                     }
  1247                     break;
  1248                 }
  1249                 case opc_dup_x2: {
  1250                     final Variable vi1 = smapper.pop(this);
  1251                     final Variable vi2 = smapper.pop(this);
  1252 
  1253                     if (vi2.isCategory2()) {
  1254                         final Variable vo3 = smapper.pushT(vi1.getType());
  1255                         final Variable vo2 = smapper.pushT(vi2.getType());
  1256                         final Variable vo1 = smapper.pushT(vi1.getType());
  1257 
  1258                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1259                              vo1, vi1, vo2, vi2, vo3, vo1);
  1260                     } else {
  1261                         final Variable vi3 = smapper.pop(this);
  1262                         final Variable vo4 = smapper.pushT(vi1.getType());
  1263                         final Variable vo3 = smapper.pushT(vi3.getType());
  1264                         final Variable vo2 = smapper.pushT(vi2.getType());
  1265                         final Variable vo1 = smapper.pushT(vi1.getType());
  1266 
  1267                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1268                              vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1269                     }
  1270                     break;
  1271                 }
  1272                 case opc_dup2_x2: {
  1273                     final Variable vi1 = smapper.pop(this);
  1274                     final Variable vi2 = smapper.pop(this);
  1275 
  1276                     if (vi1.isCategory2()) {
  1277                         if (vi2.isCategory2()) {
  1278                             final Variable vo3 = smapper.pushT(vi1.getType());
  1279                             final Variable vo2 = smapper.pushT(vi2.getType());
  1280                             final Variable vo1 = smapper.pushT(vi1.getType());
  1281 
  1282                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1283                                  vo1, vi1, vo2, vi2, vo3, vo1);
  1284                         } else {
  1285                             final Variable vi3 = smapper.pop(this);
  1286                             final Variable vo4 = smapper.pushT(vi1.getType());
  1287                             final Variable vo3 = smapper.pushT(vi3.getType());
  1288                             final Variable vo2 = smapper.pushT(vi2.getType());
  1289                             final Variable vo1 = smapper.pushT(vi1.getType());
  1290 
  1291                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1292                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1293                         }
  1294                     } else {
  1295                         final Variable vi3 = smapper.pop(this);
  1296 
  1297                         if (vi3.isCategory2()) {
  1298                             final Variable vo5 = smapper.pushT(vi2.getType());
  1299                             final Variable vo4 = smapper.pushT(vi1.getType());
  1300                             final Variable vo3 = smapper.pushT(vi3.getType());
  1301                             final Variable vo2 = smapper.pushT(vi2.getType());
  1302                             final Variable vo1 = smapper.pushT(vi1.getType());
  1303 
  1304                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
  1305                                  vo1, vi1, vo2, vi2, vo3, vi3);
  1306                             emit(smapper, this, " @1 = @2, @3 = @4;",
  1307                                  vo4, vo1, vo5, vo2);
  1308                         } else {
  1309                             final Variable vi4 = smapper.pop(this);
  1310                             final Variable vo6 = smapper.pushT(vi2.getType());
  1311                             final Variable vo5 = smapper.pushT(vi1.getType());
  1312                             final Variable vo4 = smapper.pushT(vi4.getType());
  1313                             final Variable vo3 = smapper.pushT(vi3.getType());
  1314                             final Variable vo2 = smapper.pushT(vi2.getType());
  1315                             final Variable vo1 = smapper.pushT(vi1.getType());
  1316                             
  1317                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
  1318                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
  1319                             emit(smapper, this, " @1 = @2, @3 = @4;",
  1320                                  vo5, vo1, vo6, vo2);
  1321                         }
  1322                     }
  1323                     break;
  1324                 }
  1325                 case opc_swap: {
  1326                     final Variable vi1 = smapper.get(0);
  1327                     final Variable vi2 = smapper.get(1);
  1328 
  1329                     if (vi1.getType() == vi2.getType()) {
  1330                         final Variable tmp = smapper.pushT(vi1.getType());
  1331 
  1332                         emit(smapper, this, "var @1 = @2, @2 = @3, @3 = @1;",
  1333                              tmp, vi1, vi2);
  1334                         smapper.pop(1);
  1335                     } else {
  1336                         smapper.pop(2);
  1337                         smapper.pushT(vi1.getType());
  1338                         smapper.pushT(vi2.getType());
  1339                     }
  1340                     break;
  1341                 }
  1342                 case opc_bipush:
  1343                     smapper.assign(this, VarType.INTEGER, 
  1344                         "(" + Integer.toString(byteCodes[++i]) + ")");
  1345                     break;
  1346                 case opc_sipush:
  1347                     smapper.assign(this, VarType.INTEGER, 
  1348                         "(" + Integer.toString(readShortArg(byteCodes, i)) + ")"
  1349                     );
  1350                     i += 2;
  1351                     break;
  1352                 case opc_getfield: {
  1353                     int indx = readUShortArg(byteCodes, i);
  1354                     String[] fi = jc.getFieldInfoName(indx);
  1355                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1356                     final String mangleClass = mangleClassName(fi[0]);
  1357                     final String mangleClassAccess = accessClass(mangleClass);
  1358                     smapper.replace(this, type, "@2.call(@1)",
  1359                          smapper.getA(0),
  1360                          accessField(mangleClassAccess + "(false)",
  1361                                      "_" + fi[1], fi)
  1362                     );
  1363                     i += 2;
  1364                     break;
  1365                 }
  1366                 case opc_putfield: {
  1367                     int indx = readUShortArg(byteCodes, i);
  1368                     String[] fi = jc.getFieldInfoName(indx);
  1369                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1370                     final String mangleClass = mangleClassName(fi[0]);
  1371                     final String mangleClassAccess = accessClass(mangleClass);
  1372                     emit(smapper, this, "@3.call(@2, @1);",
  1373                          smapper.popT(type),
  1374                          smapper.popA(),
  1375                          accessField(mangleClassAccess + "(false)",
  1376                                      "_" + fi[1], fi));
  1377                     i += 2;
  1378                     break;
  1379                 }
  1380                 case opc_getstatic: {
  1381                     int indx = readUShortArg(byteCodes, i);
  1382                     String[] fi = jc.getFieldInfoName(indx);
  1383                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1384                     String ac = accessClass(mangleClassName(fi[0]));
  1385                     String af = accessField(ac + "(false)", "_" + fi[1], fi);
  1386                     smapper.assign(this, type, af + "()");
  1387                     i += 2;
  1388                     addReference(fi[0]);
  1389                     break;
  1390                 }
  1391                 case opc_putstatic: {
  1392                     int indx = readUShortArg(byteCodes, i);
  1393                     String[] fi = jc.getFieldInfoName(indx);
  1394                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1395                     emit(smapper, this, "@1(false)._@2(@3);",
  1396                          accessClass(mangleClassName(fi[0])), fi[1],
  1397                          smapper.popT(type));
  1398                     i += 2;
  1399                     addReference(fi[0]);
  1400                     break;
  1401                 }
  1402                 case opc_checkcast: {
  1403                     int indx = readUShortArg(byteCodes, i);
  1404                     generateCheckcast(indx, smapper);
  1405                     i += 2;
  1406                     break;
  1407                 }
  1408                 case opc_instanceof: {
  1409                     int indx = readUShortArg(byteCodes, i);
  1410                     generateInstanceOf(indx, smapper);
  1411                     i += 2;
  1412                     break;
  1413                 }
  1414                 case opc_athrow: {
  1415                     final CharSequence v = smapper.popA();
  1416                     smapper.clear();
  1417 
  1418                     emit(smapper, this, "{ var @1 = @2; throw @2; }",
  1419                          smapper.pushA(), v);
  1420                     break;
  1421                 }
  1422 
  1423                 case opc_monitorenter: {
  1424                     debug("/* monitor enter */");
  1425                     smapper.popA();
  1426                     break;
  1427                 }
  1428 
  1429                 case opc_monitorexit: {
  1430                     debug("/* monitor exit */");
  1431                     smapper.popA();
  1432                     break;
  1433                 }
  1434 
  1435                 case opc_wide:
  1436                     wide = true;
  1437                     break;
  1438 
  1439                 default: {
  1440                     wide = false;
  1441                     emit(smapper, this, "throw 'unknown bytecode @1';",
  1442                          Integer.toString(c));
  1443                 }
  1444             }
  1445             if (debug(" //")) {
  1446                 generateByteCodeComment(prev, i, byteCodes);
  1447             }
  1448             if (outChanged) {
  1449                 append("\n");
  1450             }
  1451         }
  1452         if (previousTrap != null) {
  1453             generateCatch(previousTrap, byteCodes.length, topMostLabel);
  1454         }
  1455         if (didBranches) {
  1456             append("\n    }\n");
  1457         }
  1458         while (openBraces-- > 0) {
  1459             append('}');
  1460         }
  1461         if (obj) {
  1462             append("\n}});");
  1463         } else {
  1464             append("\n};");
  1465         }
  1466     }
  1467 
  1468     private int generateIf(StackMapper mapper, byte[] byteCodes, 
  1469         int i, final CharSequence v2, final CharSequence v1, 
  1470         final String test, int topMostLabel
  1471     ) throws IOException {
  1472         mapper.flush(this);
  1473         int indx = i + readShortArg(byteCodes, i);
  1474         append("if ((").append(v1)
  1475            .append(") ").append(test).append(" (")
  1476            .append(v2).append(")) ");
  1477         goTo(this, i, indx, topMostLabel);
  1478         return i + 2;
  1479     }
  1480     
  1481     private int readInt4(byte[] byteCodes, int offset) {
  1482         final int d = byteCodes[offset + 0] << 24;
  1483         final int c = byteCodes[offset + 1] << 16;
  1484         final int b = byteCodes[offset + 2] << 8;
  1485         final int a = byteCodes[offset + 3];
  1486         return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
  1487     }
  1488     private static int readUByte(byte[] byteCodes, int offset) {
  1489         return byteCodes[offset] & 0xff;
  1490     }
  1491 
  1492     private static int readUShort(byte[] byteCodes, int offset) {
  1493         return ((byteCodes[offset] & 0xff) << 8)
  1494                     | (byteCodes[offset + 1] & 0xff);
  1495     }
  1496     private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
  1497         return readUShort(byteCodes, offsetInstruction + 1);
  1498     }
  1499 
  1500     private static int readShort(byte[] byteCodes, int offset) {
  1501         int signed = byteCodes[offset];
  1502         byte b0 = (byte)signed;
  1503         return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
  1504     }
  1505     private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
  1506         return readShort(byteCodes, offsetInstruction + 1);
  1507     }
  1508 
  1509     private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
  1510         int i = 0;
  1511         Boolean count = null;
  1512         boolean array = false;
  1513         sig.append("__");
  1514         int firstPos = sig.length();
  1515         while (i < descriptor.length()) {
  1516             char ch = descriptor.charAt(i++);
  1517             switch (ch) {
  1518                 case '(':
  1519                     count = true;
  1520                     continue;
  1521                 case ')':
  1522                     count = false;
  1523                     continue;
  1524                 case 'B': 
  1525                 case 'C': 
  1526                 case 'D': 
  1527                 case 'F': 
  1528                 case 'I': 
  1529                 case 'J': 
  1530                 case 'S': 
  1531                 case 'Z': 
  1532                     if (count) {
  1533                         if (array) {
  1534                             sig.append("_3");
  1535                         }
  1536                         sig.append(ch);
  1537                         if (ch == 'J' || ch == 'D') {
  1538                             cnt.append('1');
  1539                         } else {
  1540                             cnt.append('0');
  1541                         }
  1542                     } else {
  1543                         sig.insert(firstPos, ch);
  1544                         if (array) {
  1545                             returnType[0] = '[';
  1546                             sig.insert(firstPos, "_3");
  1547                         } else {
  1548                             returnType[0] = ch;
  1549                         }
  1550                     }
  1551                     array = false;
  1552                     continue;
  1553                 case 'V': 
  1554                     assert !count;
  1555                     returnType[0] = 'V';
  1556                     sig.insert(firstPos, 'V');
  1557                     continue;
  1558                 case 'L':
  1559                     int next = descriptor.indexOf(';', i);
  1560                     String realSig = mangleSig(descriptor, i - 1, next + 1);
  1561                     if (count) {
  1562                         if (array) {
  1563                             sig.append("_3");
  1564                         }
  1565                         sig.append(realSig);
  1566                         cnt.append('0');
  1567                     } else {
  1568                         sig.insert(firstPos, realSig);
  1569                         if (array) {
  1570                             sig.insert(firstPos, "_3");
  1571                         }
  1572                         returnType[0] = 'L';
  1573                     }
  1574                     i = next + 1;
  1575                     array = false;
  1576                     continue;
  1577                 case '[':
  1578                     array = true;
  1579                     continue;
  1580                 default:
  1581                     throw new IllegalStateException("Invalid char: " + ch);
  1582             }
  1583         }
  1584     }
  1585     
  1586     static String mangleSig(String sig) {
  1587         return mangleSig(sig, 0, sig.length());
  1588     }
  1589     
  1590     private static String mangleMethodName(String name) {
  1591         StringBuilder sb = new StringBuilder(name.length() * 2);
  1592         int last = name.length();
  1593         for (int i = 0; i < last; i++) {
  1594             final char ch = name.charAt(i);
  1595             switch (ch) {
  1596                 case '_': sb.append("_1"); break;
  1597                 default: sb.append(ch); break;
  1598             }
  1599         }
  1600         return sb.toString();
  1601     }
  1602     private static String mangleSig(String txt, int first, int last) {
  1603         StringBuilder sb = new StringBuilder((last - first) * 2);
  1604         for (int i = first; i < last; i++) {
  1605             final char ch = txt.charAt(i);
  1606             switch (ch) {
  1607                 case '/': sb.append('_'); break;
  1608                 case '_': sb.append("_1"); break;
  1609                 case ';': sb.append("_2"); break;
  1610                 case '[': sb.append("_3"); break;
  1611                 default: 
  1612                     if (Character.isJavaIdentifierPart(ch)) {
  1613                         sb.append(ch);
  1614                     } else {
  1615                         sb.append("_0");
  1616                         String hex = Integer.toHexString(ch).toLowerCase();
  1617                         for (int m = hex.length(); m < 4; m++) {
  1618                             sb.append("0");
  1619                         }
  1620                         sb.append(hex);
  1621                     }
  1622                 break;
  1623             }
  1624         }
  1625         return sb.toString();
  1626     }
  1627     
  1628     private static String mangleClassName(String name) {
  1629         return mangleSig(name);
  1630     }
  1631 
  1632     private static String findMethodName(MethodData m, StringBuilder cnt) {
  1633         StringBuilder name = new StringBuilder();
  1634         if ("<init>".equals(m.getName())) { // NOI18N
  1635             name.append("cons"); // NOI18N
  1636         } else if ("<clinit>".equals(m.getName())) { // NOI18N
  1637             name.append("class"); // NOI18N
  1638         } else {
  1639             name.append(mangleMethodName(m.getName()));
  1640         } 
  1641         
  1642         countArgs(m.getInternalSig(), new char[1], name, cnt);
  1643         return name.toString();
  1644     }
  1645 
  1646     static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
  1647         StringBuilder name = new StringBuilder();
  1648         String descr = mi[2];//mi.getDescriptor();
  1649         String nm= mi[1];
  1650         if ("<init>".equals(nm)) { // NOI18N
  1651             name.append("cons"); // NOI18N
  1652         } else {
  1653             name.append(mangleMethodName(nm));
  1654         }
  1655         countArgs(descr, returnType, name, cnt);
  1656         return name.toString();
  1657     }
  1658 
  1659     private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
  1660     throws IOException {
  1661         int methodIndex = readUShortArg(byteCodes, i);
  1662         String[] mi = jc.getFieldInfoName(methodIndex);
  1663         char[] returnType = { 'V' };
  1664         StringBuilder cnt = new StringBuilder();
  1665         String mn = findMethodName(mi, cnt, returnType);
  1666 
  1667         final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
  1668         final CharSequence[] vars = new CharSequence[numArguments];
  1669 
  1670         for (int j = numArguments - 1; j >= 0; --j) {
  1671             vars[j] = mapper.popValue();
  1672         }
  1673 
  1674         if (returnType[0] != 'V') {
  1675             mapper.flush(this);
  1676             append("var ")
  1677                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1678                .append(" = ");
  1679         }
  1680 
  1681         final String in = mi[0];
  1682         String mcn;
  1683         if (callbacks && in.equals("org/apidesign/html/boot/spi/Fn")) {
  1684             mcn = "java_lang_Class";
  1685         } else {
  1686             mcn = mangleClassName(in);
  1687         }
  1688         String object = accessClass(mcn) + "(false)";
  1689         if (mn.startsWith("cons_")) {
  1690             object += ".constructor";
  1691         }
  1692         append(accessStaticMethod(object, mn, mi));
  1693         if (isStatic) {
  1694             append('(');
  1695         } else {
  1696             append(".call(");
  1697         }
  1698         if (numArguments > 0) {
  1699             append(vars[0]);
  1700             for (int j = 1; j < numArguments; ++j) {
  1701                 append(", ");
  1702                 append(vars[j]);
  1703             }
  1704         }
  1705         append(");");
  1706         i += 2;
  1707         addReference(in);
  1708         return i;
  1709     }
  1710     private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
  1711     throws IOException {
  1712         int methodIndex = readUShortArg(byteCodes, i);
  1713         String[] mi = jc.getFieldInfoName(methodIndex);
  1714         char[] returnType = { 'V' };
  1715         StringBuilder cnt = new StringBuilder();
  1716         String mn = findMethodName(mi, cnt, returnType);
  1717 
  1718         final int numArguments = cnt.length() + 1;
  1719         final CharSequence[] vars = new CharSequence[numArguments];
  1720 
  1721         for (int j = numArguments - 1; j >= 0; --j) {
  1722             vars[j] = mapper.popValue();
  1723         }
  1724 
  1725         if (returnType[0] != 'V') {
  1726             append("var ")
  1727                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1728                .append(" = ");
  1729         }
  1730 
  1731         append(accessVirtualMethod(vars[0].toString(), mn, mi, numArguments));
  1732         String sep = "";
  1733         for (int j = 1; j < numArguments; ++j) {
  1734             append(sep);
  1735             append(vars[j]);
  1736             sep = ", ";
  1737         }
  1738         append(");");
  1739         i += 2;
  1740         return i;
  1741     }
  1742 
  1743     private void addReference(String cn) throws IOException {
  1744         if (requireReference(cn)) {
  1745             debug(" /* needs " + cn + " */");
  1746         }
  1747     }
  1748 
  1749     private void outType(String d, StringBuilder out) {
  1750         int arr = 0;
  1751         while (d.charAt(0) == '[') {
  1752             out.append('A');
  1753             d = d.substring(1);
  1754         }
  1755         if (d.charAt(0) == 'L') {
  1756             assert d.charAt(d.length() - 1) == ';';
  1757             out.append(d.replace('/', '_').substring(0, d.length() - 1));
  1758         } else {
  1759             out.append(d);
  1760         }
  1761     }
  1762 
  1763     private String encodeConstant(int entryIndex) throws IOException {
  1764         String[] classRef = { null };
  1765         String s = jc.stringValue(entryIndex, classRef);
  1766         if (classRef[0] != null) {
  1767             if (classRef[0].startsWith("[")) {
  1768                 s = accessClass("java_lang_Class") + "(false)['forName__Ljava_lang_Class_2Ljava_lang_String_2']('" + classRef[0] + "')";
  1769             } else {
  1770                 addReference(classRef[0]);
  1771                 s = accessClass(mangleClassName(s)) + "(false).constructor.$class";
  1772             }
  1773         }
  1774         return s;
  1775     }
  1776 
  1777     private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
  1778         byte[] arr = m.findAnnotationData(true);
  1779         if (arr == null) {
  1780             return null;
  1781         }
  1782         final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
  1783         final String htmlType = "Lnet/java/html/js/JavaScriptBody;";
  1784         class P extends AnnotationParser {
  1785             public P() {
  1786                 super(false, true);
  1787             }
  1788             
  1789             int cnt;
  1790             String[] args = new String[30];
  1791             String body;
  1792             boolean javacall;
  1793             boolean html4j;
  1794             
  1795             @Override
  1796             protected void visitAttr(String type, String attr, String at, String value) {
  1797                 if (type.equals(jvmType)) {
  1798                     if ("body".equals(attr)) {
  1799                         body = value;
  1800                     } else if ("args".equals(attr)) {
  1801                         args[cnt++] = value;
  1802                     } else {
  1803                         throw new IllegalArgumentException(attr);
  1804                     }
  1805                 }
  1806                 if (type.equals(htmlType)) {
  1807                     html4j = true;
  1808                     if ("body".equals(attr)) {
  1809                         body = value;
  1810                     } else if ("args".equals(attr)) {
  1811                         args[cnt++] = value;
  1812                     } else if ("javacall".equals(attr)) {
  1813                         javacall = "1".equals(value);
  1814                     } else if ("wait4js".equals(attr)) {
  1815                         // ignore, we always invoke synchronously
  1816                     } else {
  1817                         throw new IllegalArgumentException(attr);
  1818                     }
  1819                 }
  1820             }
  1821         }
  1822         P p = new P();
  1823         p.parse(arr, jc);
  1824         if (p.body == null) {
  1825             return null;
  1826         }
  1827         StringBuilder cnt = new StringBuilder();
  1828         final String mn = findMethodName(m, cnt);
  1829         append(destObject).append(".").append(mn);
  1830         append(" = function(");
  1831         String space = "";
  1832         int index = 0;
  1833         StringBuilder toValue = new StringBuilder();
  1834         for (int i = 0; i < cnt.length(); i++) {
  1835             append(space);
  1836             space = outputArg(this, p.args, index);
  1837             if (p.html4j && space.length() > 0) {
  1838                 toValue.append("\n  ").append(p.args[index]).append(" = ")
  1839                     .append(accessClass("java_lang_Class")).append("(false).toJS(").
  1840                     append(p.args[index]).append(");");
  1841             }
  1842             index++;
  1843         }
  1844         append(") {").append("\n");
  1845         append(toValue.toString());
  1846         if (p.javacall) {
  1847             int lastSlash = jc.getClassName().lastIndexOf('/');
  1848             final String pkg = jc.getClassName().substring(0, lastSlash);
  1849             append(mangleCallbacks(pkg, p.body));
  1850             requireReference(pkg + "/$JsCallbacks$");
  1851         } else {
  1852             append(p.body);
  1853         }
  1854         append("\n}\n");
  1855         return mn;
  1856     }
  1857     
  1858     private static CharSequence mangleCallbacks(String pkgName, String body) {
  1859         StringBuilder sb = new StringBuilder();
  1860         int pos = 0;
  1861         for (;;) {
  1862             int next = body.indexOf(".@", pos);
  1863             if (next == -1) {
  1864                 sb.append(body.substring(pos));
  1865                 body = sb.toString();
  1866                 break;
  1867             }
  1868             int ident = next;
  1869             while (ident > 0) {
  1870                 if (!Character.isJavaIdentifierPart(body.charAt(--ident))) {
  1871                     ident++;
  1872                     break;
  1873                 }
  1874             }
  1875             String refId = body.substring(ident, next);
  1876 
  1877             sb.append(body.substring(pos, ident));
  1878 
  1879             int sigBeg = body.indexOf('(', next);
  1880             int sigEnd = body.indexOf(')', sigBeg);
  1881             int colon4 = body.indexOf("::", next);
  1882             if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
  1883                 throw new IllegalStateException("Malformed body " + body);
  1884             }
  1885             String fqn = body.substring(next + 2, colon4);
  1886             String method = body.substring(colon4 + 2, sigBeg);
  1887             String params = body.substring(sigBeg, sigEnd + 1);
  1888 
  1889             int paramBeg = body.indexOf('(', sigEnd + 1);
  1890             
  1891             sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
  1892             sb.append(mangleJsCallbacks(fqn, method, params, false));
  1893             sb.append("(").append(refId);
  1894             if (body.charAt(paramBeg + 1) != ')') {
  1895                 sb.append(",");
  1896             }
  1897             pos = paramBeg + 1;
  1898         }
  1899         sb = null;
  1900         pos = 0;
  1901         for (;;) {
  1902             int next = body.indexOf("@", pos);
  1903             if (next == -1) {
  1904                 if (sb == null) {
  1905                     return body;
  1906                 }
  1907                 sb.append(body.substring(pos));
  1908                 return sb;
  1909             }
  1910             if (sb == null) {
  1911                 sb = new StringBuilder();
  1912             }
  1913 
  1914             sb.append(body.substring(pos, next));
  1915 
  1916             int sigBeg = body.indexOf('(', next);
  1917             int sigEnd = body.indexOf(')', sigBeg);
  1918             int colon4 = body.indexOf("::", next);
  1919             if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
  1920                 throw new IllegalStateException("Malformed body " + body);
  1921             }
  1922             String fqn = body.substring(next + 1, colon4);
  1923             String method = body.substring(colon4 + 2, sigBeg);
  1924             String params = body.substring(sigBeg, sigEnd + 1);
  1925 
  1926             int paramBeg = body.indexOf('(', sigEnd + 1);
  1927             
  1928             sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
  1929             sb.append(mangleJsCallbacks(fqn, method, params, true));
  1930             sb.append("(");
  1931             pos = paramBeg + 1;
  1932         }
  1933     }
  1934 
  1935     static String mangleJsCallbacks(String fqn, String method, String params, boolean isStatic) {
  1936         if (params.startsWith("(")) {
  1937             params = params.substring(1);
  1938         }
  1939         if (params.endsWith(")")) {
  1940             params = params.substring(0, params.length() - 1);
  1941         }
  1942         StringBuilder sb = new StringBuilder();
  1943         final String fqnu = fqn.replace('.', '_');
  1944         final String rfqn = mangleClassName(fqnu);
  1945         final String rm = mangleMethodName(method);
  1946         final String srp;
  1947         {
  1948             StringBuilder pb = new StringBuilder();
  1949             int len = params.length();
  1950             int indx = 0;
  1951             while (indx < len) {
  1952                 char ch = params.charAt(indx);
  1953                 if (ch == '[' || ch == 'L') {
  1954                     pb.append("Ljava/lang/Object;");
  1955                     indx = params.indexOf(';', indx) + 1;
  1956                 } else {
  1957                     pb.append(ch);
  1958                     indx++;
  1959                 }
  1960             }
  1961             srp = mangleSig(pb.toString());
  1962         }
  1963         final String rp = mangleSig(params);
  1964         final String mrp = mangleMethodName(rp);
  1965         sb.append(rfqn).append("$").append(rm).
  1966             append('$').append(mrp).append("__Ljava_lang_Object_2");
  1967         if (!isStatic) {
  1968             sb.append('L').append(fqnu).append("_2");
  1969         }
  1970         sb.append(srp);
  1971         return sb.toString();
  1972     }
  1973 
  1974     private static String className(ClassData jc) {
  1975         //return jc.getName().getInternalName().replace('/', '_');
  1976         return mangleClassName(jc.getClassName());
  1977     }
  1978     
  1979     private static String[] findAnnotation(
  1980         byte[] arr, ClassData cd, final String className, 
  1981         final String... attrNames
  1982     ) throws IOException {
  1983         if (arr == null) {
  1984             return null;
  1985         }
  1986         final String[] values = new String[attrNames.length];
  1987         final boolean[] found = { false };
  1988         final String jvmType = "L" + className.replace('.', '/') + ";";
  1989         AnnotationParser ap = new AnnotationParser(false, true) {
  1990             @Override
  1991             protected void visitAttr(String type, String attr, String at, String value) {
  1992                 if (type.equals(jvmType)) {
  1993                     found[0] = true;
  1994                     for (int i = 0; i < attrNames.length; i++) {
  1995                         if (attrNames[i].equals(attr)) {
  1996                             values[i] = value;
  1997                         }
  1998                     }
  1999                 }
  2000             }
  2001             
  2002         };
  2003         ap.parse(arr, cd);
  2004         return found[0] ? values : null;
  2005     }
  2006 
  2007     private CharSequence initField(FieldData v) {
  2008         final String is = v.getInternalSig();
  2009         if (is.length() == 1) {
  2010             switch (is.charAt(0)) {
  2011                 case 'S':
  2012                 case 'J':
  2013                 case 'B':
  2014                 case 'Z':
  2015                 case 'C':
  2016                 case 'I': return " = 0;";
  2017                 case 'F': 
  2018                 case 'D': return " = 0.0;";
  2019                 default:
  2020                     throw new IllegalStateException(is);
  2021             }
  2022         }
  2023         return " = null;";
  2024     }
  2025 
  2026     private void generateAnno(ClassData cd, byte[] data) throws IOException {
  2027         AnnotationParser ap = new AnnotationParser(true, false) {
  2028             int[] cnt = new int[32];
  2029             int depth;
  2030             
  2031             @Override
  2032             protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
  2033                 final String slashType = attrType.substring(1, attrType.length() - 1);
  2034                 requireReference(slashType);
  2035                 
  2036                 if (cnt[depth]++ > 0) {
  2037                     append(",");
  2038                 }
  2039                 if (top) {
  2040                     append('"').append(attrType).append("\" : ");
  2041                 }
  2042                 append("{\n");
  2043                 cnt[++depth] = 0;
  2044             }
  2045 
  2046             @Override
  2047             protected void visitAnnotationEnd(String type, boolean top) throws IOException {
  2048                 append("\n}\n");
  2049                 depth--;
  2050             }
  2051 
  2052             @Override
  2053             protected void visitValueStart(String attrName, char type) throws IOException {
  2054                 if (cnt[depth]++ > 0) {
  2055                     append(",\n");
  2056                 }
  2057                 cnt[++depth] = 0;
  2058                 if (attrName != null) {
  2059                     append('"').append(attrName).append("\" : ");
  2060                 }
  2061                 if (type == '[') {
  2062                     append("[");
  2063                 }
  2064             }
  2065 
  2066             @Override
  2067             protected void visitValueEnd(String attrName, char type) throws IOException {
  2068                 if (type == '[') {
  2069                     append("]");
  2070                 }
  2071                 depth--;
  2072             }
  2073             
  2074             @Override
  2075             protected void visitAttr(String type, String attr, String attrType, String value) 
  2076             throws IOException {
  2077                 if (attr == null && value == null) {
  2078                     return;
  2079                 }
  2080                 append(value);
  2081             }
  2082 
  2083             @Override
  2084             protected void visitEnumAttr(String type, String attr, String attrType, String value) 
  2085             throws IOException {
  2086                 final String slashType = attrType.substring(1, attrType.length() - 1);
  2087                 requireReference(slashType);
  2088                 
  2089                 final String cn = mangleClassName(slashType);
  2090                 append(accessClass(cn))
  2091                    .append("(false)['valueOf__L").
  2092                     append(cn).
  2093                     append("_2Ljava_lang_String_2']('").
  2094                     append(value).
  2095                     append("')");
  2096             }
  2097         };
  2098         ap.parse(data, cd);
  2099     }
  2100 
  2101     private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
  2102         final String name = args[indx];
  2103         if (name == null) {
  2104             return "";
  2105         }
  2106         if (name.contains(",")) {
  2107             throw new IOException("Wrong parameter with ',': " + name);
  2108         }
  2109         out.append(name);
  2110         return ",";
  2111     }
  2112 
  2113     final void emitNoFlush(
  2114         StackMapper sm, 
  2115         final String format, final CharSequence... params
  2116     ) throws IOException {
  2117         emitImpl(this, format, params);
  2118     }
  2119     static final void emit(
  2120         StackMapper sm, 
  2121         final Appendable out, 
  2122         final String format, final CharSequence... params
  2123     ) throws IOException {
  2124         sm.flush(out);
  2125         emitImpl(out, format, params);
  2126     }
  2127     static void emitImpl(final Appendable out,
  2128                              final String format,
  2129                              final CharSequence... params) throws IOException {
  2130         final int length = format.length();
  2131 
  2132         int processed = 0;
  2133         int paramOffset = format.indexOf('@');
  2134         while ((paramOffset != -1) && (paramOffset < (length - 1))) {
  2135             final char paramChar = format.charAt(paramOffset + 1);
  2136             if ((paramChar >= '1') && (paramChar <= '9')) {
  2137                 final int paramIndex = paramChar - '0' - 1;
  2138 
  2139                 out.append(format, processed, paramOffset);
  2140                 out.append(params[paramIndex]);
  2141 
  2142                 ++paramOffset;
  2143                 processed = paramOffset + 1;
  2144             }
  2145 
  2146             paramOffset = format.indexOf('@', paramOffset + 1);
  2147         }
  2148 
  2149         out.append(format, processed, length);
  2150     }
  2151 
  2152     private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
  2153         append("} catch (e) {\n");
  2154         int finallyPC = -1;
  2155         for (TrapData e : traps) {
  2156             if (e == null) {
  2157                 break;
  2158             }
  2159             if (e.catch_cpx != 0) { //not finally
  2160                 final String classInternalName = jc.getClassName(e.catch_cpx);
  2161                 addReference(classInternalName);
  2162                 append("e = vm.java_lang_Class(false).bck2BrwsrThrwrbl(e);");
  2163                 append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
  2164                 append("var stA0 = e;");
  2165                 goTo(this, current, e.handler_pc, topMostLabel);
  2166                 append("}\n");
  2167             } else {
  2168                 finallyPC = e.handler_pc;
  2169             }
  2170         }
  2171         if (finallyPC == -1) {
  2172             append("throw e;");
  2173         } else {
  2174             append("var stA0 = e;");
  2175             goTo(this, current, finallyPC, topMostLabel);
  2176         }
  2177         append("\n}");
  2178     }
  2179 
  2180     private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
  2181         if (to < current) {
  2182             if (canBack < to) {
  2183                 out.append("{ gt = 0; continue X_" + to + "; }");
  2184             } else {
  2185                 out.append("{ gt = " + to + "; continue X_0; }");
  2186             }
  2187         } else {
  2188             out.append("{ gt = " + to + "; break IF; }");
  2189         }
  2190     }
  2191 
  2192     private static void emitIf(
  2193         StackMapper sm, 
  2194         Appendable out, String pattern, 
  2195         CharSequence param, 
  2196         int current, int to, int canBack
  2197     ) throws IOException {
  2198         sm.flush(out);
  2199         emitImpl(out, pattern, param);
  2200         goTo(out, current, to, canBack);
  2201     }
  2202 
  2203     private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
  2204         String jvmType;
  2205         switch (atype) {
  2206             case 4: jvmType = "[Z"; break;
  2207             case 5: jvmType = "[C"; break;
  2208             case 6: jvmType = "[F"; break;
  2209             case 7: jvmType = "[D"; break;
  2210             case 8: jvmType = "[B"; break;
  2211             case 9: jvmType = "[S"; break;
  2212             case 10: jvmType = "[I"; break;
  2213             case 11: jvmType = "[J"; break;
  2214             default: throw new IllegalStateException("Array type: " + atype);
  2215         }
  2216         emit(smapper, this, 
  2217             "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](true, '@3', null, @1);",
  2218              smapper.popI(), smapper.pushA(), jvmType);
  2219     }
  2220 
  2221     private void generateANewArray(int type, final StackMapper smapper) throws IOException {
  2222         String typeName = jc.getClassName(type);
  2223         String ref = "null";
  2224         if (typeName.startsWith("[")) {
  2225             typeName = "'[" + typeName + "'";
  2226         } else {
  2227             ref = "vm." + mangleClassName(typeName);
  2228             typeName = "'[L" + typeName + ";'";
  2229         }
  2230         emit(smapper, this,
  2231             "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](false, @3, @4, @1);",
  2232              smapper.popI(), smapper.pushA(), typeName, ref);
  2233     }
  2234 
  2235     private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
  2236         String typeName = jc.getClassName(type);
  2237         int dim = readUByte(byteCodes, ++i);
  2238         StringBuilder dims = new StringBuilder();
  2239         dims.append('[');
  2240         for (int d = 0; d < dim; d++) {
  2241             if (d != 0) {
  2242                 dims.insert(1, ",");
  2243             }
  2244             dims.insert(1, smapper.popI());
  2245         }
  2246         dims.append(']');
  2247         String fn = "null";
  2248         if (typeName.charAt(dim) == 'L') {
  2249             fn = "vm." + mangleClassName(typeName.substring(dim + 1, typeName.length() - 1));
  2250         }
  2251         emit(smapper, this, 
  2252             "var @2 = Array.prototype['multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3ILjava_lang_Object_2']('@3', @1, @4);",
  2253              dims.toString(), smapper.pushA(), typeName, fn
  2254         );
  2255         return i;
  2256     }
  2257 
  2258     private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  2259         int table = i / 4 * 4 + 4;
  2260         int dflt = i + readInt4(byteCodes, table);
  2261         table += 4;
  2262         int low = readInt4(byteCodes, table);
  2263         table += 4;
  2264         int high = readInt4(byteCodes, table);
  2265         table += 4;
  2266         final CharSequence swVar = smapper.popValue();
  2267         smapper.flush(this);
  2268         append("switch (").append(swVar).append(") {\n");
  2269         while (low <= high) {
  2270             int offset = i + readInt4(byteCodes, table);
  2271             table += 4;
  2272             append("  case " + low).append(":"); goTo(this, i, offset, topMostLabel); append('\n');
  2273             low++;
  2274         }
  2275         append("  default: ");
  2276         goTo(this, i, dflt, topMostLabel);
  2277         append("\n}");
  2278         i = table - 1;
  2279         return i;
  2280     }
  2281 
  2282     private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  2283         int table = i / 4 * 4 + 4;
  2284         int dflt = i + readInt4(byteCodes, table);
  2285         table += 4;
  2286         int n = readInt4(byteCodes, table);
  2287         table += 4;
  2288         final CharSequence swVar = smapper.popValue();
  2289         smapper.flush(this);
  2290         append("switch (").append(swVar).append(") {\n");
  2291         while (n-- > 0) {
  2292             int cnstnt = readInt4(byteCodes, table);
  2293             table += 4;
  2294             int offset = i + readInt4(byteCodes, table);
  2295             table += 4;
  2296             append("  case " + cnstnt).append(": "); goTo(this, i, offset, topMostLabel); append('\n');
  2297         }
  2298         append("  default: ");
  2299         goTo(this, i, dflt, topMostLabel);
  2300         append("\n}");
  2301         i = table - 1;
  2302         return i;
  2303     }
  2304 
  2305     private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
  2306         String type = jc.getClassName(indx);
  2307         if (!type.startsWith("[")) {
  2308             emit(smapper, this, 
  2309                     "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
  2310                  smapper.popA(), smapper.pushI(),
  2311                  type.replace('/', '_'));
  2312         } else {
  2313             int cnt = 0;
  2314             while (type.charAt(cnt) == '[') {
  2315                 cnt++;
  2316             }
  2317             if (type.charAt(cnt) == 'L') {
  2318                 type = "vm." + mangleClassName(type.substring(cnt + 1, type.length() - 1));
  2319                 emit(smapper, this, 
  2320                     "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @4, @3);",
  2321                     smapper.popA(), smapper.pushI(),
  2322                     type, "" + cnt
  2323                 );
  2324             } else {
  2325                 emit(smapper, this, 
  2326                     "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@3');",
  2327                     smapper.popA(), smapper.pushI(), type
  2328                 );
  2329             }
  2330         }
  2331     }
  2332 
  2333     private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
  2334         String type = jc.getClassName(indx);
  2335         if (!type.startsWith("[")) {
  2336             emitNoFlush(smapper, 
  2337                  "if (@1 !== null && !@1['$instOf_@2']) throw vm.java_lang_ClassCastException(true);",
  2338                  smapper.getT(0, VarType.REFERENCE, false), type.replace('/', '_'));
  2339         } else {
  2340             int cnt = 0;
  2341             while (type.charAt(cnt) == '[') {
  2342                 cnt++;
  2343             }
  2344             if (type.charAt(cnt) == 'L') {
  2345                 type = "vm." + mangleClassName(type.substring(cnt + 1, type.length() - 1));
  2346                 emitNoFlush(smapper, 
  2347                     "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @3, @2)) throw vm.java_lang_ClassCastException(true);",
  2348                      smapper.getT(0, VarType.REFERENCE, false), type, "" + cnt
  2349                 );
  2350             } else {
  2351                 emitNoFlush(smapper, 
  2352                     "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@2')) throw vm.java_lang_ClassCastException(true);",
  2353                      smapper.getT(0, VarType.REFERENCE, false), type
  2354                 );
  2355             }
  2356         }
  2357     }
  2358 
  2359     private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
  2360         for (int j = prev; j <= i; j++) {
  2361             append(" ");
  2362             final int cc = readUByte(byteCodes, j);
  2363             append(Integer.toString(cc));
  2364         }
  2365     }
  2366     
  2367     @JavaScriptBody(args = "msg", body = "")
  2368     private static void println(String msg) {
  2369         System.err.println(msg);
  2370     }
  2371 }