No need to embed String definition 82 times. Once is enough.
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 final 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 out.append("Array.prototype.fillNulls = function() {\n" +
45 " for(var i = 0; i < this.length; i++) {\n" +
46 " this[i] = null;\n" +
52 StringArray processed = new StringArray();
53 StringArray initCode = new StringArray();
54 for (String baseClass : names.toArray()) {
55 GenJS js = new GenJS(out);
56 js.references.add(baseClass);
59 for (String n : js.references.toArray()) {
60 if (processed.contains(n)) {
68 InputStream is = loadClass(l, name);
70 throw new IOException("Can't find class " + name);
73 String ic = js.compile(is);
75 initCode.add(ic == null ? "" : ic);
76 } catch (RuntimeException ex) {
77 if (out instanceof CharSequence) {
78 CharSequence seq = (CharSequence)out;
79 int lastBlock = seq.length();
80 while (lastBlock-- > 0) {
81 if (seq.charAt(lastBlock) == '{') {
85 throw new IOException("Error while compiling " + name + "\n"
86 + seq.subSequence(lastBlock + 1, seq.length()), ex
89 throw new IOException("Error while compiling " + name + "\n"
94 for (String resource : js.scripts.toArray()) {
95 while (resource.startsWith("/")) {
96 resource = resource.substring(1);
98 InputStream emul = l.getResourceAsStream(resource);
100 throw new IOException("Can't find " + resource);
102 readResource(emul, out);
104 js.scripts = new StringArray();
107 StringArray toInit = StringArray.asList(js.references.toArray());
110 for (String ic : toInit.toArray()) {
111 int indx = processed.indexOf(ic);
113 out.append(initCode.toArray()[indx]).append("\n");
114 initCode.toArray()[indx] = "";
120 private static void readResource(InputStream emul, Appendable out) throws IOException {
124 int ch = emul.read();
128 if (ch < 0 || ch > 255) {
129 throw new IOException("Invalid char in emulation " + ch);
136 out.append((char)ch);
143 out.append('/').append((char)ch);
166 private static InputStream loadClass(ClassLoader l, String name) throws IOException {
167 Enumeration<URL> en = l.getResources(name + ".class");
169 while (en.hasMoreElements()) {
170 u = en.nextElement();
173 throw new IOException("Can't find " + name);
175 if (u.toExternalForm().contains("rt.jar!")) {
176 throw new IOException("No emulation for " + u);
178 return u.openStream();
181 static String toString(String name) throws IOException {
182 StringBuilder sb = new StringBuilder();
184 return sb.toString().toString();
187 private StringArray scripts = new StringArray();
188 private StringArray references = new StringArray();
191 protected boolean requireReference(String cn) {
192 if (references.contains(cn)) {
200 protected void requireScript(String resourcePath) {
201 scripts.add(resourcePath);