Skip to content

Commit

Permalink
Cast arrays to JsArrayLike (#19)
Browse files Browse the repository at this point in the history
This brings the behavior closer to current GWT 2.10 version -- avoiding
cast to `elemental2.core.JsArray` means that `isArray` check is not
used in superdev mode.

Fixes gwtproject/gwt-event-dom#12
  • Loading branch information
zbynek authored Oct 20, 2023
1 parent cf6c05a commit 68b08e2
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gwt-core-gwt2-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<properties>
<maven.gwt.plugin>1.0.0</maven.gwt.plugin>

<gwt.version>2.9.0</gwt.version>
<gwt.version>2.10.0</gwt.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ public void testJsArrayString() {
assertEquals(0, jsArray.length());
}

/**
* Checks that get, length and set methods work even if the JS object
* is not actual Array
*/
public void testFakeArray() {
JsArray<JsPoint> points = makeFakeArray();
points.set(0, makeJsPoint(1, 2));
JsPoint pt = points.get(0);
assertEquals(1, pt.x());
assertEquals(7, points.length());
}

private native JsArray<JsPoint> makeFakeArray() /*-{
return {length: 7}
}-*/;

private native JsArray<JsPoint> makeJsArray() /*-{
return [
{ x: 0, y: 1, toString: function() { return 'JsPoint';} },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import jsinterop.base.JsArrayLike;

/**
* A simple wrapper around a homogeneous native array of {@link
Expand Down Expand Up @@ -50,7 +51,7 @@ protected JsArray() {}
*/
@JsOverlay
public final T get(int index) {
return this.<elemental2.core.JsArray<T>>cast().getAt(index);
return this.<JsArrayLike<T>>cast().getAt(index);
}

/**
Expand Down Expand Up @@ -85,7 +86,7 @@ public final String join(String separator) {
*/
@JsOverlay
public final int length() {
return this.<elemental2.core.JsArray<T>>cast().length;
return this.<JsArrayLike<T>>cast().getLength();
}

/**
Expand All @@ -106,7 +107,7 @@ public final int length() {
*/
@JsOverlay
public final void set(int index, T value) {
this.<elemental2.core.JsArray<T>>cast().setAt(index, value);
this.<JsArrayLike<T>>cast().setAt(index, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import jsinterop.base.JsArrayLike;

/**
* A simple wrapper around a homogeneous native array of boolean values.
Expand Down Expand Up @@ -46,7 +47,7 @@ protected JsArrayBoolean() {}
*/
@JsOverlay
public final boolean get(int index) {
return this.<elemental2.core.JsArray<Boolean>>cast().getAt(index);
return this.<JsArrayLike<Boolean>>cast().getAt(index);
}

/**
Expand Down Expand Up @@ -81,7 +82,7 @@ public final String join(String separator) {
*/
@JsOverlay
public final int length() {
return this.<elemental2.core.JsArray<Boolean>>cast().length;
return this.<JsArrayLike<Boolean>>cast().getLength();
}

/**
Expand All @@ -102,7 +103,7 @@ public final int length() {
*/
@JsOverlay
public final void set(int index, boolean value) {
this.<elemental2.core.JsArray<Boolean>>cast().setAt(index, value);
this.<JsArrayLike<Boolean>>cast().setAt(index, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import jsinterop.base.JsArrayLike;

/**
* A simple wrapper around a homogeneous native array of integer values.
Expand Down Expand Up @@ -49,7 +50,7 @@ protected JsArrayInteger() {}
*/
@JsOverlay
public final int get(int index) {
return (int) (double) this.<JsArray<Double>>cast().getAt(index);
return (int) (double) this.<JsArrayLike<Double>>cast().getAt(index);
}

/**
Expand Down Expand Up @@ -84,7 +85,7 @@ public final String join(String separator) {
*/
@JsOverlay
public final int length() {
return this.<JsArray<Double>>cast().length;
return this.<JsArrayLike<Double>>cast().getLength();
}

/**
Expand All @@ -105,7 +106,7 @@ public final int length() {
*/
@JsOverlay
public final void set(int index, int value) {
this.<JsArray<Double>>cast().setAt(index, (double) value);
this.<JsArrayLike<Double>>cast().setAt(index, (double) value);
}

/**
Expand Down
21 changes: 11 additions & 10 deletions gwt-core/src/main/java/org/gwtproject/core/client/JsArrayMixed.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import jsinterop.base.Js;
import jsinterop.base.JsArrayLike;

/**
* A simple wrapper around an heterogeneous native array of values.
Expand Down Expand Up @@ -49,7 +50,7 @@ protected JsArrayMixed() {}
*/
@JsOverlay
public final boolean getBoolean(int index) {
return Js.isTruthy(this.<JsArray<Object>>cast().getAtAsAny(index));
return Js.isTruthy(this.<JsArrayLike<Object>>cast().getAtAsAny(index));
}

/**
Expand All @@ -60,7 +61,7 @@ public final boolean getBoolean(int index) {
*/
@JsOverlay
public final double getNumber(int index) {
return Js.coerceToDouble(this.<JsArray<Object>>cast().getAtAsAny(index));
return Js.coerceToDouble(this.<JsArrayLike<Object>>cast().getAtAsAny(index));
}

/**
Expand All @@ -72,8 +73,8 @@ public final double getNumber(int index) {
*/
@JsOverlay
public final <T extends JavaScriptObject> T getObject(int index) {
return this.<JsArray<Object>>cast().getAt(index) != null
? this.<JsArray<Object>>cast().getAtAsAny(index).<T>cast()
return this.<JsArrayLike<Object>>cast().getAt(index) != null
? this.<JsArrayLike<Object>>cast().getAtAsAny(index).<T>cast()
: null;
}

Expand All @@ -85,7 +86,7 @@ public final <T extends JavaScriptObject> T getObject(int index) {
*/
@JsOverlay
public final String getString(int index) {
Object value = this.<JsArray<Object>>cast().getAt(index);
Object value = this.<JsArrayLike<Object>>cast().getAt(index);
return value == null ? null : value.toString();
}

Expand Down Expand Up @@ -121,7 +122,7 @@ public final String join(String separator) {
*/
@JsOverlay
public final int length() {
return this.<JsArray<Object>>cast().length;
return this.<JsArrayLike<Object>>cast().getLength();
}

/**
Expand Down Expand Up @@ -163,7 +164,7 @@ public final int length() {
*/
@JsOverlay
public final void set(int index, boolean value) {
this.<JsArray<Object>>cast().setAt(index, value);
this.<JsArrayLike<Object>>cast().setAt(index, value);
}

/**
Expand All @@ -177,7 +178,7 @@ public final void set(int index, boolean value) {
*/
@JsOverlay
public final void set(int index, double value) {
this.<JsArray<Object>>cast().setAt(index, value);
this.<JsArrayLike<Object>>cast().setAt(index, value);
}

/**
Expand All @@ -191,7 +192,7 @@ public final void set(int index, double value) {
*/
@JsOverlay
public final void set(int index, JavaScriptObject value) {
this.<JsArray<Object>>cast().setAt(index, value);
this.<JsArrayLike<Object>>cast().setAt(index, value);
}

/**
Expand All @@ -205,7 +206,7 @@ public final void set(int index, JavaScriptObject value) {
*/
@JsOverlay
public final void set(int index, String value) {
this.<JsArray<Object>>cast().setAt(index, value);
this.<JsArrayLike<Object>>cast().setAt(index, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import jsinterop.base.Js;
import jsinterop.base.JsArrayLike;

/**
* A simple wrapper around a homogeneous native array of numeric values.
Expand Down Expand Up @@ -51,7 +52,7 @@ protected JsArrayNumber() {}
*/
@JsOverlay
public final double get(int index) {
return this.<JsArray<Number>>cast().getAt(index).doubleValue();
return this.<JsArrayLike<Number>>cast().getAt(index).doubleValue();
}

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ public final String join(String separator) {
*/
@JsOverlay
public final int length() {
return this.<JsArray<Number>>cast().length;
return this.<JsArrayLike<Number>>cast().getLength();
}

/**
Expand All @@ -107,7 +108,7 @@ public final int length() {
*/
@JsOverlay
public final void set(int index, double value) {
this.<JsArray<Number>>cast().setAt(index, Js.uncheckedCast(value));
this.<JsArrayLike<Number>>cast().setAt(index, Js.uncheckedCast(value));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import jsinterop.base.Js;
import jsinterop.base.JsArrayLike;

/**
* A simple wrapper around a homogeneous native array of string values.
Expand All @@ -45,7 +46,7 @@ protected JsArrayString() {}
*/
@JsOverlay
public final String get(int index) {
String value = this.<JsArray<String>>cast().getAt(index);
String value = this.<JsArrayLike<String>>cast().getAt(index);
return value == null ? null : value;
}

Expand Down Expand Up @@ -81,7 +82,7 @@ public final String join(String separator) {
*/
@JsOverlay
public final int length() {
return this.<JsArray<String>>cast().length;
return this.<JsArrayLike<String>>cast().getLength();
}

/**
Expand All @@ -102,7 +103,7 @@ public final int length() {
*/
@JsOverlay
public final void set(int index, String value) {
this.<JsArray<String>>cast().setAt(index, Js.uncheckedCast(value));
this.<JsArrayLike<String>>cast().setAt(index, Js.uncheckedCast(value));
}

/**
Expand Down

0 comments on commit 68b08e2

Please sign in to comment.