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) {
35 // uses VMLazy to load dynamic classes
39 static void compile(Appendable out, String... names) throws IOException {
40 compile(out, StringArray.asList(names));
42 static void compile(ClassLoader l, Appendable out, String... names) throws IOException {
43 compile(l, out, StringArray.asList(names));
45 static void compile(Appendable out, StringArray names) throws IOException {
46 compile(GenJS.class.getClassLoader(), out, names);
48 static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
49 new GenJS(out).doCompile(l, names);
51 protected void doCompile(ClassLoader l, StringArray names) throws IOException {
52 out.append("(function VM(global) {");
53 out.append("\n var vm = {};");
54 StringArray processed = new StringArray();
55 StringArray initCode = new StringArray();
56 for (String baseClass : names.toArray()) {
57 references.add(baseClass);
60 for (String n : references.toArray()) {
61 if (processed.contains(n)) {
69 InputStream is = loadClass(l, name);
71 throw new IOException("Can't find class " + name);
74 String ic = compile(is);
76 initCode.add(ic == null ? "" : ic);
77 } catch (RuntimeException ex) {
78 if (out instanceof CharSequence) {
79 CharSequence seq = (CharSequence)out;
80 int lastBlock = seq.length();
81 while (lastBlock-- > 0) {
82 if (seq.charAt(lastBlock) == '{') {
86 throw new IOException("Error while compiling " + name + "\n"
87 + seq.subSequence(lastBlock + 1, seq.length()), ex
90 throw new IOException("Error while compiling " + name + "\n"
97 for (String resource : scripts.toArray()) {
98 while (resource.startsWith("/")) {
99 resource = resource.substring(1);
101 InputStream emul = l.getResourceAsStream(resource);
103 throw new IOException("Can't find " + resource);
105 readResource(emul, out);
107 scripts = new StringArray();
109 StringArray toInit = StringArray.asList(references.toArray());
112 for (String ic : toInit.toArray()) {
113 int indx = processed.indexOf(ic);
115 out.append(initCode.toArray()[indx]).append("\n");
116 initCode.toArray()[indx] = "";
121 " global.bck2brwsr = function() {\n"
122 + " var args = arguments;\n"
123 + " var loader = {};\n"
124 + " loader.vm = vm;\n"
125 + " loader.loadClass = function(name) {\n"
126 + " var attr = name.replace__Ljava_lang_String_2CC(name, '.','_');\n"
127 + " var fn = vm[attr];\n"
128 + " if (fn) return fn(false);\n"
129 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
130 + " load___3Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
132 + " return loader;\n"
134 out.append("}(this));");
136 private static void readResource(InputStream emul, Appendable out) throws IOException {
140 int ch = emul.read();
144 if (ch < 0 || ch > 255) {
145 throw new IOException("Invalid char in emulation " + ch);
152 out.append((char)ch);
159 out.append('/').append((char)ch);
182 private static InputStream loadClass(ClassLoader l, String name) throws IOException {
183 Enumeration<URL> en = l.getResources(name + ".class");
185 while (en.hasMoreElements()) {
186 u = en.nextElement();
189 throw new IOException("Can't find " + name);
191 if (u.toExternalForm().contains("rt.jar!")) {
192 throw new IOException("No emulation for " + u);
194 return u.openStream();
197 static String toString(String name) throws IOException {
198 StringBuilder sb = new StringBuilder();
200 return sb.toString().toString();
203 private StringArray scripts = new StringArray();
204 private StringArray references = new StringArray();
207 protected boolean requireReference(String cn) {
208 if (references.contains(cn)) {
216 protected void requireScript(String resourcePath) {
217 scripts.add(resourcePath);
221 String assignClass(String className) {
222 return "vm." + className + " = ";
226 String accessClass(String className) {
227 return "vm." + className;