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
38 boolean debug(String msg) throws IOException {
42 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
43 new VM(out).doCompile(l, names);
45 protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
46 out.append("(function VM(global) {");
47 out.append("\n var vm = {};");
48 StringArray processed = new StringArray();
49 StringArray initCode = new StringArray();
50 for (String baseClass : names.toArray()) {
51 references.add(baseClass);
54 for (String n : references.toArray()) {
55 if (processed.contains(n)) {
63 InputStream is = loadClass(l, name);
65 throw new IOException("Can't find class " + name);
68 String ic = compile(is);
70 initCode.add(ic == null ? "" : ic);
71 } catch (RuntimeException ex) {
72 if (out instanceof CharSequence) {
73 CharSequence seq = (CharSequence)out;
74 int lastBlock = seq.length();
75 while (lastBlock-- > 0) {
76 if (seq.charAt(lastBlock) == '{') {
80 throw new IOException("Error while compiling " + name + "\n"
81 + seq.subSequence(lastBlock + 1, seq.length()), ex
84 throw new IOException("Error while compiling " + name + "\n"
91 for (String resource : scripts.toArray()) {
92 while (resource.startsWith("/")) {
93 resource = resource.substring(1);
95 InputStream emul = l.get(resource);
97 throw new IOException("Can't find " + resource);
99 readResource(emul, out);
101 scripts = new StringArray();
103 StringArray toInit = StringArray.asList(references.toArray());
106 for (String ic : toInit.toArray()) {
107 int indx = processed.indexOf(ic);
109 out.append(initCode.toArray()[indx]).append("\n");
110 initCode.toArray()[indx] = "";
115 " global.bck2brwsr = function() {\n"
116 + " var args = arguments;\n"
117 + " var loader = {};\n"
118 + " loader.vm = vm;\n"
119 + " loader.loadClass = function(name) {\n"
120 + " var attr = name.replace__sCC(name, '.','_');\n"
121 + " var fn = vm[attr];\n"
122 + " if (fn) return fn(false);\n"
123 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
124 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
125 + " load___3oos_3o(loader, name, args);\n"
127 + " if (args[0]) vm.loadClass = loader.loadClass;\n"
128 + " return loader;\n"
130 out.append("}(this));");
132 private static void readResource(InputStream emul, Appendable out) throws IOException {
136 int ch = emul.read();
140 if (ch < 0 || ch > 255) {
141 throw new IOException("Invalid char in emulation " + ch);
148 out.append((char)ch);
155 out.append('/').append((char)ch);
178 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
179 return l.get(name + ".class"); // NOI18N
182 static String toString(String name) throws IOException {
183 StringBuilder sb = new StringBuilder();
184 // compile(sb, name);
185 return sb.toString().toString();
188 private StringArray scripts = new StringArray();
189 private StringArray references = new StringArray();
192 protected boolean requireReference(String cn) {
193 if (references.contains(cn)) {
201 protected void requireScript(String resourcePath) {
202 scripts.add(resourcePath);
206 String assignClass(String className) {
207 return "vm." + className + " = ";
211 String accessClass(String className) {
212 return "vm." + className;