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;
22 import java.io.InputStream;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipInputStream;
26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
28 /** Conversion from classpath to load function.
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
36 public static void init() {
39 public static byte[] loadFromCp(Object[] classpath, String res) {
40 for (int i = 0; i < classpath.length; i++) {
41 Object c = classpath[i];
42 if (c instanceof String) {
44 String url = (String)c;
45 final Zips z = toZip(url);
47 final byte[] man = z.findRes("META-INF/MANIFEST.MF");
49 processClassPathAttr(man, url, classpath);
51 } catch (IOException ex) {
55 if (c instanceof Zips) {
56 Object checkRes = ((Zips)c).findRes(res);
57 if (checkRes instanceof byte[]) {
58 return (byte[])checkRes;
65 @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;")
66 private native byte[] findRes(String res);
68 @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;")
69 private native void putRes(String res, byte[] arr);
71 private static Zips toZip(String path) throws IOException {
72 URL u = new URL(path);
73 ZipInputStream zip = new ZipInputStream(u.openStream());
76 ZipEntry entry = zip.getNextEntry();
80 byte[] arr = new byte[4096];
83 int len = zip.read(arr, offset, arr.length - offset);
88 if (offset == arr.length) {
89 enlargeArray(arr, arr.length + 4096);
92 sliceArray(arr, offset);
93 z.putRes(entry.getName(), arr);
98 private static void processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException {
99 try (InputStream is = initIS(new ByteArrayInputStream(man))) {
100 String cp = is.toString();
105 for (int p = 0; p < cp.length();) {
106 int n = cp.indexOf(' ', p);
110 String el = cp.substring(p, n);
111 URL u = new URL(new URL(url), el);
112 classpath = addToArray(classpath, u.toString());
118 private static InputStream initIS(InputStream is) throws IOException {
119 return new ParseCPAttr(is);
122 private static Object[] addToArray(Object[] arr, String value) {
123 final int last = arr.length;
124 Object[] ret = enlargeArray(arr, last + 1);
129 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(null); return arr;throw('Arr: ' + arr);")
130 private static native Object[] enlargeArray(Object[] arr, int len);
131 @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);")
132 private static native void enlargeArray(byte[] arr, int len);
134 @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);")
135 private static native void sliceArray(byte[] arr, int len);