Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
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.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
25 import org.apidesign.bck2brwsr.emul.zip.FastJar;
27 /** Conversion from classpath to load function.
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 private final FastJar fj;
34 private Zips(String path, byte[] zipData) throws IOException {
36 fj = new FastJar(zipData);
37 for (FastJar.Entry e : fj.list()) {
40 log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
43 public static void init() {
45 @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
46 private static native int length(Object arr);
47 @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
48 private static native Object at(Object arr, int index);
49 @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
50 private static native Object set(Object arr, int index, Object value);
52 public static byte[] loadFromCp(Object classpath, String res)
53 throws IOException, ClassNotFoundException {
54 for (int i = 0; i < length(classpath); i++) {
55 Object c = at(classpath, i);
56 if (c instanceof String) {
58 String url = (String)c;
59 final Zips z = toZip(url);
60 c = set(classpath, i, z);
61 final byte[] man = z.findRes("META-INF/MANIFEST.MF");
63 String mainClass = processClassPathAttr(man, url, classpath);
64 // if (mainClass != null) {
65 // Class.forName(mainClass);
68 } catch (IOException ex) {
69 set(classpath, i, ex);
70 log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
75 if (c instanceof Zips) {
76 checkRes = ((Zips)c).findRes(res);
78 checkRes = callFunction(c, res);
80 if (checkRes != null) {
88 @JavaScriptBody(args = { "fn", "res" }, body =
89 "if (typeof fn === 'function') return fn(res);\n"
92 private static native byte[] callFunction(Object fn, String res);
94 @JavaScriptBody(args = { "msg" }, body = "console.log(msg.toString());")
95 private static native void log(String msg);
97 private byte[] findRes(String res) throws IOException {
98 Object arr = findResImpl(res);
99 if (arr instanceof FastJar.Entry) {
100 long bef = timeNow();
101 InputStream zip = fj.getInputStream((FastJar.Entry)arr);
102 arr = readFully(new byte[512], zip);
104 log("Reading " + res + " took " + (timeNow() - bef) + "ms");
109 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
110 private native Object findResImpl(String res);
112 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
113 private native void putRes(String res, Object arr);
115 private static Zips toZip(String path) throws IOException {
116 URL u = new URL(path);
117 byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
118 return new Zips(path, zipData);
121 private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
122 try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
123 String cp = is.toString();
126 for (int p = 0; p < cp.length();) {
127 int n = cp.indexOf(' ', p);
131 String el = cp.substring(p, n);
132 URL u = new URL(new URL(url), el);
133 classpath = addToArray(classpath, u.toString());
137 return is.getMainClass();
141 private static Object addToArray(Object arr, String value) {
142 final int last = length(arr);
143 Object ret = enlargeArray(arr, last + 1);
144 set(ret, last, value);
148 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
149 private static native Object enlargeArray(Object arr, int len);
150 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
151 private static native void enlargeBytes(byte[] arr, int len);
153 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
154 private static native void sliceArray(byte[] arr, int len);
156 private static Object readFully(byte[] arr, InputStream zip) throws IOException {
159 int len = zip.read(arr, offset, arr.length - offset);
164 if (offset == arr.length) {
165 enlargeBytes(arr, arr.length + 4096);
168 sliceArray(arr, offset);
172 private static long timeNow() {
177 return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
179 @JavaScriptBody(args = {}, body =
180 "if (typeof window.performance === 'undefined') return -1;\n"
181 + "if (typeof window.performance.now === 'undefined') return -1;\n"
182 + "return window.performance.now();"
184 private static native double m();