Need to skip URI and System references, as they are not part of minimal API profile
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) {
32 private VM(Appendable out, ObfuscationDelegate obfuscationDelegate) {
33 super(out, obfuscationDelegate);
37 // uses VMLazy to load dynamic classes
38 boolean assertsOn = false;
39 assert assertsOn = true;
47 boolean debug(String msg) throws IOException {
51 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
52 new VM(out).doCompile(l, names);
55 static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names,
56 ObfuscationDelegate obfuscationDelegate) throws IOException {
57 new VM(out, obfuscationDelegate).doCompile(l, names);
60 protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
61 out.append("(function VM(global) {var fillInVMSkeleton = function(vm) {");
62 StringArray processed = new StringArray();
63 StringArray initCode = new StringArray();
64 StringArray skipClass = new StringArray();
65 for (String baseClass : names.toArray()) {
66 references.add(baseClass);
69 for (String n : references.toArray()) {
70 if (skipClass.contains(n)) {
73 if (processed.contains(n)) {
81 InputStream is = loadClass(l, name);
87 String ic = compile(is);
89 initCode.add(ic == null ? "" : ic);
90 } catch (RuntimeException ex) {
91 if (out instanceof CharSequence) {
92 CharSequence seq = (CharSequence)out;
93 int lastBlock = seq.length();
94 while (lastBlock-- > 0) {
95 if (seq.charAt(lastBlock) == '{') {
99 throw new IOException("Error while compiling " + name + "\n"
100 + seq.subSequence(lastBlock + 1, seq.length()), ex
103 throw new IOException("Error while compiling " + name + "\n"
110 for (String resource : scripts.toArray()) {
111 while (resource.startsWith("/")) {
112 resource = resource.substring(1);
114 InputStream emul = l.get(resource);
116 throw new IOException("Can't find " + resource);
118 readResource(emul, out);
120 scripts = new StringArray();
122 StringArray toInit = StringArray.asList(references.toArray());
125 for (String ic : toInit.toArray()) {
126 int indx = processed.indexOf(ic);
128 final String theCode = initCode.toArray()[indx];
129 if (!theCode.isEmpty()) {
130 out.append(theCode).append("\n");
132 initCode.toArray()[indx] = "";
139 + " global.bck2brwsr = function() {\n"
140 + " var args = Array.prototype.slice.apply(arguments);\n"
141 + " var vm = fillInVMSkeleton({});\n"
142 + " var loader = {};\n"
143 + " loader.vm = vm;\n"
144 + " loader.loadClass = function(name) {\n"
145 + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n"
146 + " var fn = vm[attr];\n"
147 + " if (fn) return fn(false);\n"
148 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
149 + " load__Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
151 + " if (vm.loadClass) {\n"
152 + " throw 'Cannot initialize the bck2brwsr VM twice!';\n"
154 + " vm.loadClass = loader.loadClass;\n"
155 + " vm.loadBytes = function(name) {\n"
156 + " return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
157 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
159 + " vm.java_lang_reflect_Array(false);\n"
160 + " vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
161 + " loadBytes___3BLjava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, null, args);\n"
162 + " return loader;\n"
164 out.append("}(this));");
166 private static void readResource(InputStream emul, Appendable out) throws IOException {
170 int ch = emul.read();
174 if (ch < 0 || ch > 255) {
175 throw new IOException("Invalid char in emulation " + ch);
182 out.append((char)ch);
189 out.append('/').append((char)ch);
212 private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
213 return l.get(name + ".class"); // NOI18N
216 static String toString(String name) throws IOException {
217 StringBuilder sb = new StringBuilder();
218 // compile(sb, name);
219 return sb.toString().toString();
222 private StringArray scripts = new StringArray();
223 private StringArray references = new StringArray();
226 protected boolean requireReference(String cn) {
227 if (references.contains(cn)) {
235 protected void requireScript(String resourcePath) {
236 scripts.add(resourcePath);
240 String assignClass(String className) {
241 return "vm." + className + " = ";
245 String accessClass(String className) {
246 return "vm." + className;
250 String getVMObject() {