# HG changeset patch # User Jaroslav Tulach # Date 1399476269 -7200 # Node ID cb9e273dfd517b9181c66663fbba82b25fa8879d # Parent 3f4c143ff8f0d4bcab97d5a012776d6a3d2e2676 Splitting into two classes: one that deals with classpath, one that deals with ZIP content diff -r 3f4c143ff8f0 -r cb9e273dfd51 rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassPath.java Wed May 07 17:24:29 2014 +0200 @@ -0,0 +1,119 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.URL; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** Conversion from classpath to load function. + * + * @author Jaroslav Tulach + */ +final class ClassPath { + private ClassPath() { + } + + public static void init() { + } + @JavaScriptBody(args = { "arr" }, body = "return arr.length;") + private static native int length(Object arr); + @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];") + private static native Object at(Object arr, int index); + @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;") + private static native Object set(Object arr, int index, Object value); + + public static byte[] loadFromCp(Object classpath, String res, int skip) + throws IOException, ClassNotFoundException { + for (int i = 0; i < length(classpath); i++) { + Object c = at(classpath, i); + if (c instanceof String) { + try { + String url = (String)c; + final ZipHandler z = ZipHandler.toZip(url); + c = set(classpath, i, z); + final byte[] man = z.findRes("META-INF/MANIFEST.MF"); + if (man != null) { + String mainClass = processClassPathAttr(man, url, classpath); +// if (mainClass != null) { +// Class.forName(mainClass); +// } + } + } catch (IOException ex) { + set(classpath, i, ex); + log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage()); + } + } + if (res != null) { + byte[] checkRes; + if (c instanceof ZipHandler) { + checkRes = ((ZipHandler)c).findRes(res); + if (checkRes != null && --skip < 0) { + return checkRes; + } + } else { + checkRes = callFunction(c, res, skip); + if (checkRes != null) { + return checkRes; + } + } + } + } + return null; + } + + @JavaScriptBody(args = { "fn", "res", "skip" }, body = + "if (typeof fn === 'function') return fn(res, skip);\n" + + "return null;" + ) + private static native byte[] callFunction(Object fn, String res, int skip); + + @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());") + private static native void log(String msg); + + private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException { + try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) { + String cp = is.toString(); + if (cp != null) { + cp = cp.trim(); + for (int p = 0; p < cp.length();) { + int n = cp.indexOf(' ', p); + if (n == -1) { + n = cp.length(); + } + String el = cp.substring(p, n); + URL u = new URL(new URL(url), el); + classpath = addToArray(classpath, u.toString()); + p = n + 1; + } + } + return is.getMainClass(); + } + } + + private static Object addToArray(Object arr, String value) { + final int last = length(arr); + Object ret = enlargeArray(arr, last + 1); + set(ret, last, value); + return ret; + } + + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;") + private static native Object enlargeArray(Object arr, int len); +} diff -r 3f4c143ff8f0 -r cb9e273dfd51 rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed May 07 16:47:24 2014 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Wed May 07 17:24:29 2014 +0200 @@ -55,7 +55,7 @@ assert assertsOn = true; if (assertsOn) { VMLazy.init(); - Zips.init(); + ClassPath.init(); } } diff -r 3f4c143ff8f0 -r cb9e273dfd51 rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Wed May 07 16:47:24 2014 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Wed May 07 17:24:29 2014 +0200 @@ -49,13 +49,13 @@ } static byte[] loadBytes(Object loader, String name, Object[] arguments, int skip) throws Exception { - return Zips.loadFromCp(arguments, name, skip); + return ClassPath.loadFromCp(arguments, name, skip); } private Object load(String name, boolean instance) throws IOException, ClassNotFoundException { String res = name.replace('.', '/') + ".class"; - byte[] arr = Zips.loadFromCp(args, res, 0); + byte[] arr = ClassPath.loadFromCp(args, res, 0); if (arr == null) { throw new ClassNotFoundException(name); } diff -r 3f4c143ff8f0 -r cb9e273dfd51 rt/vm/src/main/java/org/apidesign/vm4brwsr/ZipHandler.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ZipHandler.java Wed May 07 17:24:29 2014 +0200 @@ -0,0 +1,113 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.apidesign.bck2brwsr.emul.zip.FastJar; + +/** Conversion from classpath to load function. + * + * @author Jaroslav Tulach + */ +final class ZipHandler { + private final FastJar fj; + + private ZipHandler(String path, byte[] zipData) throws IOException { + long bef = timeNow(); + fj = new FastJar(zipData); + for (FastJar.Entry e : fj.list()) { + putRes(e.name, e); + } + log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms"); + } + + public static void init() { + } + @JavaScriptBody(args = { "arr" }, body = "return arr.length;") + private static native int length(Object arr); + @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;") + private static native Object set(Object arr, int index, Object value); + + @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());") + private static native void log(String msg); + + byte[] findRes(String res) throws IOException { + Object arr = findResImpl(res); + if (arr instanceof FastJar.Entry) { + long bef = timeNow(); + InputStream zip = fj.getInputStream((FastJar.Entry)arr); + arr = readFully(new byte[512], zip); + putRes(res, arr); + log("Reading " + res + " took " + (timeNow() - bef) + "ms"); + } + return (byte[]) arr; + } + + @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;") + private native Object findResImpl(String res); + + @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;") + private native void putRes(String res, Object arr); + + static ZipHandler toZip(String path) throws IOException { + URL u = new URL(path); + byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class }); + return new ZipHandler(path, zipData); + } + + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);") + private static native void enlargeBytes(byte[] arr, int len); + + @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);") + private static native void sliceArray(byte[] arr, int len); + + private static Object readFully(byte[] arr, InputStream zip) throws IOException { + int offset = 0; + for (;;) { + int len = zip.read(arr, offset, arr.length - offset); + if (len == -1) { + break; + } + offset += len; + if (offset == arr.length) { + enlargeBytes(arr, arr.length + 4096); + } + } + sliceArray(arr, offset); + return arr; + } + + private static long timeNow() { + double time = m(); + if (time >= 0) { + return (long)time; + } + return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis(); + } + @JavaScriptBody(args = {}, body = + "if (typeof window.performance === 'undefined') return -1;\n" + + "if (typeof window.performance.now === 'undefined') return -1;\n" + + "return window.performance.now();" + ) + private static native double m(); + + +} diff -r 3f4c143ff8f0 -r cb9e273dfd51 rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Wed May 07 16:47:24 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,190 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.vm4brwsr; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import org.apidesign.bck2brwsr.core.JavaScriptBody; -import org.apidesign.bck2brwsr.emul.zip.FastJar; - -/** Conversion from classpath to load function. - * - * @author Jaroslav Tulach - */ -final class Zips { - private final FastJar fj; - - private Zips(String path, byte[] zipData) throws IOException { - long bef = timeNow(); - fj = new FastJar(zipData); - for (FastJar.Entry e : fj.list()) { - putRes(e.name, e); - } - log("Iterating thru " + path + " took " + (timeNow() - bef) + "ms"); - } - - public static void init() { - } - @JavaScriptBody(args = { "arr" }, body = "return arr.length;") - private static native int length(Object arr); - @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];") - private static native Object at(Object arr, int index); - @JavaScriptBody(args = { "arr", "index", "value" }, body = "arr[index] = value; return value;") - private static native Object set(Object arr, int index, Object value); - - public static byte[] loadFromCp(Object classpath, String res, int skip) - throws IOException, ClassNotFoundException { - for (int i = 0; i < length(classpath); i++) { - Object c = at(classpath, i); - if (c instanceof String) { - try { - String url = (String)c; - final Zips z = toZip(url); - c = set(classpath, i, z); - final byte[] man = z.findRes("META-INF/MANIFEST.MF"); - if (man != null) { - String mainClass = processClassPathAttr(man, url, classpath); -// if (mainClass != null) { -// Class.forName(mainClass); -// } - } - } catch (IOException ex) { - set(classpath, i, ex); - log("Cannot load " + c + " - " + ex.getClass().getName() + ":" + ex.getMessage()); - } - } - if (res != null) { - byte[] checkRes; - if (c instanceof Zips) { - checkRes = ((Zips)c).findRes(res); - if (checkRes != null && --skip < 0) { - return checkRes; - } - } else { - checkRes = callFunction(c, res, skip); - if (checkRes != null) { - return checkRes; - } - } - } - } - return null; - } - - @JavaScriptBody(args = { "fn", "res", "skip" }, body = - "if (typeof fn === 'function') return fn(res, skip);\n" - + "return null;" - ) - private static native byte[] callFunction(Object fn, String res, int skip); - - @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg.toString());") - private static native void log(String msg); - - private byte[] findRes(String res) throws IOException { - Object arr = findResImpl(res); - if (arr instanceof FastJar.Entry) { - long bef = timeNow(); - InputStream zip = fj.getInputStream((FastJar.Entry)arr); - arr = readFully(new byte[512], zip); - putRes(res, arr); - log("Reading " + res + " took " + (timeNow() - bef) + "ms"); - } - return (byte[]) arr; - } - - @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;") - private native Object findResImpl(String res); - - @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;") - private native void putRes(String res, Object arr); - - private static Zips toZip(String path) throws IOException { - URL u = new URL(path); - byte[] zipData = (byte[]) u.getContent(new Class[] { byte[].class }); - return new Zips(path, zipData); - } - - private static String processClassPathAttr(final byte[] man, String url, Object classpath) throws IOException { - try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) { - String cp = is.toString(); - if (cp != null) { - cp = cp.trim(); - for (int p = 0; p < cp.length();) { - int n = cp.indexOf(' ', p); - if (n == -1) { - n = cp.length(); - } - String el = cp.substring(p, n); - URL u = new URL(new URL(url), el); - classpath = addToArray(classpath, u.toString()); - p = n + 1; - } - } - return is.getMainClass(); - } - } - - private static Object addToArray(Object arr, String value) { - final int last = length(arr); - Object ret = enlargeArray(arr, last + 1); - set(ret, last, value); - return ret; - } - - @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;") - private static native Object enlargeArray(Object arr, int len); - @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);") - private static native void enlargeBytes(byte[] arr, int len); - - @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);") - private static native void sliceArray(byte[] arr, int len); - - private static Object readFully(byte[] arr, InputStream zip) throws IOException { - int offset = 0; - for (;;) { - int len = zip.read(arr, offset, arr.length - offset); - if (len == -1) { - break; - } - offset += len; - if (offset == arr.length) { - enlargeBytes(arr, arr.length + 4096); - } - } - sliceArray(arr, offset); - return arr; - } - - private static long timeNow() { - double time = m(); - if (time >= 0) { - return (long)time; - } - return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis(); - } - @JavaScriptBody(args = {}, body = - "if (typeof window.performance === 'undefined') return -1;\n" - + "if (typeof window.performance.now === 'undefined') return -1;\n" - + "return window.performance.now();" - ) - private static native double m(); - - -}