Introducing "native" JsTypes

459 views
Skip to first unread message

Goktug Gokdogan

unread,
Sep 9, 2015, 10:05:46 PM9/9/15
to google-web-toolkit-contributors
If you have followed some jsinterop discussions earlier [1], we decided to drop Prototype class generation in favor of concrete classes with native methods:

 @JsType(prototype="Object")
 public class JsObject {
   public static native String[] keys(JsObject obj);
   public native boolean hasOwnProperty(String prop);
 }

I'm implementing this and it works well but the prototype attribute is serving a very similar purpose to name and namespace attribute. The attribute is simple there because we cannot differentiate if the type represents a type that is already available in native code or if the type is introduced by java.

Similarly, we hit such ambiguity for interfaces. If you have a JsType interface, it is not clear that if such type is introduced by the app or if it is just a stub for existing type that is already available natively. These affects several stuff like how instanceof works (which is quite problematic) or if we should generate any code for the interface or not (which is more problematic in j2cl).

So the solution we came up with is to remove the prototype attribute and instead introduce isNative attribute. When the attribute is set to true, it basically tells the compiler to assume that the type already exists in native code (similar to the native keyword available for methods).
The prototype will be equivalent to the fully qualified javascript name (which is calculated based on JsType name/namespace, JsPackage etc). So above code becomes:

 @JsType(namepace=GLOBAL, name="Object", isNative=true)
 public class JsObject {
   public static native String[] keys(JsObject obj);
   public native boolean hasOwnProperty(String prop);
 }

or if the package-info.java already has @JsPackage(name=GLOBAL), it could be re-written as

 @JsType(isNative=true)
 public class Object {
   public static native String[] keys(Object obj);
   public native boolean hasOwnProperty(String prop);
 }

Based on this change, we also redefining how the instanceof works for native JsTypes.
If the native type is concrete, the instanceof will be generated in JavaScript as

  obj instanceof <fully_qualified_js_name>

If the native type is an interface, the instanceof operation will always return true as it simply represents a loose javascript contract. [2]

For all non-native JsTypes, regular java instanceof semantic will apply. That means, for example, if you do (jso instanceof SomeJsTypeInterface), it will return false (earlier it would have returned true).

That's all for now. As always, feel free to send any comments and let me know what you think.

Cheers,

Goktug.


PS: Note that due this change, when the new annotations introduced, you will need to add isNative=true for all interfaces that is abstracting some javascript API. If you were using it for just generating unobfuscated names for java APIs then you can keep it as it is.


[2] We may later provide a way for developers to customize this behavior, like providing their own method to be called for instanceof.

Cristian Rinaldi

unread,
Sep 12, 2015, 12:39:36 PM9/12/15
to GWT Contributors
The modification is in the master or not yet?

Cristian Rinaldi

unread,
Sep 12, 2015, 1:56:49 PM9/12/15
to GWT Contributors
This code:

 @JsType(isNative=true)
 public class Object {
   public static native String[] keys(Object obj);
   public native boolean hasOwnProperty(String prop);
 }

It is the code generated by the compiler? If yes, what is the source code, I ask, by the definition of static methods on interfaces such as keys.





El miércoles, 9 de septiembre de 2015, 23:05:46 (UTC-3), Goktug Gokdogan escribió:

Goktug Gokdogan

unread,
Sep 13, 2015, 1:42:03 PM9/13/15
to google-web-toolkit-contributors
No, the code is not in the master yet. I need to land https://gwt-review.googlesource.com/#/c/13520/ and https://gwt-review.googlesource.com/#/c/13523/ first.

The example code that I sent is written by hand, there will the no code generation in the compiler.

--
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/97446615-4e87-4d5c-83c0-86e1b1161dfc%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages