2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 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.vm4brwsr;
20 import java.io.BufferedWriter;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.Writer;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.LinkedHashSet;
33 import java.util.LinkedList;
34 import java.util.List;
37 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
39 * @author Jaroslav Tulach <jtulach@netbeans.org>
44 public static void main(String... args) throws IOException {
45 if (args.length < 2) {
46 System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
50 Writer w = new BufferedWriter(new FileWriter(args[0]));
51 List<String> classes = Arrays.asList(args).subList(1, args.length);
56 static void compile(Appendable out, String... names) throws IOException {
57 compile(out, Arrays.asList(names));
59 static void compile(Appendable out, List<String> names) throws IOException {
60 compile(GenJS.class.getClassLoader(), out, names);
62 static void compile(ClassLoader l, Appendable out, List<String> names) throws IOException {
63 final Map<String,String> processed = new HashMap<String, String>();
64 for (String baseClass : names) {
65 LinkedHashSet<String> toProcess = new LinkedHashSet<String>() {
67 public boolean add(String e) {
68 if (processed.containsKey(e)) {
74 toProcess.add(baseClass);
77 Iterator<String> it = toProcess.iterator();
78 while (it.hasNext() && name == null) {
80 if (processed.get(n) != null) {
88 if (name.startsWith("sun/")) {
89 processed.put(name, "");
92 InputStream is = loadClass(l, name);
94 throw new IOException("Can't find class " + name);
96 LinkedList<String> scripts = new LinkedList<String>();
98 String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
99 processed.put(name, initCode == null ? "" : initCode);
100 } catch (RuntimeException ex) {
101 if (out instanceof CharSequence) {
102 CharSequence seq = (CharSequence)out;
103 int lastBlock = seq.length();
104 while (lastBlock-- >= 0) {
105 if (seq.charAt(lastBlock) == '{') {
109 throw new IOException("Error while compiling " + name + "\n"
110 + seq.subSequence(lastBlock + 1, seq.length()), ex
113 throw new IOException("Error while compiling " + name + "\n"
118 for (String resource : scripts) {
119 while (resource.startsWith("/")) {
120 resource = resource.substring(1);
122 InputStream emul = l.getResourceAsStream(resource);
124 throw new IOException("Can't find " + resource);
126 readResource(emul, out);
130 List<String> toInit = new ArrayList<String>(toProcess);
131 Collections.reverse(toInit);
133 for (String clazz : toInit) {
134 String initCode = processed.get(clazz);
135 if (initCode != null && !initCode.isEmpty()) {
136 out.append(initCode).append("\n");
137 processed.put(clazz, "");
143 private static void readResource(InputStream emul, Appendable out) throws IOException {
147 int ch = emul.read();
151 if (ch < 0 || ch > 255) {
152 throw new IOException("Invalid char in emulation " + ch);
159 out.append((char)ch);
166 out.append('/').append((char)ch);
189 private static InputStream loadClass(ClassLoader l, String name) throws IOException {
190 Enumeration<URL> en = l.getResources(name + ".class");
192 while (en.hasMoreElements()) {
193 u = en.nextElement();
196 throw new IOException("Can't find " + name);
198 return u.openStream();