src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 27 Aug 2012 06:12:35 +0200
changeset 3 e44f0155d946
parent 2 4679bf342a1f
child 4 f352a33fb71b
permissions -rw-r--r--
Using floor when converting from double, float to int, long. Support for more than four parameters
     1 /*
     2 Java 4 Browser Bytecode Translator
     3 Copyright (C) 2012-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.java4browser;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.util.List;
    23 import static org.netbeans.modules.classfile.ByteCodes.*;
    24 import org.netbeans.modules.classfile.ClassFile;
    25 import org.netbeans.modules.classfile.Code;
    26 import org.netbeans.modules.classfile.Method;
    27 import org.netbeans.modules.classfile.Parameter;
    28 
    29 /** Translator of the code inside class files to JavaScript.
    30  *
    31  * @author Jaroslav Tulach <jtulach@netbeans.org>
    32  */
    33 public final class ByteCodeToJavaScript {
    34     private final ClassFile jc;
    35     private final Appendable out;
    36 
    37     private ByteCodeToJavaScript(ClassFile jc, Appendable out) {
    38         this.jc = jc;
    39         this.out = out;
    40     }
    41     
    42     /** Converts a given class file to a JavaScript version.
    43      * @param fileName the name of the file we are reading
    44      * @param classFile input stream with code of the .class file
    45      * @param out a {@link StringBuilder} or similar to generate the output to
    46      * @throws IOException if something goes wrong during read or write or translating
    47      */
    48     public static void compile(String fileName, InputStream classFile, Appendable out) throws IOException {
    49         ClassFile jc = new ClassFile(classFile, true);
    50         ByteCodeToJavaScript compiler = new ByteCodeToJavaScript(jc, out);
    51         for (Method m : jc.getMethods()) {
    52             if (m.isStatic()) {
    53                 compiler.generateStaticMethod(m);
    54             }
    55         }
    56     }
    57     private void generateStaticMethod(Method m) throws IOException {
    58         out.append("\nfunction ").append(
    59             jc.getName().getExternalName().replace('.', '_')
    60         ).append('_').append(
    61             m.getName()
    62         );
    63         out.append(m.getReturnType());
    64         List<Parameter> args = m.getParameters();
    65         for (Parameter t : args) {
    66             out.append(t.getDescriptor());
    67         }
    68         out.append('(');
    69         String space = "";
    70         for (int index = 0, i = 0; i < args.size(); i++) {
    71             out.append(space);
    72             out.append("arg").append(String.valueOf(index));
    73             space = ",";
    74             final String desc = args.get(i).getDescriptor();
    75             if ("D".equals(desc) || "J".equals(desc)) {
    76                 index += 2;
    77             } else {
    78                 index++;
    79             }
    80         }
    81         out.append(") {").append("\n  var ");
    82         final Code code = m.getCode();
    83         int len = code.getMaxLocals();
    84         space = "";
    85         for (int i = 0; i < len; i++) {
    86             out.append(space);
    87             out.append("var").append(String.valueOf(i));
    88             space = ",";
    89         }
    90         out.append(";\n  var stack = new Array(");
    91         out.append(Integer.toString(code.getMaxStack()));
    92         out.append(");\n");
    93         produceCode(code.getByteCodes());
    94         out.append("}");
    95     }
    96 
    97     private void produceCode(byte[] byteCodes) throws IOException {
    98         for (int i = 0; i < byteCodes.length; i++) {
    99             int prev = i;
   100             out.append("  ");
   101             final int c = (byteCodes[i] + 256) % 256;
   102             switch (c) {
   103                 case bc_aload_0:
   104                 case bc_iload_0:
   105                 case bc_lload_0:
   106                 case bc_fload_0:
   107                 case bc_dload_0:
   108                     out.append("stack.push(arg0);");
   109                     break;
   110                 case bc_aload_1:
   111                 case bc_iload_1:
   112                 case bc_lload_1:
   113                 case bc_fload_1:
   114                 case bc_dload_1:
   115                     out.append("stack.push(arg1);");
   116                     break;
   117                 case bc_aload_2:
   118                 case bc_iload_2:
   119                 case bc_lload_2:
   120                 case bc_fload_2:
   121                 case bc_dload_2:
   122                     out.append("stack.push(arg2);");
   123                     break;
   124                 case bc_aload_3:
   125                 case bc_iload_3:
   126                 case bc_lload_3:
   127                 case bc_fload_3:
   128                 case bc_dload_3:
   129                     out.append("stack.push(arg3);");
   130                     break;
   131                 case bc_iload:
   132                 case bc_lload:
   133                 case bc_fload:
   134                 case bc_dload:
   135                 case bc_aload: {
   136                     final int indx = (byteCodes[++i] + 256) % 256;
   137                     out.append("stack.push(arg").append(indx + ");");
   138                     break;
   139                 }
   140                 case bc_iadd:
   141                 case bc_ladd:
   142                 case bc_fadd:
   143                 case bc_dadd:
   144                     out.append("stack.push(stack.pop() + stack.pop());");
   145                     break;
   146                 case bc_isub:
   147                 case bc_lsub:
   148                 case bc_fsub:
   149                 case bc_dsub:
   150                     out.append("{ var tmp = stack.pop(); stack.push(stack.pop() - tmp); }");
   151                     break;
   152                 case bc_imul:
   153                 case bc_lmul:
   154                 case bc_fmul:
   155                 case bc_dmul:
   156                     out.append("stack.push(stack.pop() * stack.pop());");
   157                     break;
   158                 case bc_idiv:
   159                 case bc_ldiv:
   160                     out.append("{ var tmp = stack.pop(); stack.push(Math.floor(stack.pop() / tmp)); }");
   161                     break;
   162                 case bc_fdiv:
   163                 case bc_ddiv:
   164                     out.append("{ var tmp = stack.pop(); stack.push(stack.pop() / tmp); }");
   165                     break;
   166                 case bc_ireturn:
   167                 case bc_lreturn:
   168                 case bc_freturn:
   169                 case bc_dreturn:
   170                     out.append("return stack.pop();");
   171                     break;
   172                 case bc_i2l:
   173                 case bc_i2f:
   174                 case bc_i2d:
   175                 case bc_l2i:
   176                     // max int check?
   177                 case bc_l2f:
   178                 case bc_l2d:
   179                 case bc_f2d:
   180                 case bc_d2f:
   181                     out.append("/* number conversion */");
   182                     break;
   183                 case bc_f2i:
   184                 case bc_f2l:
   185                 case bc_d2i:
   186                 case bc_d2l:
   187                     out.append("stack.push(Math.floor(stack.pop()));");
   188                     break;
   189                 case bc_i2b:
   190                 case bc_i2c:
   191                 case bc_i2s:
   192                     out.append("/* number conversion */");
   193                     break;
   194             }
   195             out.append(" /*");
   196             for (int j = prev; j <= i; j++) {
   197                 out.append(" ");
   198                 final int cc = (byteCodes[j] + 256) % 256;
   199                 out.append(Integer.toString(cc));
   200             }
   201             out.append("*/\n");
   202         }
   203     }
   204 }