Pero
Pero

Reputation: 794

How to work with int values in gwt jsni methods

How is it possible to work with int values in jsni methods?

public class Person extends JavaScriptObject{                                                                                                                     

   // some other methods

   public final native void setPoints(int i)/*-{
       this.points= this.points + i;
   }-*/;

   public final native int getPoints()/*-{
        return this.points;
   }-*/;                                                                                    
}

I am using this in a method combined with a JsArray

public static boolean moreThanZeroPoints(JsArray<Person> arr, int index){
     if(arr.get(index).getPoints() > 0){
         return true;
     }
     return false;
}

In arr.get(index).getPoints() gives the following error-message:

    uncaught: Exception caught: Exception: caught: something other than an int was returned from JSNI method.
    package-path:: getPoints(): JS value of type undefined, expected int

For arr.get(index).setPoints(1) i get the same error-message. What is wrong? Please help.

Upvotes: 0

Views: 1799

Answers (2)

Thomas Broyer
Thomas Broyer

Reputation: 64541

Because points might not exist, you have to coerce undefined into some integer value in getPoints(); either that or add an hasPoints() method and only call getPoints() when points is defined.

// coerce undefined (or null, or 0) to 0
public final native int getPoints() /*-{
  return this.points || 0;
}-*/;

or

// coerce undefined or null to -1
public final native int getPoints() /*-{
  return (this.points != null) ? this.points : -1;
}-*/;

For setPoints(), you had declared a return type of int but never actually returned anything, which is equivalent to returning undefined in JS. qben edited your answer and fixed it by changing the return type to void.

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Note that the code did not reference the JavaScript window object directly inside the method. When accessing the browser's window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page's window and document.

public final native int setPoints(int i)/-{ 
    $wnd.points=  $wnd.points + i;
 }-/;

And should like below:

public final native int getPoints()/*-{
    return  eval('$wnd.' + points);;

}-*/;

Upvotes: 0

Related Questions