# HG changeset patch # User Jaroslav Tulach # Date 1359740527 -3600 # Node ID cfbd4eecb9415034b87f722e4138c6648e4e36c8 # Parent a2c0afa35c090116a35405237ddd8a3911221463 Can initialize VM with classpath composed of a JAR file and read resources from it diff -r a2c0afa35c09 -r cfbd4eecb941 vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Fri Feb 01 18:35:21 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Fri Feb 01 18:42:07 2013 +0100 @@ -32,6 +32,7 @@ static { // uses VMLazy to load dynamic classes VMLazy.init(); + Zips.init(); } @Override @@ -116,6 +117,12 @@ + " var args = arguments;\n" + " var loader = {};\n" + " loader.vm = vm;\n" + + " if (args.length == 1 && typeof args[0] !== 'function') {;\n" + + " var classpath = args[0];\n" + + " args[0] = function(name) {\n" + + " return vm.org_apidesign_vm4brwsr_Zips(false).loadFromCp___3B_3Ljava_lang_Object_2Ljava_lang_String_2(classpath, name);\n" + + " };\n" + + " };\n" + " loader.loadClass = function(name) {\n" + " var attr = name.replace__Ljava_lang_String_2CC('.','_');\n" + " var fn = vm[attr];\n" diff -r a2c0afa35c09 -r cfbd4eecb941 vm/src/main/java/org/apidesign/vm4brwsr/Zips.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Fri Feb 01 18:42:07 2013 +0100 @@ -0,0 +1,97 @@ +/** + * 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.net.URL; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** Conversion from classpath to load function. + * + * @author Jaroslav Tulach + */ +final class Zips { + private Zips() { + } + + public static void init() { + } + + public static byte[] loadFromCp(Object[] classpath, String res) { + for (int i = 0; i < classpath.length; i++) { + Object c = classpath[i]; + if (c instanceof String) { + try { + c = classpath[i] = toZip((String)c); + } catch (IOException ex) { + classpath[i] = ex; + } + } + if (c instanceof Zips) { + Object checkRes = ((Zips)c).findRes(res); + if (checkRes instanceof byte[]) { + return (byte[])checkRes; + } + } + } + return null; + } + + @JavaScriptBody(args = { "res" }, body = "var r = this[res]; return r ? r : null;") + private native byte[] findRes(String res); + + @JavaScriptBody(args = { "res", "arr" }, body = "this[res] = arr;") + private native void putRes(String res, byte[] arr); + + private static Zips toZip(String path) throws IOException { + URL u = new URL(path); + ZipInputStream zip = new ZipInputStream(u.openStream()); + Zips z = new Zips(); + for (;;) { + ZipEntry entry = zip.getNextEntry(); + if (entry == null) { + break; + } + byte[] arr = new byte[4096]; + int offset = 0; + for (;;) { + int len = zip.read(arr, offset, arr.length - offset); + if (len == -1) { + break; + } + offset += len; + if (offset == arr.length) { + enlargeArray(arr, arr.length + 4096); + } + } + sliceArray(arr, offset); + z.putRes(entry.getName(), arr); + } + return z; + } + + @JavaScriptBody(args = { "arr", "len" }, body = "while (arr.length < len) arr.push(0);") + private static native void enlargeArray(byte[] arr, int len); + + @JavaScriptBody(args = { "arr", "len" }, body = "arr.splice(len, arr.length - len);") + private static native void sliceArray(byte[] arr, int len); + + +} diff -r a2c0afa35c09 -r cfbd4eecb941 vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Fri Feb 01 18:35:21 2013 +0100 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Fri Feb 01 18:42:07 2013 +0100 @@ -53,6 +53,26 @@ return ret; } + @JavaScriptBody(args = { "res", "path" }, body = + "var myvm = new bck2brwsr(path);\n" + + "var cls = myvm.loadClass('java.lang.String');\n" + + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n" + ) + private static native Object loadVMResource(String res, String...path); + + @HttpResource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip") + @BrwsrTest public void canVmLoadResourceFromZip() throws IOException { + Object res = loadVMResource("/my/main/file.txt", "http:/readAnEntry.jar"); + assert res instanceof InputStream : "Got array of bytes: " + res; + InputStream is = (InputStream)res; + + byte[] arr = new byte[4096]; + int len = is.read(arr); + + final String ret = new String(arr, 0, len, "UTF-8"); + + assertEquals(ret, "Hello World!", "Can read the bytes"); + } private static void assertEquals(Object real, Object exp, String msg) { assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;