src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java
changeset 0 71aab30ab2b7
child 1 48b1dce93691
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/java/org/apidesign/java4browser/ByteCodeToJavaScript.java	Mon Aug 27 05:08:30 2012 +0200
     1.3 @@ -0,0 +1,121 @@
     1.4 +/*
     1.5 +Java 4 Browser Bytecode Translator
     1.6 +Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU General Public License as published by
    1.10 +the Free Software Foundation, version 2 of the License.
    1.11 +
    1.12 +This program is distributed in the hope that it will be useful,
    1.13 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +GNU General Public License for more details.
    1.16 +
    1.17 +You should have received a copy of the GNU General Public License
    1.18 +along with this program. Look for COPYING file in the top folder.
    1.19 +If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 +*/
    1.21 +package org.apidesign.java4browser;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +import java.util.List;
    1.26 +import org.netbeans.modules.classfile.ByteCodes;
    1.27 +import org.netbeans.modules.classfile.ClassFile;
    1.28 +import org.netbeans.modules.classfile.Code;
    1.29 +import org.netbeans.modules.classfile.Method;
    1.30 +import org.netbeans.modules.classfile.Parameter;
    1.31 +
    1.32 +/** Translator of the code inside class files to JavaScript.
    1.33 + *
    1.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.35 + */
    1.36 +public final class ByteCodeToJavaScript {
    1.37 +    private final ClassFile jc;
    1.38 +    private final Appendable out;
    1.39 +
    1.40 +    private ByteCodeToJavaScript(ClassFile jc, Appendable out) {
    1.41 +        this.jc = jc;
    1.42 +        this.out = out;
    1.43 +    }
    1.44 +    
    1.45 +    /** Converts a given class file to a JavaScript version.
    1.46 +     * @param fileName the name of the file we are reading
    1.47 +     * @param classFile input stream with code of the .class file
    1.48 +     * @param out a {@link StringBuilder} or similar to generate the output to
    1.49 +     * @throws IOException if something goes wrong during read or write or translating
    1.50 +     */
    1.51 +    public static void compile(String fileName, InputStream classFile, Appendable out) throws IOException {
    1.52 +        ClassFile jc = new ClassFile(classFile, true);
    1.53 +        ByteCodeToJavaScript compiler = new ByteCodeToJavaScript(jc, out);
    1.54 +        for (Method m : jc.getMethods()) {
    1.55 +            if (m.isStatic()) {
    1.56 +                compiler.generateStaticMethod(m);
    1.57 +            }
    1.58 +        }
    1.59 +    }
    1.60 +    private void generateStaticMethod(Method m) throws IOException {
    1.61 +        out.append("function ").append(
    1.62 +            jc.getName().getExternalName().replace('.', '_')
    1.63 +        ).append('_').append(
    1.64 +            m.getName()
    1.65 +        );
    1.66 +        out.append(m.getReturnType());
    1.67 +        List<Parameter> args = m.getParameters();
    1.68 +        for (Parameter t : args) {
    1.69 +            out.append(t.getDescriptor());
    1.70 +        }
    1.71 +        out.append('(');
    1.72 +        String space = "";
    1.73 +        for (int i = 0; i < args.size(); i++) {
    1.74 +            out.append(space);
    1.75 +            out.append("arg").append(String.valueOf(i));
    1.76 +            space = ",";
    1.77 +        }
    1.78 +        out.append(") {").append("\n  var ");
    1.79 +        final Code code = m.getCode();
    1.80 +        int len = code.getMaxLocals();
    1.81 +        space = "";
    1.82 +        for (int i = 0; i < len; i++) {
    1.83 +            out.append(space);
    1.84 +            out.append("var").append(String.valueOf(i));
    1.85 +            space = ",";
    1.86 +        }
    1.87 +        out.append(";\n  var stack = new Array(");
    1.88 +        out.append(Integer.toString(code.getMaxStack()));
    1.89 +        out.append(");\n");
    1.90 +        produceCode(code.getByteCodes());
    1.91 +        out.append(";\nreturn 1;");
    1.92 +        out.append("}");
    1.93 +    }
    1.94 +
    1.95 +    private void produceCode(byte[] byteCodes) throws IOException {
    1.96 +        for (int i = 0; i < byteCodes.length; i++) {
    1.97 +            int prev = i;
    1.98 +            out.append("  ");
    1.99 +            final int c = (byteCodes[i] + 256) % 256;
   1.100 +            switch (c) {
   1.101 +                case ByteCodes.bc_aload_0:
   1.102 +                case ByteCodes.bc_iload_0:
   1.103 +                    out.append("stack.push(arg0);");
   1.104 +                    break;
   1.105 +                case ByteCodes.bc_aload_1:
   1.106 +                case ByteCodes.bc_iload_1:
   1.107 +                    out.append("stack.push(arg1);");
   1.108 +                    break;
   1.109 +                case ByteCodes.bc_iadd:
   1.110 +                    out.append("stack.push(stack.pop() + stack.pop());");
   1.111 +                    break;
   1.112 +                case ByteCodes.bc_ireturn:
   1.113 +                    out.append("return stack.pop();");
   1.114 +            }
   1.115 +            out.append("/*");
   1.116 +            for (int j = prev; j <= i; j++) {
   1.117 +                out.append(" ");
   1.118 +                final int cc = (byteCodes[j] + 256) % 256;
   1.119 +                out.append(Integer.toString(cc));
   1.120 +            }
   1.121 +            out.append("*/\n");
   1.122 +        }
   1.123 +    }
   1.124 +}