2 Java 4 Browser Bytecode Translator
3 Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.java4browser;
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.CPMethodInfo;
25 import org.netbeans.modules.classfile.ClassFile;
26 import org.netbeans.modules.classfile.Code;
27 import org.netbeans.modules.classfile.Method;
28 import org.netbeans.modules.classfile.Parameter;
30 /** Translator of the code inside class files to JavaScript.
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 public final class ByteCodeToJavaScript {
35 private final ClassFile jc;
36 private final Appendable out;
38 private ByteCodeToJavaScript(ClassFile jc, Appendable out) {
43 /** Converts a given class file to a JavaScript version.
44 * @param fileName the name of the file we are reading
45 * @param classFile input stream with code of the .class file
46 * @param out a {@link StringBuilder} or similar to generate the output to
47 * @throws IOException if something goes wrong during read or write or translating
49 public static void compile(String fileName, InputStream classFile, Appendable out) throws IOException {
50 ClassFile jc = new ClassFile(classFile, true);
51 ByteCodeToJavaScript compiler = new ByteCodeToJavaScript(jc, out);
52 for (Method m : jc.getMethods()) {
54 compiler.generateStaticMethod(m);
58 private void generateStaticMethod(Method m) throws IOException {
59 out.append("\nfunction ").append(
60 jc.getName().getExternalName().replace('.', '_')
64 out.append(m.getReturnType());
65 List<Parameter> args = m.getParameters();
66 for (Parameter t : args) {
67 out.append(t.getDescriptor());
71 for (int index = 0, i = 0; i < args.size(); i++) {
73 out.append("arg").append(String.valueOf(index));
75 final String desc = args.get(i).getDescriptor();
76 if ("D".equals(desc) || "J".equals(desc)) {
82 out.append(") {").append("\n");
83 final Code code = m.getCode();
84 int len = code.getMaxLocals();
85 for (int index = args.size(), i = args.size(); i < len; i++) {
87 out.append("arg").append(String.valueOf(i)).append(";\n");
89 out.append(";\n var stack = new Array(");
90 out.append(Integer.toString(code.getMaxStack()));
92 produceCode(code.getByteCodes());
96 private void produceCode(byte[] byteCodes) throws IOException {
97 out.append("\nvar gt = 0;\nfor(;;) switch(gt) {\n");
98 for (int i = 0; i < byteCodes.length; i++) {
100 out.append(" case " + i).append(": ");
101 final int c = (byteCodes[i] + 256) % 256;
108 out.append("stack.push(arg0);");
115 out.append("stack.push(arg1);");
122 out.append("stack.push(arg2);");
129 out.append("stack.push(arg3);");
136 final int indx = (byteCodes[++i] + 256) % 256;
137 out.append("stack.push(arg").append(indx + ");");
144 out.append("arg0 = stack.pop();");
150 out.append("arg1 = stack.pop();");
156 out.append("arg2 = stack.pop();");
162 out.append("arg3 = stack.pop();");
168 out.append("stack.push(stack.pop() + stack.pop());");
174 out.append("{ var tmp = stack.pop(); stack.push(stack.pop() - tmp); }");
180 out.append("stack.push(stack.pop() * stack.pop());");
184 out.append("{ var tmp = stack.pop(); stack.push(Math.floor(stack.pop() / tmp)); }");
188 out.append("{ var tmp = stack.pop(); stack.push(stack.pop() / tmp); }");
191 final int varIndx = (byteCodes[++i] + 256) % 256;
192 final int incrBy = (byteCodes[++i] + 256) % 256;
194 out.append("arg" + varIndx).append("++;");
196 out.append("arg" + varIndx).append(" += " + incrBy).append(";");
204 out.append("return stack.pop();");
215 out.append("/* number conversion */");
221 out.append("stack.push(Math.floor(stack.pop()));");
226 out.append("/* number conversion */");
232 out.append("stack.push(0);");
238 out.append("stack.push(1);");
242 out.append("stack.push(2);");
245 out.append("stack.push(3);");
248 out.append("stack.push(4);");
251 out.append("stack.push(5);");
254 i = generateIf(byteCodes, i, "==");
258 i = generateIf(byteCodes, i, "!=");
261 i = generateIf(byteCodes, i, ">");
264 i = generateIf(byteCodes, i, ">=");
267 i = generateIf(byteCodes, i, "<");
270 i = generateIf(byteCodes, i, "<=");
273 int indx = i + readIntArg(byteCodes, i);
274 out.append("gt = " + indx).append("; continue;");
278 case bc_invokestatic: {
279 int methodIndex = readIntArg(byteCodes, i);
280 CPMethodInfo mi = (CPMethodInfo) jc.getConstantPool().get(methodIndex);
281 boolean[] hasReturn = { false };
282 StringBuilder signature = new StringBuilder();
283 int cnt = countArgs(mi.getDescriptor(), hasReturn, signature);
286 out.append("stack.push(");
288 out.append(mi.getClassName().getInternalName().replace('/', '_'));
290 out.append(mi.getName());
291 out.append(signature.toString());
294 for (int j = 0; j < cnt; j++) {
296 out.append("stack.pop()");
309 for (int j = prev; j <= i; j++) {
311 final int cc = (byteCodes[j] + 256) % 256;
312 out.append(Integer.toString(cc));
319 private int generateIf(byte[] byteCodes, int i, final String test) throws IOException {
320 int indx = i + readIntArg(byteCodes, i);
321 out.append("if (stack.pop() ").append(test).append(" stack.pop()) { gt = " + indx);
322 out.append("; continue; }");
326 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
327 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
328 final int indxLo = byteCodes[offsetInstruction + 2];
329 return (indxHi & 0xffffff00) | (indxLo & 0xff);
332 private static int countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig) {
335 Boolean count = null;
336 while (i < descriptor.length()) {
337 char ch = descriptor.charAt(i++);
357 hasReturnType[0] = true;
363 hasReturnType[0] = false;
367 i = descriptor.indexOf(';', i);
371 hasReturnType[0] = true;
378 break; // invalid character