As GenJS has no reference to java.util classes, we need to comunicate via old good String[]
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 StringArray processed = new StringArray();
45 StringArray initCode = new StringArray();
46 for (String baseClass : names.toArray()) {
47 GenJS js = new GenJS(out);
48 js.references.add(baseClass);
51 for (String n : js.references.toArray()) {
52 if (processed.contains(n)) {
60 InputStream is = loadClass(l, name);
62 throw new IOException("Can't find class " + name);
65 String ic = js.compile(is);
67 initCode.add(ic == null ? "" : ic);
68 } catch (RuntimeException ex) {
69 if (out instanceof CharSequence) {
70 CharSequence seq = (CharSequence)out;
71 int lastBlock = seq.length();
72 while (lastBlock-- > 0) {
73 if (seq.charAt(lastBlock) == '{') {
77 throw new IOException("Error while compiling " + name + "\n"
78 + seq.subSequence(lastBlock + 1, seq.length()), ex
81 throw new IOException("Error while compiling " + name + "\n"
86 for (String resource : js.scripts.toArray()) {
87 while (resource.startsWith("/")) {
88 resource = resource.substring(1);
90 InputStream emul = l.getResourceAsStream(resource);
92 throw new IOException("Can't find " + resource);
94 readResource(emul, out);
98 StringArray toInit = StringArray.asList(js.references.toArray());
101 for (String ic : toInit.toArray()) {
102 int indx = processed.indexOf(ic);
104 out.append(initCode.toArray()[indx]).append("\n");
105 initCode.toArray()[indx] = "";
111 private static void readResource(InputStream emul, Appendable out) throws IOException {
115 int ch = emul.read();
119 if (ch < 0 || ch > 255) {
120 throw new IOException("Invalid char in emulation " + ch);
127 out.append((char)ch);
134 out.append('/').append((char)ch);
157 private static InputStream loadClass(ClassLoader l, String name) throws IOException {
158 Enumeration<URL> en = l.getResources(name + ".class");
160 while (en.hasMoreElements()) {
161 u = en.nextElement();
164 throw new IOException("Can't find " + name);
166 if (u.toExternalForm().contains("rt.jar!")) {
167 throw new IOException("No emulation for " + u);
169 return u.openStream();
172 static String toString(String name) throws IOException {
173 StringBuilder sb = new StringBuilder();
175 return sb.toString().toString();
178 private StringArray scripts = new StringArray();
179 private StringArray references = new StringArray();
182 protected boolean requireReference(String cn) {
183 if (references.contains(cn)) {
191 protected void requireScript(String resourcePath) {
192 scripts.add(resourcePath);