Special handling of undefined before passing it into Java in a callback
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 02 May 2016 05:53:36 +0200
changeset 108657c08b33f351
parent 1085 dfa26220d065
child 1087 7865ad598184
Special handling of undefined before passing it into Java in a callback
boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java
boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java
json-tck/src/main/java/net/java/html/js/tests/Bodies.java
json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
     1.1 --- a/boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java	Wed Apr 27 18:23:24 2016 +0200
     1.2 +++ b/boot-fx/src/main/java/org/netbeans/html/boot/fx/AbstractFXPresenter.java	Mon May 02 05:53:36 2016 +0200
     1.3 @@ -282,11 +282,14 @@
     1.4      }
     1.5  
     1.6      @Override
     1.7 -    public Object toJava(Object jsArray) {
     1.8 -        if (jsArray instanceof Weak) {
     1.9 -            jsArray = ((Weak)jsArray).get();
    1.10 +    public Object toJava(Object toJS) {
    1.11 +        if (toJS instanceof Weak) {
    1.12 +            toJS = ((Weak)toJS).get();
    1.13          }
    1.14 -        return checkArray(jsArray);
    1.15 +        if (toJS == undefined()) {
    1.16 +            return null;
    1.17 +        }
    1.18 +        return checkArray(toJS);
    1.19      }
    1.20  
    1.21      @Override
     2.1 --- a/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java	Wed Apr 27 18:23:24 2016 +0200
     2.2 +++ b/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java	Mon May 02 05:53:36 2016 +0200
     2.3 @@ -44,6 +44,7 @@
     2.4  
     2.5  import java.io.Closeable;
     2.6  import java.io.IOException;
     2.7 +import java.io.ObjectOutput;
     2.8  import java.io.Reader;
     2.9  import java.lang.ref.WeakReference;
    2.10  import java.net.URL;
    2.11 @@ -87,6 +88,7 @@
    2.12      }
    2.13      private final ScriptEngine eng;
    2.14      private final Executor exc;
    2.15 +    private final Object undefined;
    2.16  
    2.17      public ScriptPresenter(Executor exc) {
    2.18          this.exc = exc;
    2.19 @@ -95,6 +97,13 @@
    2.20              eng.eval("function alert(msg) { Packages.java.lang.System.out.println(msg); };");
    2.21              eng.eval("function confirm(msg) { Packages.java.lang.System.out.println(msg); return true; };");
    2.22              eng.eval("function prompt(msg, txt) { Packages.java.lang.System.out.println(msg + ':' + txt); return txt; };");
    2.23 +            Object undef;
    2.24 +            if (JDK7) {
    2.25 +                undef = new JDK7Callback().undefined(eng);
    2.26 +            } else {
    2.27 +                undef = ((Object[])eng.eval("Java.to([undefined])"))[0];
    2.28 +            }
    2.29 +            this.undefined = undef;
    2.30          } catch (ScriptException ex) {
    2.31              throw new IllegalStateException(ex);
    2.32          }
    2.33 @@ -211,12 +220,15 @@
    2.34      }
    2.35  
    2.36      @Override
    2.37 -    public Object toJava(Object jsArray) {
    2.38 -        if (jsArray instanceof Weak) {
    2.39 -            jsArray = ((Weak)jsArray).get();
    2.40 +    public Object toJava(Object toJS) {
    2.41 +        if (toJS instanceof Weak) {
    2.42 +            toJS = ((Weak)toJS).get();
    2.43 +        }
    2.44 +        if (toJS == undefined) {
    2.45 +            return null;
    2.46          }
    2.47          try {
    2.48 -            return checkArray(jsArray);
    2.49 +            return checkArray(toJS);
    2.50          } catch (Exception ex) {
    2.51              throw new IllegalStateException(ex);
    2.52          }
    2.53 @@ -343,4 +355,89 @@
    2.54              super(referent);
    2.55          }
    2.56      }
    2.57 +
    2.58 +    private static final class JDK7Callback implements ObjectOutput {
    2.59 +        private Object undefined;
    2.60 +
    2.61 +        @Override
    2.62 +        public void writeObject(Object obj) throws IOException {
    2.63 +            undefined = obj;
    2.64 +        }
    2.65 +
    2.66 +        @Override
    2.67 +        public void write(int b) throws IOException {
    2.68 +        }
    2.69 +
    2.70 +        @Override
    2.71 +        public void write(byte[] b) throws IOException {
    2.72 +        }
    2.73 +
    2.74 +        @Override
    2.75 +        public void write(byte[] b, int off, int len) throws IOException {
    2.76 +        }
    2.77 +
    2.78 +        @Override
    2.79 +        public void flush() throws IOException {
    2.80 +        }
    2.81 +
    2.82 +        @Override
    2.83 +        public void close() throws IOException {
    2.84 +        }
    2.85 +
    2.86 +        @Override
    2.87 +        public void writeBoolean(boolean v) throws IOException {
    2.88 +        }
    2.89 +
    2.90 +        @Override
    2.91 +        public void writeByte(int v) throws IOException {
    2.92 +        }
    2.93 +
    2.94 +        @Override
    2.95 +        public void writeShort(int v) throws IOException {
    2.96 +        }
    2.97 +
    2.98 +        @Override
    2.99 +        public void writeChar(int v) throws IOException {
   2.100 +        }
   2.101 +
   2.102 +        @Override
   2.103 +        public void writeInt(int v) throws IOException {
   2.104 +        }
   2.105 +
   2.106 +        @Override
   2.107 +        public void writeLong(long v) throws IOException {
   2.108 +        }
   2.109 +
   2.110 +        @Override
   2.111 +        public void writeFloat(float v) throws IOException {
   2.112 +        }
   2.113 +
   2.114 +        @Override
   2.115 +        public void writeDouble(double v) throws IOException {
   2.116 +        }
   2.117 +
   2.118 +        @Override
   2.119 +        public void writeBytes(String s) throws IOException {
   2.120 +        }
   2.121 +
   2.122 +        @Override
   2.123 +        public void writeChars(String s) throws IOException {
   2.124 +        }
   2.125 +
   2.126 +        @Override
   2.127 +        public void writeUTF(String s) throws IOException {
   2.128 +        }
   2.129 +
   2.130 +        public Object undefined(ScriptEngine eng) {
   2.131 +            try {
   2.132 +                eng.eval("function isJDK7Undefined(js) { js.writeObject(undefined); }");
   2.133 +                Invocable inv = (Invocable) eng;
   2.134 +                inv.invokeFunction("isJDK7Undefined", this);
   2.135 +            } catch (NoSuchMethodException | ScriptException ex) {
   2.136 +                throw new IllegalStateException(ex);
   2.137 +            }
   2.138 +            return undefined;
   2.139 +        }
   2.140 +    }
   2.141 +
   2.142  }
     3.1 --- a/json-tck/src/main/java/net/java/html/js/tests/Bodies.java	Wed Apr 27 18:23:24 2016 +0200
     3.2 +++ b/json-tck/src/main/java/net/java/html/js/tests/Bodies.java	Mon May 02 05:53:36 2016 +0200
     3.3 @@ -216,7 +216,7 @@
     3.4          + "obj.x = 1;\n"
     3.5          + "return sum.@net.java.html.js.tests.Sum::checkNonNull(Ljava/lang/Object;)(obj[p]);\n"
     3.6      )
     3.7 -    static native boolean nonNull(Sum sum, String p);
     3.8 +    static native Boolean nonNull(Sum sum, String p);
     3.9  
    3.10      @JavaScriptBody(args = {}, javacall = true, body = 
    3.11          "return @net.java.html.js.tests.Bodies::problematicString()();"
    3.12 @@ -234,6 +234,9 @@
    3.13      )
    3.14      static native String yesNo(Callable<Boolean> call);
    3.15  
    3.16 +    @JavaScriptBody(args = {"arr", "val"}, body = "return arr[0] === val;")
    3.17 +    public static native boolean isInArray(Object[] arr, Object val);
    3.18 +    
    3.19      @JavaScriptBody(args = {}, body = "return globalString;")
    3.20      static native String readGlobalString();
    3.21      
     4.1 --- a/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java	Wed Apr 27 18:23:24 2016 +0200
     4.2 +++ b/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java	Mon May 02 05:53:36 2016 +0200
     4.3 @@ -384,8 +384,8 @@
     4.4      
     4.5      @KOTest public void callbackUnknown() {
     4.6          Sum s = new Sum();
     4.7 -        boolean isNull = Bodies.nonNull(s, "y");
     4.8 -        assertTrue(isNull, "y property doesn't exist");
     4.9 +        Boolean nonNull = Bodies.nonNull(s, "y");
    4.10 +        assertFalse(Boolean.TRUE.equals(nonNull), "y property doesn't exist");
    4.11      }
    4.12  
    4.13      @KOTest public void callbackUnknownArray() {
    4.14 @@ -412,6 +412,13 @@
    4.15          }
    4.16          fail("The JS string is different: " + js);
    4.17      }
    4.18 +
    4.19 +    @KOTest
    4.20 +    public void doubleInAnArray() throws Exception {
    4.21 +        Double val = 2.2;
    4.22 +        boolean res = Bodies.isInArray(new Object[] { val }, val);
    4.23 +        assertTrue(res, "Should be in the array");
    4.24 +    }
    4.25      
    4.26      Later l;
    4.27      @KOTest public void callLater() throws Exception{