launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java
branchNbHtml4J
changeset 1423 237dbcd482dc
parent 1249 cdaeea7becf2
child 1752 c5157116bbc4
     1.1 --- a/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java	Mon Aug 26 08:56:37 2013 +0200
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java	Thu Jan 09 11:55:01 2014 +0100
     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 net.java.html.js.JavaScriptBody;
     1.8  import netscape.javascript.JSObject;
     1.9 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.10  
    1.11  /**
    1.12   *
    1.13 @@ -60,8 +60,7 @@
    1.14      }
    1.15      
    1.16      private static void beginTest(Case c) {
    1.17 -        Object[] arr = new Object[2];
    1.18 -        beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
    1.19 +        Object[] arr = beginTest(c.getClassName() + "." + c.getMethodName(), c, new Object[2]);
    1.20          textArea = arr[0];
    1.21          statusArea = arr[1];
    1.22      }
    1.23 @@ -102,23 +101,23 @@
    1.24          + "ul.appendChild(li);\n"
    1.25          + "arr[0] = pre;\n"
    1.26          + "arr[1] = status;\n"
    1.27 +        + "return arr;"
    1.28      )
    1.29 -    private static native void beginTest(String test, Case c, Object[] arr);
    1.30 +    private static native Object[] beginTest(String test, Case c, Object[] arr);
    1.31      
    1.32 -    @JavaScriptBody(args = { "url", "callback", "arr" }, body =
    1.33 +    @JavaScriptBody(args = { "url", "callback" }, javacall = true, body =
    1.34            "var request = new XMLHttpRequest();\n"
    1.35          + "request.open('GET', url, true);\n"
    1.36          + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
    1.37          + "request.onreadystatechange = function() {\n"
    1.38          + "  if (this.readyState!==4) return;\n"
    1.39          + " try {\n"
    1.40 -        + "  arr[0] = this.responseText;\n"
    1.41 -        + "  callback.run();\n"
    1.42 +        + "  callback.@org.apidesign.bck2brwsr.launcher.fximpl.OnMessage::onMessage(Ljava/lang/String;)(this.responseText);\n"
    1.43          + " } catch (e) { alert(e); }\n"
    1.44          + "};\n"
    1.45          + "request.send();\n"
    1.46      )
    1.47 -    private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
    1.48 +    private static native void loadText(String url, OnMessage callback) throws IOException;
    1.49      
    1.50      public static void runHarness(String url) throws IOException {
    1.51          new Console().harness(url);
    1.52 @@ -129,7 +128,7 @@
    1.53          Request r = new Request(url);
    1.54      }
    1.55      
    1.56 -    private static class Request implements Runnable {
    1.57 +    private static class Request implements Runnable, OnMessage {
    1.58          private final String[] arr = { null };
    1.59          private final String url;
    1.60          private Case c;
    1.61 @@ -137,11 +136,17 @@
    1.62  
    1.63          private Request(String url) throws IOException {
    1.64              this.url = url;
    1.65 -            loadText(url, new Run(this), arr);
    1.66 +            loadText(url, this);
    1.67          }
    1.68          private Request(String url, String u) throws IOException {
    1.69              this.url = url;
    1.70 -            loadText(u, new Run(this), arr);
    1.71 +            loadText(u, this);
    1.72 +        }
    1.73 +
    1.74 +        @Override
    1.75 +        public void onMessage(String msg) {
    1.76 +            arr[0] = msg;
    1.77 +            run();
    1.78          }
    1.79          
    1.80          @Override
    1.81 @@ -177,7 +182,7 @@
    1.82              } catch (Exception ex) {
    1.83                  if (ex instanceof InterruptedException) {
    1.84                      log("Re-scheduling in 100ms");
    1.85 -                    schedule(new Run(this), 100);
    1.86 +                    schedule(this, 100);
    1.87                      return;
    1.88                  }
    1.89                  log(ex.getClass().getName() + ":" + ex.getMessage());
    1.90 @@ -250,7 +255,11 @@
    1.91      private static void turnAssetionStatusOn() {
    1.92      }
    1.93  
    1.94 -    @JavaScriptBody(args = { "r", "time" }, body = "return window.setTimeout(function() { r.run(); }, time);")
    1.95 +    @JavaScriptBody(args = { "r", "time" }, javacall = true, body = 
    1.96 +        "return window.setTimeout(function() { "
    1.97 +        + "r.@java.lang.Runnable::run()(); "
    1.98 +        + "}, time);"
    1.99 +    )
   1.100      private static native Object schedule(Runnable r, int time);
   1.101      
   1.102      private static final class Case {