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 abstract class VM extends ByteCodeToJavaScript {
28 private VM(Appendable out, ObfuscationDelegate obfuscationDelegate) {
29 super(out, obfuscationDelegate);
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 VM vm = new Standalone(out, ObfuscationDelegate.NULL);
49 vm.doCompile(l, names);
52 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names,
53 ObfuscationDelegate obfuscationDelegate) throws IOException {
54 VM vm = new Standalone(out, obfuscationDelegate);
55 vm.doCompile(l, names);
58 private void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
60 generateBody(l, names);
64 protected abstract void generatePrologue() throws IOException;
66 protected abstract void generateEpilogue() throws IOException;
68 protected void generateBody(Bck2Brwsr.Resources l, StringArray names) throws IOException {
69 StringArray processed = new StringArray();
70 StringArray initCode = new StringArray();
71 for (String baseClass : names.toArray()) {
72 references.add(baseClass);
75 for (String n : references.toArray()) {
76 if (processed.contains(n)) {
84 InputStream is = loadClass(l, name);
86 throw new IOException("Can't find class " + name);
89 String ic = compile(is);
91 initCode.add(ic == null ? "" : ic);
92 } catch (RuntimeException ex) {
93 if (out instanceof CharSequence) {
94 CharSequence seq = (CharSequence)out;
95 int lastBlock = seq.length();
96 while (lastBlock-- > 0) {
97 if (seq.charAt(lastBlock) == '{') {
101 throw new IOException("Error while compiling " + name + "\n"
102 + seq.subSequence(lastBlock + 1, seq.length()), ex
105 throw new IOException("Error while compiling " + name + "\n"
112 for (String resource : scripts.toArray()) {
113 while (resource.startsWith("/")) {
114 resource = resource.substring(1);
116 InputStream emul = l.get(resource);
118 throw new IOException("Can't find " + resource);
120 readResource(emul, out);
122 scripts = new StringArray();
124 StringArray toInit = StringArray.asList(references.toArray());
127 for (String ic : toInit.toArray()) {
128 int indx = processed.indexOf(ic);
130 final String theCode = initCode.toArray()[indx];
131 if (!theCode.isEmpty()) {
132 out.append(theCode).append("\n");
134 initCode.toArray()[indx] = "";
140 private static void readResource(InputStream emul, Appendable out) throws IOException {
144 int ch = emul.read();
148 if (ch < 0 || ch > 255) {
149 throw new IOException("Invalid char in emulation " + ch);
156 out.append((char)ch);
163 out.append('/').append((char)ch);
186 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
187 return l.get(name + ".class"); // NOI18N
190 static String toString(String name) throws IOException {
191 StringBuilder sb = new StringBuilder();
192 // compile(sb, name);
193 return sb.toString().toString();
196 private StringArray scripts = new StringArray();
197 private StringArray references = new StringArray();
200 protected boolean requireReference(String cn) {
201 if (references.contains(cn)) {
209 protected void requireScript(String resourcePath) {
210 scripts.add(resourcePath);
214 String assignClass(String className) {
215 return "vm." + className + " = ";
219 String accessClass(String className) {
220 return "vm." + className;
223 private static final class Standalone extends VM {
224 private Standalone(Appendable out,
225 ObfuscationDelegate obfuscationDelegate) {
226 super(out, obfuscationDelegate);
230 protected void generatePrologue() throws IOException {
231 out.append("(function VM(global) {var fillInVMSkeleton = function(vm) {");
235 protected void generateEpilogue() throws IOException {
239 + " global.bck2brwsr = function() {\n"
240 + " var args = Array.prototype.slice.apply(arguments);\n"
241 + " var vm = fillInVMSkeleton({});\n"
242 + " var loader = {};\n"
243 + " loader.vm = vm;\n"
244 + " loader.loadClass = function(name) {\n"
245 + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
246 + " var fn = vm[attr];\n"
247 + " if (fn) return fn(false);\n"
248 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
249 + " load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
251 + " if (vm.loadClass) {\n"
252 + " throw 'Cannot initialize the bck2brwsr VM twice!';\n"
254 + " vm.loadClass = loader.loadClass;\n"
255 + " vm.loadBytes = function(name) {\n"
256 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
257 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
259 + " vm.java_lang_reflect_Array(false);\n"
260 + " vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
261 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, null, args);\n"
262 + " return loader;\n"
264 out.append("}(this));");
268 String getExportsObject() {
273 private static final class Extension extends VM {
274 private final String name;
276 private Extension(String name,
278 ObfuscationDelegate obfuscationDelegate) {
279 super(out, obfuscationDelegate);
284 protected void generatePrologue() throws IOException {
285 out.append("bck2brwsr.registerExtension(function(exports) {\n"
286 + " var vm = {};\n");
290 protected void generateEpilogue() throws IOException {
295 String getExportsObject() {