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 {
35 long bef = currentTimeMillis();
36 fj = new FastJar(zipData);
37 for (FastJar.Entry e : fj.list()) {
40 log("Iterating thru " + path + " took " + (currentTimeMillis() - bef) + "ms");
43 public static void init() {
46 public static byte[] loadFromCp(Object[] classpath, String res) throws Exception {
47 for (int i = 0; i < classpath.length; i++) {
48 Object c = classpath[i];
49 if (c instanceof String) {
51 String url = (String)c;
52 final Zips z = toZip(url);
54 final byte[] man = z.findRes("META-INF/MANIFEST.MF");
56 String mainClass = processClassPathAttr(man, url, classpath);
57 if (mainClass != null) {
58 Class.forName(mainClass);
61 } catch (Exception ex) {
66 if (res != null && c instanceof Zips) {
67 Object checkRes = ((Zips)c).findRes(res);
68 if (checkRes instanceof byte[]) {
69 return (byte[])checkRes;
76 @JavaScriptBody(args = { "msg" }, body = "console.log(msg.toString());")
77 private static native void log(String msg);
79 private byte[] findRes(String res) throws IOException {
80 Object arr = findResImpl(res);
81 if (arr instanceof FastJar.Entry) {
82 long bef = currentTimeMillis();
83 InputStream zip = fj.getInputStream((FastJar.Entry)arr);
84 arr = readFully(new byte[512], zip);
86 log("Reading " + res + " took " + (currentTimeMillis() - bef) + "ms");
91 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
92 private native Object findResImpl(String res);
94 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
95 private native void putRes(String res, Object arr);
97 private static Zips toZip(String path) throws IOException {
98 URL u = new URL(path);
99 byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
100 return new Zips(path, zipData);
103 private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
104 try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
105 String cp = is.toString();
108 for (int p = 0; p < cp.length();) {
109 int n = cp.indexOf(' ', p);
113 String el = cp.substring(p, n);
114 URL u = new URL(new URL(url), el);
115 classpath = addToArray(classpath, u.toString());
119 return is.getMainClass();
123 private static Object[] addToArray(Object[] arr, String value) {
124 final int last = arr.length;
125 Object[] ret = enlargeArray(arr, last + 1);
130 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;throw('Arr: ' + arr);")
131 private static native Object[] enlargeArray(Object[] arr, int len);
132 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
133 private static native void enlargeArray(byte[] arr, int len);
135 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
136 private static native void sliceArray(byte[] arr, int len);
138 private static Object readFully(byte[] arr, InputStream zip) throws IOException {
141 int len = zip.read(arr, offset, arr.length - offset);
146 if (offset == arr.length) {
147 enlargeArray(arr, arr.length + 4096);
150 sliceArray(arr, offset);
154 private static long currentTimeMillis() {
157 @JavaScriptBody(args = { }, body = "return window.performance.now();")
158 private static native double m();