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 +}