# HG changeset patch # User Jaroslav Tulach # Date 1371483630 -7200 # Node ID 2fee889b9830f59dc7a90789a9d9e879d1d51197 # Parent 4ae766848ce0feddbaa01c70dc251cd58c428c81 Console using JavaScriptBody diff -r 4ae766848ce0 -r 2fee889b9830 launcher/fx/pom.xml --- a/launcher/fx/pom.xml Mon Jun 17 16:17:46 2013 +0200 +++ b/launcher/fx/pom.xml Mon Jun 17 17:40:30 2013 +0200 @@ -68,7 +68,7 @@ ${project.groupId} core ${project.version} - test + compile org.ow2.asm diff -r 4ae766848ce0 -r 2fee889b9830 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java Mon Jun 17 16:17:46 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java Mon Jun 17 17:40:30 2013 +0200 @@ -25,8 +25,8 @@ import java.lang.reflect.Modifier; import java.net.URL; import java.util.Enumeration; -import javafx.scene.web.WebEngine; import netscape.javascript.JSObject; +import org.apidesign.bck2brwsr.core.JavaScriptBody; /** * @@ -35,17 +35,15 @@ public final class Console { public Console() { } - - private static Object getAttr(Object elem, String attr) { - return InvokeJS.CObject.call("getAttr", elem, attr); - } - private static void setAttr(String id, String attr, Object value) { - InvokeJS.CObject.call("setAttrId", id, attr, value); - } - private static void setAttr(Object id, String attr, Object value) { - InvokeJS.CObject.call("setAttr", id, attr, value); - } + @JavaScriptBody(args = { "elem", "attr" }, body = "return elem[attr].toString();") + private static native Object getAttr(Object elem, String attr); + + @JavaScriptBody(args = { "id", "attr", "value" }, body = "window.document.getElementById(id)[attr] = value;") + private static native void setAttr(String id, String attr, Object value); + + @JavaScriptBody(args = { "elem", "attr", "value" }, body = "elem[attr] = value;") + private static native void setAttr(Object id, String attr, Object value); private static void closeWindow() {} @@ -78,7 +76,7 @@ textArea = null; } - private static final String BEGIN_TEST = + @JavaScriptBody(args = { "test", "c", "arr" }, body = "var ul = window.document.getElementById('bck2brwsr.result');\n" + "var li = window.document.createElement('li');\n" + "var span = window.document.createElement('span');" @@ -103,27 +101,24 @@ + "p.appendChild(pre);\n" + "ul.appendChild(li);\n" + "arr[0] = pre;\n" - + "arr[1] = status;\n"; - - private static void beginTest(String test, Case c, Object[] arr) { - InvokeJS.CObject.call("beginTest", test, c, arr); - } + + "arr[1] = status;\n" + ) + private static native void beginTest(String test, Case c, Object[] arr); - private static final String LOAD_TEXT = + @JavaScriptBody(args = { "url", "callback", "arr" }, body = "var request = new XMLHttpRequest();\n" + "request.open('GET', url, true);\n" + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n" + "request.onreadystatechange = function() {\n" + " if (this.readyState!==4) return;\n" - + " try {" + + " try {\n" + " arr[0] = this.responseText;\n" - + " callback.run__V();\n" - + " } catch (e) { alert(e); }" - + "};" - + "request.send();"; - private static void loadText(String url, Runnable callback, String[] arr) throws IOException { - InvokeJS.CObject.call("loadText", url, new Run(callback), arr); - } + + " callback.run();\n" + + " } catch (e) { alert(e); }\n" + + "};\n" + + "request.send();\n" + ) + private static native void loadText(String url, Runnable callback, String[] arr) throws IOException; public static void runHarness(String url) throws IOException { new Console().harness(url); @@ -142,11 +137,11 @@ private Request(String url) throws IOException { this.url = url; - loadText(url, this, arr); + loadText(url, new Run(this), arr); } private Request(String url, String u) throws IOException { this.url = url; - loadText(u, this, arr); + loadText(u, new Run(this), arr); } @Override @@ -181,7 +176,7 @@ } catch (Exception ex) { if (ex instanceof InterruptedException) { log("Re-scheduling in 100ms"); - schedule(this, 100); + schedule(new Run(this), 100); return; } log(ex.getClass().getName() + ":" + ex.getMessage()); @@ -254,9 +249,8 @@ private static void turnAssetionStatusOn() { } - private static Object schedule(Runnable r, int time) { - return InvokeJS.CObject.call("schedule", new Run(r), time); - } + @JavaScriptBody(args = { "r", "time" }, body = "return window.setTimeout(function() { r.run(); }, time);") + private static native Object schedule(Runnable r, int time); private static final class Case { private final Object data; @@ -352,48 +346,16 @@ } return res; } - - private static Object toJSON(String s) { - return InvokeJS.CObject.call("toJSON", s); - } + + @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');") + private static native Object toJSON(String s); private static Object value(String p, Object d) { return ((JSObject)d).getMember(p); } } - private static String safe(String txt) { - return "try {" + txt + "} catch (err) { alert(err); }"; - } - static { turnAssetionStatusOn(); } - - private static final class InvokeJS { - static final JSObject CObject = initJS(); - - private static JSObject initJS() { - WebEngine web = (WebEngine) System.getProperties().get("webEngine"); - return (JSObject) web.executeScript("(function() {" - + "var CObject = {};" - - + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };" - - + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };" - + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };" - - + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};" - - + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};" - - + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };" - - + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };" - - + "return CObject;" - + "})(this)"); - } - } - } diff -r 4ae766848ce0 -r 2fee889b9830 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 Mon Jun 17 16:17:46 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java Mon Jun 17 17:40:30 2013 +0200 @@ -40,7 +40,7 @@ JVMBridge(WebEngine eng) { this.engine = eng; - this.cl = new WebClassLoader(JVMBridge.class.getClassLoader()); + this.cl = new WebClassLoader(JVMBridge.class.getClassLoader().getParent()); } public static void registerClassLoaders(ClassLoader[] loaders) { @@ -126,7 +126,15 @@ @Override public Object invoke(Object... args) throws Exception { - return fn.call("fn", args); // NOI18N + try { + return fn.call("fn", args); // NOI18N + } catch (Error t) { + t.printStackTrace(); + throw t; + } catch (Exception t) { + t.printStackTrace(); + throw t; + } } } } diff -r 4ae766848ce0 -r 2fee889b9830 launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java Mon Jun 17 16:17:46 2013 +0200 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoader.java Mon Jun 17 17:40:30 2013 +0200 @@ -51,6 +51,21 @@ @Override protected Class findClass(String name) throws ClassNotFoundException { + if (name.startsWith("javafx")) { + return Class.forName(name); + } + if (name.startsWith("netscape")) { + return Class.forName(name); + } + if (name.startsWith("com.sun")) { + return Class.forName(name); + } + if (name.equals(JsClassLoader.class.getName())) { + return JsClassLoader.class; + } + if (name.equals(Fn.class.getName())) { + return Fn.class; + } URL u = findResource(name.replace('.', '/') + ".class"); if (u != null) { InputStream is = null;