vm/src/main/java/org/apidesign/vm4brwsr/Zips.java
branchemul
changeset 729 1ee59fe94653
parent 715 7572022945a0
child 743 6dc02c92ba9e
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java	Tue Feb 12 16:46:13 2013 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java	Tue Feb 12 23:30:01 2013 +0100
     1.3 @@ -42,15 +42,22 @@
     1.4      
     1.5      public static void init() {
     1.6      }
     1.7 +    @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
     1.8 +    private static native int length(Object arr);
     1.9 +    @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
    1.10 +    private static native Object at(Object arr, int index);
    1.11 +    @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
    1.12 +    private static native Object set(Object arr, int index, Object value);
    1.13      
    1.14 -    public static byte[] loadFromCp(Object[] classpath, String res) throws Exception {
    1.15 -        for (int i = 0; i < classpath.length; i++) {
    1.16 -            Object c = classpath[i];
    1.17 +    public static byte[] loadFromCp(Object classpath, String res) 
    1.18 +    throws IOException, ClassNotFoundException {
    1.19 +        for (int i = 0; i < length(classpath); i++) {
    1.20 +            Object c = at(classpath, i);
    1.21              if (c instanceof String) {
    1.22                  try {
    1.23                      String url = (String)c;
    1.24                      final Zips z = toZip(url);
    1.25 -                    c = classpath[i] = z;
    1.26 +                    c = set(classpath, i, z);
    1.27                      final byte[] man = z.findRes("META-INF/MANIFEST.MF");
    1.28                      if (man != null) {
    1.29                          String mainClass = processClassPathAttr(man, url, classpath);
    1.30 @@ -58,21 +65,32 @@
    1.31                              Class.forName(mainClass);
    1.32                          }
    1.33                      }
    1.34 -                } catch (Exception ex) {
    1.35 -                    classpath[i] = ex;
    1.36 +                } catch (IOException | ClassNotFoundException ex) {
    1.37 +                    set(classpath, i, ex);
    1.38                      throw ex;
    1.39                  }
    1.40              }
    1.41 -            if (res != null && c instanceof Zips) {
    1.42 -                Object checkRes = ((Zips)c).findRes(res);
    1.43 -                if (checkRes instanceof byte[]) {
    1.44 -                    return (byte[])checkRes;
    1.45 +            if (res != null) {
    1.46 +                byte[] checkRes;
    1.47 +                if (c instanceof Zips) {
    1.48 +                    checkRes = ((Zips)c).findRes(res);
    1.49 +                } else {
    1.50 +                    checkRes = callFunction(c, res);
    1.51 +                }
    1.52 +                if (checkRes != null) {
    1.53 +                    return checkRes;
    1.54                  }
    1.55              }
    1.56          }
    1.57          return null;
    1.58      }
    1.59      
    1.60 +    @JavaScriptBody(args = { "fn", "res" }, body = 
    1.61 +        "if (typeof fn === 'function') return fn(res);\n"
    1.62 +      + "return null;"
    1.63 +    )
    1.64 +    private static native byte[] callFunction(Object fn, String res);
    1.65 +    
    1.66      @JavaScriptBody(args = { "msg" }, body = "console.log(msg.toString());")
    1.67      private static native void log(String msg);
    1.68  
    1.69 @@ -100,7 +118,7 @@
    1.70          return new Zips(path, zipData);
    1.71      }
    1.72  
    1.73 -    private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
    1.74 +    private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
    1.75          try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
    1.76              String cp = is.toString();
    1.77              if (cp != null) {
    1.78 @@ -120,17 +138,17 @@
    1.79          }
    1.80      }
    1.81  
    1.82 -    private static Object[] addToArray(Object[] arr, String value) {
    1.83 -        final int last = arr.length;
    1.84 -        Object[] ret = enlargeArray(arr, last + 1);
    1.85 -        ret[last] = value;
    1.86 +    private static Object addToArray(Object arr, String value) {
    1.87 +        final int last = length(arr);
    1.88 +        Object ret = enlargeArray(arr, last + 1);
    1.89 +        set(ret, last, value);
    1.90          return ret;
    1.91      }
    1.92  
    1.93 -    @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;throw('Arr: ' + arr);")
    1.94 -    private static native Object[] enlargeArray(Object[] arr, int len);
    1.95 +    @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
    1.96 +    private static native Object enlargeArray(Object arr, int len);
    1.97      @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
    1.98 -    private static native void enlargeArray(byte[] arr, int len);
    1.99 +    private static native void enlargeBytes(byte[] arr, int len);
   1.100  
   1.101      @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
   1.102      private static native void sliceArray(byte[] arr, int len);
   1.103 @@ -144,7 +162,7 @@
   1.104              }
   1.105              offset += len;
   1.106              if (offset == arr.length) {
   1.107 -                enlargeArray(arr, arr.length + 4096);
   1.108 +                enlargeBytes(arr, arr.length + 4096);
   1.109              }
   1.110          }
   1.111          sliceArray(arr, offset);