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) {var fillInVMSkeleton = function(vm) {");
52 StringArray processed = new StringArray();
53 StringArray initCode = new StringArray();
54 for (String baseClass : names.toArray()) {
55 references.add(baseClass);
58 for (String n : references.toArray()) {
59 if (processed.contains(n)) {
67 InputStream is = loadClass(l, name);
69 throw new IOException("Can't find class " + name);
72 String ic = compile(is);
74 initCode.add(ic == null ? "" : ic);
75 } catch (RuntimeException ex) {
76 if (out instanceof CharSequence) {
77 CharSequence seq = (CharSequence)out;
78 int lastBlock = seq.length();
79 while (lastBlock-- > 0) {
80 if (seq.charAt(lastBlock) == '{') {
84 throw new IOException("Error while compiling " + name + "\n"
85 + seq.subSequence(lastBlock + 1, seq.length()), ex
88 throw new IOException("Error while compiling " + name + "\n"
95 for (String resource : scripts.toArray()) {
96 while (resource.startsWith("/")) {
97 resource = resource.substring(1);
99 InputStream emul = l.get(resource);
101 throw new IOException("Can't find " + resource);
103 readResource(emul, out);
105 scripts = new StringArray();
107 StringArray toInit = StringArray.asList(references.toArray());
110 for (String ic : toInit.toArray()) {
111 int indx = processed.indexOf(ic);
113 out.append(initCode.toArray()[indx]).append("\n");
114 initCode.toArray()[indx] = "";
121 + " global.bck2brwsr = function() {\n"
122 + " var args = arguments;\n"
123 + " var vm = fillInVMSkeleton({});\n"
124 + " var loader = {};\n"
125 + " var init = null;\n"
126 + " loader.vm = vm;\n"
127 + " if (args.length == 1 && typeof args[0] !== 'function') {;\n"
128 + " var classpath = args[0];\n"
129 + " init = args[0] = function(name) {\n"
130 + " return vm.org_apidesign_vm4brwsr_Zips(false).loadFromCp___3B_3Ljava_lang_Object_2Ljava_lang_String_2(classpath, name);\n"
133 + " loader.loadClass = function(name) {\n"
134 + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
135 + " var fn = vm[attr];\n"
136 + " if (fn) return fn(false);\n"
137 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
138 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
139 + " load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
141 + " if (args[0]) {\n"
142 + " if (vm.loadClass) {\n"
143 + " throw 'Cannot initialize the bck2brwsr VM twice!';\n"
145 + " vm.loadClass = loader.loadClass;\n"
146 + " vm.loadBytes = function(name) {\n"
147 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
148 + " return args[0](name);\n"
151 + " if (init) init(null);\n"
152 + " return loader;\n"
154 out.append("}(this));");
156 private static void readResource(InputStream emul, Appendable out) throws IOException {
160 int ch = emul.read();
164 if (ch < 0 || ch > 255) {
165 throw new IOException("Invalid char in emulation " + ch);
172 out.append((char)ch);
179 out.append('/').append((char)ch);
202 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
203 return l.get(name + ".class"); // NOI18N
206 static String toString(String name) throws IOException {
207 StringBuilder sb = new StringBuilder();
208 // compile(sb, name);
209 return sb.toString().toString();
212 private StringArray scripts = new StringArray();
213 private StringArray references = new StringArray();
216 protected boolean requireReference(String cn) {
217 if (references.contains(cn)) {
225 protected void requireScript(String resourcePath) {
226 scripts.add(resourcePath);
230 String assignClass(String className) {
231 return "vm." + className + " = ";
235 String accessClass(String className) {
236 return "vm." + className;