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.Enumeration;
25 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 class GenJS extends ByteCodeToJavaScript {
30 public GenJS(Appendable out) {
34 static void compile(Appendable out, String... names) throws IOException {
35 compile(out, StringArray.asList(names));
37 static void compile(ClassLoader l, Appendable out, String... names) throws IOException {
38 compile(l, out, StringArray.asList(names));
40 static void compile(Appendable out, StringArray names) throws IOException {
41 compile(GenJS.class.getClassLoader(), out, names);
43 static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
44 new GenJS(out).doCompile(l, names);
46 protected void doCompile(ClassLoader l, StringArray names) throws IOException {
47 StringArray processed = new StringArray();
48 StringArray initCode = new StringArray();
49 for (String baseClass : names.toArray()) {
50 references.add(baseClass);
53 for (String n : references.toArray()) {
54 if (processed.contains(n)) {
62 InputStream is = loadClass(l, name);
64 throw new IOException("Can't find class " + name);
67 String ic = compile(is);
69 initCode.add(ic == null ? "" : ic);
70 } catch (RuntimeException ex) {
71 if (out instanceof CharSequence) {
72 CharSequence seq = (CharSequence)out;
73 int lastBlock = seq.length();
74 while (lastBlock-- > 0) {
75 if (seq.charAt(lastBlock) == '{') {
79 throw new IOException("Error while compiling " + name + "\n"
80 + seq.subSequence(lastBlock + 1, seq.length()), ex
83 throw new IOException("Error while compiling " + name + "\n"
90 for (String resource : scripts.toArray()) {
91 while (resource.startsWith("/")) {
92 resource = resource.substring(1);
94 InputStream emul = l.getResourceAsStream(resource);
96 throw new IOException("Can't find " + resource);
98 readResource(emul, out);
100 scripts = new StringArray();
102 StringArray toInit = StringArray.asList(references.toArray());
105 for (String ic : toInit.toArray()) {
106 int indx = processed.indexOf(ic);
108 out.append(initCode.toArray()[indx]).append("\n");
109 initCode.toArray()[indx] = "";
115 private static void readResource(InputStream emul, Appendable out) throws IOException {
119 int ch = emul.read();
123 if (ch < 0 || ch > 255) {
124 throw new IOException("Invalid char in emulation " + ch);
131 out.append((char)ch);
138 out.append('/').append((char)ch);
161 private static InputStream loadClass(ClassLoader l, String name) throws IOException {
162 Enumeration<URL> en = l.getResources(name + ".class");
164 while (en.hasMoreElements()) {
165 u = en.nextElement();
168 throw new IOException("Can't find " + name);
170 if (u.toExternalForm().contains("rt.jar!")) {
171 throw new IOException("No emulation for " + u);
173 return u.openStream();
176 static String toString(String name) throws IOException {
177 StringBuilder sb = new StringBuilder();
179 return sb.toString().toString();
182 private StringArray scripts = new StringArray();
183 private StringArray references = new StringArray();
186 protected boolean requireReference(String cn) {
187 if (references.contains(cn)) {
195 protected void requireScript(String resourcePath) {
196 scripts.add(resourcePath);