launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java
branchclassloader
changeset 1179 2fee889b9830
parent 1165 06e7a74c72cf
child 1249 cdaeea7becf2
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java	Tue May 28 21:49:38 2013 +0200
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java	Mon Jun 17 17:40:30 2013 +0200
     1.3 @@ -25,8 +25,8 @@
     1.4  import java.lang.reflect.Modifier;
     1.5  import java.net.URL;
     1.6  import java.util.Enumeration;
     1.7 -import javafx.scene.web.WebEngine;
     1.8  import netscape.javascript.JSObject;
     1.9 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.10  
    1.11  /**
    1.12   *
    1.13 @@ -35,17 +35,15 @@
    1.14  public final class Console {
    1.15      public Console() {
    1.16      }
    1.17 -    
    1.18 -    private static Object getAttr(Object elem, String attr) {
    1.19 -        return InvokeJS.CObject.call("getAttr", elem, attr);
    1.20 -    }
    1.21  
    1.22 -    private static void setAttr(String id, String attr, Object value) {
    1.23 -        InvokeJS.CObject.call("setAttrId", id, attr, value);
    1.24 -    }
    1.25 -    private static void setAttr(Object id, String attr, Object value) {
    1.26 -        InvokeJS.CObject.call("setAttr", id, attr, value);
    1.27 -    }
    1.28 +    @JavaScriptBody(args = { "elem", "attr" }, body = "return elem[attr].toString();")
    1.29 +    private static native Object getAttr(Object elem, String attr);
    1.30 +
    1.31 +    @JavaScriptBody(args = { "id", "attr", "value" }, body = "window.document.getElementById(id)[attr] = value;")
    1.32 +    private static native void setAttr(String id, String attr, Object value);
    1.33 +
    1.34 +    @JavaScriptBody(args = { "elem", "attr", "value" }, body = "elem[attr] = value;")
    1.35 +    private static native void setAttr(Object id, String attr, Object value);
    1.36      
    1.37      private static void closeWindow() {}
    1.38  
    1.39 @@ -78,7 +76,7 @@
    1.40          textArea = null;
    1.41      }
    1.42  
    1.43 -    private static final String BEGIN_TEST =  
    1.44 +    @JavaScriptBody(args = { "test", "c", "arr" }, body = 
    1.45          "var ul = window.document.getElementById('bck2brwsr.result');\n"
    1.46          + "var li = window.document.createElement('li');\n"
    1.47          + "var span = window.document.createElement('span');"
    1.48 @@ -103,27 +101,24 @@
    1.49          + "p.appendChild(pre);\n"
    1.50          + "ul.appendChild(li);\n"
    1.51          + "arr[0] = pre;\n"
    1.52 -        + "arr[1] = status;\n";
    1.53 -        
    1.54 -    private static void beginTest(String test, Case c, Object[] arr) {
    1.55 -        InvokeJS.CObject.call("beginTest", test, c, arr);
    1.56 -    }
    1.57 +        + "arr[1] = status;\n"
    1.58 +    )
    1.59 +    private static native void beginTest(String test, Case c, Object[] arr);
    1.60      
    1.61 -    private static final String LOAD_TEXT = 
    1.62 +    @JavaScriptBody(args = { "url", "callback", "arr" }, body =
    1.63            "var request = new XMLHttpRequest();\n"
    1.64          + "request.open('GET', url, true);\n"
    1.65          + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
    1.66          + "request.onreadystatechange = function() {\n"
    1.67          + "  if (this.readyState!==4) return;\n"
    1.68 -        + " try {"
    1.69 +        + " try {\n"
    1.70          + "  arr[0] = this.responseText;\n"
    1.71 -        + "  callback.run__V();\n"
    1.72 -        + " } catch (e) { alert(e); }"
    1.73 -        + "};"
    1.74 -        + "request.send();";
    1.75 -    private static void loadText(String url, Runnable callback, String[] arr) throws IOException {
    1.76 -        InvokeJS.CObject.call("loadText", url, new Run(callback), arr);
    1.77 -    }
    1.78 +        + "  callback.run();\n"
    1.79 +        + " } catch (e) { alert(e); }\n"
    1.80 +        + "};\n"
    1.81 +        + "request.send();\n"
    1.82 +    )
    1.83 +    private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
    1.84      
    1.85      public static void runHarness(String url) throws IOException {
    1.86          new Console().harness(url);
    1.87 @@ -142,11 +137,11 @@
    1.88  
    1.89          private Request(String url) throws IOException {
    1.90              this.url = url;
    1.91 -            loadText(url, this, arr);
    1.92 +            loadText(url, new Run(this), arr);
    1.93          }
    1.94          private Request(String url, String u) throws IOException {
    1.95              this.url = url;
    1.96 -            loadText(u, this, arr);
    1.97 +            loadText(u, new Run(this), arr);
    1.98          }
    1.99          
   1.100          @Override
   1.101 @@ -181,7 +176,7 @@
   1.102              } catch (Exception ex) {
   1.103                  if (ex instanceof InterruptedException) {
   1.104                      log("Re-scheduling in 100ms");
   1.105 -                    schedule(this, 100);
   1.106 +                    schedule(new Run(this), 100);
   1.107                      return;
   1.108                  }
   1.109                  log(ex.getClass().getName() + ":" + ex.getMessage());
   1.110 @@ -254,9 +249,8 @@
   1.111      private static void turnAssetionStatusOn() {
   1.112      }
   1.113  
   1.114 -    private static Object schedule(Runnable r, int time) {
   1.115 -        return InvokeJS.CObject.call("schedule", new Run(r), time);
   1.116 -    }
   1.117 +    @JavaScriptBody(args = { "r", "time" }, body = "return window.setTimeout(function() { r.run(); }, time);")
   1.118 +    private static native Object schedule(Runnable r, int time);
   1.119      
   1.120      private static final class Case {
   1.121          private final Object data;
   1.122 @@ -352,48 +346,16 @@
   1.123              }
   1.124              return res;
   1.125          }
   1.126 -        
   1.127 -        private static Object toJSON(String s) {
   1.128 -            return InvokeJS.CObject.call("toJSON", s);
   1.129 -        }
   1.130 +
   1.131 +        @JavaScriptBody(args = { "s" }, body = "return eval('(' + s + ')');")
   1.132 +        private static native Object toJSON(String s);
   1.133          
   1.134          private static Object value(String p, Object d) {
   1.135              return ((JSObject)d).getMember(p);
   1.136          }
   1.137      }
   1.138      
   1.139 -    private static String safe(String txt) {
   1.140 -        return "try {" + txt + "} catch (err) { alert(err); }";
   1.141 -    }
   1.142 -    
   1.143      static {
   1.144          turnAssetionStatusOn();
   1.145      }
   1.146 -    
   1.147 -    private static final class InvokeJS {
   1.148 -        static final JSObject CObject = initJS();
   1.149 -
   1.150 -        private static JSObject initJS() {
   1.151 -            WebEngine web = (WebEngine) System.getProperties().get("webEngine");
   1.152 -            return (JSObject) web.executeScript("(function() {"
   1.153 -                + "var CObject = {};"
   1.154 -
   1.155 -                + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
   1.156 -
   1.157 -                + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
   1.158 -                + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
   1.159 -
   1.160 -                + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
   1.161 -
   1.162 -                + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};"
   1.163 -
   1.164 -                + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
   1.165 -
   1.166 -                + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
   1.167 -
   1.168 -                + "return CObject;"
   1.169 -            + "})(this)");
   1.170 -        }
   1.171 -    }
   1.172 -    
   1.173  }