Skip to content

Commit 68b08e2

Browse files
authored
Cast arrays to JsArrayLike (#19)
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
1 parent cf6c05a commit 68b08e2

File tree

8 files changed

+48
-26
lines changed

8 files changed

+48
-26
lines changed

gwt-core-gwt2-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<properties>
4848
<maven.gwt.plugin>1.0.0</maven.gwt.plugin>
4949

50-
<gwt.version>2.9.0</gwt.version>
50+
<gwt.version>2.10.0</gwt.version>
5151
</properties>
5252

5353
<dependencies>

gwt-core-gwt2-tests/src/test/java/org/gwtproject/core/client/JsArrayTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ public void testJsArrayString() {
212212
assertEquals(0, jsArray.length());
213213
}
214214

215+
/**
216+
* Checks that get, length and set methods work even if the JS object
217+
* is not actual Array
218+
*/
219+
public void testFakeArray() {
220+
JsArray<JsPoint> points = makeFakeArray();
221+
points.set(0, makeJsPoint(1, 2));
222+
JsPoint pt = points.get(0);
223+
assertEquals(1, pt.x());
224+
assertEquals(7, points.length());
225+
}
226+
227+
private native JsArray<JsPoint> makeFakeArray() /*-{
228+
return {length: 7}
229+
}-*/;
230+
215231
private native JsArray<JsPoint> makeJsArray() /*-{
216232
return [
217233
{ x: 0, y: 1, toString: function() { return 'JsPoint';} },

gwt-core/src/main/java/org/gwtproject/core/client/JsArray.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import jsinterop.annotations.JsOverlay;
1919
import jsinterop.annotations.JsPackage;
2020
import jsinterop.annotations.JsType;
21+
import jsinterop.base.JsArrayLike;
2122

2223
/**
2324
* A simple wrapper around a homogeneous native array of {@link
@@ -50,7 +51,7 @@ protected JsArray() {}
5051
*/
5152
@JsOverlay
5253
public final T get(int index) {
53-
return this.<elemental2.core.JsArray<T>>cast().getAt(index);
54+
return this.<JsArrayLike<T>>cast().getAt(index);
5455
}
5556

5657
/**
@@ -85,7 +86,7 @@ public final String join(String separator) {
8586
*/
8687
@JsOverlay
8788
public final int length() {
88-
return this.<elemental2.core.JsArray<T>>cast().length;
89+
return this.<JsArrayLike<T>>cast().getLength();
8990
}
9091

9192
/**
@@ -106,7 +107,7 @@ public final int length() {
106107
*/
107108
@JsOverlay
108109
public final void set(int index, T value) {
109-
this.<elemental2.core.JsArray<T>>cast().setAt(index, value);
110+
this.<JsArrayLike<T>>cast().setAt(index, value);
110111
}
111112

112113
/**

gwt-core/src/main/java/org/gwtproject/core/client/JsArrayBoolean.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import jsinterop.annotations.JsOverlay;
1919
import jsinterop.annotations.JsPackage;
2020
import jsinterop.annotations.JsType;
21+
import jsinterop.base.JsArrayLike;
2122

2223
/**
2324
* A simple wrapper around a homogeneous native array of boolean values.
@@ -46,7 +47,7 @@ protected JsArrayBoolean() {}
4647
*/
4748
@JsOverlay
4849
public final boolean get(int index) {
49-
return this.<elemental2.core.JsArray<Boolean>>cast().getAt(index);
50+
return this.<JsArrayLike<Boolean>>cast().getAt(index);
5051
}
5152

5253
/**
@@ -81,7 +82,7 @@ public final String join(String separator) {
8182
*/
8283
@JsOverlay
8384
public final int length() {
84-
return this.<elemental2.core.JsArray<Boolean>>cast().length;
85+
return this.<JsArrayLike<Boolean>>cast().getLength();
8586
}
8687

8788
/**
@@ -102,7 +103,7 @@ public final int length() {
102103
*/
103104
@JsOverlay
104105
public final void set(int index, boolean value) {
105-
this.<elemental2.core.JsArray<Boolean>>cast().setAt(index, value);
106+
this.<JsArrayLike<Boolean>>cast().setAt(index, value);
106107
}
107108

108109
/**

gwt-core/src/main/java/org/gwtproject/core/client/JsArrayInteger.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import jsinterop.annotations.JsOverlay;
2020
import jsinterop.annotations.JsPackage;
2121
import jsinterop.annotations.JsType;
22+
import jsinterop.base.JsArrayLike;
2223

2324
/**
2425
* A simple wrapper around a homogeneous native array of integer values.
@@ -49,7 +50,7 @@ protected JsArrayInteger() {}
4950
*/
5051
@JsOverlay
5152
public final int get(int index) {
52-
return (int) (double) this.<JsArray<Double>>cast().getAt(index);
53+
return (int) (double) this.<JsArrayLike<Double>>cast().getAt(index);
5354
}
5455

5556
/**
@@ -84,7 +85,7 @@ public final String join(String separator) {
8485
*/
8586
@JsOverlay
8687
public final int length() {
87-
return this.<JsArray<Double>>cast().length;
88+
return this.<JsArrayLike<Double>>cast().getLength();
8889
}
8990

9091
/**
@@ -105,7 +106,7 @@ public final int length() {
105106
*/
106107
@JsOverlay
107108
public final void set(int index, int value) {
108-
this.<JsArray<Double>>cast().setAt(index, (double) value);
109+
this.<JsArrayLike<Double>>cast().setAt(index, (double) value);
109110
}
110111

111112
/**

gwt-core/src/main/java/org/gwtproject/core/client/JsArrayMixed.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import jsinterop.annotations.JsPackage;
2121
import jsinterop.annotations.JsType;
2222
import jsinterop.base.Js;
23+
import jsinterop.base.JsArrayLike;
2324

2425
/**
2526
* A simple wrapper around an heterogeneous native array of values.
@@ -49,7 +50,7 @@ protected JsArrayMixed() {}
4950
*/
5051
@JsOverlay
5152
public final boolean getBoolean(int index) {
52-
return Js.isTruthy(this.<JsArray<Object>>cast().getAtAsAny(index));
53+
return Js.isTruthy(this.<JsArrayLike<Object>>cast().getAtAsAny(index));
5354
}
5455

5556
/**
@@ -60,7 +61,7 @@ public final boolean getBoolean(int index) {
6061
*/
6162
@JsOverlay
6263
public final double getNumber(int index) {
63-
return Js.coerceToDouble(this.<JsArray<Object>>cast().getAtAsAny(index));
64+
return Js.coerceToDouble(this.<JsArrayLike<Object>>cast().getAtAsAny(index));
6465
}
6566

6667
/**
@@ -72,8 +73,8 @@ public final double getNumber(int index) {
7273
*/
7374
@JsOverlay
7475
public final <T extends JavaScriptObject> T getObject(int index) {
75-
return this.<JsArray<Object>>cast().getAt(index) != null
76-
? this.<JsArray<Object>>cast().getAtAsAny(index).<T>cast()
76+
return this.<JsArrayLike<Object>>cast().getAt(index) != null
77+
? this.<JsArrayLike<Object>>cast().getAtAsAny(index).<T>cast()
7778
: null;
7879
}
7980

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

@@ -121,7 +122,7 @@ public final String join(String separator) {
121122
*/
122123
@JsOverlay
123124
public final int length() {
124-
return this.<JsArray<Object>>cast().length;
125+
return this.<JsArrayLike<Object>>cast().getLength();
125126
}
126127

127128
/**
@@ -163,7 +164,7 @@ public final int length() {
163164
*/
164165
@JsOverlay
165166
public final void set(int index, boolean value) {
166-
this.<JsArray<Object>>cast().setAt(index, value);
167+
this.<JsArrayLike<Object>>cast().setAt(index, value);
167168
}
168169

169170
/**
@@ -177,7 +178,7 @@ public final void set(int index, boolean value) {
177178
*/
178179
@JsOverlay
179180
public final void set(int index, double value) {
180-
this.<JsArray<Object>>cast().setAt(index, value);
181+
this.<JsArrayLike<Object>>cast().setAt(index, value);
181182
}
182183

183184
/**
@@ -191,7 +192,7 @@ public final void set(int index, double value) {
191192
*/
192193
@JsOverlay
193194
public final void set(int index, JavaScriptObject value) {
194-
this.<JsArray<Object>>cast().setAt(index, value);
195+
this.<JsArrayLike<Object>>cast().setAt(index, value);
195196
}
196197

197198
/**
@@ -205,7 +206,7 @@ public final void set(int index, JavaScriptObject value) {
205206
*/
206207
@JsOverlay
207208
public final void set(int index, String value) {
208-
this.<JsArray<Object>>cast().setAt(index, value);
209+
this.<JsArrayLike<Object>>cast().setAt(index, value);
209210
}
210211

211212
/**

gwt-core/src/main/java/org/gwtproject/core/client/JsArrayNumber.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import jsinterop.annotations.JsPackage;
2121
import jsinterop.annotations.JsType;
2222
import jsinterop.base.Js;
23+
import jsinterop.base.JsArrayLike;
2324

2425
/**
2526
* A simple wrapper around a homogeneous native array of numeric values.
@@ -51,7 +52,7 @@ protected JsArrayNumber() {}
5152
*/
5253
@JsOverlay
5354
public final double get(int index) {
54-
return this.<JsArray<Number>>cast().getAt(index).doubleValue();
55+
return this.<JsArrayLike<Number>>cast().getAt(index).doubleValue();
5556
}
5657

5758
/**
@@ -86,7 +87,7 @@ public final String join(String separator) {
8687
*/
8788
@JsOverlay
8889
public final int length() {
89-
return this.<JsArray<Number>>cast().length;
90+
return this.<JsArrayLike<Number>>cast().getLength();
9091
}
9192

9293
/**
@@ -107,7 +108,7 @@ public final int length() {
107108
*/
108109
@JsOverlay
109110
public final void set(int index, double value) {
110-
this.<JsArray<Number>>cast().setAt(index, Js.uncheckedCast(value));
111+
this.<JsArrayLike<Number>>cast().setAt(index, Js.uncheckedCast(value));
111112
}
112113

113114
/**

gwt-core/src/main/java/org/gwtproject/core/client/JsArrayString.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import jsinterop.annotations.JsPackage;
2121
import jsinterop.annotations.JsType;
2222
import jsinterop.base.Js;
23+
import jsinterop.base.JsArrayLike;
2324

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

@@ -81,7 +82,7 @@ public final String join(String separator) {
8182
*/
8283
@JsOverlay
8384
public final int length() {
84-
return this.<JsArray<String>>cast().length;
85+
return this.<JsArrayLike<String>>cast().getLength();
8586
}
8687

8788
/**
@@ -102,7 +103,7 @@ public final int length() {
102103
*/
103104
@JsOverlay
104105
public final void set(int index, String value) {
105-
this.<JsArray<String>>cast().setAt(index, Js.uncheckedCast(value));
106+
this.<JsArrayLike<String>>cast().setAt(index, Js.uncheckedCast(value));
106107
}
107108

108109
/**

0 commit comments

Comments
 (0)