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.IOException;
21 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.LinkedHashSet;
30 import java.util.LinkedList;
31 import java.util.List;
34 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
41 static void compile(Appendable out, String... names) throws IOException {
42 compile(out, Arrays.asList(names));
44 static void compile(Appendable out, List<String> names) throws IOException {
45 compile(GenJS.class.getClassLoader(), out, names);
47 static void compile(ClassLoader l, Appendable out, List<String> names) throws IOException {
48 final Map<String,String> processed = new HashMap<String, String>();
49 for (String baseClass : names) {
50 LinkedHashSet<String> toProcess = new LinkedHashSet<String>() {
52 public boolean add(String e) {
53 if (processed.containsKey(e)) {
59 toProcess.add(baseClass);
62 Iterator<String> it = toProcess.iterator();
63 while (it.hasNext() && name == null) {
65 if (processed.get(n) != null) {
73 if (name.startsWith("sun/")) {
74 processed.put(name, "");
77 InputStream is = loadClass(l, name);
79 throw new IOException("Can't find class " + name);
81 LinkedList<String> scripts = new LinkedList<String>();
83 String initCode = ByteCodeToJavaScript.compile(is, out, toProcess, scripts);
84 processed.put(name, initCode == null ? "" : initCode);
85 } catch (RuntimeException ex) {
86 if (out instanceof CharSequence) {
87 CharSequence seq = (CharSequence)out;
88 int lastBlock = seq.length();
89 while (lastBlock-- >= 0) {
90 if (seq.charAt(lastBlock) == '{') {
94 throw new IOException("Error while compiling " + name + "\n"
95 + seq.subSequence(lastBlock + 1, seq.length()), ex
98 throw new IOException("Error while compiling " + name + "\n"
103 for (String resource : scripts) {
104 while (resource.startsWith("/")) {
105 resource = resource.substring(1);
107 InputStream emul = l.getResourceAsStream(resource);
109 throw new IOException("Can't find " + resource);
111 readResource(emul, out);
115 List<String> toInit = new ArrayList<String>(toProcess);
116 Collections.reverse(toInit);
118 for (String clazz : toInit) {
119 String initCode = processed.get(clazz);
120 if (initCode != null && !initCode.isEmpty()) {
121 out.append(initCode).append("\n");
122 processed.put(clazz, "");
128 private static void readResource(InputStream emul, Appendable out) throws IOException {
132 int ch = emul.read();
136 if (ch < 0 || ch > 255) {
137 throw new IOException("Invalid char in emulation " + ch);
144 out.append((char)ch);
151 out.append('/').append((char)ch);
174 private static InputStream loadClass(ClassLoader l, String name) throws IOException {
175 Enumeration<URL> en = l.getResources(name + ".class");
177 while (en.hasMoreElements()) {
178 u = en.nextElement();
181 throw new IOException("Can't find " + name);
183 return u.openStream();
186 static String toString(String name) throws IOException {
187 StringBuilder sb = new StringBuilder();
189 return sb.toString().toString();