Recognizing Main-Class attribute, so initializing the VM is just about passing in correct classpath
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
23 import java.util.zip.ZipEntry;
24 import java.util.zip.ZipInputStream;
25 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 /** Conversion from classpath to load function.
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public static void init() {
38 public static byte[] loadFromCp(Object[] classpath, String res) throws Exception {
39 for (int i = 0; i < classpath.length; i++) {
40 Object c = classpath[i];
41 if (c instanceof String) {
43 String url = (String)c;
44 final Zips z = toZip(url);
46 final byte[] man = z.findRes("META-INF/MANIFEST.MF");
48 String mainClass = processClassPathAttr(man, url, classpath);
49 if (mainClass != null) {
50 Class.forName(mainClass);
53 } catch (Exception ex) {
58 if (res != null && c instanceof Zips) {
59 Object checkRes = ((Zips)c).findRes(res);
60 if (checkRes instanceof byte[]) {
61 return (byte[])checkRes;
68 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
69 private native byte[] findRes(String res);
71 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
72 private native void putRes(String res, byte[] arr);
74 private static Zips toZip(String path) throws IOException {
75 URL u = new URL(path);
76 ZipInputStream zip = new ZipInputStream(u.openStream());
79 ZipEntry entry = zip.getNextEntry();
83 byte[] arr = new byte[4096];
86 int len = zip.read(arr, offset, arr.length - offset);
91 if (offset == arr.length) {
92 enlargeArray(arr, arr.length + 4096);
95 sliceArray(arr, offset);
96 z.putRes(entry.getName(), arr);
101 private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
102 try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) {
103 String cp = is.toString();
106 for (int p = 0; p < cp.length();) {
107 int n = cp.indexOf(' ', p);
111 String el = cp.substring(p, n);
112 URL u = new URL(new URL(url), el);
113 classpath = addToArray(classpath, u.toString());
117 return is.getMainClass();
121 private static Object[] addToArray(Object[] arr, String value) {
122 final int last = arr.length;
123 Object[] ret = enlargeArray(arr, last + 1);
128 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;throw('Arr: ' + arr);")
129 private static native Object[] enlargeArray(Object[] arr, int len);
130 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
131 private static native void enlargeArray(byte[] arr, int len);
133 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
134 private static native void sliceArray(byte[] arr, int len);