rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Feb 2013 14:38:16 +0100
changeset 775 a13e33fd5c2e
parent 772 d382dacfd73f
child 788 bced0d6a7b20
permissions -rw-r--r--
Need to use method accessors to access static fields
     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 org.apidesign.javap.AnnotationParser;
    24 import org.apidesign.javap.ClassData;
    25 import org.apidesign.javap.FieldData;
    26 import org.apidesign.javap.MethodData;
    27 import org.apidesign.javap.StackMapIterator;
    28 import static org.apidesign.javap.RuntimeConstants.*;
    29 import org.apidesign.javap.TrapData;
    30 import org.apidesign.javap.TrapDataIterator;
    31 
    32 /** Translator of the code inside class files to JavaScript.
    33  *
    34  * @author Jaroslav Tulach <jtulach@netbeans.org>
    35  */
    36 abstract class ByteCodeToJavaScript {
    37     private ClassData jc;
    38     final Appendable out;
    39 
    40     protected ByteCodeToJavaScript(Appendable out) {
    41         this.out = out;
    42     }
    43     
    44     /* Collects additional required resources.
    45      * 
    46      * @param internalClassName classes that were referenced and should be loaded in order the
    47      *   generated JavaScript code works properly. The names are in internal 
    48      *   JVM form so String is <code>java/lang/String</code>. 
    49      */
    50     protected abstract boolean requireReference(String internalClassName);
    51     
    52     /*
    53      * @param resourcePath name of resources to read
    54      */
    55     protected abstract void requireScript(String resourcePath) throws IOException;
    56     
    57     /** Allows subclasses to redefine what field a function representing a
    58      * class gets assigned. By default it returns the suggested name followed
    59      * by <code>" = "</code>;
    60      * 
    61      * @param className suggested name of the class
    62      */
    63     /* protected */ String assignClass(String className) {
    64         return className + " = ";
    65     }
    66     /* protected */ String accessClass(String classOperation) {
    67         return classOperation;
    68     }
    69     
    70     /** Prints out a debug message. 
    71      * 
    72      * @param msg the message
    73      * @return true if the message has been printed
    74      * @throws IOException 
    75      */
    76     boolean debug(String msg) throws IOException {
    77         out.append(msg);
    78         return true;
    79     }
    80 
    81     /**
    82      * Converts a given class file to a JavaScript version.
    83      *
    84      * @param classFile input stream with code of the .class file
    85      * @return the initialization code for this class, if any. Otherwise <code>null</code>
    86      * 
    87      * @throws IOException if something goes wrong during read or write or translating
    88      */
    89     
    90     public String compile(InputStream classFile) throws IOException {
    91         this.jc = new ClassData(classFile);
    92         if (jc.getMajor_version() < 50) {
    93             throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
    94                 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
    95             );
    96         }
    97         byte[] arrData = jc.findAnnotationData(true);
    98         String[] arr = findAnnotation(arrData, jc, 
    99             "org.apidesign.bck2brwsr.core.ExtraJavaScript", 
   100             "resource", "processByteCode"
   101         );
   102         if (arr != null) {
   103             requireScript(arr[0]);
   104             if ("0".equals(arr[1])) {
   105                 return null;
   106             }
   107         }
   108         String[] proto = findAnnotation(arrData, jc, 
   109             "org.apidesign.bck2brwsr.core.JavaScriptPrototype", 
   110             "container", "prototype"
   111         );
   112         StringArray toInitilize = new StringArray();
   113         final String className = className(jc);
   114         out.append("\n\n").append(assignClass(className));
   115         out.append("function CLS() {");
   116         out.append("\n  if (!CLS.$class) {");
   117         if (proto == null) {
   118             String sc = jc.getSuperClassName(); // with _
   119             out.append("\n    var pp = ").
   120                 append(accessClass(sc.replace('/', '_'))).append("(true);");
   121             out.append("\n    var p = CLS.prototype = pp;");
   122             out.append("\n    var c = p;");
   123             out.append("\n    var sprcls = pp.constructor.$class;");
   124         } else {
   125             out.append("\n    var p = CLS.prototype = ").append(proto[1]).append(";");
   126             if (proto[0] == null) {
   127                 proto[0] = "p";
   128             }
   129             out.append("\n    var c = ").append(proto[0]).append(";");
   130             out.append("\n    var sprcls = null;");
   131         }
   132         for (FieldData v : jc.getFields()) {
   133             if (v.isStatic()) {
   134                 out.append("\n  CLS.").append(v.getName()).append(initField(v));
   135                 out.append("\n  c._").append(v.getName()).append(" = function (v) {")
   136                    .append("  if (arguments.length == 1) CLS.").append(v.getName())
   137                    .append(" = v; return CLS.").
   138                     append(v.getName()).append("; };");
   139             } else {
   140                 out.append("\n  c._").append(v.getName()).append(" = function (v) {")
   141                    .append("  if (arguments.length == 1) this.fld_").
   142                     append(className).append('_').append(v.getName())
   143                    .append(" = v; return this.fld_").
   144                     append(className).append('_').append(v.getName())
   145                    .append("; };");
   146             }
   147         }
   148         for (MethodData m : jc.getMethods()) {
   149             byte[] onlyArr = m.findAnnotationData(true);
   150             String[] only = findAnnotation(onlyArr, jc, 
   151                 "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
   152                 "name", "value"
   153             );
   154             if (only != null) {
   155                 if (only[0] != null && only[1] != null) {
   156                     out.append("\n    p.").append(only[0]).append(" = ")
   157                         .append(only[1]).append(";");
   158                 }
   159                 continue;
   160             }
   161             String prefix;
   162             String mn;
   163             if (m.isStatic()) {
   164                 prefix = "\n    c.";
   165                 mn = generateStaticMethod(prefix, m, toInitilize);
   166             } else {
   167                 if (m.isConstructor()) {
   168                     prefix = "\n    CLS.";
   169                     mn = generateInstanceMethod(prefix, m);
   170                 } else {
   171                     prefix = "\n    c.";
   172                     mn = generateInstanceMethod(prefix, m);
   173                 }
   174             }
   175             byte[] runAnno = m.findAnnotationData(false);
   176             if (runAnno != null) {
   177                 out.append(prefix).append(mn).append(".anno = {");
   178                 generateAnno(jc, out, runAnno);
   179                 out.append("\n    };");
   180             }
   181             out.append(prefix).append(mn).append(".access = " + m.getAccess()).append(";");
   182             out.append(prefix).append(mn).append(".cls = CLS;");
   183         }
   184         out.append("\n    c.constructor = CLS;");
   185         out.append("\n    c.$instOf_").append(className).append(" = true;");
   186         for (String superInterface : jc.getSuperInterfaces()) {
   187             out.append("\n    c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
   188         }
   189         out.append("\n    CLS.$class = 'temp';");
   190         out.append("\n    CLS.$class = ");
   191         out.append(accessClass("java_lang_Class(true);"));
   192         out.append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
   193         out.append("\n    CLS.$class.superclass = sprcls;");
   194         out.append("\n    CLS.$class.access = ").append(jc.getAccessFlags()+";");
   195         out.append("\n    CLS.$class.cnstr = CLS;");
   196         byte[] classAnno = jc.findAnnotationData(false);
   197         if (classAnno != null) {
   198             out.append("\n    CLS.$class.anno = {");
   199             generateAnno(jc, out, classAnno);
   200             out.append("\n    };");
   201         }
   202         out.append("\n  }");
   203         out.append("\n  if (arguments.length === 0) {");
   204         out.append("\n    if (!(this instanceof CLS)) {");
   205         out.append("\n      return new CLS();");
   206         out.append("\n    }");
   207         for (FieldData v : jc.getFields()) {
   208             byte[] onlyArr = v.findAnnotationData(true);
   209             String[] only = findAnnotation(onlyArr, jc, 
   210                 "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
   211                 "name", "value"
   212             );
   213             if (only != null) {
   214                 if (only[0] != null && only[1] != null) {
   215                     out.append("\n    p.").append(only[0]).append(" = ")
   216                         .append(only[1]).append(";");
   217                 }
   218                 continue;
   219             }
   220             if (!v.isStatic()) {
   221                 out.append("\n    this.fld_").
   222                     append(className).append('_').
   223                     append(v.getName()).append(initField(v));
   224             }
   225         }
   226         out.append("\n    return this;");
   227         out.append("\n  }");
   228         out.append("\n  return arguments[0] ? new CLS() : CLS.prototype;");
   229         out.append("\n};");
   230         StringBuilder sb = new StringBuilder();
   231         for (String init : toInitilize.toArray()) {
   232             sb.append("\n").append(init).append("();");
   233         }
   234         return sb.toString();
   235     }
   236     private String generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
   237         String jsb = javaScriptBody(prefix, m, true);
   238         if (jsb != null) {
   239             return jsb;
   240         }
   241         final String mn = findMethodName(m, new StringBuilder());
   242         if (mn.equals("class__V")) {
   243             toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
   244         }
   245         generateMethod(prefix, mn, m);
   246         return mn;
   247     }
   248 
   249     private String generateInstanceMethod(String prefix, MethodData m) throws IOException {
   250         String jsb = javaScriptBody(prefix, m, false);
   251         if (jsb != null) {
   252             return jsb;
   253         }
   254         final String mn = findMethodName(m, new StringBuilder());
   255         generateMethod(prefix, mn, m);
   256         return mn;
   257     }
   258 
   259     private void generateMethod(String prefix, String name, MethodData m)
   260             throws IOException {
   261         final StackMapIterator stackMapIterator = m.createStackMapIterator();
   262         TrapDataIterator trap = m.getTrapDataIterator();
   263         final LocalsMapper lmapper =
   264                 new LocalsMapper(stackMapIterator.getArguments());
   265 
   266         out.append(prefix).append(name).append(" = function(");
   267         lmapper.outputArguments(out, m.isStatic());
   268         out.append(") {").append("\n");
   269 
   270         final byte[] byteCodes = m.getCode();
   271         if (byteCodes == null) {
   272             out.append("  throw 'no code found for ")
   273                .append(jc.getClassName()).append('.')
   274                .append(m.getName()).append("';\n");
   275             out.append("};");
   276             return;
   277         }
   278 
   279         final StackMapper smapper = new StackMapper();
   280 
   281         if (!m.isStatic()) {
   282             out.append("  var ").append(" lcA0 = this;\n");
   283         }
   284 
   285         int lastStackFrame = -1;
   286         TrapData[] previousTrap = null;
   287         boolean wide = false;
   288         
   289         out.append("\n  var gt = 0;\n  for(;;) switch(gt) {\n");
   290         for (int i = 0; i < byteCodes.length; i++) {
   291             int prev = i;
   292             stackMapIterator.advanceTo(i);
   293             boolean changeInCatch = trap.advanceTo(i);
   294             if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
   295                 if (previousTrap != null) {
   296                     generateCatch(previousTrap);
   297                     previousTrap = null;
   298                 }
   299             }
   300             if (lastStackFrame != stackMapIterator.getFrameIndex()) {
   301                 lastStackFrame = stackMapIterator.getFrameIndex();
   302                 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
   303                 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
   304                 out.append("    case " + i).append(": ");            
   305                 changeInCatch = true;
   306             } else {
   307                 debug("    /* " + i + " */ ");
   308             }
   309             if (changeInCatch && trap.useTry()) {
   310                 out.append("try {");
   311                 previousTrap = trap.current();
   312             }
   313             final int c = readUByte(byteCodes, i);
   314             switch (c) {
   315                 case opc_aload_0:
   316                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
   317                     break;
   318                 case opc_iload_0:
   319                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
   320                     break;
   321                 case opc_lload_0:
   322                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
   323                     break;
   324                 case opc_fload_0:
   325                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
   326                     break;
   327                 case opc_dload_0:
   328                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
   329                     break;
   330                 case opc_aload_1:
   331                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
   332                     break;
   333                 case opc_iload_1:
   334                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
   335                     break;
   336                 case opc_lload_1:
   337                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
   338                     break;
   339                 case opc_fload_1:
   340                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
   341                     break;
   342                 case opc_dload_1:
   343                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
   344                     break;
   345                 case opc_aload_2:
   346                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
   347                     break;
   348                 case opc_iload_2:
   349                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
   350                     break;
   351                 case opc_lload_2:
   352                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
   353                     break;
   354                 case opc_fload_2:
   355                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
   356                     break;
   357                 case opc_dload_2:
   358                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
   359                     break;
   360                 case opc_aload_3:
   361                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
   362                     break;
   363                 case opc_iload_3:
   364                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
   365                     break;
   366                 case opc_lload_3:
   367                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
   368                     break;
   369                 case opc_fload_3:
   370                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
   371                     break;
   372                 case opc_dload_3:
   373                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
   374                     break;
   375                 case opc_iload: {
   376                     ++i;
   377                     final int indx = wide ? readUShort(byteCodes, i++)
   378                                           : readUByte(byteCodes, i);
   379                     wide = false;
   380                     emit(out, "var @1 = @2;",
   381                          smapper.pushI(), lmapper.getI(indx));
   382                     break;
   383                 }
   384                 case opc_lload: {
   385                     ++i;
   386                     final int indx = wide ? readUShort(byteCodes, i++)
   387                                           : readUByte(byteCodes, i);
   388                     wide = false;
   389                     emit(out, "var @1 = @2;",
   390                          smapper.pushL(), lmapper.getL(indx));
   391                     break;
   392                 }
   393                 case opc_fload: {
   394                     ++i;
   395                     final int indx = wide ? readUShort(byteCodes, i++)
   396                                           : readUByte(byteCodes, i);
   397                     wide = false;
   398                     emit(out, "var @1 = @2;",
   399                          smapper.pushF(), lmapper.getF(indx));
   400                     break;
   401                 }
   402                 case opc_dload: {
   403                     ++i;
   404                     final int indx = wide ? readUShort(byteCodes, i++)
   405                                           : readUByte(byteCodes, i);
   406                     wide = false;
   407                     emit(out, "var @1 = @2;",
   408                          smapper.pushD(), lmapper.getD(indx));
   409                     break;
   410                 }
   411                 case opc_aload: {
   412                     ++i;
   413                     final int indx = wide ? readUShort(byteCodes, i++)
   414                                           : readUByte(byteCodes, i);
   415                     wide = false;
   416                     emit(out, "var @1 = @2;",
   417                          smapper.pushA(), lmapper.getA(indx));
   418                     break;
   419                 }
   420                 case opc_istore: {
   421                     ++i;
   422                     final int indx = wide ? readUShort(byteCodes, i++)
   423                                           : readUByte(byteCodes, i);
   424                     wide = false;
   425                     emit(out, "var @1 = @2;",
   426                          lmapper.setI(indx), smapper.popI());
   427                     break;
   428                 }
   429                 case opc_lstore: {
   430                     ++i;
   431                     final int indx = wide ? readUShort(byteCodes, i++)
   432                                           : readUByte(byteCodes, i);
   433                     wide = false;
   434                     emit(out, "var @1 = @2;",
   435                          lmapper.setL(indx), smapper.popL());
   436                     break;
   437                 }
   438                 case opc_fstore: {
   439                     ++i;
   440                     final int indx = wide ? readUShort(byteCodes, i++)
   441                                           : readUByte(byteCodes, i);
   442                     wide = false;
   443                     emit(out, "var @1 = @2;",
   444                          lmapper.setF(indx), smapper.popF());
   445                     break;
   446                 }
   447                 case opc_dstore: {
   448                     ++i;
   449                     final int indx = wide ? readUShort(byteCodes, i++)
   450                                           : readUByte(byteCodes, i);
   451                     wide = false;
   452                     emit(out, "var @1 = @2;",
   453                          lmapper.setD(indx), smapper.popD());
   454                     break;
   455                 }
   456                 case opc_astore: {
   457                     ++i;
   458                     final int indx = wide ? readUShort(byteCodes, i++)
   459                                           : readUByte(byteCodes, i);
   460                     wide = false;
   461                     emit(out, "var @1 = @2;",
   462                          lmapper.setA(indx), smapper.popA());
   463                     break;
   464                 }
   465                 case opc_astore_0:
   466                     emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
   467                     break;
   468                 case opc_istore_0:
   469                     emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
   470                     break;
   471                 case opc_lstore_0:
   472                     emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
   473                     break;
   474                 case opc_fstore_0:
   475                     emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
   476                     break;
   477                 case opc_dstore_0:
   478                     emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
   479                     break;
   480                 case opc_astore_1:
   481                     emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
   482                     break;
   483                 case opc_istore_1:
   484                     emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
   485                     break;
   486                 case opc_lstore_1:
   487                     emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
   488                     break;
   489                 case opc_fstore_1:
   490                     emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
   491                     break;
   492                 case opc_dstore_1:
   493                     emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
   494                     break;
   495                 case opc_astore_2:
   496                     emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
   497                     break;
   498                 case opc_istore_2:
   499                     emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
   500                     break;
   501                 case opc_lstore_2:
   502                     emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
   503                     break;
   504                 case opc_fstore_2:
   505                     emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
   506                     break;
   507                 case opc_dstore_2:
   508                     emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
   509                     break;
   510                 case opc_astore_3:
   511                     emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
   512                     break;
   513                 case opc_istore_3:
   514                     emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
   515                     break;
   516                 case opc_lstore_3:
   517                     emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
   518                     break;
   519                 case opc_fstore_3:
   520                     emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
   521                     break;
   522                 case opc_dstore_3:
   523                     emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
   524                     break;
   525                 case opc_iadd:
   526                     emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
   527                     break;
   528                 case opc_ladd:
   529                     emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
   530                     break;
   531                 case opc_fadd:
   532                     emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
   533                     break;
   534                 case opc_dadd:
   535                     emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
   536                     break;
   537                 case opc_isub:
   538                     emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
   539                     break;
   540                 case opc_lsub:
   541                     emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
   542                     break;
   543                 case opc_fsub:
   544                     emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
   545                     break;
   546                 case opc_dsub:
   547                     emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
   548                     break;
   549                 case opc_imul:
   550                     emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
   551                     break;
   552                 case opc_lmul:
   553                     emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
   554                     break;
   555                 case opc_fmul:
   556                     emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
   557                     break;
   558                 case opc_dmul:
   559                     emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
   560                     break;
   561                 case opc_idiv:
   562                     emit(out, "@1 = @1.div32(@2);",
   563                          smapper.getI(1), smapper.popI());
   564                     break;
   565                 case opc_ldiv:
   566                     emit(out, "@1 = @1.div64(@2);",
   567                          smapper.getL(1), smapper.popL());
   568                     break;
   569                 case opc_fdiv:
   570                     emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
   571                     break;
   572                 case opc_ddiv:
   573                     emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
   574                     break;
   575                 case opc_irem:
   576                     emit(out, "@1 = @1.mod32(@2);",
   577                          smapper.getI(1), smapper.popI());
   578                     break;
   579                 case opc_lrem:
   580                     emit(out, "@1 = @1.mod64(@2);",
   581                          smapper.getL(1), smapper.popL());
   582                     break;
   583                 case opc_frem:
   584                     emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
   585                     break;
   586                 case opc_drem:
   587                     emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
   588                     break;
   589                 case opc_iand:
   590                     emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
   591                     break;
   592                 case opc_land:
   593                     emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
   594                     break;
   595                 case opc_ior:
   596                     emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
   597                     break;
   598                 case opc_lor:
   599                     emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
   600                     break;
   601                 case opc_ixor:
   602                     emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
   603                     break;
   604                 case opc_lxor:
   605                     emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
   606                     break;
   607                 case opc_ineg:
   608                     emit(out, "@1 = @1.neg32();", smapper.getI(0));
   609                     break;
   610                 case opc_lneg:
   611                     emit(out, "@1 = @1.neg64();", smapper.getL(0));
   612                     break;
   613                 case opc_fneg:
   614                     emit(out, "@1 = -@1;", smapper.getF(0));
   615                     break;
   616                 case opc_dneg:
   617                     emit(out, "@1 = -@1;", smapper.getD(0));
   618                     break;
   619                 case opc_ishl:
   620                     emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
   621                     break;
   622                 case opc_lshl:
   623                     emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
   624                     break;
   625                 case opc_ishr:
   626                     emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
   627                     break;
   628                 case opc_lshr:
   629                     emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
   630                     break;
   631                 case opc_iushr:
   632                     emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
   633                     break;
   634                 case opc_lushr:
   635                     emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
   636                     break;
   637                 case opc_iinc: {
   638                     ++i;
   639                     final int varIndx = wide ? readUShort(byteCodes, i++)
   640                                              : readUByte(byteCodes, i);
   641                     ++i;
   642                     final int incrBy = wide ? readShort(byteCodes, i++)
   643                                             : byteCodes[i];
   644                     wide = false;
   645                     if (incrBy == 1) {
   646                         emit(out, "@1++;", lmapper.getI(varIndx));
   647                     } else {
   648                         emit(out, "@1 += @2;",
   649                              lmapper.getI(varIndx),
   650                              Integer.toString(incrBy));
   651                     }
   652                     break;
   653                 }
   654                 case opc_return:
   655                     emit(out, "return;");
   656                     break;
   657                 case opc_ireturn:
   658                     emit(out, "return @1;", smapper.popI());
   659                     break;
   660                 case opc_lreturn:
   661                     emit(out, "return @1;", smapper.popL());
   662                     break;
   663                 case opc_freturn:
   664                     emit(out, "return @1;", smapper.popF());
   665                     break;
   666                 case opc_dreturn:
   667                     emit(out, "return @1;", smapper.popD());
   668                     break;
   669                 case opc_areturn:
   670                     emit(out, "return @1;", smapper.popA());
   671                     break;
   672                 case opc_i2l:
   673                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
   674                     break;
   675                 case opc_i2f:
   676                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
   677                     break;
   678                 case opc_i2d:
   679                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
   680                     break;
   681                 case opc_l2i:
   682                     emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
   683                     break;
   684                     // max int check?
   685                 case opc_l2f:
   686                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
   687                     break;
   688                 case opc_l2d:
   689                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
   690                     break;
   691                 case opc_f2d:
   692                     emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
   693                     break;
   694                 case opc_d2f:
   695                     emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
   696                     break;
   697                 case opc_f2i:
   698                     emit(out, "var @2 = Math.floor(@1).toInt32();",
   699                          smapper.popF(), smapper.pushI());
   700                     break;
   701                 case opc_f2l:
   702                     emit(out, "var @2 = Math.floor(@1).toLong();",
   703                          smapper.popF(), smapper.pushL());
   704                     break;
   705                 case opc_d2i:
   706                     emit(out, "var @2 = Math.floor(@1).toInt32();",
   707                          smapper.popD(), smapper.pushI());
   708                     break;
   709                 case opc_d2l:
   710                     emit(out, "var @2 = Math.floor(@1).toLong();",
   711                          smapper.popD(), smapper.pushL());
   712                     break;
   713                 case opc_i2b:
   714                     emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
   715                     break;
   716                 case opc_i2c:
   717                     out.append("{ /* number conversion */ }");
   718                     break;
   719                 case opc_i2s:
   720                     emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
   721                     break;
   722                 case opc_aconst_null:
   723                     emit(out, "var @1 = null;", smapper.pushA());
   724                     break;
   725                 case opc_iconst_m1:
   726                     emit(out, "var @1 = -1;", smapper.pushI());
   727                     break;
   728                 case opc_iconst_0:
   729                     emit(out, "var @1 = 0;", smapper.pushI());
   730                     break;
   731                 case opc_dconst_0:
   732                     emit(out, "var @1 = 0;", smapper.pushD());
   733                     break;
   734                 case opc_lconst_0:
   735                     emit(out, "var @1 = 0;", smapper.pushL());
   736                     break;
   737                 case opc_fconst_0:
   738                     emit(out, "var @1 = 0;", smapper.pushF());
   739                     break;
   740                 case opc_iconst_1:
   741                     emit(out, "var @1 = 1;", smapper.pushI());
   742                     break;
   743                 case opc_lconst_1:
   744                     emit(out, "var @1 = 1;", smapper.pushL());
   745                     break;
   746                 case opc_fconst_1:
   747                     emit(out, "var @1 = 1;", smapper.pushF());
   748                     break;
   749                 case opc_dconst_1:
   750                     emit(out, "var @1 = 1;", smapper.pushD());
   751                     break;
   752                 case opc_iconst_2:
   753                     emit(out, "var @1 = 2;", smapper.pushI());
   754                     break;
   755                 case opc_fconst_2:
   756                     emit(out, "var @1 = 2;", smapper.pushF());
   757                     break;
   758                 case opc_iconst_3:
   759                     emit(out, "var @1 = 3;", smapper.pushI());
   760                     break;
   761                 case opc_iconst_4:
   762                     emit(out, "var @1 = 4;", smapper.pushI());
   763                     break;
   764                 case opc_iconst_5:
   765                     emit(out, "var @1 = 5;", smapper.pushI());
   766                     break;
   767                 case opc_ldc: {
   768                     int indx = readUByte(byteCodes, ++i);
   769                     String v = encodeConstant(indx);
   770                     int type = VarType.fromConstantType(jc.getTag(indx));
   771                     emit(out, "var @1 = @2;", smapper.pushT(type), v);
   772                     break;
   773                 }
   774                 case opc_ldc_w:
   775                 case opc_ldc2_w: {
   776                     int indx = readIntArg(byteCodes, i);
   777                     i += 2;
   778                     String v = encodeConstant(indx);
   779                     int type = VarType.fromConstantType(jc.getTag(indx));
   780                     if (type == VarType.LONG) {
   781                         final Long lv = new Long(v);
   782                         final int low = (int)(lv.longValue() & 0xFFFFFFFF);
   783                         final int hi = (int)(lv.longValue() >> 32);
   784                         emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(), 
   785                                 Integer.toHexString(low), Integer.toHexString(hi));
   786                     } else {
   787                         emit(out, "var @1 = @2;", smapper.pushT(type), v);
   788                     }
   789                     break;
   790                 }
   791                 case opc_lcmp:
   792                     emit(out, "var @3 = @2.compare64(@1);",
   793                          smapper.popL(), smapper.popL(), smapper.pushI());
   794                     break;
   795                 case opc_fcmpl:
   796                 case opc_fcmpg:
   797                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   798                          smapper.popF(), smapper.popF(), smapper.pushI());
   799                     break;
   800                 case opc_dcmpl:
   801                 case opc_dcmpg:
   802                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   803                          smapper.popD(), smapper.popD(), smapper.pushI());
   804                     break;
   805                 case opc_if_acmpeq:
   806                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   807                                    "===");
   808                     break;
   809                 case opc_if_acmpne:
   810                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   811                                    "!=");
   812                     break;
   813                 case opc_if_icmpeq:
   814                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   815                                    "==");
   816                     break;
   817                 case opc_ifeq: {
   818                     int indx = i + readIntArg(byteCodes, i);
   819                     emit(out, "if (@1 == 0) { gt = @2; continue; }",
   820                          smapper.popI(), Integer.toString(indx));
   821                     i += 2;
   822                     break;
   823                 }
   824                 case opc_ifne: {
   825                     int indx = i + readIntArg(byteCodes, i);
   826                     emit(out, "if (@1 != 0) { gt = @2; continue; }",
   827                          smapper.popI(), Integer.toString(indx));
   828                     i += 2;
   829                     break;
   830                 }
   831                 case opc_iflt: {
   832                     int indx = i + readIntArg(byteCodes, i);
   833                     emit(out, "if (@1 < 0) { gt = @2; continue; }",
   834                          smapper.popI(), Integer.toString(indx));
   835                     i += 2;
   836                     break;
   837                 }
   838                 case opc_ifle: {
   839                     int indx = i + readIntArg(byteCodes, i);
   840                     emit(out, "if (@1 <= 0) { gt = @2; continue; }",
   841                          smapper.popI(), Integer.toString(indx));
   842                     i += 2;
   843                     break;
   844                 }
   845                 case opc_ifgt: {
   846                     int indx = i + readIntArg(byteCodes, i);
   847                     emit(out, "if (@1 > 0) { gt = @2; continue; }",
   848                          smapper.popI(), Integer.toString(indx));
   849                     i += 2;
   850                     break;
   851                 }
   852                 case opc_ifge: {
   853                     int indx = i + readIntArg(byteCodes, i);
   854                     emit(out, "if (@1 >= 0) { gt = @2; continue; }",
   855                          smapper.popI(), Integer.toString(indx));
   856                     i += 2;
   857                     break;
   858                 }
   859                 case opc_ifnonnull: {
   860                     int indx = i + readIntArg(byteCodes, i);
   861                     emit(out, "if (@1 !== null) { gt = @2; continue; }",
   862                          smapper.popA(), Integer.toString(indx));
   863                     i += 2;
   864                     break;
   865                 }
   866                 case opc_ifnull: {
   867                     int indx = i + readIntArg(byteCodes, i);
   868                     emit(out, "if (@1 === null) { gt = @2; continue; }",
   869                          smapper.popA(), Integer.toString(indx));
   870                     i += 2;
   871                     break;
   872                 }
   873                 case opc_if_icmpne:
   874                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   875                                    "!=");
   876                     break;
   877                 case opc_if_icmplt:
   878                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   879                                    "<");
   880                     break;
   881                 case opc_if_icmple:
   882                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   883                                    "<=");
   884                     break;
   885                 case opc_if_icmpgt:
   886                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   887                                    ">");
   888                     break;
   889                 case opc_if_icmpge:
   890                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   891                                    ">=");
   892                     break;
   893                 case opc_goto: {
   894                     int indx = i + readIntArg(byteCodes, i);
   895                     emit(out, "gt = @1; continue;", Integer.toString(indx));
   896                     i += 2;
   897                     break;
   898                 }
   899                 case opc_lookupswitch: {
   900                     int table = i / 4 * 4 + 4;
   901                     int dflt = i + readInt4(byteCodes, table);
   902                     table += 4;
   903                     int n = readInt4(byteCodes, table);
   904                     table += 4;
   905                     out.append("switch (").append(smapper.popI()).append(") {\n");
   906                     while (n-- > 0) {
   907                         int cnstnt = readInt4(byteCodes, table);
   908                         table += 4;
   909                         int offset = i + readInt4(byteCodes, table);
   910                         table += 4;
   911                         out.append("  case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
   912                     }
   913                     out.append("  default: gt = " + dflt).append("; continue;\n}");
   914                     i = table - 1;
   915                     break;
   916                 }
   917                 case opc_tableswitch: {
   918                     int table = i / 4 * 4 + 4;
   919                     int dflt = i + readInt4(byteCodes, table);
   920                     table += 4;
   921                     int low = readInt4(byteCodes, table);
   922                     table += 4;
   923                     int high = readInt4(byteCodes, table);
   924                     table += 4;
   925                     out.append("switch (").append(smapper.popI()).append(") {\n");
   926                     while (low <= high) {
   927                         int offset = i + readInt4(byteCodes, table);
   928                         table += 4;
   929                         out.append("  case " + low).append(": gt = " + offset).append("; continue;\n");
   930                         low++;
   931                     }
   932                     out.append("  default: gt = " + dflt).append("; continue;\n}");
   933                     i = table - 1;
   934                     break;
   935                 }
   936                 case opc_invokeinterface: {
   937                     i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
   938                     break;
   939                 }
   940                 case opc_invokevirtual:
   941                     i = invokeVirtualMethod(byteCodes, i, smapper);
   942                     break;
   943                 case opc_invokespecial:
   944                     i = invokeStaticMethod(byteCodes, i, smapper, false);
   945                     break;
   946                 case opc_invokestatic:
   947                     i = invokeStaticMethod(byteCodes, i, smapper, true);
   948                     break;
   949                 case opc_new: {
   950                     int indx = readIntArg(byteCodes, i);
   951                     String ci = jc.getClassName(indx);
   952                     emit(out, "var @1 = new @2;",
   953                          smapper.pushA(), accessClass(ci.replace('/', '_')));
   954                     addReference(ci);
   955                     i += 2;
   956                     break;
   957                 }
   958                 case opc_newarray:
   959                     int atype = readUByte(byteCodes, ++i);
   960                     String jvmType;
   961                     switch (atype) {
   962                         case 4: jvmType = "[Z"; break;
   963                         case 5: jvmType = "[C"; break;
   964                         case 6: jvmType = "[F"; break;
   965                         case 7: jvmType = "[D"; break;
   966                         case 8: jvmType = "[B"; break;
   967                         case 9: jvmType = "[S"; break;
   968                         case 10: jvmType = "[I"; break;
   969                         case 11: jvmType = "[J"; break;
   970                         default: throw new IllegalStateException("Array type: " + atype);
   971                     }
   972                     emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
   973                          smapper.popI(), smapper.pushA(), jvmType);
   974                     break;
   975                 case opc_anewarray: {
   976                     int type = readIntArg(byteCodes, i);
   977                     i += 2;
   978                     String typeName = jc.getClassName(type);
   979                     if (typeName.startsWith("[")) {
   980                         typeName = "[" + typeName;
   981                     } else {
   982                         typeName = "[L" + typeName + ";";
   983                     }
   984                     emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
   985                          smapper.popI(), smapper.pushA(), typeName);
   986                     break;
   987                 }
   988                 case opc_multianewarray: {
   989                     int type = readIntArg(byteCodes, i);
   990                     i += 2;
   991                     String typeName = jc.getClassName(type);
   992                     int dim = readUByte(byteCodes, ++i);
   993                     StringBuilder dims = new StringBuilder();
   994                     dims.append('[');
   995                     for (int d = 0; d < dim; d++) {
   996                         if (d != 0) {
   997                             dims.append(",");
   998                         }
   999                         dims.append(smapper.popI());
  1000                     }
  1001                     dims.append(']');
  1002                     emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
  1003                          dims.toString(), smapper.pushA(), typeName);
  1004                     break;
  1005                 }
  1006                 case opc_arraylength:
  1007                     emit(out, "var @2 = @1.length;",
  1008                          smapper.popA(), smapper.pushI());
  1009                     break;
  1010                 case opc_lastore:
  1011                     emit(out, "@3.at(@2, @1);",
  1012                          smapper.popL(), smapper.popI(), smapper.popA());
  1013                     break;
  1014                 case opc_fastore:
  1015                     emit(out, "@3.at(@2, @1);",
  1016                          smapper.popF(), smapper.popI(), smapper.popA());
  1017                     break;
  1018                 case opc_dastore:
  1019                     emit(out, "@3.at(@2, @1);",
  1020                          smapper.popD(), smapper.popI(), smapper.popA());
  1021                     break;
  1022                 case opc_aastore:
  1023                     emit(out, "@3.at(@2, @1);",
  1024                          smapper.popA(), smapper.popI(), smapper.popA());
  1025                     break;
  1026                 case opc_iastore:
  1027                 case opc_bastore:
  1028                 case opc_castore:
  1029                 case opc_sastore:
  1030                     emit(out, "@3.at(@2, @1);",
  1031                          smapper.popI(), smapper.popI(), smapper.popA());
  1032                     break;
  1033                 case opc_laload:
  1034                     emit(out, "var @3 = @2.at(@1);",
  1035                          smapper.popI(), smapper.popA(), smapper.pushL());
  1036                     break;
  1037                 case opc_faload:
  1038                     emit(out, "var @3 = @2.at(@1);",
  1039                          smapper.popI(), smapper.popA(), smapper.pushF());
  1040                     break;
  1041                 case opc_daload:
  1042                     emit(out, "var @3 = @2.at(@1);",
  1043                          smapper.popI(), smapper.popA(), smapper.pushD());
  1044                     break;
  1045                 case opc_aaload:
  1046                     emit(out, "var @3 = @2.at(@1);",
  1047                          smapper.popI(), smapper.popA(), smapper.pushA());
  1048                     break;
  1049                 case opc_iaload:
  1050                 case opc_baload:
  1051                 case opc_caload:
  1052                 case opc_saload:
  1053                     emit(out, "var @3 = @2.at(@1);",
  1054                          smapper.popI(), smapper.popA(), smapper.pushI());
  1055                     break;
  1056                 case opc_pop:
  1057                 case opc_pop2:
  1058                     smapper.pop(1);
  1059                     debug("/* pop */");
  1060                     break;
  1061                 case opc_dup: {
  1062                     final Variable v = smapper.get(0);
  1063                     emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
  1064                     break;
  1065                 }
  1066                 case opc_dup2: {
  1067                     final Variable vi1 = smapper.get(0);
  1068 
  1069                     if (vi1.isCategory2()) {
  1070                         emit(out, "var @1 = @2;",
  1071                              smapper.pushT(vi1.getType()), vi1);
  1072                     } else {
  1073                         final Variable vi2 = smapper.get(1);
  1074                         emit(out, "var @1 = @2, @3 = @4;",
  1075                              smapper.pushT(vi2.getType()), vi2,
  1076                              smapper.pushT(vi1.getType()), vi1);
  1077                     }
  1078                     break;
  1079                 }
  1080                 case opc_dup_x1: {
  1081                     final Variable vi1 = smapper.pop();
  1082                     final Variable vi2 = smapper.pop();
  1083                     final Variable vo3 = smapper.pushT(vi1.getType());
  1084                     final Variable vo2 = smapper.pushT(vi2.getType());
  1085                     final Variable vo1 = smapper.pushT(vi1.getType());
  1086 
  1087                     emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1088                          vo1, vi1, vo2, vi2, vo3, vo1);
  1089                     break;
  1090                 }
  1091                 case opc_dup2_x1: {
  1092                     final Variable vi1 = smapper.pop();
  1093                     final Variable vi2 = smapper.pop();
  1094 
  1095                     if (vi1.isCategory2()) {
  1096                         final Variable vo3 = smapper.pushT(vi1.getType());
  1097                         final Variable vo2 = smapper.pushT(vi2.getType());
  1098                         final Variable vo1 = smapper.pushT(vi1.getType());
  1099 
  1100                         emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1101                              vo1, vi1, vo2, vi2, vo3, vo1);
  1102                     } else {
  1103                         final Variable vi3 = smapper.pop();
  1104                         final Variable vo5 = smapper.pushT(vi2.getType());
  1105                         final Variable vo4 = smapper.pushT(vi1.getType());
  1106                         final Variable vo3 = smapper.pushT(vi3.getType());
  1107                         final Variable vo2 = smapper.pushT(vi2.getType());
  1108                         final Variable vo1 = smapper.pushT(vi1.getType());
  1109 
  1110                         emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1111                              vo1, vi1, vo2, vi2, vo3, vi3);
  1112                         emit(out, " @1 = @2, @3 = @4;",
  1113                              vo4, vo1, vo5, vo2);
  1114                     }
  1115                     break;
  1116                 }
  1117                 case opc_dup_x2: {
  1118                     final Variable vi1 = smapper.pop();
  1119                     final Variable vi2 = smapper.pop();
  1120 
  1121                     if (vi2.isCategory2()) {
  1122                         final Variable vo3 = smapper.pushT(vi1.getType());
  1123                         final Variable vo2 = smapper.pushT(vi2.getType());
  1124                         final Variable vo1 = smapper.pushT(vi1.getType());
  1125 
  1126                         emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1127                              vo1, vi1, vo2, vi2, vo3, vo1);
  1128                     } else {
  1129                         final Variable vi3 = smapper.pop();
  1130                         final Variable vo4 = smapper.pushT(vi1.getType());
  1131                         final Variable vo3 = smapper.pushT(vi3.getType());
  1132                         final Variable vo2 = smapper.pushT(vi2.getType());
  1133                         final Variable vo1 = smapper.pushT(vi1.getType());
  1134 
  1135                         emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1136                              vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1137                     }
  1138                     break;
  1139                 }
  1140                 case opc_dup2_x2: {
  1141                     final Variable vi1 = smapper.pop();
  1142                     final Variable vi2 = smapper.pop();
  1143 
  1144                     if (vi1.isCategory2()) {
  1145                         if (vi2.isCategory2()) {
  1146                             final Variable vo3 = smapper.pushT(vi1.getType());
  1147                             final Variable vo2 = smapper.pushT(vi2.getType());
  1148                             final Variable vo1 = smapper.pushT(vi1.getType());
  1149 
  1150                             emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1151                                  vo1, vi1, vo2, vi2, vo3, vo1);
  1152                         } else {
  1153                             final Variable vi3 = smapper.pop();
  1154                             final Variable vo4 = smapper.pushT(vi1.getType());
  1155                             final Variable vo3 = smapper.pushT(vi3.getType());
  1156                             final Variable vo2 = smapper.pushT(vi2.getType());
  1157                             final Variable vo1 = smapper.pushT(vi1.getType());
  1158 
  1159                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1160                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1161                         }
  1162                     } else {
  1163                         final Variable vi3 = smapper.pop();
  1164 
  1165                         if (vi3.isCategory2()) {
  1166                             final Variable vo5 = smapper.pushT(vi2.getType());
  1167                             final Variable vo4 = smapper.pushT(vi1.getType());
  1168                             final Variable vo3 = smapper.pushT(vi3.getType());
  1169                             final Variable vo2 = smapper.pushT(vi2.getType());
  1170                             final Variable vo1 = smapper.pushT(vi1.getType());
  1171 
  1172                             emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1173                                  vo1, vi1, vo2, vi2, vo3, vi3);
  1174                             emit(out, " @1 = @2, @3 = @4;",
  1175                                  vo4, vo1, vo5, vo2);
  1176                         } else {
  1177                             final Variable vi4 = smapper.pop();
  1178                             final Variable vo6 = smapper.pushT(vi2.getType());
  1179                             final Variable vo5 = smapper.pushT(vi1.getType());
  1180                             final Variable vo4 = smapper.pushT(vi4.getType());
  1181                             final Variable vo3 = smapper.pushT(vi3.getType());
  1182                             final Variable vo2 = smapper.pushT(vi2.getType());
  1183                             final Variable vo1 = smapper.pushT(vi1.getType());
  1184                             
  1185                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
  1186                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
  1187                             emit(out, " @1 = @2, @3 = @4;",
  1188                                  vo5, vo1, vo6, vo2);
  1189                         }
  1190                     }
  1191                     break;
  1192                 }
  1193                 case opc_swap: {
  1194                     final Variable vi1 = smapper.get(0);
  1195                     final Variable vi2 = smapper.get(1);
  1196 
  1197                     if (vi1.getType() == vi2.getType()) {
  1198                         final Variable tmp = smapper.pushT(vi1.getType());
  1199 
  1200                         emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
  1201                              tmp, vi1, vi2);
  1202                         smapper.pop(1);
  1203                     } else {
  1204                         smapper.pop(2);
  1205                         smapper.pushT(vi1.getType());
  1206                         smapper.pushT(vi2.getType());
  1207                     }
  1208                     break;
  1209                 }
  1210                 case opc_bipush:
  1211                     emit(out, "var @1 = @2;",
  1212                          smapper.pushI(), Integer.toString(byteCodes[++i]));
  1213                     break;
  1214                 case opc_sipush:
  1215                     emit(out, "var @1 = @2;",
  1216                          smapper.pushI(),
  1217                          Integer.toString(readIntArg(byteCodes, i)));
  1218                     i += 2;
  1219                     break;
  1220                 case opc_getfield: {
  1221                     int indx = readIntArg(byteCodes, i);
  1222                     String[] fi = jc.getFieldInfoName(indx);
  1223                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1224                     final String mangleClass = mangleSig(fi[0]);
  1225                     final String mangleClassAccess = accessClass(mangleClass);
  1226                     emit(out, "var @2 = @4(false)._@3.call(@1);",
  1227                          smapper.popA(),
  1228                          smapper.pushT(type), fi[1], mangleClassAccess
  1229                     );
  1230                     i += 2;
  1231                     break;
  1232                 }
  1233                 case opc_putfield: {
  1234                     int indx = readIntArg(byteCodes, i);
  1235                     String[] fi = jc.getFieldInfoName(indx);
  1236                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1237                     final String mangleClass = mangleSig(fi[0]);
  1238                     final String mangleClassAccess = accessClass(mangleClass);
  1239                     emit(out, "@4(false)._@3.call(@2, @1);",
  1240                          smapper.popT(type),
  1241                          smapper.popA(), fi[1], 
  1242                          mangleClassAccess
  1243                     );
  1244                     i += 2;
  1245                     break;
  1246                 }
  1247                 case opc_getstatic: {
  1248                     int indx = readIntArg(byteCodes, i);
  1249                     String[] fi = jc.getFieldInfoName(indx);
  1250                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1251                     emit(out, "var @1 = @2(false)._@3();",
  1252                          smapper.pushT(type),
  1253                          accessClass(fi[0].replace('/', '_')), fi[1]);
  1254                     i += 2;
  1255                     addReference(fi[0]);
  1256                     break;
  1257                 }
  1258                 case opc_putstatic: {
  1259                     int indx = readIntArg(byteCodes, i);
  1260                     String[] fi = jc.getFieldInfoName(indx);
  1261                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1262                     emit(out, "@1(false)._@2(@3);",
  1263                          accessClass(fi[0].replace('/', '_')), fi[1],
  1264                          smapper.popT(type));
  1265                     i += 2;
  1266                     addReference(fi[0]);
  1267                     break;
  1268                 }
  1269                 case opc_checkcast: {
  1270                     int indx = readIntArg(byteCodes, i);
  1271                     final String type = jc.getClassName(indx);
  1272                     if (!type.startsWith("[")) {
  1273                         emit(out,
  1274                              "if (@1 !== null && !@1.$instOf_@2) throw {};",
  1275                              smapper.getA(0), type.replace('/', '_'));
  1276                     } else {
  1277                         emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
  1278                              smapper.getA(0), type
  1279                         );
  1280                     }
  1281                     i += 2;
  1282                     break;
  1283                 }
  1284                 case opc_instanceof: {
  1285                     int indx = readIntArg(byteCodes, i);
  1286                     final String type = jc.getClassName(indx);
  1287                     if (!type.startsWith("[")) {
  1288                         emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
  1289                              smapper.popA(), smapper.pushI(),
  1290                              type.replace('/', '_'));
  1291                     } else {
  1292                         emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
  1293                             smapper.popA(), smapper.pushI(),
  1294                             type
  1295                         );
  1296                     }
  1297                     i += 2;
  1298                     break;
  1299                 }
  1300                 case opc_athrow: {
  1301                     final Variable v = smapper.popA();
  1302                     smapper.clear();
  1303 
  1304                     emit(out, "{ var @1 = @2; throw @2; }",
  1305                          smapper.pushA(), v);
  1306                     break;
  1307                 }
  1308 
  1309                 case opc_monitorenter: {
  1310                     out.append("/* monitor enter */");
  1311                     smapper.popA();
  1312                     break;
  1313                 }
  1314 
  1315                 case opc_monitorexit: {
  1316                     out.append("/* monitor exit */");
  1317                     smapper.popA();
  1318                     break;
  1319                 }
  1320 
  1321                 case opc_wide:
  1322                     wide = true;
  1323                     break;
  1324 
  1325                 default: {
  1326                     wide = false;
  1327                     emit(out, "throw 'unknown bytecode @1';",
  1328                          Integer.toString(c));
  1329                 }
  1330             }
  1331             if (debug(" //")) {
  1332                 for (int j = prev; j <= i; j++) {
  1333                     out.append(" ");
  1334                     final int cc = readUByte(byteCodes, j);
  1335                     out.append(Integer.toString(cc));
  1336                 }
  1337             }
  1338             out.append("\n");            
  1339         }
  1340         if (previousTrap != null) {
  1341             generateCatch(previousTrap);
  1342         }
  1343         out.append("  }\n");
  1344         out.append("};");
  1345     }
  1346 
  1347     private int generateIf(byte[] byteCodes, int i,
  1348                            final Variable v2, final Variable v1,
  1349                            final String test) throws IOException {
  1350         int indx = i + readIntArg(byteCodes, i);
  1351         out.append("if (").append(v1)
  1352            .append(' ').append(test).append(' ')
  1353            .append(v2).append(") { gt = " + indx)
  1354            .append("; continue; }");
  1355         return i + 2;
  1356     }
  1357 
  1358     private int readIntArg(byte[] byteCodes, int offsetInstruction) {
  1359         final int indxHi = byteCodes[offsetInstruction + 1] << 8;
  1360         final int indxLo = byteCodes[offsetInstruction + 2];
  1361         return (indxHi & 0xffffff00) | (indxLo & 0xff);
  1362     }
  1363     private int readInt4(byte[] byteCodes, int offset) {
  1364         final int d = byteCodes[offset + 0] << 24;
  1365         final int c = byteCodes[offset + 1] << 16;
  1366         final int b = byteCodes[offset + 2] << 8;
  1367         final int a = byteCodes[offset + 3];
  1368         return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
  1369     }
  1370     private int readUByte(byte[] byteCodes, int offset) {
  1371         return byteCodes[offset] & 0xff;
  1372     }
  1373 
  1374     private int readUShort(byte[] byteCodes, int offset) {
  1375         return ((byteCodes[offset] & 0xff) << 8)
  1376                     | (byteCodes[offset + 1] & 0xff);
  1377     }
  1378 
  1379     private int readShort(byte[] byteCodes, int offset) {
  1380         return (byteCodes[offset] << 8)
  1381                     | (byteCodes[offset + 1] & 0xff);
  1382     }
  1383 
  1384     private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
  1385         int i = 0;
  1386         Boolean count = null;
  1387         boolean array = false;
  1388         sig.append("__");
  1389         int firstPos = sig.length();
  1390         while (i < descriptor.length()) {
  1391             char ch = descriptor.charAt(i++);
  1392             switch (ch) {
  1393                 case '(':
  1394                     count = true;
  1395                     continue;
  1396                 case ')':
  1397                     count = false;
  1398                     continue;
  1399                 case 'B': 
  1400                 case 'C': 
  1401                 case 'D': 
  1402                 case 'F': 
  1403                 case 'I': 
  1404                 case 'J': 
  1405                 case 'S': 
  1406                 case 'Z': 
  1407                     if (count) {
  1408                         if (array) {
  1409                             sig.append("_3");
  1410                         }
  1411                         sig.append(ch);
  1412                         if (ch == 'J' || ch == 'D') {
  1413                             cnt.append('1');
  1414                         } else {
  1415                             cnt.append('0');
  1416                         }
  1417                     } else {
  1418                         sig.insert(firstPos, ch);
  1419                         if (array) {
  1420                             returnType[0] = '[';
  1421                             sig.insert(firstPos, "_3");
  1422                         } else {
  1423                             returnType[0] = ch;
  1424                         }
  1425                     }
  1426                     array = false;
  1427                     continue;
  1428                 case 'V': 
  1429                     assert !count;
  1430                     returnType[0] = 'V';
  1431                     sig.insert(firstPos, 'V');
  1432                     continue;
  1433                 case 'L':
  1434                     int next = descriptor.indexOf(';', i);
  1435                     String realSig = mangleSig(descriptor, i - 1, next + 1);
  1436                     if (count) {
  1437                         if (array) {
  1438                             sig.append("_3");
  1439                         }
  1440                         sig.append(realSig);
  1441                         cnt.append('0');
  1442                     } else {
  1443                         sig.insert(firstPos, realSig);
  1444                         if (array) {
  1445                             sig.insert(firstPos, "_3");
  1446                         }
  1447                         returnType[0] = 'L';
  1448                     }
  1449                     i = next + 1;
  1450                     array = false;
  1451                     continue;
  1452                 case '[':
  1453                     array = true;
  1454                     continue;
  1455                 default:
  1456                     throw new IllegalStateException("Invalid char: " + ch);
  1457             }
  1458         }
  1459     }
  1460     
  1461     static String mangleSig(String sig) {
  1462         return mangleSig(sig, 0, sig.length());
  1463     }
  1464     
  1465     private static String mangleSig(String txt, int first, int last) {
  1466         StringBuilder sb = new StringBuilder();
  1467         for (int i = first; i < last; i++) {
  1468             final char ch = txt.charAt(i);
  1469             switch (ch) {
  1470                 case '/': sb.append('_'); break;
  1471                 case '_': sb.append("_1"); break;
  1472                 case ';': sb.append("_2"); break;
  1473                 case '[': sb.append("_3"); break;
  1474                 default: sb.append(ch); break;
  1475             }
  1476         }
  1477         return sb.toString();
  1478     }
  1479 
  1480     private static String findMethodName(MethodData m, StringBuilder cnt) {
  1481         StringBuilder name = new StringBuilder();
  1482         if ("<init>".equals(m.getName())) { // NOI18N
  1483             name.append("cons"); // NOI18N
  1484         } else if ("<clinit>".equals(m.getName())) { // NOI18N
  1485             name.append("class"); // NOI18N
  1486         } else {
  1487             name.append(m.getName());
  1488         } 
  1489         
  1490         countArgs(m.getInternalSig(), new char[1], name, cnt);
  1491         return name.toString();
  1492     }
  1493 
  1494     static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
  1495         StringBuilder name = new StringBuilder();
  1496         String descr = mi[2];//mi.getDescriptor();
  1497         String nm= mi[1];
  1498         if ("<init>".equals(nm)) { // NOI18N
  1499             name.append("cons"); // NOI18N
  1500         } else {
  1501             name.append(nm);
  1502         }
  1503         countArgs(descr, returnType, name, cnt);
  1504         return name.toString();
  1505     }
  1506 
  1507     private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
  1508     throws IOException {
  1509         int methodIndex = readIntArg(byteCodes, i);
  1510         String[] mi = jc.getFieldInfoName(methodIndex);
  1511         char[] returnType = { 'V' };
  1512         StringBuilder cnt = new StringBuilder();
  1513         String mn = findMethodName(mi, cnt, returnType);
  1514 
  1515         final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
  1516         final Variable[] vars = new Variable[numArguments];
  1517 
  1518         for (int j = numArguments - 1; j >= 0; --j) {
  1519             vars[j] = mapper.pop();
  1520         }
  1521 
  1522         if (returnType[0] != 'V') {
  1523             out.append("var ")
  1524                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1525                .append(" = ");
  1526         }
  1527 
  1528         final String in = mi[0];
  1529         out.append(accessClass(in.replace('/', '_')));
  1530         out.append("(false).");
  1531         if (mn.startsWith("cons_")) {
  1532             out.append("constructor.");
  1533         }
  1534         out.append(mn);
  1535         if (isStatic) {
  1536             out.append('(');
  1537         } else {
  1538             out.append(".call(");
  1539         }
  1540         if (numArguments > 0) {
  1541             out.append(vars[0]);
  1542             for (int j = 1; j < numArguments; ++j) {
  1543                 out.append(", ");
  1544                 out.append(vars[j]);
  1545             }
  1546         }
  1547         out.append(");");
  1548         i += 2;
  1549         addReference(in);
  1550         return i;
  1551     }
  1552     private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
  1553     throws IOException {
  1554         int methodIndex = readIntArg(byteCodes, i);
  1555         String[] mi = jc.getFieldInfoName(methodIndex);
  1556         char[] returnType = { 'V' };
  1557         StringBuilder cnt = new StringBuilder();
  1558         String mn = findMethodName(mi, cnt, returnType);
  1559 
  1560         final int numArguments = cnt.length() + 1;
  1561         final Variable[] vars = new Variable[numArguments];
  1562 
  1563         for (int j = numArguments - 1; j >= 0; --j) {
  1564             vars[j] = mapper.pop();
  1565         }
  1566 
  1567         if (returnType[0] != 'V') {
  1568             out.append("var ")
  1569                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1570                .append(" = ");
  1571         }
  1572 
  1573         out.append(vars[0]).append('.');
  1574         out.append(mn);
  1575         out.append('(');
  1576         String sep = "";
  1577         for (int j = 1; j < numArguments; ++j) {
  1578             out.append(sep);
  1579             out.append(vars[j]);
  1580             sep = ", ";
  1581         }
  1582         out.append(");");
  1583         i += 2;
  1584         return i;
  1585     }
  1586 
  1587     private void addReference(String cn) throws IOException {
  1588         if (requireReference(cn)) {
  1589             debug(" /* needs " + cn + " */");
  1590         }
  1591     }
  1592 
  1593     private void outType(String d, StringBuilder out) {
  1594         int arr = 0;
  1595         while (d.charAt(0) == '[') {
  1596             out.append('A');
  1597             d = d.substring(1);
  1598         }
  1599         if (d.charAt(0) == 'L') {
  1600             assert d.charAt(d.length() - 1) == ';';
  1601             out.append(d.replace('/', '_').substring(0, d.length() - 1));
  1602         } else {
  1603             out.append(d);
  1604         }
  1605     }
  1606 
  1607     private String encodeConstant(int entryIndex) throws IOException {
  1608         String[] classRef = { null };
  1609         String s = jc.stringValue(entryIndex, classRef);
  1610         if (classRef[0] != null) {
  1611             if (classRef[0].startsWith("[")) {
  1612                 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
  1613             } else {
  1614                 addReference(classRef[0]);
  1615                 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
  1616             }
  1617         }
  1618         return s;
  1619     }
  1620 
  1621     private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
  1622         byte[] arr = m.findAnnotationData(true);
  1623         if (arr == null) {
  1624             return null;
  1625         }
  1626         final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
  1627         class P extends AnnotationParser {
  1628             public P() {
  1629                 super(false, true);
  1630             }
  1631             
  1632             int cnt;
  1633             String[] args = new String[30];
  1634             String body;
  1635             
  1636             @Override
  1637             protected void visitAttr(String type, String attr, String at, String value) {
  1638                 if (type.equals(jvmType)) {
  1639                     if ("body".equals(attr)) {
  1640                         body = value;
  1641                     } else if ("args".equals(attr)) {
  1642                         args[cnt++] = value;
  1643                     } else {
  1644                         throw new IllegalArgumentException(attr);
  1645                     }
  1646                 }
  1647             }
  1648         }
  1649         P p = new P();
  1650         p.parse(arr, jc);
  1651         if (p.body == null) {
  1652             return null;
  1653         }
  1654         StringBuilder cnt = new StringBuilder();
  1655         final String mn = findMethodName(m, cnt);
  1656         out.append(prefix).append(mn);
  1657         out.append(" = function(");
  1658         String space = "";
  1659         int index = 0;
  1660         for (int i = 0; i < cnt.length(); i++) {
  1661             out.append(space);
  1662             space = outputArg(out, p.args, index);
  1663             index++;
  1664         }
  1665         out.append(") {").append("\n");
  1666         out.append(p.body);
  1667         out.append("\n}\n");
  1668         return mn;
  1669     }
  1670     private static String className(ClassData jc) {
  1671         //return jc.getName().getInternalName().replace('/', '_');
  1672         return jc.getClassName().replace('/', '_');
  1673     }
  1674     
  1675     private static String[] findAnnotation(
  1676         byte[] arr, ClassData cd, final String className, 
  1677         final String... attrNames
  1678     ) throws IOException {
  1679         if (arr == null) {
  1680             return null;
  1681         }
  1682         final String[] values = new String[attrNames.length];
  1683         final boolean[] found = { false };
  1684         final String jvmType = "L" + className.replace('.', '/') + ";";
  1685         AnnotationParser ap = new AnnotationParser(false, true) {
  1686             @Override
  1687             protected void visitAttr(String type, String attr, String at, String value) {
  1688                 if (type.equals(jvmType)) {
  1689                     found[0] = true;
  1690                     for (int i = 0; i < attrNames.length; i++) {
  1691                         if (attrNames[i].equals(attr)) {
  1692                             values[i] = value;
  1693                         }
  1694                     }
  1695                 }
  1696             }
  1697             
  1698         };
  1699         ap.parse(arr, cd);
  1700         return found[0] ? values : null;
  1701     }
  1702 
  1703     private CharSequence initField(FieldData v) {
  1704         final String is = v.getInternalSig();
  1705         if (is.length() == 1) {
  1706             switch (is.charAt(0)) {
  1707                 case 'S':
  1708                 case 'J':
  1709                 case 'B':
  1710                 case 'Z':
  1711                 case 'C':
  1712                 case 'I': return " = 0;";
  1713                 case 'F': 
  1714                 case 'D': return " = 0.0;";
  1715                 default:
  1716                     throw new IllegalStateException(is);
  1717             }
  1718         }
  1719         return " = null;";
  1720     }
  1721 
  1722     private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
  1723         AnnotationParser ap = new AnnotationParser(true, false) {
  1724             int[] cnt = new int[32];
  1725             int depth;
  1726             
  1727             @Override
  1728             protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
  1729                 final String slashType = attrType.substring(1, attrType.length() - 1);
  1730                 requireReference(slashType);
  1731                 
  1732                 if (cnt[depth]++ > 0) {
  1733                     out.append(",");
  1734                 }
  1735                 if (top) {
  1736                     out.append('"').append(attrType).append("\" : ");
  1737                 }
  1738                 out.append("{\n");
  1739                 cnt[++depth] = 0;
  1740             }
  1741 
  1742             @Override
  1743             protected void visitAnnotationEnd(String type, boolean top) throws IOException {
  1744                 out.append("\n}\n");
  1745                 depth--;
  1746             }
  1747 
  1748             @Override
  1749             protected void visitValueStart(String attrName, char type) throws IOException {
  1750                 if (cnt[depth]++ > 0) {
  1751                     out.append(",\n");
  1752                 }
  1753                 cnt[++depth] = 0;
  1754                 if (attrName != null) {
  1755                     out.append(attrName).append(" : ");
  1756                 }
  1757                 if (type == '[') {
  1758                     out.append("[");
  1759                 }
  1760             }
  1761 
  1762             @Override
  1763             protected void visitValueEnd(String attrName, char type) throws IOException {
  1764                 if (type == '[') {
  1765                     out.append("]");
  1766                 }
  1767                 depth--;
  1768             }
  1769             
  1770             @Override
  1771             protected void visitAttr(String type, String attr, String attrType, String value) 
  1772             throws IOException {
  1773                 if (attr == null && value == null) {
  1774                     return;
  1775                 }
  1776                 out.append(value);
  1777             }
  1778 
  1779             @Override
  1780             protected void visitEnumAttr(String type, String attr, String attrType, String value) 
  1781             throws IOException {
  1782                 final String slashType = attrType.substring(1, attrType.length() - 1);
  1783                 requireReference(slashType);
  1784                 
  1785                 out.append(accessClass(slashType.replace('/', '_')))
  1786                    .append("(false).constructor.").append(value);
  1787             }
  1788         };
  1789         ap.parse(data, cd);
  1790     }
  1791 
  1792     private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
  1793         final String name = args[indx];
  1794         if (name == null) {
  1795             return "";
  1796         }
  1797         if (name.contains(",")) {
  1798             throw new IOException("Wrong parameter with ',': " + name);
  1799         }
  1800         out.append(name);
  1801         return ",";
  1802     }
  1803 
  1804     private static void emit(final Appendable out,
  1805                              final String format,
  1806                              final CharSequence... params) throws IOException {
  1807         final int length = format.length();
  1808 
  1809         int processed = 0;
  1810         int paramOffset = format.indexOf('@');
  1811         while ((paramOffset != -1) && (paramOffset < (length - 1))) {
  1812             final char paramChar = format.charAt(paramOffset + 1);
  1813             if ((paramChar >= '1') && (paramChar <= '9')) {
  1814                 final int paramIndex = paramChar - '0' - 1;
  1815 
  1816                 out.append(format, processed, paramOffset);
  1817                 out.append(params[paramIndex]);
  1818 
  1819                 ++paramOffset;
  1820                 processed = paramOffset + 1;
  1821             }
  1822 
  1823             paramOffset = format.indexOf('@', paramOffset + 1);
  1824         }
  1825 
  1826         out.append(format, processed, length);
  1827     }
  1828 
  1829     private void generateCatch(TrapData[] traps) throws IOException {
  1830         out.append("} catch (e) {\n");
  1831         int finallyPC = -1;
  1832         for (TrapData e : traps) {
  1833             if (e == null) {
  1834                 break;
  1835             }
  1836             if (e.catch_cpx != 0) { //not finally
  1837                 final String classInternalName = jc.getClassName(e.catch_cpx);
  1838                 addReference(classInternalName);
  1839                 if ("java/lang/Throwable".equals(classInternalName)) {
  1840                     out.append("if (e.$instOf_java_lang_Throwable) {");
  1841                     out.append("  var stA0 = e;");
  1842                     out.append("} else {");
  1843                     out.append("  var stA0 = vm.java_lang_Throwable(true);");
  1844                     out.append("  vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
  1845                     out.append("}");
  1846                     out.append("gt=" + e.handler_pc + "; continue;");
  1847                 } else {
  1848                     out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
  1849                     out.append("gt=" + e.handler_pc + "; var stA0 = e; continue;");
  1850                     out.append("}\n");
  1851                 }
  1852             } else {
  1853                 finallyPC = e.handler_pc;
  1854             }
  1855         }
  1856         if (finallyPC == -1) {
  1857             out.append("throw e;");
  1858         } else {
  1859             out.append("gt=" + finallyPC + "; var stA0 = e; continue;");
  1860         }
  1861         out.append("\n}");
  1862     }
  1863 }