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); }");
192 out.append("stack.push(stack.pop() & stack.pop());");
196 out.append("stack.push(stack.pop() | stack.pop());");
200 out.append("stack.push(stack.pop() ^ stack.pop());");
203 final int varIndx = (byteCodes[++i] + 256) % 256;
204 final int incrBy = (byteCodes[++i] + 256) % 256;
206 out.append("arg" + varIndx).append("++;");
208 out.append("arg" + varIndx).append(" += " + incrBy).append(";");
216 out.append("return stack.pop();");
227 out.append("/* number conversion */");
233 out.append("stack.push(Math.floor(stack.pop()));");
238 out.append("/* number conversion */");
244 out.append("stack.push(0);");
250 out.append("stack.push(1);");
254 out.append("stack.push(2);");
257 out.append("stack.push(3);");
260 out.append("stack.push(4);");
263 out.append("stack.push(5);");
266 i = generateIf(byteCodes, i, "==");
270 int indx = i + readIntArg(byteCodes, i);
271 out.append("if (stack.pop() == 0) { gt = " + indx);
272 out.append("; continue; }");
277 i = generateIf(byteCodes, i, "!=");
280 i = generateIf(byteCodes, i, ">");
283 i = generateIf(byteCodes, i, ">=");
286 i = generateIf(byteCodes, i, "<");
289 i = generateIf(byteCodes, i, "<=");
292 int indx = i + readIntArg(byteCodes, i);
293 out.append("gt = " + indx).append("; continue;");
297 case bc_invokestatic: {
298 int methodIndex = readIntArg(byteCodes, i);
299 CPMethodInfo mi = (CPMethodInfo) jc.getConstantPool().get(methodIndex);
300 boolean[] hasReturn = { false };
301 StringBuilder signature = new StringBuilder();
302 int cnt = countArgs(mi.getDescriptor(), hasReturn, signature);
305 out.append("stack.push(");
307 out.append(mi.getClassName().getInternalName().replace('/', '_'));
309 out.append(mi.getName());
310 out.append(signature.toString());
313 for (int j = 0; j < cnt; j++) {
315 out.append("stack.pop()");
328 for (int j = prev; j <= i; j++) {
330 final int cc = (byteCodes[j] + 256) % 256;
331 out.append(Integer.toString(cc));
338 private int generateIf(byte[] byteCodes, int i, final String test) throws IOException {
339 int indx = i + readIntArg(byteCodes, i);
340 out.append("if (stack.pop() ").append(test).append(" stack.pop()) { gt = " + indx);
341 out.append("; continue; }");
345 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
346 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
347 final int indxLo = byteCodes[offsetInstruction + 2];
348 return (indxHi & 0xffffff00) | (indxLo & 0xff);
351 private static int countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig) {
354 Boolean count = null;
355 while (i < descriptor.length()) {
356 char ch = descriptor.charAt(i++);
376 hasReturnType[0] = true;
382 hasReturnType[0] = false;
386 i = descriptor.indexOf(';', i);
390 hasReturnType[0] = true;
397 break; // invalid character