Skip to content

Commit

Permalink
Adds static field&method support for native JsTypes.
Browse files Browse the repository at this point in the history
Change-Id: I0a3ec05509b58e4237654be99cb5bda81a525e66
  • Loading branch information
gkdn committed Sep 22, 2015
1 parent 9c20ce7 commit 3237ac2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
2 changes: 2 additions & 0 deletions dev/core/src/com/google/gwt/dev/jjs/ast/HasJsInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public interface HasJsInfo {
String getJsNamespace();

String getQualifiedJsName();

boolean isJsNative();
}
4 changes: 4 additions & 0 deletions dev/core/src/com/google/gwt/dev/jjs/ast/JField.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ public boolean isJsProperty() {
return jsName != null;
}

public boolean isJsNative() {
return enclosingType.isJsNative();
}

@Override
public String getJsName() {
return jsName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ private JsExpression dispatchToStaticField(JFieldRef x, JsExpression unnecessary
* in endVisit(JBinaryOperation) rectifies the situation.
*/

JsExpression result = names.get(x.getField()).makeRef(x.getSourceInfo());
JsExpression result = createStaticReference(x.getField(), x.getSourceInfo());

// Add clinit (if needed).
result = createCommaExpression(maybeCreateClinitCall(x.getField(), false), result);
Expand Down Expand Up @@ -1317,10 +1317,7 @@ public void endVisit(JMethodCall x, Context ctx) {

private JsExpression dispatchToStatic(JsExpression unnecessaryQualifier, JMethod method,
List<JsExpression> args, SourceInfo sourceInfo) {
JsNameRef methodName =
method.isJsNative()
? createJsQualifier(method.getQualifiedJsName(), sourceInfo)
: names.get(method).makeRef(sourceInfo);
JsNameRef methodName = createStaticReference(method, sourceInfo);
JsExpression result = new JsInvocation(sourceInfo, methodName, args);

return createCommaExpression(unnecessaryQualifier, result);
Expand Down Expand Up @@ -2163,6 +2160,13 @@ private JsExpression createCommaExpression(JsExpression lhs, JsExpression rhs) {
return new JsBinaryOperation(lhs.getSourceInfo(), JsBinaryOperator.COMMA, lhs, rhs);
}

private JsNameRef createStaticReference(JMember member, SourceInfo sourceInfo) {
assert member.isStatic();
return member.isJsNative()
? createJsQualifier(member.getQualifiedJsName(), sourceInfo)
: names.get(member).makeRef(sourceInfo);
}

private JsExpression generateCastableTypeMap(JClassType x) {
JCastMap castMap = program.getCastMap(x);
if (castMap != null) {
Expand Down
22 changes: 17 additions & 5 deletions user/test/com/google/gwt/core/client/interop/JsTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public String getModuleName() {
@Override
protected void gwtSetUp() throws Exception {
ScriptInjector.fromString("function MyJsInterface() {}\n"
+ "MyJsInterface.staticX = 33;"
+ "MyJsInterface.answerToLife = function() { return 42;};"
+ "MyJsInterface.prototype.sum = function sum(bias) { return this.x + bias; };")
.setWindow(TOP_WINDOW).inject();
patchPrototype(MyClassExtendsJsPrototype.class);
Expand Down Expand Up @@ -138,20 +140,30 @@ public void testRevealedOverrideJsType() {
}

public void testConcreteNativeType() {
assertEquals(33, MyJsClassWithPrototype.staticX);
MyJsClassWithPrototype.staticX = 34;
assertEquals(34, MyJsClassWithPrototype.staticX);
assertEquals(42, MyJsClassWithPrototype.answerToLife());

MyJsClassWithPrototype obj = new MyJsClassWithPrototype();
assertTrue(isUndefined(obj.getX()));
obj.setX(72);
assertEquals(72, obj.getX());
assertTrue(isUndefined(obj.x));
obj.x = 72;
assertEquals(72, obj.x);
assertEquals(74, obj.sum(2));

assertTrue(isUndefined(obj.getY()));
obj.setY(91);
assertEquals(91, obj.getY());
}

public void testConcreteNativeType_sublasss() {
MyClassExtendsJsPrototype mc = new MyClassExtendsJsPrototype();
assertEquals(143, mc.sum(1));

// Also test with setting properties
mc.setX(-mc.getX());
mc.x = -mc.x;
assertEquals(58, mc.sum(0));

assertEquals(52, mc.getY());
}

public void testJsPropertyIsX() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
class MyClassExtendsJsPrototype extends MyJsClassWithPrototype {

MyClassExtendsJsPrototype() {
setX(42);
this.x = 42;
setY(52);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@
*/
@JsType(prototype = "MyJsInterface")
public class MyJsClassWithPrototype {

public static int staticX;

public static native int answerToLife();

public int x;

@JsProperty
public native int getX();
public native int getY();

@JsProperty
public native void setX(int x);
public native void setY(int x);

public native int sum(int bias);
}

0 comments on commit 3237ac2

Please sign in to comment.