jaroslav@29
|
1 |
package org.apidesign.vm4brwsr;
|
jaroslav@29
|
2 |
|
jaroslav@29
|
3 |
import java.io.BufferedWriter;
|
jaroslav@29
|
4 |
import java.io.FileWriter;
|
jaroslav@29
|
5 |
import java.io.IOException;
|
jaroslav@29
|
6 |
import java.io.InputStream;
|
jaroslav@29
|
7 |
import java.io.Writer;
|
jaroslav@29
|
8 |
import java.util.Arrays;
|
jaroslav@29
|
9 |
import java.util.HashSet;
|
jaroslav@29
|
10 |
import java.util.LinkedList;
|
jaroslav@29
|
11 |
import java.util.List;
|
jaroslav@29
|
12 |
import java.util.Set;
|
jaroslav@29
|
13 |
|
jaroslav@29
|
14 |
/** Generator of JavaScript from bytecode of classes on classpath of the VM.
|
jaroslav@29
|
15 |
*
|
jaroslav@29
|
16 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@29
|
17 |
*/
|
jaroslav@29
|
18 |
final class GenJS {
|
jaroslav@29
|
19 |
private GenJS() {}
|
jaroslav@29
|
20 |
|
jaroslav@29
|
21 |
public static void main(String... args) throws IOException {
|
jaroslav@29
|
22 |
if (args.length < 2) {
|
jaroslav@29
|
23 |
System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
|
jaroslav@29
|
24 |
return;
|
jaroslav@29
|
25 |
}
|
jaroslav@29
|
26 |
|
jaroslav@29
|
27 |
Writer w = new BufferedWriter(new FileWriter(args[0]));
|
jaroslav@29
|
28 |
List<String> classes = Arrays.asList(args).subList(1, args.length);
|
jaroslav@29
|
29 |
compile(w, classes);
|
jaroslav@29
|
30 |
w.close();
|
jaroslav@29
|
31 |
}
|
jaroslav@29
|
32 |
|
jaroslav@29
|
33 |
static void compile(Appendable out, String... names) throws IOException {
|
jaroslav@29
|
34 |
compile(out, Arrays.asList(names));
|
jaroslav@29
|
35 |
}
|
jaroslav@29
|
36 |
static void compile(Appendable out, List<String> names) throws IOException {
|
jaroslav@29
|
37 |
Set<String> processed = new HashSet<String>();
|
jaroslav@29
|
38 |
LinkedList<String> toProcess = new LinkedList<String>(names);
|
jaroslav@29
|
39 |
for (;;) {
|
jaroslav@29
|
40 |
toProcess.removeAll(processed);
|
jaroslav@29
|
41 |
if (toProcess.isEmpty()) {
|
jaroslav@29
|
42 |
break;
|
jaroslav@29
|
43 |
}
|
jaroslav@29
|
44 |
String name = toProcess.getFirst();
|
jaroslav@29
|
45 |
processed.add(name);
|
jaroslav@29
|
46 |
if (name.startsWith("java/") && !name.equals("java/lang/Object")) {
|
jaroslav@29
|
47 |
continue;
|
jaroslav@29
|
48 |
}
|
jaroslav@29
|
49 |
InputStream is = GenJS.class.getClassLoader().getResourceAsStream(name + ".class");
|
jaroslav@29
|
50 |
if (is == null) {
|
jaroslav@29
|
51 |
throw new IOException("Can't find class " + name);
|
jaroslav@29
|
52 |
}
|
jaroslav@29
|
53 |
try {
|
jaroslav@29
|
54 |
ByteCodeToJavaScript.compile(is, out, toProcess);
|
jaroslav@29
|
55 |
} catch (RuntimeException ex) {
|
jaroslav@29
|
56 |
if (out instanceof CharSequence) {
|
jaroslav@29
|
57 |
CharSequence seq = (CharSequence)out;
|
jaroslav@29
|
58 |
int lastBlock = seq.length();
|
jaroslav@29
|
59 |
while (lastBlock-- >= 0) {
|
jaroslav@29
|
60 |
if (seq.charAt(lastBlock) == '{') {
|
jaroslav@29
|
61 |
break;
|
jaroslav@29
|
62 |
}
|
jaroslav@29
|
63 |
}
|
jaroslav@29
|
64 |
throw new IOException("Error while compiling " + name + "\n"
|
jaroslav@29
|
65 |
+ seq.subSequence(lastBlock + 1, seq.length()), ex
|
jaroslav@29
|
66 |
);
|
jaroslav@29
|
67 |
} else {
|
jaroslav@29
|
68 |
throw new IOException("Error while compiling " + name + "\n"
|
jaroslav@29
|
69 |
+ out, ex
|
jaroslav@29
|
70 |
);
|
jaroslav@29
|
71 |
}
|
jaroslav@29
|
72 |
}
|
jaroslav@29
|
73 |
}
|
jaroslav@29
|
74 |
}
|
jaroslav@29
|
75 |
|
jaroslav@29
|
76 |
}
|