rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 14 May 2014 15:57:52 +0200
branchclosure
changeset 1575 08c8821c962d
parent 1554 7ba27baf5f3f
child 1576 3e9bfed0d88e
permissions -rw-r--r--
First step towards calling directly into ClassPath and using VMLazy only for JIT compilation
     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.Exported;
    25 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    26 
    27 /** Conversion from classpath to load function.
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 final class ClassPath {
    32     private ClassPath() {
    33     }
    34     
    35     public static void init() {
    36     }
    37     @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
    38     private static native int length(Object arr);
    39     @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
    40     private static native Object at(Object arr, int index);
    41     @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
    42     private static native Object set(Object arr, int index, Object value);
    43     
    44     private static boolean doingToZip;
    45     
    46     
    47     @Exported static byte[] loadBytes(Object loader, String name, Object[] arguments, int skip) throws Exception {
    48         return ClassPath.loadFromCp(arguments, name, skip);
    49     }
    50     static byte[] loadFromCp(Object classpath, String res, int skip) 
    51     throws IOException, ClassNotFoundException {
    52         for (int i = 0; i < length(classpath); i++) {
    53             Object c = at(classpath, i);
    54             if (c instanceof String && !doingToZip) {
    55                 try {
    56                     doingToZip = true;
    57                     String url = (String)c;
    58                     final Bck2Brwsr.Resources z = toZip(url);
    59                     c = set(classpath, i, z);
    60                     final byte[] man = readBytes(z, "META-INF/MANIFEST.MF");
    61                     if (man != null) {
    62                         String mainClass = processClassPathAttr(man, url, classpath);
    63 //                        if (mainClass != null) {
    64 //                            Class.forName(mainClass);
    65 //                        }
    66                     }
    67                 } catch (IOException ex) {
    68                     set(classpath, i, ex);
    69                     log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
    70                 } finally {
    71                     doingToZip = false;
    72                 }
    73             }
    74             if (res != null) {
    75                 byte[] checkRes;
    76                 if (c instanceof Bck2Brwsr.Resources) {
    77                     checkRes = readBytes((Bck2Brwsr.Resources)c, res);
    78                     if (checkRes != null && --skip < 0) {
    79                         return checkRes;
    80                     }
    81                 } else {
    82                     checkRes = callFunction(c, res, skip);
    83                     if (checkRes != null) {
    84                         return checkRes;
    85                     }
    86                 }
    87             }
    88         }
    89         return null;
    90     }
    91     
    92     @JavaScriptBody(args = { "fn", "res", "skip" }, body = 
    93         "if (typeof fn === 'function') return fn(res, skip);\n"
    94       + "return null;"
    95     )
    96     private static native byte[] callFunction(Object fn, String res, int skip);
    97     
    98     @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
    99     private static native void log(String msg);
   100 
   101     private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
   102         try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
   103             String cp = is.toString();
   104             if (cp != null) {
   105                 cp = cp.trim();
   106                 for (int p = 0; p < cp.length();) {
   107                     int n = cp.indexOf(' ', p);
   108                     if (n == -1) {
   109                         n = cp.length();
   110                     }
   111                     String el = cp.substring(p, n);
   112                     URL u = new URL(new URL(url), el);
   113                     classpath = addToArray(classpath, u.toString());
   114                     p = n + 1;
   115                 }
   116             }
   117             return is.getMainClass();
   118         }
   119     }
   120 
   121     private static Object addToArray(Object arr, String value) {
   122         final int last = length(arr);
   123         Object ret = enlargeArray(arr, last + 1);
   124         set(ret, last, value);
   125         return ret;
   126     }
   127 
   128     @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
   129     private static native Object enlargeArray(Object arr, int len);
   130 
   131     private static Bck2Brwsr.Resources toZip(String path) throws IOException {
   132         URL u = new URL(path);
   133         byte[] zipData = (byte[]) u.getContent(new Class[]{byte[].class});
   134         Bck2Brwsr.Resources r;
   135         try {
   136             Class<?> fastJar = Class.forName("org.apidesign.bck2brwsr.vmzip.ZipResources");
   137             return (Bck2Brwsr.Resources) fastJar.getConstructor(byte[].class).newInstance(zipData);
   138         } catch (Exception ex) {
   139             log("Reading JARs is only possible with enum.zip module included: " + ex.getMessage());
   140             ex.printStackTrace();
   141             throw new IOException(ex);
   142         }
   143     }
   144     
   145     private static byte[] readBytes(Bck2Brwsr.Resources r, String res) throws IOException {
   146         InputStream is = r.get(res);
   147         if (is == null) {
   148             return null;
   149         }
   150         byte[] arr = new byte[is.available()];
   151         int off = 0;
   152         for (;;) {
   153             int len = is.read(arr, off, arr.length - off);
   154             if (len == -1) {
   155                 break;
   156             }
   157             off += len;
   158         }
   159         is.close();
   160         return arr;
   161     }
   162 }