1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,187 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.vm4brwsr;
1.22 +
1.23 +import java.io.ByteArrayInputStream;
1.24 +import java.io.IOException;
1.25 +import java.io.InputStream;
1.26 +import java.net.URL;
1.27 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.28 +import org.apidesign.bck2brwsr.emul.zip.FastJar;
1.29 +
1.30 +/** Conversion from classpath to load function.
1.31 + *
1.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.33 + */
1.34 +final class Zips {
1.35 + private final FastJar fj;
1.36 +
1.37 + private Zips(String path, byte[] zipData) throws IOException {
1.38 + long bef = timeNow();
1.39 + fj = new FastJar(zipData);
1.40 + for (FastJar.Entry e : fj.list()) {
1.41 + putRes(e.name, e);
1.42 + }
1.43 + log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
1.44 + }
1.45 +
1.46 + public static void init() {
1.47 + }
1.48 + @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
1.49 + private static native int length(Object arr);
1.50 + @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
1.51 + private static native Object at(Object arr, int index);
1.52 + @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
1.53 + private static native Object set(Object arr, int index, Object value);
1.54 +
1.55 + public static byte[] loadFromCp(Object classpath, String res)
1.56 + throws IOException, ClassNotFoundException {
1.57 + for (int i = 0; i < length(classpath); i++) {
1.58 + Object c = at(classpath, i);
1.59 + if (c instanceof String) {
1.60 + try {
1.61 + String url = (String)c;
1.62 + final Zips z = toZip(url);
1.63 + c = set(classpath, i, z);
1.64 + final byte[] man = z.findRes("META-INF/MANIFEST.MF");
1.65 + if (man != null) {
1.66 + String mainClass = processClassPathAttr(man, url, classpath);
1.67 +// if (mainClass != null) {
1.68 +// Class.forName(mainClass);
1.69 +// }
1.70 + }
1.71 + } catch (IOException ex) {
1.72 + set(classpath, i, ex);
1.73 + log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
1.74 + }
1.75 + }
1.76 + if (res != null) {
1.77 + byte[] checkRes;
1.78 + if (c instanceof Zips) {
1.79 + checkRes = ((Zips)c).findRes(res);
1.80 + } else {
1.81 + checkRes = callFunction(c, res);
1.82 + }
1.83 + if (checkRes != null) {
1.84 + return checkRes;
1.85 + }
1.86 + }
1.87 + }
1.88 + return null;
1.89 + }
1.90 +
1.91 + @JavaScriptBody(args = { "fn", "res" }, body =
1.92 + "if (typeof fn === 'function') return fn(res);\n"
1.93 + + "return null;"
1.94 + )
1.95 + private static native byte[] callFunction(Object fn, String res);
1.96 +
1.97 + @JavaScriptBody(args = { "msg" }, body = "console.log(msg.toString());")
1.98 + private static native void log(String msg);
1.99 +
1.100 + private byte[] findRes(String res) throws IOException {
1.101 + Object arr = findResImpl(res);
1.102 + if (arr instanceof FastJar.Entry) {
1.103 + long bef = timeNow();
1.104 + InputStream zip = fj.getInputStream((FastJar.Entry)arr);
1.105 + arr = readFully(new byte[512], zip);
1.106 + putRes(res, arr);
1.107 + log("Reading " + res + " took " + (timeNow() - bef) + "ms");
1.108 + }
1.109 + return (byte[]) arr;
1.110 + }
1.111 +
1.112 + @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
1.113 + private native Object findResImpl(String res);
1.114 +
1.115 + @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
1.116 + private native void putRes(String res, Object arr);
1.117 +
1.118 + private static Zips toZip(String path) throws IOException {
1.119 + URL u = new URL(path);
1.120 + byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
1.121 + return new Zips(path, zipData);
1.122 + }
1.123 +
1.124 + private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
1.125 + try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
1.126 + String cp = is.toString();
1.127 + if (cp != null) {
1.128 + cp = cp.trim();
1.129 + for (int p = 0; p < cp.length();) {
1.130 + int n = cp.indexOf(' ', p);
1.131 + if (n == -1) {
1.132 + n = cp.length();
1.133 + }
1.134 + String el = cp.substring(p, n);
1.135 + URL u = new URL(new URL(url), el);
1.136 + classpath = addToArray(classpath, u.toString());
1.137 + p = n + 1;
1.138 + }
1.139 + }
1.140 + return is.getMainClass();
1.141 + }
1.142 + }
1.143 +
1.144 + private static Object addToArray(Object arr, String value) {
1.145 + final int last = length(arr);
1.146 + Object ret = enlargeArray(arr, last + 1);
1.147 + set(ret, last, value);
1.148 + return ret;
1.149 + }
1.150 +
1.151 + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
1.152 + private static native Object enlargeArray(Object arr, int len);
1.153 + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
1.154 + private static native void enlargeBytes(byte[] arr, int len);
1.155 +
1.156 + @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
1.157 + private static native void sliceArray(byte[] arr, int len);
1.158 +
1.159 + private static Object readFully(byte[] arr, InputStream zip) throws IOException {
1.160 + int offset = 0;
1.161 + for (;;) {
1.162 + int len = zip.read(arr, offset, arr.length - offset);
1.163 + if (len == -1) {
1.164 + break;
1.165 + }
1.166 + offset += len;
1.167 + if (offset == arr.length) {
1.168 + enlargeBytes(arr, arr.length + 4096);
1.169 + }
1.170 + }
1.171 + sliceArray(arr, offset);
1.172 + return arr;
1.173 + }
1.174 +
1.175 + private static long timeNow() {
1.176 + double time = m();
1.177 + if (time >= 0) {
1.178 + return (long)time;
1.179 + }
1.180 + return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
1.181 + }
1.182 + @JavaScriptBody(args = {}, body =
1.183 + "if (typeof window.performance === 'undefined') return -1;\n"
1.184 + + "if (typeof window.performance.now === 'undefined') return -1;\n"
1.185 + + "return window.performance.now();"
1.186 + )
1.187 + private static native double m();
1.188 +
1.189 +
1.190 +}