1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Tue May 06 17:46:47 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,190 +0,0 @@
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, int skip)
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 - if (checkRes != null && --skip < 0) {
1.81 - return checkRes;
1.82 - }
1.83 - } else {
1.84 - checkRes = callFunction(c, res, skip);
1.85 - if (checkRes != null) {
1.86 - return checkRes;
1.87 - }
1.88 - }
1.89 - }
1.90 - }
1.91 - return null;
1.92 - }
1.93 -
1.94 - @JavaScriptBody(args = { "fn", "res", "skip" }, body =
1.95 - "if (typeof fn === 'function') return fn(res, skip);\n"
1.96 - + "return null;"
1.97 - )
1.98 - private static native byte[] callFunction(Object fn, String res, int skip);
1.99 -
1.100 - @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
1.101 - private static native void log(String msg);
1.102 -
1.103 - private byte[] findRes(String res) throws IOException {
1.104 - Object arr = findResImpl(res);
1.105 - if (arr instanceof FastJar.Entry) {
1.106 - long bef = timeNow();
1.107 - InputStream zip = fj.getInputStream((FastJar.Entry)arr);
1.108 - arr = readFully(new byte[512], zip);
1.109 - putRes(res, arr);
1.110 - log("Reading " + res + " took " + (timeNow() - bef) + "ms");
1.111 - }
1.112 - return (byte[]) arr;
1.113 - }
1.114 -
1.115 - @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
1.116 - private native Object findResImpl(String res);
1.117 -
1.118 - @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
1.119 - private native void putRes(String res, Object arr);
1.120 -
1.121 - private static Zips toZip(String path) throws IOException {
1.122 - URL u = new URL(path);
1.123 - byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
1.124 - return new Zips(path, zipData);
1.125 - }
1.126 -
1.127 - private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
1.128 - try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
1.129 - String cp = is.toString();
1.130 - if (cp != null) {
1.131 - cp = cp.trim();
1.132 - for (int p = 0; p < cp.length();) {
1.133 - int n = cp.indexOf(' ', p);
1.134 - if (n == -1) {
1.135 - n = cp.length();
1.136 - }
1.137 - String el = cp.substring(p, n);
1.138 - URL u = new URL(new URL(url), el);
1.139 - classpath = addToArray(classpath, u.toString());
1.140 - p = n + 1;
1.141 - }
1.142 - }
1.143 - return is.getMainClass();
1.144 - }
1.145 - }
1.146 -
1.147 - private static Object addToArray(Object arr, String value) {
1.148 - final int last = length(arr);
1.149 - Object ret = enlargeArray(arr, last + 1);
1.150 - set(ret, last, value);
1.151 - return ret;
1.152 - }
1.153 -
1.154 - @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
1.155 - private static native Object enlargeArray(Object arr, int len);
1.156 - @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
1.157 - private static native void enlargeBytes(byte[] arr, int len);
1.158 -
1.159 - @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
1.160 - private static native void sliceArray(byte[] arr, int len);
1.161 -
1.162 - private static Object readFully(byte[] arr, InputStream zip) throws IOException {
1.163 - int offset = 0;
1.164 - for (;;) {
1.165 - int len = zip.read(arr, offset, arr.length - offset);
1.166 - if (len == -1) {
1.167 - break;
1.168 - }
1.169 - offset += len;
1.170 - if (offset == arr.length) {
1.171 - enlargeBytes(arr, arr.length + 4096);
1.172 - }
1.173 - }
1.174 - sliceArray(arr, offset);
1.175 - return arr;
1.176 - }
1.177 -
1.178 - private static long timeNow() {
1.179 - double time = m();
1.180 - if (time >= 0) {
1.181 - return (long)time;
1.182 - }
1.183 - return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
1.184 - }
1.185 - @JavaScriptBody(args = {}, body =
1.186 - "if (typeof window.performance === 'undefined') return -1;\n"
1.187 - + "if (typeof window.performance.now === 'undefined') return -1;\n"
1.188 - + "return window.performance.now();"
1.189 - )
1.190 - private static native double m();
1.191 -
1.192 -
1.193 -}