From 5717d1d33206015876ebbd7f8610b44f286a73e2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Sat, 23 Mar 2013 02:11:52 +0100 Subject: [PATCH] returned js values in runJavascriptFunctionImpl should be boxed, share method used for boxing js values --- .../google/gwt/query/client/js/JsCache.java | 34 ++++++++++++++++--- .../google/gwt/query/client/js/JsUtils.java | 11 +++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java index 5d564cc8..c389ffa6 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java @@ -79,11 +79,7 @@ public class JsCache extends JavaScriptObject { } public final native T get(Object id) /*-{ - var r = this && this[id], t = typeof r; - // box booleans and numbers in a gwt object - if (t == 'boolean') return @java.lang.Boolean::valueOf(Z)(r); - if (t == 'number') return @java.lang.Double::valueOf(D)(r); - return r; + return @com.google.gwt.query.client.js.JsCache::gwtBox(*)([ this && this[id] ]); }-*/; public final JsCache getCache(int id) { @@ -245,4 +241,32 @@ public class JsCache extends JavaScriptObject { } return js; } + + /** + * Gets an object wrapped in a js array and boxes it with the appropriate + * Object in the GWT world. + * + * It is thought to be called from other jsni code without dealing with casting issues. + * + * It will box the unique element in the array with a Boolean or a Double in the case + * of primitive variables, otherwise it returns the object itself, or null if undefined. + * + * Example + *
+   * native Object myMethod() /*-{
+   *   var myJsVar = ... ;
+   *   return @com.google.gwt.query.client.js.JsCache::gwtBox(*)([ myJsVar ]);
+   * }-* /
+   * 
+ * + */ + public static native Object gwtBox(JavaScriptObject oneElementArray) /*-{ + var r = oneElementArray; + if (typeof r == 'object' && r.length == 1) { + var r = r[0]; t = typeof r; + if (t == 'boolean') return @java.lang.Boolean::valueOf(Z)(r); + if (t == 'number') return @java.lang.Double::valueOf(D)(r); + } + return r || null; + }-*/; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java index 2d9ba9dd..790afca3 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java @@ -49,13 +49,13 @@ public class JsUtils { return jso.equals(obj); } - private native void exec(JavaScriptObject f, Object data) /*-{ - f(data); + private native Object exec(JavaScriptObject f, Object data) /*-{ + return @com.google.gwt.query.client.js.JsCache::gwtBox(*)([ f(data) ]); }-*/; public void f() { if (jso != null) { - exec(jso, getDataObject()); + setArguments(exec(jso, arguments(0))); } } @@ -448,7 +448,9 @@ public class JsUtils { } private static native T runJavascriptFunctionImpl(JavaScriptObject o, String meth, JsArrayMixed args) /*-{ - return (f = o && o[meth]) && @com.google.gwt.query.client.js.JsUtils::isFunction(*)(f) && f.apply(o, args) || null; + return (f = o && o[meth]) + && @com.google.gwt.query.client.js.JsUtils::isFunction(*)(f) + && @com.google.gwt.query.client.js.JsCache::gwtBox(*)([f.apply(o, args)]); }-*/; /** @@ -514,4 +516,5 @@ public class JsUtils { } return ret; } + } -- 2.39.5