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 var ");
83 final Code code = m.getCode();
84 int len = code.getMaxLocals();
86 for (int i = 0; i < len; i++) {
88 out.append("var").append(String.valueOf(i));
91 out.append(";\n var stack = new Array(");
92 out.append(Integer.toString(code.getMaxStack()));
94 produceCode(code.getByteCodes());
98 private void produceCode(byte[] byteCodes) throws IOException {
99 out.append("\nvar gt = 0;\nfor(;;) switch(gt) {\n");
100 for (int i = 0; i < byteCodes.length; i++) {
102 out.append(" case " + i).append(": ");
103 final int c = (byteCodes[i] + 256) % 256;
110 out.append("stack.push(arg0);");
117 out.append("stack.push(arg1);");
124 out.append("stack.push(arg2);");
131 out.append("stack.push(arg3);");
138 final int indx = (byteCodes[++i] + 256) % 256;
139 out.append("stack.push(arg").append(indx + ");");
146 out.append("stack.push(stack.pop() + stack.pop());");
152 out.append("{ var tmp = stack.pop(); stack.push(stack.pop() - tmp); }");
158 out.append("stack.push(stack.pop() * stack.pop());");
162 out.append("{ var tmp = stack.pop(); stack.push(Math.floor(stack.pop() / tmp)); }");
166 out.append("{ var tmp = stack.pop(); stack.push(stack.pop() / tmp); }");
172 out.append("return stack.pop();");
183 out.append("/* number conversion */");
189 out.append("stack.push(Math.floor(stack.pop()));");
194 out.append("/* number conversion */");
200 out.append("stack.push(0);");
206 out.append("stack.push(1);");
210 out.append("stack.push(2);");
213 out.append("stack.push(3);");
216 out.append("stack.push(4);");
219 out.append("stack.push(5);");
222 i = generateIf(byteCodes, i, "==");
226 i = generateIf(byteCodes, i, "!=");
229 i = generateIf(byteCodes, i, ">");
232 i = generateIf(byteCodes, i, ">=");
235 i = generateIf(byteCodes, i, "<");
238 i = generateIf(byteCodes, i, "<=");
240 case bc_invokestatic: {
241 int methodIndex = readIntArg(byteCodes, i);
242 CPMethodInfo mi = (CPMethodInfo) jc.getConstantPool().get(methodIndex);
243 boolean[] hasReturn = { false };
244 StringBuilder signature = new StringBuilder();
245 int cnt = countArgs(mi.getDescriptor(), hasReturn, signature);
248 out.append("stack.push(");
250 out.append(mi.getClassName().getInternalName().replace('/', '_'));
252 out.append(mi.getName());
253 out.append(signature.toString());
256 for (int j = 0; j < cnt; j++) {
258 out.append("stack.pop()");
271 for (int j = prev; j <= i; j++) {
273 final int cc = (byteCodes[j] + 256) % 256;
274 out.append(Integer.toString(cc));
281 private int generateIf(byte[] byteCodes, int i, final String test) throws IOException {
282 int indx = i + readIntArg(byteCodes, i);
283 out.append("if (stack.pop() ").append(test).append(" stack.pop()) { gt = " + indx);
284 out.append("; continue; }");
288 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
289 final int indxHi = (byteCodes[offsetInstruction + 1] + 256) % 256;
290 final int indxLo = (byteCodes[offsetInstruction + 2] + 256) % 256;
291 return (indxHi << 16) + indxLo;
294 private static int countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig) {
297 Boolean count = null;
298 while (i < descriptor.length()) {
299 char ch = descriptor.charAt(i++);
319 hasReturnType[0] = true;
325 hasReturnType[0] = false;
329 i = descriptor.indexOf(';', i);
333 hasReturnType[0] = true;
340 break; // invalid character