JsInterop @JsFunction is not really a function?

586 views
Skip to first unread message

Marcin Okraszewski

unread,
Apr 9, 2015, 4:47:08 AM4/9/15
to google-we...@googlegroups.com
Hi,
I tried using @JsFunction to define a callback for jQuery click handler. jQuery is failing on calling the function, as there was no .apply() method. I've checked and apparently I can call the @JsFunction with brackets (eg. variable()), but nothing like call() or apply() available. Looking into debugger it looks like the @JsFunction instance is having different prototype than normal function (screenshots below).

Am I doing something wrong or is it not yet supported? Will it be supported? I'm running on trunk version from April 3rd.



Thank you,
Marcin

P.S.
I was asking the question in another thread about Java 8. Sorry for re-posting, but I guess it wasn't answered due to thread name not matching the question any more.

Jens

unread,
Apr 9, 2015, 7:45:00 AM4/9/15
to google-we...@googlegroups.com
Hmm not 100% sure but it looks intended.

How the @JsFunction is implemented in the AST: 

How the Javascript function is generated that wraps the call to your @JsFunction Java SAM method: 

So you can see the generated javascript function is calling the SAM method by providing itself as "this" (using samMethod.call(lambda, ..) ) and the proto is set to the Foo instance that has the samMethod defined.


-- J.

Marcin Okraszewski

unread,
Apr 9, 2015, 8:31:19 AM4/9/15
to google-we...@googlegroups.com
Hi Jens,
Thanks for answering again :) 

I just wonder - if this is intended, how should I then pass callback to JS functions? I was doing similar jQuery example as was presented on "Deep dive in JS Interop" persentation - https://www.youtube.com/watch?feature=player_embedded&v=-s4Wfnojh1Q#t=200 . My even simplified version. And this is failing. For me if the @JsFunction doesn't fully behave like a function, it doesn't fulfill the premise and the integration with JS will be a bit harder. I hope it is just not yet finished. 

@JsType(prototype = "jQuery")
public interface JQuery {
JQuery css(String attr, String value);
JQuery click(Callback callback);
}

@JsFunction
public interface Callback {
void callback();

static native JQuery jquery(String selector) /*-{
return $wnd.jQuery(selector);
}-*/;
 
// ...

jquery("button").click( () -> Window.alert("Click!") );


Marcin

Jens

unread,
Apr 14, 2015, 5:56:23 AM4/14/15
to google-we...@googlegroups.com, gok...@google.com
Maybe Goktug can say something about it.

The spec document [1] has quite a bit of information about SAM handling however @JsFunction isn't directly mentioned yet and the implementation of @JsFunction is actually a bit different. I am pretty sure @JsFunction should somehow behave like a real function because you never know how a JS library will execute a callback. It would indeed hurt interoperability if callback.apply and callback.call can't be called by a JS lib, e.g. JQuery, to run the SAM method as you pointed out.

However as the spec document points out SAM handling is a bit more complicated as the lambda expression might get passed back to Java and then should behave as Java again.

Marcin Okraszewski

unread,
Apr 14, 2015, 7:40:30 AM4/14/15
to google-we...@googlegroups.com, gok...@google.com
The document you posted is very interesting. Is there any place that would tell what is implemented out of it?

I understand the problem with call() and apply(), as these are used to specify what object will be visible as "this". I cannot think about corresponding concept in Java. I don't know how that should be solved, but without it, it is very difficult to say we have a seamless integration with JS world. Maybe there should be always passed "this" as first parameter? It wouldn't be very natural, but at least gave a way to work with it.

Thank you,
Marcin

Goktug Gokdogan

unread,
Jul 16, 2015, 11:27:37 PM7/16/15
to Marcin Okraszewski, google-web-toolkit
(It looks like I forgot this email in my drafts.)
Reply all
Reply to author
Forward
0 new messages