If a class is not available during Ahead-Of-Time compilation it needs to be ready for dynamic loading
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) {
32 private VM(Appendable out, ObfuscationDelegate obfuscationDelegate) {
33 super(out, obfuscationDelegate);
37 // uses VMLazy to load dynamic classes
38 boolean assertsOn = false;
39 assert assertsOn = true;
47 boolean debug(String msg) throws IOException {
51 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
52 new VM(out).doCompile(l, names);
55 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names,
56 ObfuscationDelegate obfuscationDelegate) throws IOException {
57 new VM(out, obfuscationDelegate).doCompile(l, names);
60 protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
61 out.append("(function VM(global) {var fillInVMSkeleton = function(vm) {");
62 StringArray processed = new StringArray();
63 StringArray initCode = new StringArray();
64 StringArray skipClass = new StringArray();
65 for (String baseClass : names.toArray()) {
66 references.add(baseClass);
69 for (String n : references.toArray()) {
70 if (skipClass.contains(n)) {
73 if (processed.contains(n)) {
81 InputStream is = loadClass(l, name);
83 lazyReference(out, name);
88 String ic = compile(is);
90 initCode.add(ic == null ? "" : ic);
91 } catch (RuntimeException ex) {
92 if (out instanceof CharSequence) {
93 CharSequence seq = (CharSequence)out;
94 int lastBlock = seq.length();
95 while (lastBlock-- > 0) {
96 if (seq.charAt(lastBlock) == '{') {
100 throw new IOException("Error while compiling " + name + "\n"
101 + seq.subSequence(lastBlock + 1, seq.length()), ex
104 throw new IOException("Error while compiling " + name + "\n"
111 for (String resource : scripts.toArray()) {
112 while (resource.startsWith("/")) {
113 resource = resource.substring(1);
115 InputStream emul = l.get(resource);
117 throw new IOException("Can't find " + resource);
119 readResource(emul, out);
121 scripts = new StringArray();
123 StringArray toInit = StringArray.asList(references.toArray());
126 for (String ic : toInit.toArray()) {
127 int indx = processed.indexOf(ic);
129 final String theCode = initCode.toArray()[indx];
130 if (!theCode.isEmpty()) {
131 out.append(theCode).append("\n");
133 initCode.toArray()[indx] = "";
140 + " global.bck2brwsr = function() {\n"
141 + " var args = Array.prototype.slice.apply(arguments);\n"
142 + " var vm = fillInVMSkeleton({});\n"
143 + " var loader = {};\n"
144 + " loader.vm = vm;\n"
145 + " loader.loadClass = function(name) {\n"
146 + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
147 + " var fn = vm[attr];\n"
148 + " if (fn) return fn(false);\n"
149 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
150 + " load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
152 + " if (vm.loadClass) {\n"
153 + " throw 'Cannot initialize the bck2brwsr VM twice!';\n"
155 + " vm.loadClass = loader.loadClass;\n"
156 + " vm.loadBytes = function(name) {\n"
157 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
158 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
160 + " vm.java_lang_reflect_Array(false);\n"
161 + " vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
162 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, null, args);\n"
163 + " return loader;\n"
165 out.append("}(this));");
167 private static void readResource(InputStream emul, Appendable out) throws IOException {
171 int ch = emul.read();
175 if (ch < 0 || ch > 255) {
176 throw new IOException("Invalid char in emulation " + ch);
183 out.append((char)ch);
190 out.append('/').append((char)ch);
213 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
214 return l.get(name + ".class"); // NOI18N
217 static String toString(String name) throws IOException {
218 StringBuilder sb = new StringBuilder();
219 // compile(sb, name);
220 return sb.toString().toString();
223 private StringArray scripts = new StringArray();
224 private StringArray references = new StringArray();
227 protected boolean requireReference(String cn) {
228 if (references.contains(cn)) {
236 protected void requireScript(String resourcePath) {
237 scripts.add(resourcePath);
241 String assignClass(String className) {
242 return "vm." + className + " = ";
246 String accessClass(String className) {
247 return "vm." + className;
251 String getVMObject() {
255 private static void lazyReference(Appendable out, String n) throws IOException {
256 String cls = n.replace('/', '_');
257 String dot = n.replace('/', '.');
259 out.append("\nvm.").append(cls).append(" = function() {");
260 out.append("\n var instance = arguments.length == 0 || arguments[0] === true;");
261 out.append("\n delete vm.").append(cls).append(";");
262 out.append("\n var c = vm.loadClass('").append(dot).append("');");
263 out.append("\n return vm.").append(cls).append("(instance);");