When you call into JS from Java, the compiler trusts that the JSNI code follows the method signature exactly. This means that if a double
value is returned from an int
method, you might get other effects elsewhere. This yields the following results:
- Casting is required if you don't trust the JS code to return the correct type. For example, if the method declares an int, but the js code may return a double, you should coerce to an int. Your linked question is more about returning null instead of a number.
In terms of performance it makes no difference at all. Since GWT assumes that the JS will do what you say it will do, it doesn't do any extra work to verify that (except in dev mode, as a way to validate that the code makes sense)
There are several ways to cast from a JS Number which may be floating point to an integer, so that you can be certain that GWT/Java code can accept it. The first is to simply return a
double
, and then in your Java code, cast to anint
, or useMath.round
to remove any unneeded precision. Another is to do in your JSNI whatcom.google.gwt.dom.client.Element#toInt32
does as of GWT 2.6:/** * Fast helper method to convert small doubles to 32-bit int. * * <p>Note: you should be aware that this uses JavaScript rounding and thus * does NOT provide the same semantics as <code>int b = (int) someDouble;</code>. * In particular, if x is outside the range [-2^31,2^31), then toInt32(x) would return a value * equivalent to x modulo 2^32, whereas (int) x would evaluate to either MIN_INT or MAX_INT. */ private static native int toInt32(double val) /*-{ return val | 0; }-*/;
Note the use of
| 0
versus|| 0
in your linked answer - the first is a bitwise OR, while the second is a boolean OR.There is no problem returning
int
from JSNI - the linked issue is about returningjava.lang.Integer
, the boxed Java type around anint
. That issue is flawed, since there is no need for aJsArray<Integer>
sincecom.google.gwt.core.client.JsArrayNumber
exists already, you simply must cast to an int to deal with rounding issues.