Don't really initialize the classes. Just pretend they will be initialized so bck2brwsr includes them in transitive closure
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 /** Generator of JavaScript from bytecode of classes on classpath of the VM.
25 * @author Jaroslav Tulach <jtulach@netbeans.org>
27 class VM extends ByteCodeToJavaScript {
28 public VM(Appendable out) {
33 // uses VMLazy to load dynamic classes
34 boolean assertsOn = false;
35 assert assertsOn = true;
43 boolean debug(String msg) throws IOException {
47 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
48 new VM(out).doCompile(l, names);
50 protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
51 out.append("(function VM(global) {");
52 out.append("\n var vm = {};");
53 StringArray processed = new StringArray();
54 StringArray initCode = new StringArray();
55 for (String baseClass : names.toArray()) {
56 references.add(baseClass);
59 for (String n : 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 = 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"
96 for (String resource : scripts.toArray()) {
97 while (resource.startsWith("/")) {
98 resource = resource.substring(1);
100 InputStream emul = l.get(resource);
102 throw new IOException("Can't find " + resource);
104 readResource(emul, out);
106 scripts = new StringArray();
108 StringArray toInit = StringArray.asList(references.toArray());
111 for (String ic : toInit.toArray()) {
112 int indx = processed.indexOf(ic);
114 out.append(initCode.toArray()[indx]).append("\n");
115 initCode.toArray()[indx] = "";
120 " global.bck2brwsr = function() {\n"
121 + " var args = arguments;\n"
122 + " var loader = {};\n"
123 + " loader.vm = vm;\n"
124 + " if (args.length == 1 && typeof args[0] !== 'function') {;\n"
125 + " var classpath = args[0];\n"
126 + " args[0] = function(name) {\n"
127 + " return vm.org_apidesign_vm4brwsr_Zips(false).loadFromCp___3B_3Ljava_lang_Object_2Ljava_lang_String_2(classpath, name);\n"
130 + " loader.loadClass = function(name) {\n"
131 + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
132 + " var fn = vm[attr];\n"
133 + " if (fn) return fn(false);\n"
134 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
135 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
136 + " load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
138 + " if (args[0]) {\n"
139 + " vm.loadClass = loader.loadClass;\n"
140 + " vm.loadBytes = function(name) {\n"
141 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
142 + " return args[0](name);\n"
145 + " return loader;\n"
147 out.append("}(this));");
149 private static void readResource(InputStream emul, Appendable out) throws IOException {
153 int ch = emul.read();
157 if (ch < 0 || ch > 255) {
158 throw new IOException("Invalid char in emulation " + ch);
165 out.append((char)ch);
172 out.append('/').append((char)ch);
195 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
196 return l.get(name + ".class"); // NOI18N
199 static String toString(String name) throws IOException {
200 StringBuilder sb = new StringBuilder();
201 // compile(sb, name);
202 return sb.toString().toString();
205 private StringArray scripts = new StringArray();
206 private StringArray references = new StringArray();
209 protected boolean requireReference(String cn) {
210 if (references.contains(cn)) {
218 protected void requireScript(String resourcePath) {
219 scripts.add(resourcePath);
223 String assignClass(String className) {
224 return "vm." + className + " = ";
228 String accessClass(String className) {
229 return "vm." + className;