1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 16:58:55 2013 +0200
1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 23:09:30 2013 +0200
1.3 @@ -36,23 +36,22 @@
1.4 public final class Console {
1.5 public Console() {
1.6 }
1.7 - private static final JSObject CObject;
1.8
1.9 @JavaScriptBody(args = {"elem", "attr"}, body =
1.10 "return elem[attr].toString();")
1.11 private static Object getAttr(Object elem, String attr) {
1.12 - return CObject.call("getAttr", elem, attr);
1.13 + return InvokeJS.CObject.call("getAttr", elem, attr);
1.14 }
1.15
1.16 @JavaScriptBody(args = {"id", "attr", "value"}, body =
1.17 "window.document.getElementById(id)[attr] = value;")
1.18 private static void setAttr(String id, String attr, Object value) {
1.19 - CObject.call("setAttrId", id, attr, value);
1.20 + InvokeJS.CObject.call("setAttrId", id, attr, value);
1.21 }
1.22 @JavaScriptBody(args = {"elem", "attr", "value"}, body =
1.23 "elem[attr] = value;")
1.24 private static void setAttr(Object id, String attr, Object value) {
1.25 - CObject.call("setAttr", id, attr, value);
1.26 + InvokeJS.CObject.call("setAttr", id, attr, value);
1.27 }
1.28
1.29 @JavaScriptBody(args = {}, body = "return; window.close();")
1.30 @@ -116,7 +115,7 @@
1.31
1.32 @JavaScriptBody(args = { "test", "c", "arr" }, body = BEGIN_TEST)
1.33 private static void beginTest(String test, Case c, Object[] arr) {
1.34 - CObject.call("beginTest", test, c, arr);
1.35 + InvokeJS.CObject.call("beginTest", test, c, arr);
1.36 }
1.37
1.38 private static final String LOAD_TEXT =
1.39 @@ -127,13 +126,17 @@
1.40 + " if (this.readyState!==4) return;\n"
1.41 + " try {"
1.42 + " arr[0] = this.responseText;\n"
1.43 - + " callback.run();\n"
1.44 + + " callback.run__V();\n"
1.45 + " } catch (e) { alert(e); }"
1.46 + "};"
1.47 + "request.send();";
1.48 @JavaScriptBody(args = { "url", "callback", "arr" }, body = LOAD_TEXT)
1.49 private static void loadText(String url, Runnable callback, String[] arr) throws IOException {
1.50 - CObject.call("loadText", url, new Run(callback), arr);
1.51 + InvokeJS.CObject.call("loadText", url, new Run(callback), arr);
1.52 + }
1.53 +
1.54 + public static void runHarness(String url) throws IOException {
1.55 + new Console().harness(url);
1.56 }
1.57
1.58 public void harness(String url) throws IOException {
1.59 @@ -261,7 +264,7 @@
1.60 @JavaScriptBody(args = {"r", "time"}, body =
1.61 "return window.setTimeout(function() { r.run__V(); }, time);")
1.62 private static Object schedule(Runnable r, int time) {
1.63 - return CObject.call("schedule", new Run(r), time);
1.64 + return InvokeJS.CObject.call("schedule", new Run(r), time);
1.65 }
1.66
1.67 private static final class Case {
1.68 @@ -285,7 +288,11 @@
1.69 }
1.70
1.71 public int getRequestId() {
1.72 - return (int) value("request", data);
1.73 + Object v = value("request", data);
1.74 + if (v instanceof Number) {
1.75 + return ((Number)v).intValue();
1.76 + }
1.77 + return Integer.parseInt(v.toString());
1.78 }
1.79
1.80 public String getHtmlFragment() {
1.81 @@ -357,7 +364,7 @@
1.82
1.83 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
1.84 private static Object toJSON(String s) {
1.85 - return CObject.call("toJSON", s);
1.86 + return InvokeJS.CObject.call("toJSON", s);
1.87 }
1.88
1.89 @JavaScriptBody(args = {"p", "d"}, body =
1.90 @@ -376,26 +383,33 @@
1.91
1.92 static {
1.93 turnAssetionStatusOn();
1.94 + }
1.95 +
1.96 + private static final class InvokeJS {
1.97 + static final JSObject CObject = initJS();
1.98
1.99 - WebEngine web = (WebEngine) System.getProperties().get("webEngine");
1.100 - CObject = (JSObject) web.executeScript("(function() {"
1.101 - + "var CObject = {};"
1.102 + @JavaScriptBody(args = { }, body = "return null;")
1.103 + private static JSObject initJS() {
1.104 + WebEngine web = (WebEngine) System.getProperties().get("webEngine");
1.105 + return (JSObject) web.executeScript("(function() {"
1.106 + + "var CObject = {};"
1.107
1.108 - + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
1.109 + + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
1.110
1.111 - + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
1.112 - + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
1.113 + + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
1.114 + + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
1.115
1.116 - + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
1.117 + + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
1.118
1.119 - + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT) + "};"
1.120 -
1.121 - + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
1.122 -
1.123 - + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
1.124 + + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};"
1.125
1.126 - + "return CObject;"
1.127 - + "})(this)");
1.128 + + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
1.129 +
1.130 + + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
1.131 +
1.132 + + "return CObject;"
1.133 + + "})(this)");
1.134 + }
1.135 }
1.136
1.137 }