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