# HG changeset patch # User Jaroslav Tulach # Date 1381478283 -7200 # Node ID 6193e735f4d1824d7f6883d7258cb01b2e0e0152 # Parent add89df8447efbb65ad1a092bfbce4d37109996a If a class is not available during Ahead-Of-Time compilation it needs to be ready for dynamic loading diff -r add89df8447e -r 6193e735f4d1 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Thu Oct 10 14:00:32 2013 +0200 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Fri Oct 11 09:58:03 2013 +0200 @@ -19,6 +19,7 @@ import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; import java.util.Locale; import org.apidesign.bck2brwsr.vmtest.Compare; @@ -64,6 +65,10 @@ @Compare public static Object compareURLs() throws MalformedURLException { return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString(); } + + @Compare public static Object compareURLsViaURIs() throws Exception { + return new URL("http://apidesign.org:8080/wiki/").toURI().toString(); + } @Compare public String deleteLastTwoCharacters() { StringBuilder sb = new StringBuilder(); diff -r add89df8447e -r 6193e735f4d1 rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Thu Oct 10 14:00:32 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Fri Oct 11 09:58:03 2013 +0200 @@ -80,6 +80,7 @@ } InputStream is = loadClass(l, name); if (is == null) { + lazyReference(out, name); skipClass.add(name); continue; } @@ -250,4 +251,16 @@ String getVMObject() { return "vm"; } + + private static void lazyReference(Appendable out, String n) throws IOException { + String cls = n.replace('/', '_'); + String dot = n.replace('/', '.'); + + out.append("\nvm.").append(cls).append(" = function() {"); + out.append("\n var instance = arguments.length == 0 || arguments[0] === true;"); + out.append("\n delete vm.").append(cls).append(";"); + out.append("\n var c = vm.loadClass('").append(dot).append("');"); + out.append("\n return vm.").append(cls).append("(instance);"); + out.append("\n}"); + } } diff -r add89df8447e -r 6193e735f4d1 rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Oct 10 14:00:32 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Fri Oct 11 09:58:03 2013 +0200 @@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Array; import org.apidesign.bck2brwsr.core.JavaScriptBody; /** @@ -62,8 +61,6 @@ int prelude = out.length(); String initCode = new Gen(this, out).compile(new ByteArrayInputStream(arr)); String code = out.toString().toString(); -// dump("Loading " + name); - dump(code); String under = name.replace('.', '_'); Object fn = applyCode(loader, under, code, instance); @@ -71,26 +68,12 @@ out.setLength(prelude); out.append(initCode); code = out.toString().toString(); - dump(code); applyCode(loader, null, code, false); } return fn; } -// @JavaScriptBody(args = "s", body = "java.lang.System.out.println(s.toString());") - static void dump(String s) { - } - -/* possibly not needed: - @JavaScriptBody(args = {"loader", "n" }, body = - "var cls = n.replace__Ljava_lang_String_2CC(n, '.','_').toString();" + - "loader.vm[cls] = true;\n" - ) - private static native void beingDefined(Object loader, String name); -*/ - - @JavaScriptBody(args = {"loader", "name", "script", "instance" }, body = "try {\n" + " new Function(script)(loader, name);\n" + diff -r add89df8447e -r 6193e735f4d1 rt/vm/src/test/java/org/apidesign/vm4brwsr/DelayedLoading.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/DelayedLoading.java Fri Oct 11 09:58:03 2013 +0200 @@ -0,0 +1,31 @@ +/** + * 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.net.URL; + +/** + * + * @author Jaroslav Tulach + */ +public class DelayedLoading { + public static String toStrViaURI(String url) throws Exception { + URL u = new URL(url); + return u.toURI().toString(); + } +} diff -r add89df8447e -r 6193e735f4d1 rt/vm/src/test/java/org/apidesign/vm4brwsr/DelayedLoadingTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/DelayedLoadingTest.java Fri Oct 11 09:58:03 2013 +0200 @@ -0,0 +1,55 @@ +/** + * 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.net.URL; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.AfterClass; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class DelayedLoadingTest { + private static TestVM code; + + @Test public void verifyUsageOf() throws Exception { + code.register(new BytesLoader()); + + URL u = new URL("http://apidesign.org"); + + Object str = code.execCode("Access URI", + DelayedLoading.class, "toStrViaURI__Ljava_lang_String_2Ljava_lang_String_2", + u.toExternalForm(), u.toExternalForm() + ); + } + + + @BeforeClass + public static void compileTheCode() throws Exception { + code = TestVM.compileClass( + "org/apidesign/vm4brwsr/DelayedLoading"); + } + @AfterClass + public static void releaseTheCode() { + code = null; + } + +} + diff -r add89df8447e -r 6193e735f4d1 rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Thu Oct 10 14:00:32 2013 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Fri Oct 11 09:58:03 2013 +0200 @@ -24,23 +24,33 @@ import java.net.URL; import java.util.Enumeration; import javax.script.Invocable; +import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import static org.testng.Assert.*; -final class TestVM { +public final class TestVM { private final Invocable code; private final CharSequence codeSeq; private final Object bck2brwsr; + private BytesLoader resources; private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException { this.code = code; this.codeSeq = codeSeq; - this.bck2brwsr = code.invokeFunction("bck2brwsr"); + this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })"); + ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE); } + public void register(BytesLoader res) { + this.resources = res; + } + + public byte[] get(String res) throws IOException { + return resources != null ? resources.get(res) : null; + } public Object execCode( String msg, Class clazz, String method,