src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 27 Aug 2012 05:17:08 +0200
changeset 1 48b1dce93691
parent 0 71aab30ab2b7
child 2 4679bf342a1f
permissions -rw-r--r--
Handle float multiplication
     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 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 i = 0; i < args.size(); i++) {
    71             out.append(space);
    72             out.append("arg").append(String.valueOf(i));
    73             space = ",";
    74         }
    75         out.append(") {").append("\n  var ");
    76         final Code code = m.getCode();
    77         int len = code.getMaxLocals();
    78         space = "";
    79         for (int i = 0; i < len; i++) {
    80             out.append(space);
    81             out.append("var").append(String.valueOf(i));
    82             space = ",";
    83         }
    84         out.append(";\n  var stack = new Array(");
    85         out.append(Integer.toString(code.getMaxStack()));
    86         out.append(");\n");
    87         produceCode(code.getByteCodes());
    88         out.append("}");
    89     }
    90 
    91     private void produceCode(byte[] byteCodes) throws IOException {
    92         for (int i = 0; i < byteCodes.length; i++) {
    93             int prev = i;
    94             out.append("  ");
    95             final int c = (byteCodes[i] + 256) % 256;
    96             switch (c) {
    97                 case ByteCodes.bc_aload_0:
    98                 case ByteCodes.bc_iload_0:
    99                 case ByteCodes.bc_lload_0:
   100                 case ByteCodes.bc_fload_0:
   101                 case ByteCodes.bc_dload_0:
   102                     out.append("stack.push(arg0);");
   103                     break;
   104                 case ByteCodes.bc_aload_1:
   105                 case ByteCodes.bc_iload_1:
   106                 case ByteCodes.bc_lload_1:
   107                 case ByteCodes.bc_fload_1:
   108                 case ByteCodes.bc_dload_1:
   109                     out.append("stack.push(arg1);");
   110                     break;
   111                 case ByteCodes.bc_iadd:
   112                 case ByteCodes.bc_ladd:
   113                 case ByteCodes.bc_fadd:
   114                 case ByteCodes.bc_dadd:
   115                     out.append("stack.push(stack.pop() + stack.pop());");
   116                     break;
   117                 case ByteCodes.bc_imul:
   118                 case ByteCodes.bc_lmul:
   119                 case ByteCodes.bc_fmul:
   120                 case ByteCodes.bc_dmul:
   121                     out.append("stack.push(stack.pop() * stack.pop());");
   122                     break;
   123                 case ByteCodes.bc_ireturn:
   124                 case ByteCodes.bc_lreturn:
   125                 case ByteCodes.bc_freturn:
   126                 case ByteCodes.bc_dreturn:
   127                     out.append("return stack.pop();");
   128                     break;
   129             }
   130             out.append("/*");
   131             for (int j = prev; j <= i; j++) {
   132                 out.append(" ");
   133                 final int cc = (byteCodes[j] + 256) % 256;
   134                 out.append(Integer.toString(cc));
   135             }
   136             out.append("*/\n");
   137         }
   138     }
   139 }