google / jsinterop-generator

Generates Java annotated with JsInterop from JavaScript extern sources
Apache License 2.0
78 stars 24 forks source link

Union types unnecessarily Autobox primitive values #19

Closed realityforge closed 5 years ago

realityforge commented 5 years ago

It seems that the union types do not get optimally generated when passed as parameters to methods and one of the union types is a primitive value. Consider the type

/**
 * @param {string} type
 * @param {EventListener|function(!Event):*} listener
 * @param {(boolean|!AddEventListenerOptions)=} opt_options
 * @return {undefined}
 * @see https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener
 */
EventTarget.prototype.addEventListener = function(type, listener, opt_options) {

This will generate

  void addEventListener(
      String type, EventListener listener, EventTarget.AddEventListenerOptionsUnionType options);

  @JsOverlay
  default void addEventListener(String type, EventListener listener, boolean options) {
    addEventListener(
        type, listener, Js.<EventTarget.AddEventListenerOptionsUnionType>uncheckedCast(options));
  }
...

This means that code such as

DomGlobal.window.addEventListener( "hashchange", _listener, false )

will optimize in GWT2.x to something like a.addEventListener(b,c,($g(),d?true:false)) where g$ is the Boolean.<clinit>() call. which is of course less optimal than a.addEventListener(b,c,d).

A possible solution may be to add @DonNotAutobox to the union parameter although this solution has yet to be investigated.

realityforge commented 5 years ago

@niloc132 Seems to think it is more likely that the parameter in Js.uncheckedCast() should have the @DonNotAutobox added.

gkdn commented 5 years ago

Head version of Js.uncheckedCast already has @DonNotAutobox

realityforge commented 5 years ago

Thanks