Recognizing Main-Class attribute, so initializing the VM is just about passing in correct classpath
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) {");
52 out.append("\n var vm = {};");
53 StringArray processed = new StringArray();
54 StringArray initCode = new StringArray();
55 for (String baseClass : names.toArray()) {
56 references.add(baseClass);
59 for (String n : references.toArray()) {
60 if (processed.contains(n)) {
68 InputStream is = loadClass(l, name);
70 throw new IOException("Can't find class " + name);
73 String ic = compile(is);
75 initCode.add(ic == null ? "" : ic);
76 } catch (RuntimeException ex) {
77 if (out instanceof CharSequence) {
78 CharSequence seq = (CharSequence)out;
79 int lastBlock = seq.length();
80 while (lastBlock-- > 0) {
81 if (seq.charAt(lastBlock) == '{') {
85 throw new IOException("Error while compiling " + name + "\n"
86 + seq.subSequence(lastBlock + 1, seq.length()), ex
89 throw new IOException("Error while compiling " + name + "\n"
96 for (String resource : scripts.toArray()) {
97 while (resource.startsWith("/")) {
98 resource = resource.substring(1);
100 InputStream emul = l.get(resource);
102 throw new IOException("Can't find " + resource);
104 readResource(emul, out);
106 scripts = new StringArray();
108 StringArray toInit = StringArray.asList(references.toArray());
111 for (String ic : toInit.toArray()) {
112 int indx = processed.indexOf(ic);
114 out.append(initCode.toArray()[indx]).append("\n");
115 initCode.toArray()[indx] = "";
120 " global.bck2brwsr = function() {\n"
121 + " var args = arguments;\n"
122 + " var loader = {};\n"
123 + " var init = null;\n"
124 + " loader.vm = vm;\n"
125 + " if (args.length == 1 && typeof args[0] !== 'function') {;\n"
126 + " var classpath = args[0];\n"
127 + " init = args[0] = function(name) {\n"
128 + " return vm.org_apidesign_vm4brwsr_Zips(false).loadFromCp___3B_3Ljava_lang_Object_2Ljava_lang_String_2(classpath, name);\n"
131 + " loader.loadClass = function(name) {\n"
132 + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
133 + " var fn = vm[attr];\n"
134 + " if (fn) return fn(false);\n"
135 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
136 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
137 + " load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
139 + " if (args[0]) {\n"
140 + " vm.loadClass = loader.loadClass;\n"
141 + " vm.loadBytes = function(name) {\n"
142 + " if (!args[0]) throw 'bck2brwsr initialized without loader function, cannot load ' + name;\n"
143 + " return args[0](name);\n"
146 + " if (init) init(null);\n"
147 + " return loader;\n"
149 out.append("}(this));");
151 private static void readResource(InputStream emul, Appendable out) throws IOException {
155 int ch = emul.read();
159 if (ch < 0 || ch > 255) {
160 throw new IOException("Invalid char in emulation " + ch);
167 out.append((char)ch);
174 out.append('/').append((char)ch);
197 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
198 return l.get(name + ".class"); // NOI18N
201 static String toString(String name) throws IOException {
202 StringBuilder sb = new StringBuilder();
203 // compile(sb, name);
204 return sb.toString().toString();
207 private StringArray scripts = new StringArray();
208 private StringArray references = new StringArray();
211 protected boolean requireReference(String cn) {
212 if (references.contains(cn)) {
220 protected void requireScript(String resourcePath) {
221 scripts.add(resourcePath);
225 String assignClass(String className) {
226 return "vm." + className + " = ";
230 String accessClass(String className) {
231 return "vm." + className;