If a class is not available during Ahead-Of-Time compilation it needs to be ready for dynamic loading
1.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Thu Oct 10 14:00:32 2013 +0200
1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Fri Oct 11 09:58:03 2013 +0200
1.3 @@ -19,6 +19,7 @@
1.4
1.5 import java.io.UnsupportedEncodingException;
1.6 import java.net.MalformedURLException;
1.7 +import java.net.URISyntaxException;
1.8 import java.net.URL;
1.9 import java.util.Locale;
1.10 import org.apidesign.bck2brwsr.vmtest.Compare;
1.11 @@ -64,6 +65,10 @@
1.12 @Compare public static Object compareURLs() throws MalformedURLException {
1.13 return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
1.14 }
1.15 +
1.16 + @Compare public static Object compareURLsViaURIs() throws Exception {
1.17 + return new URL("http://apidesign.org:8080/wiki/").toURI().toString();
1.18 + }
1.19
1.20 @Compare public String deleteLastTwoCharacters() {
1.21 StringBuilder sb = new StringBuilder();
2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Thu Oct 10 14:00:32 2013 +0200
2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Fri Oct 11 09:58:03 2013 +0200
2.3 @@ -80,6 +80,7 @@
2.4 }
2.5 InputStream is = loadClass(l, name);
2.6 if (is == null) {
2.7 + lazyReference(out, name);
2.8 skipClass.add(name);
2.9 continue;
2.10 }
2.11 @@ -250,4 +251,16 @@
2.12 String getVMObject() {
2.13 return "vm";
2.14 }
2.15 +
2.16 + private static void lazyReference(Appendable out, String n) throws IOException {
2.17 + String cls = n.replace('/', '_');
2.18 + String dot = n.replace('/', '.');
2.19 +
2.20 + out.append("\nvm.").append(cls).append(" = function() {");
2.21 + out.append("\n var instance = arguments.length == 0 || arguments[0] === true;");
2.22 + out.append("\n delete vm.").append(cls).append(";");
2.23 + out.append("\n var c = vm.loadClass('").append(dot).append("');");
2.24 + out.append("\n return vm.").append(cls).append("(instance);");
2.25 + out.append("\n}");
2.26 + }
2.27 }
3.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Oct 10 14:00:32 2013 +0200
3.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Fri Oct 11 09:58:03 2013 +0200
3.3 @@ -20,7 +20,6 @@
3.4 import java.io.ByteArrayInputStream;
3.5 import java.io.IOException;
3.6 import java.io.InputStream;
3.7 -import java.lang.reflect.Array;
3.8 import org.apidesign.bck2brwsr.core.JavaScriptBody;
3.9
3.10 /**
3.11 @@ -62,8 +61,6 @@
3.12 int prelude = out.length();
3.13 String initCode = new Gen(this, out).compile(new ByteArrayInputStream(arr));
3.14 String code = out.toString().toString();
3.15 -// dump("Loading " + name);
3.16 - dump(code);
3.17 String under = name.replace('.', '_');
3.18 Object fn = applyCode(loader, under, code, instance);
3.19
3.20 @@ -71,26 +68,12 @@
3.21 out.setLength(prelude);
3.22 out.append(initCode);
3.23 code = out.toString().toString();
3.24 - dump(code);
3.25 applyCode(loader, null, code, false);
3.26 }
3.27
3.28 return fn;
3.29 }
3.30
3.31 -// @JavaScriptBody(args = "s", body = "java.lang.System.out.println(s.toString());")
3.32 - static void dump(String s) {
3.33 - }
3.34 -
3.35 -/* possibly not needed:
3.36 - @JavaScriptBody(args = {"loader", "n" }, body =
3.37 - "var cls = n.replace__Ljava_lang_String_2CC(n, '.','_').toString();" +
3.38 - "loader.vm[cls] = true;\n"
3.39 - )
3.40 - private static native void beingDefined(Object loader, String name);
3.41 -*/
3.42 -
3.43 -
3.44 @JavaScriptBody(args = {"loader", "name", "script", "instance" }, body =
3.45 "try {\n" +
3.46 " new Function(script)(loader, name);\n" +
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/DelayedLoading.java Fri Oct 11 09:58:03 2013 +0200
4.3 @@ -0,0 +1,31 @@
4.4 +/**
4.5 + * Back 2 Browser Bytecode Translator
4.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.7 + *
4.8 + * This program is free software: you can redistribute it and/or modify
4.9 + * it under the terms of the GNU General Public License as published by
4.10 + * the Free Software Foundation, version 2 of the License.
4.11 + *
4.12 + * This program is distributed in the hope that it will be useful,
4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.15 + * GNU General Public License for more details.
4.16 + *
4.17 + * You should have received a copy of the GNU General Public License
4.18 + * along with this program. Look for COPYING file in the top folder.
4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
4.20 + */
4.21 +package org.apidesign.vm4brwsr;
4.22 +
4.23 +import java.net.URL;
4.24 +
4.25 +/**
4.26 + *
4.27 + * @author Jaroslav Tulach <jtulach@netbeans.org>
4.28 + */
4.29 +public class DelayedLoading {
4.30 + public static String toStrViaURI(String url) throws Exception {
4.31 + URL u = new URL(url);
4.32 + return u.toURI().toString();
4.33 + }
4.34 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/DelayedLoadingTest.java Fri Oct 11 09:58:03 2013 +0200
5.3 @@ -0,0 +1,55 @@
5.4 +/**
5.5 + * Back 2 Browser Bytecode Translator
5.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.7 + *
5.8 + * This program is free software: you can redistribute it and/or modify
5.9 + * it under the terms of the GNU General Public License as published by
5.10 + * the Free Software Foundation, version 2 of the License.
5.11 + *
5.12 + * This program is distributed in the hope that it will be useful,
5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.15 + * GNU General Public License for more details.
5.16 + *
5.17 + * You should have received a copy of the GNU General Public License
5.18 + * along with this program. Look for COPYING file in the top folder.
5.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
5.20 + */
5.21 +package org.apidesign.vm4brwsr;
5.22 +
5.23 +import java.net.URL;
5.24 +import org.testng.annotations.BeforeClass;
5.25 +import org.testng.annotations.AfterClass;
5.26 +import org.testng.annotations.Test;
5.27 +
5.28 +/**
5.29 + *
5.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
5.31 + */
5.32 +public class DelayedLoadingTest {
5.33 + private static TestVM code;
5.34 +
5.35 + @Test public void verifyUsageOf() throws Exception {
5.36 + code.register(new BytesLoader());
5.37 +
5.38 + URL u = new URL("http://apidesign.org");
5.39 +
5.40 + Object str = code.execCode("Access URI",
5.41 + DelayedLoading.class, "toStrViaURI__Ljava_lang_String_2Ljava_lang_String_2",
5.42 + u.toExternalForm(), u.toExternalForm()
5.43 + );
5.44 + }
5.45 +
5.46 +
5.47 + @BeforeClass
5.48 + public static void compileTheCode() throws Exception {
5.49 + code = TestVM.compileClass(
5.50 + "org/apidesign/vm4brwsr/DelayedLoading");
5.51 + }
5.52 + @AfterClass
5.53 + public static void releaseTheCode() {
5.54 + code = null;
5.55 + }
5.56 +
5.57 +}
5.58 +
6.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Thu Oct 10 14:00:32 2013 +0200
6.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Fri Oct 11 09:58:03 2013 +0200
6.3 @@ -24,23 +24,33 @@
6.4 import java.net.URL;
6.5 import java.util.Enumeration;
6.6 import javax.script.Invocable;
6.7 +import javax.script.ScriptContext;
6.8 import javax.script.ScriptEngine;
6.9 import javax.script.ScriptEngineManager;
6.10 import javax.script.ScriptException;
6.11 import static org.testng.Assert.*;
6.12
6.13 -final class TestVM {
6.14 +public final class TestVM {
6.15 private final Invocable code;
6.16 private final CharSequence codeSeq;
6.17 private final Object bck2brwsr;
6.18 + private BytesLoader resources;
6.19
6.20
6.21 private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
6.22 this.code = code;
6.23 this.codeSeq = codeSeq;
6.24 - this.bck2brwsr = code.invokeFunction("bck2brwsr");
6.25 + this.bck2brwsr = ((ScriptEngine)code).eval("bck2brwsr(function(n) { return loader.get(n); })");
6.26 + ((ScriptEngine)code).getContext().setAttribute("loader", this, ScriptContext.ENGINE_SCOPE);
6.27 }
6.28
6.29 + public void register(BytesLoader res) {
6.30 + this.resources = res;
6.31 + }
6.32 +
6.33 + public byte[] get(String res) throws IOException {
6.34 + return resources != null ? resources.get(res) : null;
6.35 + }
6.36
6.37 public Object execCode(
6.38 String msg, Class<?> clazz, String method,