Doing the initial classpath cycle in JavaScript to allow loading of the VM on demand closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 18 May 2014 08:42:56 +0200
branchclosure
changeset 1581cf0f746f1a7f
parent 1580 315c740b8c86
child 1582 32499c3c9cfa
Doing the initial classpath cycle in JavaScript to allow loading of the VM on demand
rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java	Sun May 18 07:43:33 2014 +0200
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java	Sun May 18 08:42:56 2014 +0200
     1.3 @@ -43,43 +43,50 @@
     1.4      
     1.5      private static boolean doingToZip;
     1.6      
     1.7 -    
     1.8 -    @Exported static byte[] loadBytes(String resource, Object classpath, int skip) 
     1.9 +    static byte[] loadBytes(String resource, Object classpath, int skip)
    1.10      throws IOException, ClassNotFoundException {
    1.11          for (int i = 0; i < length(classpath); i++) {
    1.12 -            Object c = at(classpath, i);
    1.13 -            if (c instanceof String && !doingToZip) {
    1.14 -                try {
    1.15 -                    doingToZip = true;
    1.16 -                    String url = (String)c;
    1.17 -                    final Bck2Brwsr.Resources z = toZip(url);
    1.18 -                    c = set(classpath, i, z);
    1.19 -                    final byte[] man = readBytes(z, "META-INF/MANIFEST.MF");
    1.20 -                    if (man != null) {
    1.21 -                        String mainClass = processClassPathAttr(man, url, classpath);
    1.22 +            byte[] arr = loadBytes(resource, classpath, i, skip);
    1.23 +            if (arr != null) {
    1.24 +                return arr;
    1.25 +            }
    1.26 +        }
    1.27 +        return null;
    1.28 +    }
    1.29 +    @Exported static byte[] loadBytes(String resource, Object classpath, int i, int skip) 
    1.30 +    throws IOException, ClassNotFoundException {
    1.31 +        Object c = at(classpath, i);
    1.32 +        if (c instanceof String && !doingToZip) {
    1.33 +            try {
    1.34 +                doingToZip = true;
    1.35 +                String url = (String)c;
    1.36 +                final Bck2Brwsr.Resources z = toZip(url);
    1.37 +                c = set(classpath, i, z);
    1.38 +                final byte[] man = readBytes(z, "META-INF/MANIFEST.MF");
    1.39 +                if (man != null) {
    1.40 +                    String mainClass = processClassPathAttr(man, url, classpath);
    1.41  //                        if (mainClass != null) {
    1.42  //                            Class.forName(mainClass);
    1.43  //                        }
    1.44 -                    }
    1.45 -                } catch (IOException ex) {
    1.46 -                    set(classpath, i, ex);
    1.47 -                    log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
    1.48 -                } finally {
    1.49 -                    doingToZip = false;
    1.50                  }
    1.51 +            } catch (IOException ex) {
    1.52 +                set(classpath, i, ex);
    1.53 +                log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
    1.54 +            } finally {
    1.55 +                doingToZip = false;
    1.56              }
    1.57 -            if (resource != null) {
    1.58 -                byte[] checkRes;
    1.59 -                if (c instanceof Bck2Brwsr.Resources) {
    1.60 -                    checkRes = readBytes((Bck2Brwsr.Resources)c, resource);
    1.61 -                    if (checkRes != null && --skip < 0) {
    1.62 -                        return checkRes;
    1.63 -                    }
    1.64 -                } else {
    1.65 -                    checkRes = callFunction(c, resource, skip);
    1.66 -                    if (checkRes != null) {
    1.67 -                        return checkRes;
    1.68 -                    }
    1.69 +        }
    1.70 +        if (resource != null) {
    1.71 +            byte[] checkRes;
    1.72 +            if (c instanceof Bck2Brwsr.Resources) {
    1.73 +                checkRes = readBytes((Bck2Brwsr.Resources)c, resource);
    1.74 +                if (checkRes != null && --skip < 0) {
    1.75 +                    return checkRes;
    1.76 +                }
    1.77 +            } else {
    1.78 +                checkRes = callFunction(c, resource, skip);
    1.79 +                if (checkRes != null) {
    1.80 +                    return checkRes;
    1.81                  }
    1.82              }
    1.83          }
     2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Sun May 18 07:43:33 2014 +0200
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Sun May 18 08:42:56 2014 +0200
     2.3 @@ -506,9 +506,14 @@
     2.4                  + "      } else {\n"
     2.5                  + "        var arrSize = 0;\n"
     2.6                  + "      };\n"
     2.7 -                + "      var ret = vm['org_apidesign_vm4brwsr_ClassPath'](false).\n"
     2.8 -                + "        loadBytes___3BLjava_lang_String_2Ljava_lang_Object_2I(name, args, skip);\n"
     2.9 -                + "      if (ret !== null) return ret;\n"
    2.10 +                + "      for (var i = 0; i < args.length; i++) {\n"
    2.11 +                + "        var at = args[i];\n"
    2.12 +                + "        var ret;\n"
    2.13 +                + "        if (typeof at === 'function') ret = at(name, skip);\n"
    2.14 +                + "        else ret = vm['org_apidesign_vm4brwsr_ClassPath'](false).\n"
    2.15 +                + "          loadBytes___3BLjava_lang_String_2Ljava_lang_Object_2II(name, args, i, skip);\n"
    2.16 +                + "        if (ret !== null) return ret;\n"
    2.17 +                + "      }\n"
    2.18                  + "      while (knownExtensions < extensions.length) {\n"
    2.19                  + "        vm['registerResource'] = registerResource;\n"
    2.20                  + "        extensions[knownExtensions++](vm);\n"