# HG changeset patch # User Jaroslav Tulach # Date 1373396795 -7200 # Node ID b6317079abe1422e4e49a346cdf5707832a5e31e # Parent a0f89ad86b1c09ba9863afef131e8a5205f30258 Basic support for net.java.html.js annotations diff -r a0f89ad86b1c -r b6317079abe1 ko/pom.xml --- a/ko/pom.xml Fri Jun 28 16:10:26 2013 +0200 +++ b/ko/pom.xml Tue Jul 09 21:06:35 2013 +0200 @@ -11,9 +11,6 @@ bck2brwsr 0.8-SNAPSHOT - - 0.4-SNAPSHOT - archetype archetype-test diff -r a0f89ad86b1c -r b6317079abe1 pom.xml --- a/pom.xml Fri Jun 28 16:10:26 2013 +0200 +++ b/pom.xml Tue Jul 09 21:06:35 2013 +0200 @@ -15,6 +15,7 @@ UTF-8 RELEASE73 COPYING + 0.4-SNAPSHOT dew diff -r a0f89ad86b1c -r b6317079abe1 rt/vm/pom.xml --- a/rt/vm/pom.xml Fri Jun 28 16:10:26 2013 +0200 +++ b/rt/vm/pom.xml Tue Jul 09 21:06:35 2013 +0200 @@ -151,6 +151,12 @@ closure-compiler r2388 compile - + + + org.apidesign.html + net.java.html.boot + test + ${net.java.html.version} + diff -r a0f89ad86b1c -r b6317079abe1 rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri Jun 28 16:10:26 2013 +0200 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Tue Jul 09 21:06:35 2013 +0200 @@ -96,16 +96,34 @@ ); } byte[] arrData = jc.findAnnotationData(true); - String[] arr = findAnnotation(arrData, jc, - "org.apidesign.bck2brwsr.core.ExtraJavaScript", - "resource", "processByteCode" - ); - if (arr != null) { - if (!arr[0].isEmpty()) { - requireScript(arr[0]); + { + String[] arr = findAnnotation(arrData, jc, + "org.apidesign.bck2brwsr.core.ExtraJavaScript", + "resource", "processByteCode" + ); + if (arr != null) { + if (!arr[0].isEmpty()) { + requireScript(arr[0]); + } + if ("0".equals(arr[1])) { + return null; + } } - if ("0".equals(arr[1])) { - return null; + } + { + String[] arr = findAnnotation(arrData, jc, + "net.java.html.js.JavaScriptResource", + "value" + ); + if (arr != null) { + if (arr[0].startsWith("/")) { + requireScript(arr[0]); + } else { + int last = jc.getClassName().lastIndexOf('/'); + requireScript( + jc.getClassName().substring(0, last + 1).replace('.', '/') + arr[0] + ); + } } } String[] proto = findAnnotation(arrData, jc, @@ -1575,6 +1593,7 @@ return null; } final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;"; + final String htmlType = "Lnet/java/html/js/JavaScriptBody;"; class P extends AnnotationParser { public P() { super(false, true); @@ -1595,6 +1614,15 @@ throw new IllegalArgumentException(attr); } } + if (type.equals(htmlType)) { + if ("body".equals(attr)) { + body = value; + } else if ("args".equals(attr)) { + args[cnt++] = value; + } else { + throw new IllegalArgumentException(attr); + } + } } } P p = new P(); diff -r a0f89ad86b1c -r b6317079abe1 rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotations.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotations.java Tue Jul 09 21:06:35 2013 +0200 @@ -0,0 +1,37 @@ +/** + * 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 { + @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); + +} diff -r a0f89ad86b1c -r b6317079abe1 rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotationsTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/HtmlAnnotationsTest.java Tue Jul 09 21:06:35 2013 +0200 @@ -0,0 +1,59 @@ +/** + * 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 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 + ); + } + + + 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 a0f89ad86b1c -r b6317079abe1 rt/vm/src/test/resources/org/apidesign/vm4brwsr/htmlannotations.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/resources/org/apidesign/vm4brwsr/htmlannotations.js Tue Jul 09 21:06:35 2013 +0200 @@ -0,0 +1,19 @@ +/* + * 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; }