Splitting into two classes: one that deals with classpath, one that deals with ZIP content
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java Wed May 07 17:24:29 2014 +0200
1.3 @@ -0,0 +1,119 @@
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.net.URL;
1.26 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.27 +
1.28 +/** Conversion from classpath to load function.
1.29 + *
1.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.31 + */
1.32 +final class ClassPath {
1.33 + private ClassPath() {
1.34 + }
1.35 +
1.36 + public static void init() {
1.37 + }
1.38 + @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
1.39 + private static native int length(Object arr);
1.40 + @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
1.41 + private static native Object at(Object arr, int index);
1.42 + @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
1.43 + private static native Object set(Object arr, int index, Object value);
1.44 +
1.45 + public static byte[] loadFromCp(Object classpath, String res, int skip)
1.46 + throws IOException, ClassNotFoundException {
1.47 + for (int i = 0; i < length(classpath); i++) {
1.48 + Object c = at(classpath, i);
1.49 + if (c instanceof String) {
1.50 + try {
1.51 + String url = (String)c;
1.52 + final ZipHandler z = ZipHandler.toZip(url);
1.53 + c = set(classpath, i, z);
1.54 + final byte[] man = z.findRes("META-INF/MANIFEST.MF");
1.55 + if (man != null) {
1.56 + String mainClass = processClassPathAttr(man, url, classpath);
1.57 +// if (mainClass != null) {
1.58 +// Class.forName(mainClass);
1.59 +// }
1.60 + }
1.61 + } catch (IOException ex) {
1.62 + set(classpath, i, ex);
1.63 + log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
1.64 + }
1.65 + }
1.66 + if (res != null) {
1.67 + byte[] checkRes;
1.68 + if (c instanceof ZipHandler) {
1.69 + checkRes = ((ZipHandler)c).findRes(res);
1.70 + if (checkRes != null && --skip < 0) {
1.71 + return checkRes;
1.72 + }
1.73 + } else {
1.74 + checkRes = callFunction(c, res, skip);
1.75 + if (checkRes != null) {
1.76 + return checkRes;
1.77 + }
1.78 + }
1.79 + }
1.80 + }
1.81 + return null;
1.82 + }
1.83 +
1.84 + @JavaScriptBody(args = { "fn", "res", "skip" }, body =
1.85 + "if (typeof fn === 'function') return fn(res, skip);\n"
1.86 + + "return null;"
1.87 + )
1.88 + private static native byte[] callFunction(Object fn, String res, int skip);
1.89 +
1.90 + @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
1.91 + private static native void log(String msg);
1.92 +
1.93 + private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
1.94 + try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
1.95 + String cp = is.toString();
1.96 + if (cp != null) {
1.97 + cp = cp.trim();
1.98 + for (int p = 0; p < cp.length();) {
1.99 + int n = cp.indexOf(' ', p);
1.100 + if (n == -1) {
1.101 + n = cp.length();
1.102 + }
1.103 + String el = cp.substring(p, n);
1.104 + URL u = new URL(new URL(url), el);
1.105 + classpath = addToArray(classpath, u.toString());
1.106 + p = n + 1;
1.107 + }
1.108 + }
1.109 + return is.getMainClass();
1.110 + }
1.111 + }
1.112 +
1.113 + private static Object addToArray(Object arr, String value) {
1.114 + final int last = length(arr);
1.115 + Object ret = enlargeArray(arr, last + 1);
1.116 + set(ret, last, value);
1.117 + return ret;
1.118 + }
1.119 +
1.120 + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
1.121 + private static native Object enlargeArray(Object arr, int len);
1.122 +}
2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed May 07 16:47:24 2014 +0200
2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed May 07 17:24:29 2014 +0200
2.3 @@ -55,7 +55,7 @@
2.4 assert assertsOn = true;
2.5 if (assertsOn) {
2.6 VMLazy.init();
2.7 - Zips.init();
2.8 + ClassPath.init();
2.9 }
2.10 }
2.11
3.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Wed May 07 16:47:24 2014 +0200
3.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Wed May 07 17:24:29 2014 +0200
3.3 @@ -49,13 +49,13 @@
3.4 }
3.5
3.6 static byte[] loadBytes(Object loader, String name, Object[] arguments, int skip) throws Exception {
3.7 - return Zips.loadFromCp(arguments, name, skip);
3.8 + return ClassPath.loadFromCp(arguments, name, skip);
3.9 }
3.10
3.11 private Object load(String name, boolean instance)
3.12 throws IOException, ClassNotFoundException {
3.13 String res = name.replace('.', '/') + ".class";
3.14 - byte[] arr = Zips.loadFromCp(args, res, 0);
3.15 + byte[] arr = ClassPath.loadFromCp(args, res, 0);
3.16 if (arr == null) {
3.17 throw new ClassNotFoundException(name);
3.18 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ZipHandler.java Wed May 07 17:24:29 2014 +0200
4.3 @@ -0,0 +1,113 @@
4.4 +/**
4.5 + * Back 2 Browser Bytecode Translator
4.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.7 + *
4.8 + * This program is free software: you can redistribute it and/or modify
4.9 + * it under the terms of the GNU General Public License as published by
4.10 + * the Free Software Foundation, version 2 of the License.
4.11 + *
4.12 + * This program is distributed in the hope that it will be useful,
4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.15 + * GNU General Public License for more details.
4.16 + *
4.17 + * You should have received a copy of the GNU General Public License
4.18 + * along with this program. Look for COPYING file in the top folder.
4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
4.20 + */
4.21 +package org.apidesign.vm4brwsr;
4.22 +
4.23 +import java.io.IOException;
4.24 +import java.io.InputStream;
4.25 +import java.net.URL;
4.26 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
4.27 +import org.apidesign.bck2brwsr.emul.zip.FastJar;
4.28 +
4.29 +/** Conversion from classpath to load function.
4.30 + *
4.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
4.32 + */
4.33 +final class ZipHandler {
4.34 + private final FastJar fj;
4.35 +
4.36 + private ZipHandler(String path, byte[] zipData) throws IOException {
4.37 + long bef = timeNow();
4.38 + fj = new FastJar(zipData);
4.39 + for (FastJar.Entry e : fj.list()) {
4.40 + putRes(e.name, e);
4.41 + }
4.42 + log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
4.43 + }
4.44 +
4.45 + public static void init() {
4.46 + }
4.47 + @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
4.48 + private static native int length(Object arr);
4.49 + @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
4.50 + private static native Object set(Object arr, int index, Object value);
4.51 +
4.52 + @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
4.53 + private static native void log(String msg);
4.54 +
4.55 + byte[] findRes(String res) throws IOException {
4.56 + Object arr = findResImpl(res);
4.57 + if (arr instanceof FastJar.Entry) {
4.58 + long bef = timeNow();
4.59 + InputStream zip = fj.getInputStream((FastJar.Entry)arr);
4.60 + arr = readFully(new byte[512], zip);
4.61 + putRes(res, arr);
4.62 + log("Reading " + res + " took " + (timeNow() - bef) + "ms");
4.63 + }
4.64 + return (byte[]) arr;
4.65 + }
4.66 +
4.67 + @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
4.68 + private native Object findResImpl(String res);
4.69 +
4.70 + @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
4.71 + private native void putRes(String res, Object arr);
4.72 +
4.73 + static ZipHandler toZip(String path) throws IOException {
4.74 + URL u = new URL(path);
4.75 + byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
4.76 + return new ZipHandler(path, zipData);
4.77 + }
4.78 +
4.79 + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
4.80 + private static native void enlargeBytes(byte[] arr, int len);
4.81 +
4.82 + @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
4.83 + private static native void sliceArray(byte[] arr, int len);
4.84 +
4.85 + private static Object readFully(byte[] arr, InputStream zip) throws IOException {
4.86 + int offset = 0;
4.87 + for (;;) {
4.88 + int len = zip.read(arr, offset, arr.length - offset);
4.89 + if (len == -1) {
4.90 + break;
4.91 + }
4.92 + offset += len;
4.93 + if (offset == arr.length) {
4.94 + enlargeBytes(arr, arr.length + 4096);
4.95 + }
4.96 + }
4.97 + sliceArray(arr, offset);
4.98 + return arr;
4.99 + }
4.100 +
4.101 + private static long timeNow() {
4.102 + double time = m();
4.103 + if (time >= 0) {
4.104 + return (long)time;
4.105 + }
4.106 + return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
4.107 + }
4.108 + @JavaScriptBody(args = {}, body =
4.109 + "if (typeof window.performance === 'undefined') return -1;\n"
4.110 + + "if (typeof window.performance.now === 'undefined') return -1;\n"
4.111 + + "return window.performance.now();"
4.112 + )
4.113 + private static native double m();
4.114 +
4.115 +
4.116 +}
5.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Wed May 07 16:47:24 2014 +0200
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,190 +0,0 @@
5.4 -/**
5.5 - * Back 2 Browser Bytecode Translator
5.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.7 - *
5.8 - * This program is free software: you can redistribute it and/or modify
5.9 - * it under the terms of the GNU General Public License as published by
5.10 - * the Free Software Foundation, version 2 of the License.
5.11 - *
5.12 - * This program is distributed in the hope that it will be useful,
5.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.15 - * GNU General Public License for more details.
5.16 - *
5.17 - * You should have received a copy of the GNU General Public License
5.18 - * along with this program. Look for COPYING file in the top folder.
5.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
5.20 - */
5.21 -package org.apidesign.vm4brwsr;
5.22 -
5.23 -import java.io.ByteArrayInputStream;
5.24 -import java.io.IOException;
5.25 -import java.io.InputStream;
5.26 -import java.net.URL;
5.27 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
5.28 -import org.apidesign.bck2brwsr.emul.zip.FastJar;
5.29 -
5.30 -/** Conversion from classpath to load function.
5.31 - *
5.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
5.33 - */
5.34 -final class Zips {
5.35 - private final FastJar fj;
5.36 -
5.37 - private Zips(String path, byte[] zipData) throws IOException {
5.38 - long bef = timeNow();
5.39 - fj = new FastJar(zipData);
5.40 - for (FastJar.Entry e : fj.list()) {
5.41 - putRes(e.name, e);
5.42 - }
5.43 - log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms");
5.44 - }
5.45 -
5.46 - public static void init() {
5.47 - }
5.48 - @JavaScriptBody(args = { "arr" }, body = "return arr.length;")
5.49 - private static native int length(Object arr);
5.50 - @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];")
5.51 - private static native Object at(Object arr, int index);
5.52 - @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;")
5.53 - private static native Object set(Object arr, int index, Object value);
5.54 -
5.55 - public static byte[] loadFromCp(Object classpath, String res, int skip)
5.56 - throws IOException, ClassNotFoundException {
5.57 - for (int i = 0; i < length(classpath); i++) {
5.58 - Object c = at(classpath, i);
5.59 - if (c instanceof String) {
5.60 - try {
5.61 - String url = (String)c;
5.62 - final Zips z = toZip(url);
5.63 - c = set(classpath, i, z);
5.64 - final byte[] man = z.findRes("META-INF/MANIFEST.MF");
5.65 - if (man != null) {
5.66 - String mainClass = processClassPathAttr(man, url, classpath);
5.67 -// if (mainClass != null) {
5.68 -// Class.forName(mainClass);
5.69 -// }
5.70 - }
5.71 - } catch (IOException ex) {
5.72 - set(classpath, i, ex);
5.73 - log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage());
5.74 - }
5.75 - }
5.76 - if (res != null) {
5.77 - byte[] checkRes;
5.78 - if (c instanceof Zips) {
5.79 - checkRes = ((Zips)c).findRes(res);
5.80 - if (checkRes != null && --skip < 0) {
5.81 - return checkRes;
5.82 - }
5.83 - } else {
5.84 - checkRes = callFunction(c, res, skip);
5.85 - if (checkRes != null) {
5.86 - return checkRes;
5.87 - }
5.88 - }
5.89 - }
5.90 - }
5.91 - return null;
5.92 - }
5.93 -
5.94 - @JavaScriptBody(args = { "fn", "res", "skip" }, body =
5.95 - "if (typeof fn === 'function') return fn(res, skip);\n"
5.96 - + "return null;"
5.97 - )
5.98 - private static native byte[] callFunction(Object fn, String res, int skip);
5.99 -
5.100 - @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());")
5.101 - private static native void log(String msg);
5.102 -
5.103 - private byte[] findRes(String res) throws IOException {
5.104 - Object arr = findResImpl(res);
5.105 - if (arr instanceof FastJar.Entry) {
5.106 - long bef = timeNow();
5.107 - InputStream zip = fj.getInputStream((FastJar.Entry)arr);
5.108 - arr = readFully(new byte[512], zip);
5.109 - putRes(res, arr);
5.110 - log("Reading " + res + " took " + (timeNow() - bef) + "ms");
5.111 - }
5.112 - return (byte[]) arr;
5.113 - }
5.114 -
5.115 - @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
5.116 - private native Object findResImpl(String res);
5.117 -
5.118 - @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
5.119 - private native void putRes(String res, Object arr);
5.120 -
5.121 - private static Zips toZip(String path) throws IOException {
5.122 - URL u = new URL(path);
5.123 - byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class });
5.124 - return new Zips(path, zipData);
5.125 - }
5.126 -
5.127 - private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException {
5.128 - try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
5.129 - String cp = is.toString();
5.130 - if (cp != null) {
5.131 - cp = cp.trim();
5.132 - for (int p = 0; p < cp.length();) {
5.133 - int n = cp.indexOf(' ', p);
5.134 - if (n == -1) {
5.135 - n = cp.length();
5.136 - }
5.137 - String el = cp.substring(p, n);
5.138 - URL u = new URL(new URL(url), el);
5.139 - classpath = addToArray(classpath, u.toString());
5.140 - p = n + 1;
5.141 - }
5.142 - }
5.143 - return is.getMainClass();
5.144 - }
5.145 - }
5.146 -
5.147 - private static Object addToArray(Object arr, String value) {
5.148 - final int last = length(arr);
5.149 - Object ret = enlargeArray(arr, last + 1);
5.150 - set(ret, last, value);
5.151 - return ret;
5.152 - }
5.153 -
5.154 - @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;")
5.155 - private static native Object enlargeArray(Object arr, int len);
5.156 - @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
5.157 - private static native void enlargeBytes(byte[] arr, int len);
5.158 -
5.159 - @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
5.160 - private static native void sliceArray(byte[] arr, int len);
5.161 -
5.162 - private static Object readFully(byte[] arr, InputStream zip) throws IOException {
5.163 - int offset = 0;
5.164 - for (;;) {
5.165 - int len = zip.read(arr, offset, arr.length - offset);
5.166 - if (len == -1) {
5.167 - break;
5.168 - }
5.169 - offset += len;
5.170 - if (offset == arr.length) {
5.171 - enlargeBytes(arr, arr.length + 4096);
5.172 - }
5.173 - }
5.174 - sliceArray(arr, offset);
5.175 - return arr;
5.176 - }
5.177 -
5.178 - private static long timeNow() {
5.179 - double time = m();
5.180 - if (time >= 0) {
5.181 - return (long)time;
5.182 - }
5.183 - return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
5.184 - }
5.185 - @JavaScriptBody(args = {}, body =
5.186 - "if (typeof window.performance === 'undefined') return -1;\n"
5.187 - + "if (typeof window.performance.now === 'undefined') return -1;\n"
5.188 - + "return window.performance.now();"
5.189 - )
5.190 - private static native double m();
5.191 -
5.192 -
5.193 -}