Starting and also shutdowning the browser. Currently optimized from chrome
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
37 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
38 new VM(out).doCompile(l, names);
40 protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
41 out.append("(function VM(global) {");
42 out.append("\n var vm = {};");
43 StringArray processed = new StringArray();
44 StringArray initCode = new StringArray();
45 for (String baseClass : names.toArray()) {
46 references.add(baseClass);
49 for (String n : references.toArray()) {
50 if (processed.contains(n)) {
58 InputStream is = loadClass(l, name);
60 throw new IOException("Can't find class " + name);
63 String ic = compile(is);
65 initCode.add(ic == null ? "" : ic);
66 } catch (RuntimeException ex) {
67 if (out instanceof CharSequence) {
68 CharSequence seq = (CharSequence)out;
69 int lastBlock = seq.length();
70 while (lastBlock-- > 0) {
71 if (seq.charAt(lastBlock) == '{') {
75 throw new IOException("Error while compiling " + name + "\n"
76 + seq.subSequence(lastBlock + 1, seq.length()), ex
79 throw new IOException("Error while compiling " + name + "\n"
86 for (String resource : scripts.toArray()) {
87 while (resource.startsWith("/")) {
88 resource = resource.substring(1);
90 InputStream emul = l.get(resource);
92 throw new IOException("Can't find " + resource);
94 readResource(emul, out);
96 scripts = new StringArray();
98 StringArray toInit = StringArray.asList(references.toArray());
101 for (String ic : toInit.toArray()) {
102 int indx = processed.indexOf(ic);
104 out.append(initCode.toArray()[indx]).append("\n");
105 initCode.toArray()[indx] = "";
110 " global.bck2brwsr = function() {\n"
111 + " var args = arguments;\n"
112 + " var loader = {};\n"
113 + " loader.vm = vm;\n"
114 + " loader.loadClass = function(name) {\n"
115 + " var attr = name.replace__Ljava_lang_String_2CC(name, '.','_');\n"
116 + " var fn = vm[attr];\n"
117 + " if (fn) return fn(false);\n"
118 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
119 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
120 + " load___3Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
122 + " if (args[0]) vm.loadClass = loader.loadClass;\n"
123 + " return loader;\n"
125 out.append("}(this));");
127 private static void readResource(InputStream emul, Appendable out) throws IOException {
131 int ch = emul.read();
135 if (ch < 0 || ch > 255) {
136 throw new IOException("Invalid char in emulation " + ch);
143 out.append((char)ch);
150 out.append('/').append((char)ch);
173 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
174 return l.get(name + ".class"); // NOI18N
177 static String toString(String name) throws IOException {
178 StringBuilder sb = new StringBuilder();
179 // compile(sb, name);
180 return sb.toString().toString();
183 private StringArray scripts = new StringArray();
184 private StringArray references = new StringArray();
187 protected boolean requireReference(String cn) {
188 if (references.contains(cn)) {
196 protected void requireScript(String resourcePath) {
197 scripts.add(resourcePath);
201 String assignClass(String className) {
202 return "vm." + className + " = ";
206 String accessClass(String className) {
207 return "vm." + className;