# HG changeset patch # User Jaroslav Tulach # Date 1378988147 -7200 # Node ID 8d29792a09c6452846176b48e4dbc7ac2aa3dbc1 # Parent 8ed05565a481f1c200c8419473b06d1cf153433d Updating to recent changes in html.java.net APIs diff -r 8ed05565a481 -r 8d29792a09c6 ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java --- a/ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java Thu Sep 12 12:26:23 2013 +0200 +++ b/ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java Thu Sep 12 14:15:47 2013 +0200 @@ -28,6 +28,7 @@ import net.java.html.BrwsrCtx; import net.java.html.js.JavaScriptBody; import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.apidesign.html.boot.impl.FnUtils; import org.apidesign.html.context.spi.Contexts; import org.apidesign.html.json.spi.Technology; import org.apidesign.html.json.spi.Transfer; @@ -59,7 +60,7 @@ @Override public BrwsrCtx createContext() { - FXContext fx = new FXContext(); + FXContext fx = new FXContext(FnUtils.currentPresenter()); TyrusContext tc = new TyrusContext(); Contexts.Builder b = Contexts.newBuilder(). register(Technology.class, fx, 10). diff -r 8ed05565a481 -r 8d29792a09c6 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java Thu Sep 12 12:26:23 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java Thu Sep 12 14:15:47 2013 +0200 @@ -39,6 +39,7 @@ public final class JVMBridge { private final WebEngine engine; private final ClassLoader cl; + private final WebPresenter presenter; private static ClassLoader[] ldrs; private static ChangeListener onBck2BrwsrLoad; @@ -46,8 +47,8 @@ JVMBridge(WebEngine eng) { this.engine = eng; final ClassLoader p = JVMBridge.class.getClassLoader().getParent(); - WebClassLoader wcl = new WebClassLoader(); - this.cl = FnUtils.newLoader(wcl, wcl, p); + this.presenter = new WebPresenter(); + this.cl = FnUtils.newLoader(presenter, presenter, p); } public static void registerClassLoaders(ClassLoader[] loaders) { @@ -69,10 +70,11 @@ } public Class loadClass(String name) throws ClassNotFoundException { + FnUtils.currentPresenter(presenter); return Class.forName(name, true, cl); } - private final class WebClassLoader implements FindResources, Fn.Presenter { + private final class WebPresenter implements FindResources, Fn.Presenter { @Override public void findResources(String name, Collection results, boolean oneIsEnough) { if (ldrs != null) for (ClassLoader l : ldrs) { @@ -99,7 +101,7 @@ sb.append("})()"); JSObject x = (JSObject) engine.executeScript(sb.toString()); - return new JSFn(x); + return new JSFn(this, x); } @Override @@ -125,10 +127,11 @@ private static final class JSFn extends Fn { private final JSObject fn; - public JSFn(JSObject fn) { + private JSFn(WebPresenter cl, JSObject fn) { + super(cl); this.fn = fn; } - + @Override public Object invoke(Object thiz, Object... args) throws Exception { try { diff -r 8ed05565a481 -r 8d29792a09c6 launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java --- a/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java Thu Sep 12 12:26:23 2013 +0200 +++ b/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java Thu Sep 12 14:15:47 2013 +0200 @@ -35,6 +35,7 @@ import org.apidesign.html.boot.impl.FnUtils; import static org.testng.Assert.*; import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; /** @@ -44,6 +45,7 @@ public class JsClassLoaderTest { private static ClassLoader loader; private static Class methodClass; + private static Fn.Presenter presenter; public JsClassLoaderTest() { } @@ -82,7 +84,7 @@ sb.append("})()"); try { final Object val = eng.eval(sb.toString()); - return new Fn() { + return new Fn(this) { @Override public Object invoke(Object thiz, Object... args) throws Exception { List all = new ArrayList(args.length + 1); @@ -109,10 +111,16 @@ } } - loader = FnUtils.newLoader(new Fr(), new Fr(), parent); + Fr fr = new Fr(); + presenter = fr; + loader = FnUtils.newLoader(fr, fr, parent); methodClass = loader.loadClass(JsMethods.class.getName()); } + @BeforeMethod public void registerPresenter() { + FnUtils.currentPresenter(presenter); + } + @Test public void noParamMethod() throws Throwable { Method plus = methodClass.getMethod("fortyTwo"); try { diff -r 8ed05565a481 -r 8d29792a09c6 pom.xml --- a/pom.xml Thu Sep 12 12:26:23 2013 +0200 +++ b/pom.xml Thu Sep 12 14:15:47 2013 +0200 @@ -15,7 +15,7 @@ UTF-8 RELEASE73 COPYING - 0.5 + 0.6-SNAPSHOT dew diff -r 8ed05565a481 -r 8d29792a09c6 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotations.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotations.java Thu Sep 12 14:15:47 2013 +0200 @@ -0,0 +1,86 @@ +/** + * 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.bck2brwsr.vmtest.impl; + +import net.java.html.js.JavaScriptBody; +import net.java.html.js.JavaScriptResource; + +/** + * + * @author Jaroslav Tulach + */ +@JavaScriptResource("htmlannotations.js") +public class HtmlAnnotations { + private Object callback; + + + @JavaScriptBody(args = {}, body = "return 42;") + public static int fourtyTwo() { + return -1; + } + + @JavaScriptBody(args = { "x", "y" }, body = "return mul(x, y);") + public static native int useExternalMul(int x, int y); + + public static int callback() { + final int[] arr = { 0 }; + callback(new Runnable() { + @Override + public void run() { + arr[0]++; + } + }); + return arr[0]; + } + + @JavaScriptBody(args = { "r" }, javacall=true, body = "r.@java.lang.Runnable::run()()") + private static native void callback(Runnable r); + + @JavaScriptBody(args = { }, javacall = true, body = "return @org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::callback()();") + public static native int staticCallback(); + + + protected long chooseLong(boolean takeFirst, boolean takeSecond, long first, long second) { + long l = 0; + if (takeFirst) l += first; + if (takeSecond) l += second; + return l; + } + + protected void onError(Object obj) throws Exception { + callback = obj; + } + + Object getError() { + return callback; + } + + public static Object create() { + return new HtmlAnnotations(); + } + @JavaScriptBody(args = { "impl", "a", "b" }, javacall = true, body = + "return impl.@org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::chooseLong(ZZJJ)(true, false, a, b);" + ) + public static native long first(Object impl, long a, long b); + + @JavaScriptBody(args = { "impl", "d" }, javacall = true, body = + "impl.@org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::onError(Ljava/lang/Object;)(d);" + + "return impl.@org.apidesign.bck2brwsr.vmtest.impl.HtmlAnnotations::getError()();" + ) + public static native Double onError(Object impl, Double d); +} diff -r 8ed05565a481 -r 8d29792a09c6 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java Thu Sep 12 14:15:47 2013 +0200 @@ -0,0 +1,71 @@ +/** + * 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.bck2brwsr.vmtest.impl; + +import org.apidesign.bck2brwsr.vmtest.BrwsrTest; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** Verify cooperation with net.java.html.js annotations. + * + * @author Jaroslav Tulach + */ +public class HtmlAnnotationsTest { + @BrwsrTest public void fourtyTwo() throws Exception { + assertEquals(HtmlAnnotations.fourtyTwo(), 42); + } + + @BrwsrTest public void externalMul() throws Exception { + assertEquals(HtmlAnnotations.useExternalMul(7, 6), 42); + } + + @BrwsrTest public void callRunnableFromJS() throws Exception { + assertEquals(HtmlAnnotations.callback(), 1); + } + + @BrwsrTest public void callStaticMethodFromJS() throws Exception { + assertEquals(HtmlAnnotations.staticCallback(), 1); + } + + @BrwsrTest public void callbackWithFourParamsAndReturnType() throws Exception { + Object instance = HtmlAnnotations.create(); + assertNotNull(instance, "Instance created"); + assertEquals(HtmlAnnotations.first(instance, 42, 31), 42); + } + + @BrwsrTest public void callbackWithObjectParamsAndReturnType() throws Exception { + Object instance = HtmlAnnotations.create(); + assertNotNull(instance, "Instance created"); + assertEquals(HtmlAnnotations.onError(instance, 42.0), 42.0); + } + + private static void assertEquals(double real, double exp) { + if (real - exp < 0.01) { + return; + } + assert false : "Expecting " + exp + " but was " + real; + } + + private static void assertNotNull(Object obj, String msg) { + assert obj != null : msg; + } + + @Factory public static Object[] create() { + return VMTest.create(HtmlAnnotationsTest.class); + } +} diff -r 8ed05565a481 -r 8d29792a09c6 rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/vmtest/impl/htmlannotations.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/vmtest/impl/htmlannotations.js Thu Sep 12 14:15:47 2013 +0200 @@ -0,0 +1,20 @@ +/* + * 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. + */ + +function mul(x, y) { return x * y; } +window.mul = mul; diff -r 8ed05565a481 -r 8d29792a09c6 rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Sep 12 12:26:23 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Thu Sep 12 14:15:47 2013 +0200 @@ -130,6 +130,14 @@ @Override protected void requireScript(String resourcePath) throws IOException { + if (!resourcePath.startsWith("/")) { + resourcePath = "/" + resourcePath; + } + String code = readCode(resourcePath); + applyCode(lazy.loader, null, code, false); + } + + private String readCode(String resourcePath) throws IOException { InputStream is = getClass().getResourceAsStream(resourcePath); StringBuilder sb = new StringBuilder(); for (;;) { @@ -139,7 +147,7 @@ } sb.append((char)ch); } - applyCode(lazy.loader, null, sb.toString(), false); + return sb.toString(); } @Override diff -r 8ed05565a481 -r 8d29792a09c6 rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotations.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotations.java Thu Sep 12 12:26:23 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -/** - * 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 net.java.html.js.JavaScriptBody; -import net.java.html.js.JavaScriptResource; - -/** - * - * @author Jaroslav Tulach - */ -@JavaScriptResource("htmlannotations.js") -public class HtmlAnnotations { - private Object callback; - - - @JavaScriptBody(args = {}, body = "return 42;") - public static int fourtyTwo() { - return -1; - } - - @JavaScriptBody(args = { "x", "y" }, body = "return mul(x, y);") - public static native int useExternalMul(int x, int y); - - public static int callback() { - final int[] arr = { 0 }; - callback(new Runnable() { - @Override - public void run() { - arr[0]++; - } - }); - return arr[0]; - } - - @JavaScriptBody(args = { "r" }, javacall=true, body = "r.@java.lang.Runnable::run()()") - private static native void callback(Runnable r); - - @JavaScriptBody(args = { }, javacall = true, body = "return @org.apidesign.vm4brwsr.HtmlAnnotations::callback()();") - public static native int staticCallback(); - - - protected long chooseLong(boolean takeFirst, boolean takeSecond, long first, long second) { - long l = 0; - if (takeFirst) l += first; - if (takeSecond) l += second; - return l; - } - - protected void onError(Object obj) throws Exception { - callback = obj; - } - - Object getError() { - return callback; - } - - public static Object create() { - return new HtmlAnnotations(); - } - @JavaScriptBody(args = { "impl", "a", "b" }, javacall = true, body = - "return impl.@org.apidesign.vm4brwsr.HtmlAnnotations::chooseLong(ZZJJ)(true, false, a, b);" - ) - public static native long first(Object impl, long a, long b); - - @JavaScriptBody(args = { "impl", "d" }, javacall = true, body = - "impl.@org.apidesign.vm4brwsr.HtmlAnnotations::onError(Ljava/lang/Object;)(d);" + - "return impl.@org.apidesign.vm4brwsr.HtmlAnnotations::getError()();" - ) - public static native Double onError(Object impl, Double d); -} diff -r 8ed05565a481 -r 8d29792a09c6 rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotationsTest.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotationsTest.java Thu Sep 12 12:26:23 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -/** - * 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 static org.testng.Assert.assertNotNull; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -/** Verify cooperation with net.java.html.js annotations. - * - * @author Jaroslav Tulach - */ -public class HtmlAnnotationsTest { - @Test public void fourtyTwo() throws Exception { - assertExec("Annotation used", HtmlAnnotations.class, - "fourtyTwo__I", - Double.valueOf(42) - ); - } - - @Test public void externalMul() throws Exception { - assertExec("mul function is loaded", HtmlAnnotations.class, - "useExternalMul__III", - Double.valueOf(42), - 7, 6 - ); - } - - @Test public void callRunnableFromJS() throws Exception { - assertExec("runnable called", HtmlAnnotations.class, - "callback__I", - Double.valueOf(1) - ); - } - - @Test public void callStaticMethodFromJS() throws Exception { - assertExec("runnable called", HtmlAnnotations.class, - "staticCallback__I", - Double.valueOf(1) - ); - } - - @Test public void callbackWithFourParamsAndReturnType() throws Exception { - Object instance = code.execCode("Get an HtmlAnnotations instance", HtmlAnnotations.class, "create__Ljava_lang_Object_2", null); - assertNotNull(instance, "Instance created"); - assertExec("runnable called", HtmlAnnotations.class, - "first__JLjava_lang_Object_2JJ", - Double.valueOf(42), instance, 42, 31 - ); - } - - @Test public void callbackWithObjectParamsAndReturnType() throws Exception { - Object instance = code.execCode("Get an HtmlAnnotations instance", HtmlAnnotations.class, "create__Ljava_lang_Object_2", null); - assertNotNull(instance, "Instance created"); - assertExec("called back and forth", HtmlAnnotations.class, - "onError__Ljava_lang_Double_2Ljava_lang_Object_2Ljava_lang_Double_2", - Double.valueOf(42), instance, 42 - ); - } - - private static TestVM code; - - @BeforeClass - public void compileTheCode() throws Exception { - code = TestVM.compileClass("org/apidesign/vm4brwsr/HtmlAnnotations"); - } - @AfterClass - public static void releaseTheCode() { - code = null; - } - private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception { - code.assertExec(msg, clazz, method, expRes, args); - } - -} diff -r 8ed05565a481 -r 8d29792a09c6 rt/vm/src/test/resources/org/apidesign/vm4brwsr/htmlannotations.js --- a/rt/vm/src/test/resources/org/apidesign/vm4brwsr/htmlannotations.js Thu Sep 12 12:26:23 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,19 +0,0 @@ -/* - * 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. - */ - -function mul(x, y) { return x * y; }