GWT JSNI Callback

2,376 views
Skip to first unread message

zorro

unread,
Jun 4, 2012, 9:42:36 PM6/4/12
to google-we...@googlegroups.com
I am trying to implement a callback using JSNI. 
 1. I don't know if changing "this" is allowed in the way I do it.
 2. I don't know if such an assignment is advisable from the perspective of JSNI functionality
 3. The compiler in Eclispe does not seem to grasp that as well and I don't know the right syntax 

for    this.@com.roamtopix.client.StaticFacad.JsCallback::callback.execute(Lcom/google/gwt/core/client/JavaScriptObject;)(jsObject); with "callback.execute" it says 
"JavaScript parsing: Missing ) after argument list"

for this.@com.roamtopix.client.StaticFacad.JsCallback::callback::execute(Lcom/google/gwt/core/client/JavaScriptObject;)(jsObject);with "callback::execute" it says
"JavaScript parsing: Invalid label"

public static interface JavaCallback {
void execute(JavaScriptObject jsObject);
public static class JsCallback extends JavaScriptObject {
public final JavaCallback callback;
public JsCallback(JavaCallback callback){
this.callback = callback;
init();
}
private native void init()/*-{
this.@com.roamtopix.client.StaticFacad.JsCallback::that(Lcom/google/gwt/core/client/JavaScriptObject;)(this);
}-*/;
private native void that(JavaScriptObject that)/*-{
var fn = function(jsObject) { 
this.@com.roamtopix.client.StaticFacad.JsCallback::callback.execute(Lcom/google/gwt/core/client/JavaScriptObject;)(jsObject);
};
that = $entry(fn);
}-*/;
}


Thomas Broyer

unread,
Jun 5, 2012, 5:37:24 AM6/5/12
to google-we...@googlegroups.com


On Tuesday, June 5, 2012 3:42:36 AM UTC+2, zorro wrote:
I am trying to implement a callback using JSNI. 
 1. I don't know if changing "this" is allowed in the way I do it.

No.
BTW, you're not doing much.
 
 2. I don't know if such an assignment is advisable from the perspective of JSNI functionality

No.
 
 3. The compiler in Eclispe does not seem to grasp that as well and I don't know the right syntax 

for    this.@com.roamtopix.client.StaticFacad.JsCallback::callback.execute(Lcom/google/gwt/core/client/JavaScriptObject;)(jsObject); with "callback.execute" it says 
"JavaScript parsing: Missing ) after argument list"

for this.@com.roamtopix.client.StaticFacad.JsCallback::callback::execute(Lcom/google/gwt/core/client/JavaScriptObject;)(jsObject);with "callback::execute" it says
"JavaScript parsing: Invalid label"


var callback = this.@com.roamtopix.client.StaticFacad.JsCallback::callback;
callback.@com.roamtopix.client.StaticFacad.JavaCallback::execute(Lcom/google/gwt/core/client/JavaScriptObject;)(jsObject);

But there's simpler.
It depends how you (intend to) use your callback though.
Have a look at https://code.google.com/p/gwt-in-the-air/source/browse/trunk/src/net/ltgt/gwt/browserplus/core/client/BrowserPlus.java#145 (the createNativeCallback method) for a way of wrapping a Java callback into a JS function to be passed as a "callback" to some API.

zorro

unread,
Jun 7, 2012, 6:17:03 PM6/7/12
to google-we...@googlegroups.com


Dne úterý, 5. června 2012 3:42:36 UTC+2 zorro napsal(a):

zorro

unread,
Jun 7, 2012, 6:19:57 PM6/7/12
to google-we...@googlegroups.com
This Google Groups interface is F***** HORRIBLE. I send a response and it disappears as I never send it. So now what is it out there or not? Wasn't this written in GWT?

Dne pátek, 8. června 2012 0:17:03 UTC+2 zorro napsal(a):

zorro

unread,
Jun 7, 2012, 6:24:06 PM6/7/12
to google-we...@googlegroups.com
I just wrote that I got a Compiler Error, using TRACE 

saying: 
references to instance methods in overlay types are illegal; use a stronger type or a Java trampoline method

But compilation succeeds.

Dne úterý, 5. června 2012 11:37:24 UTC+2 Thomas Broyer napsal(a):

zorro

unread,
Jun 7, 2012, 6:54:48 PM6/7/12
to google-we...@googlegroups.com
Regarding callbacks there is one more error I am getting:
Only one JavaScriptObject type may implement the methods of an interface that declared methods.

This
gwt-2.4.0/doc/helpInfo/jsoRestrictions.html
explains alot. But why nobody tell me that in the past? I mean tutorials, talks?


Dne pátek, 8. června 2012 0:17:03 UTC+2 zorro napsal(a):

Thomas Broyer

unread,
Jun 8, 2012, 3:52:56 AM6/8/12
to google-we...@googlegroups.com


On Friday, June 8, 2012 12:19:57 AM UTC+2, zorro wrote:
This Google Groups interface is F***** HORRIBLE. I send a response and it disappears as I never send it.

You probably clicked "Reply to Author", as I received the mail in my inbox, sent to me only, not the group.

Thomas Broyer

unread,
Jun 8, 2012, 3:57:09 AM6/8/12
to google-we...@googlegroups.com


On Friday, June 8, 2012 12:54:48 AM UTC+2, zorro wrote:
Regarding callbacks there is one more error I am getting:
Only one JavaScriptObject type may implement the methods of an interface that declared methods.

This
gwt-2.4.0/doc/helpInfo/jsoRestrictions.html
explains alot. But why nobody tell me that in the past? I mean tutorials, talks?


Thomas Broyer

unread,
Jun 8, 2012, 5:24:39 AM6/8/12
to zorro, Google Web Toolkit
On Fri, Jun 8, 2012 at 12:15 AM, zorro <jan....@gmail.com> wrote:
> I am getting, when compiled with TRACE
> Error, ... references to instance methods in overlay types are illegal; use
> a stronger type or a Java trampoline method
>
> From the compiler output I found out there is:
> gwt-2.4.0/doc/helpInfo/jsoRestrictions.html
>
> There is no link to this from
> https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI

Because JSOs have their own section in the Dev Guide:
https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsOverlay

In this case, see
http://code.google.com/p/google-web-toolkit/wiki/OverlayTypes#Restrictions
(linked to at the bottom of the above-mentioned page):
“Native JSNI methods may not refer to instance methods within
@SingleJsoImpl types, just as they may not refer to instance methods
on overlay types.”
(note that @SingleJsoImpl doesn't actually exist, it was in an earlier
iteration, now the GWT Compiler infers it from how it's actually being
used)

What it means is that if you have a JavaScriptObject subclass
com.example.client.Foo with a "public final bar()" method (native or
not), you cannot write in JSNI:
someObj.@com.example.client.Foo::bar()();
This is due to how JSOs are handled by the compiler: all methods are
transformed into static methods, they're not instance methods on the
JavaScript object (a JSO is just a Java "view" of a JS object).

> btw, What is "trampoline method"? Sounds good;-)

public static void trampoline_to_bar(Foo foo) {
foo.bar();
}

Then you can use in JSNI:
@com.example.client.Trampoline::trampoline_to_bar(Lcom/example/client/Foo;)(someObj);

zorro

unread,
Jun 8, 2012, 7:48:41 AM6/8/12
to google-we...@googlegroups.com
Yeap, the interface is a bit couterintuitive thanks to Czech translation that I am seeing. "Publicise your answer" should be "Reply publicly". Anyway.

Dne pátek, 8. června 2012 9:52:56 UTC+2 Thomas Broyer napsal(a):

zorro

unread,
Jun 8, 2012, 8:04:37 AM6/8/12
to google-we...@googlegroups.com
"gwt-2.4.0/doc/helpInfo/jsoRestrictions.html" comes with GWT distribution. So far best readable document on the topic. However, a bit of disappointment. b/c It implies I can not implement callbacks in a neat way and in general.

The example you pointed me two days ago is no more relevat, since it breaks all the RULES.

From gwt-2.4.0/doc/helpInfo/jsoRestrictions.html:

4. JSO classes cannot have instance fields. -- this is waht callbacks are about.

2. An interface type may be implemented by at most one JSO subtype. -- this excludes possibility of having a general interface for callbacks

What this implies is that anythig more complex in JSNI boils down to TRAMPOLINE METHODS. Ugly. A lots of boilerplate code. 
The otherwise "closured" instance fileds (See 4. above) now have to be static Java fileds on the trapoline. (Trampoline is not very flexible/dynamic thing indeed) 


Dne pátek, 8. června 2012 9:57:09 UTC+2 Thomas Broyer napsal(a):

Thomas Broyer

unread,
Jun 8, 2012, 8:09:43 AM6/8/12
to google-we...@googlegroups.com


On Friday, June 8, 2012 2:04:37 PM UTC+2, zorro wrote:
"gwt-2.4.0/doc/helpInfo/jsoRestrictions.html" comes with GWT distribution. So far best readable document on the topic. However, a bit of disappointment. b/c It implies I can not implement callbacks in a neat way and in general.

The example you pointed me two days ago is no more relevat, since it breaks all the RULES.

Not as long as Callback is not a JSO.
 
From gwt-2.4.0/doc/helpInfo/jsoRestrictions.html:

4. JSO classes cannot have instance fields. -- this is waht callbacks are about.

2. An interface type may be implemented by at most one JSO subtype. -- this excludes possibility of having a general interface for callbacks


Why the hell are you trying to have your callback extend JavaScriptObject ?!

Maybe you need to give more details on what you're trying to achieve.

zorro

unread,
Jun 8, 2012, 10:44:18 AM6/8/12
to google-we...@googlegroups.com
Yep, You are right. No need for my callback to be JSO. Prevopisly I was passing it to another callback, oh no.
I need only call callback as $wnd.gwt.executeCallback(callback, param); and the example you gave applies.
Thanks


Dne pátek, 8. června 2012 14:09:43 UTC+2 Thomas Broyer napsal(a):

Joseph Lust

unread,
Jun 8, 2012, 3:55:48 PM6/8/12
to google-we...@googlegroups.com
On May 30th, I submitted a reply. It then appeared as 'Deleted' in the thread. I rewrote it and submitted again. Then the next day it was undeleted and appeared in the web interface. Since I then had two of them, I deleted the first, but never deleted it on the 30th. Something wonky on their backend.

Even weirder, on May 30th, I went from having 0 unread message on the group to a week's worth (all the way back to May 24th). You'll notice that a very large number of posts appeared from no where that day that no body had replied to yet and had no views, since they were living in some parallel universe until then.

I wonder if it has to do with the 'eventual consistency' of DataStore on AppEngine? Anyway, I concur the groups interface can behave in odd ways.



Joe

Chris Price

unread,
Jun 8, 2012, 4:17:08 PM6/8/12
to google-we...@googlegroups.com
I'd assumed the bulk landing of messages was due to them being held in
a new-poster queue until being approved (normally on a Friday
morning)?
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-web-toolkit/-/6Mq6aZK5JMUJ.
>
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-web-tool...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
Reply all
Reply to author
Forward
0 new messages