google / elemental2

Type checked access to browser APIs for Java code.
Apache License 2.0
150 stars 38 forks source link

Generate static method for generic methods. #107

Open jDramaix opened 5 years ago

jDramaix commented 5 years ago

Some javascript methods like Array.prototype.shift are generics. It means they can be called or applied to objects resembling to the type defining the method.

In Closure-compiler, the @this annotations in function's jsdoc is used for that purpose and for the time being we just skip @this annotation (we even remove them by patching externs file).

We should use this information and create static methods that allow the developer to call those methods with another type of object.

Let's take for example the Array.prototype.shift definition, this method can be called on any IArrayLike object. We should generate the following method on JsArray.java class:

@JsType
public class JsArray<T> {
  @JsOverlay
  public static <T> T shift(IArrayLike<T> thisObject) {
    return (T) shift_function.call(thisObject);
  }

  @JsProperty(name = "Array.prototype.shift", namespace = JsPackage.GLOBAL)
  private static Function shift_function;  

}

Note: the code snippet is just an idea how we could implement the improvement. We should discuss the final implementation (unchecked cast, auto-boxing, introducing a specific Type for shift_function...)