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 for (String baseClass : names.toArray()) {
65 references.add(baseClass);
68 for (String n : references.toArray()) {
69 if (processed.contains(n)) {
77 InputStream is = loadClass(l, name);
79 throw new IOException("Can't find class " + name);
82 String ic = compile(is);
84 initCode.add(ic == null ? "" : ic);
85 } catch (RuntimeException ex) {
86 if (out instanceof CharSequence) {
87 CharSequence seq = (CharSequence)out;
88 int lastBlock = seq.length();
89 while (lastBlock-- > 0) {
90 if (seq.charAt(lastBlock) == '{') {
94 throw new IOException("Error while compiling " + name + "\n"
95 + seq.subSequence(lastBlock + 1, seq.length()), ex
98 throw new IOException("Error while compiling " + name + "\n"
105 for (String resource : scripts.toArray()) {
106 while (resource.startsWith("/")) {
107 resource = resource.substring(1);
109 InputStream emul = l.get(resource);
111 throw new IOException("Can't find " + resource);
113 readResource(emul, out);
115 scripts = new StringArray();
117 StringArray toInit = StringArray.asList(references.toArray());
120 for (String ic : toInit.toArray()) {
121 int indx = processed.indexOf(ic);
123 final String theCode = initCode.toArray()[indx];
124 if (!theCode.isEmpty()) {
125 out.append(theCode).append("\n");
127 initCode.toArray()[indx] = "";
134 + " global.bck2brwsr = function() {\n"
135 + " var args = Array.prototype.slice.apply(arguments);\n"
136 + " var vm = fillInVMSkeleton({});\n"
137 + " var loader = {};\n"
138 + " loader.vm = vm;\n"
139 + " loader.loadClass = function(name) {\n"
140 + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
141 + " var fn = vm[attr];\n"
142 + " if (fn) return fn(false);\n"
143 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
144 + " load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
146 + " if (vm.loadClass) {\n"
147 + " throw 'Cannot initialize the bck2brwsr VM twice!';\n"
149 + " vm.loadClass = loader.loadClass;\n"
150 + " vm.loadBytes = function(name) {\n"
151 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
152 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
154 + " vm.java_lang_reflect_Array(false);\n"
155 + " vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
156 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, null, args);\n"
157 + " return loader;\n"
159 out.append("}(this));");
161 private static void readResource(InputStream emul, Appendable out) throws IOException {
165 int ch = emul.read();
169 if (ch < 0 || ch > 255) {
170 throw new IOException("Invalid char in emulation " + ch);
177 out.append((char)ch);
184 out.append('/').append((char)ch);
207 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
208 return l.get(name + ".class"); // NOI18N
211 static String toString(String name) throws IOException {
212 StringBuilder sb = new StringBuilder();
213 // compile(sb, name);
214 return sb.toString().toString();
217 private StringArray scripts = new StringArray();
218 private StringArray references = new StringArray();
221 protected boolean requireReference(String cn) {
222 if (references.contains(cn)) {
230 protected void requireScript(String resourcePath) {
231 scripts.add(resourcePath);
235 String assignClass(String className) {
236 return "vm." + className + " = ";
240 String accessClass(String className) {
241 return "vm." + className;
245 String getVMObject() {