rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Oct 2013 14:56:02 +0200
changeset 1387 350f8aee0f60
parent 1375 a6c71e376889
permissions -rw-r--r--
Different behavior on function calls that find nothing - don't decrement the resource count at all
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.net.URL;
    24 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    25 import org.apidesign.bck2brwsr.emul.zip.FastJar;
    26 
    27 /** Conversion from classpath to load function.
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 final class Zips {
    32     private final FastJar fj;
    33 
    34     private Zips(String path, byte[] zipData) throws IOException {
    35         long bef = timeNow();
    36         fj = new FastJar(zipData);
    37         for (FastJar.Entry e : fj.list()) {
    38             putRes(e.name, e);
    39         }
    40         log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
    41     }
    42     
    43     public static void init() {
    44     }
    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);
    51     
    52     public static byte[] loadFromCp(Object classpath, String res, int skip) 
    53     throws IOException, ClassNotFoundException {
    54         for (int i = 0; i < length(classpath); i++) {
    55             Object c = at(classpath, i);
    56             if (c instanceof String) {
    57                 try {
    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");
    62                     if (man != null) {
    63                         String mainClass = processClassPathAttr(man, url, classpath);
    64 //                        if (mainClass != null) {
    65 //                            Class.forName(mainClass);
    66 //                        }
    67                     }
    68                 } catch (IOException ex) {
    69                     set(classpath, i, ex);
    70                     log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
    71                 }
    72             }
    73             if (res != null) {
    74                 byte[] checkRes;
    75                 if (c instanceof Zips) {
    76                     checkRes = ((Zips)c).findRes(res);
    77                     if (checkRes != null && --skip < 0) {
    78                         return checkRes;
    79                     }
    80                 } else {
    81                     checkRes = callFunction(c, res, skip);
    82                     if (checkRes != null) {
    83                         return checkRes;
    84                     }
    85                 }
    86             }
    87         }
    88         return null;
    89     }
    90     
    91     @JavaScriptBody(args = { "fn", "res", "skip" }, body = 
    92         "if (typeof fn === 'function') return fn(res, skip);\n"
    93       + "return null;"
    94     )
    95     private static native byte[] callFunction(Object fn, String res, int skip);
    96     
    97     @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
    98     private static native void log(String msg);
    99 
   100     private byte[] findRes(String res) throws IOException {
   101         Object arr = findResImpl(res);
   102         if (arr instanceof FastJar.Entry) {
   103             long bef = timeNow();
   104             InputStream zip = fj.getInputStream((FastJar.Entry)arr);
   105             arr = readFully(new byte[512], zip);
   106             putRes(res, arr);
   107             log("Reading " + res + " took " + (timeNow() - bef) + "ms");
   108         }
   109         return (byte[]) arr;
   110     }
   111 
   112     @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
   113     private native Object findResImpl(String res);
   114 
   115     @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
   116     private native void putRes(String res, Object arr);
   117     
   118     private static Zips toZip(String path) throws IOException {
   119         URL u = new URL(path);
   120         byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
   121         return new Zips(path, zipData);
   122     }
   123 
   124     private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
   125         try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
   126             String cp = is.toString();
   127             if (cp != null) {
   128                 cp = cp.trim();
   129                 for (int p = 0; p < cp.length();) {
   130                     int n = cp.indexOf(' ', p);
   131                     if (n == -1) {
   132                         n = cp.length();
   133                     }
   134                     String el = cp.substring(p, n);
   135                     URL u = new URL(new URL(url), el);
   136                     classpath = addToArray(classpath, u.toString());
   137                     p = n + 1;
   138                 }
   139             }
   140             return is.getMainClass();
   141         }
   142     }
   143 
   144     private static Object addToArray(Object arr, String value) {
   145         final int last = length(arr);
   146         Object ret = enlargeArray(arr, last + 1);
   147         set(ret, last, value);
   148         return ret;
   149     }
   150 
   151     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
   152     private static native Object enlargeArray(Object arr, int len);
   153     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
   154     private static native void enlargeBytes(byte[] arr, int len);
   155 
   156     @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
   157     private static native void sliceArray(byte[] arr, int len);
   158 
   159     private static Object readFully(byte[] arr, InputStream zip) throws IOException {
   160         int offset = 0;
   161         for (;;) {
   162             int len = zip.read(arr, offset, arr.length - offset);
   163             if (len == -1) {
   164                 break;
   165             }
   166             offset += len;
   167             if (offset == arr.length) {
   168                 enlargeBytes(arr, arr.length + 4096);
   169             }
   170         }
   171         sliceArray(arr, offset);
   172         return arr;
   173     }
   174 
   175     private static long timeNow() {
   176         double time = m();
   177         if (time >= 0) {
   178             return (long)time;
   179         }
   180         return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
   181     }
   182     @JavaScriptBody(args = {}, body = 
   183         "if (typeof window.performance === 'undefined') return -1;\n"
   184       + "if (typeof window.performance.now === 'undefined') return -1;\n"
   185       + "return window.performance.now();"
   186     )
   187     private static native double m();
   188     
   189     
   190 }