Jsni on String methods in GWT 2.7

179 views
Skip to first unread message

James Nelson

unread,
Mar 29, 2015, 8:42:22 PM3/29/15
to google-web-tool...@googlegroups.com
I'm not sure if this is intended, but I've noticed that JSNI methods on string do not work.
I tried equals, compareTo, compareToIgnoreCase and charAt, but none of them worked.
I used my fork and the pristine 2.7, and in super dev + regular compile; in all cases, the functions come back undefined.

native char jsni(String s)
/*-{
  var fn = s.@java.lang.String::charAt(I);
  // fn is undefined
  return fn(0); // doom
}-*/;

In compiled code, it looks something like:
var fn = string_0_g$.charAt_3_g$;


So, my question, is this expected behaviour?

If so, are there other types I need to guard against JSNI failures?

If not, should I file a bug and look into a patch?
(Assuming a patch would be a rewrite static method visitor that can replace jsni method refs with a staticified version).

For my reflection support, I'm going to guard against this by making public methods not use JSNI,
but if there are other types or gotchas I should be aware of, it would be nice to know about them.


Thanks!

Roberto Lublinerman

unread,
Mar 30, 2015, 12:06:36 PM3/30/15
to google-web-tool...@googlegroups.com
GWT strings and arrays are JS native strings and arrays (no prototype monkey patching), in Java code calls to Object, String, Array and Comparable methods are devirtualized if applied to instances typed at one of those types.

So the answer is yes: You wont be able to call instance methods this way for strings and arrays. At least, we should emit an error if you try to get a JSNI reference to a String or array method, and probably a warning if you make a jsni reference to a Object or Comparable method. Getting a jsni reference to Object or Comparable method works fine on every object as long as it is not a String or Array.


--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit-contributors/996e0a45-0048-4a61-abdd-3bff209df09d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roberto Lublinerman

unread,
Mar 30, 2015, 12:06:58 PM3/30/15
to google-web-tool...@googlegroups.com
Please file an issue to keep track.

Goktug Gokdogan

unread,
Mar 30, 2015, 2:59:20 PM3/30/15
to google-web-toolkit-contributors
Same also applies to JSOs. (i.e. You cannot call JSO methods with JSNI).

James Nelson

unread,
Mar 30, 2015, 11:27:00 PM3/30/15
to google-web-tool...@googlegroups.com
Ok, great.  Thanks for the heads up.  I was pretty sure that Strings had become native first-class objects.

Since JSOs will all have public methods, I will be safe with the same fix for String (not using JSNI if not necessary).
Given that java arrays only have one field, and that field does not work in JVM reflection, nor do any of the Object methods, I'll leave them alone
(that's what java.lang.reflect.Array is for)...


For posterity and search indexing, if anyone tries to use hashCode, equals or compareTo on String or Array in JSNI and are getting undefined reference errors, use a static method instead.

Colin Alworth

unread,
Mar 31, 2015, 10:45:02 AM3/31/15
to google-web-tool...@googlegroups.com
Getting offtopic, but there is absolutely no requirement that JSO methods are public. See dom.client.Element for several examples of private methods.

--
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit-co...@googlegroups.com.

James Nelson

unread,
Apr 2, 2015, 9:27:26 PM4/2/15
to google-web-tool...@googlegroups.com
I stand very much corrected. /hat tip/
I suppose I've already been using JsType so much it has begin to cloud my judgement.

Hm.  How ironic that the only methods unreachable to reflection-via-jsni would be private jsni methods? :-)

James Nelson

unread,
Apr 2, 2015, 9:29:00 PM4/2/15
to google-web-tool...@googlegroups.com
I suppose I technically have access to the AST nodes...  If I really wanted to I could probably find a way, but that would be exceptionally nasty, so, no thanks...
Reply all
Reply to author
Forward
0 new messages