Skip to content

Commit

Permalink
Adds support for native methods in non-native types.
Browse files Browse the repository at this point in the history
Change-Id: I4c78d30a169dcdace16b247d69e9e5acf7db7cfa
  • Loading branch information
gkdn authored and Gerrit Code Review committed Oct 23, 2015
1 parent fb00357 commit a631e79
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 7 deletions.
5 changes: 5 additions & 0 deletions dev/core/src/com/google/gwt/dev/jjs/ast/JConstructor.java
Expand Up @@ -93,6 +93,11 @@ public boolean isConstructor() {
return true; return true;
} }


@Override
public boolean isJsNative() {
return getEnclosingType().isJsNative();
}

/** /**
* Returns <code>true</code> if this constructor does no real work. * Returns <code>true</code> if this constructor does no real work.
* *
Expand Down
1 change: 1 addition & 0 deletions dev/core/src/com/google/gwt/dev/jjs/ast/JField.java
Expand Up @@ -160,6 +160,7 @@ public boolean isJsProperty() {
return jsName != null; return jsName != null;
} }


@Override
public boolean isJsNative() { public boolean isJsNative() {
return enclosingType.isJsNative(); return enclosingType.isJsNative();
} }
Expand Down
5 changes: 3 additions & 2 deletions dev/core/src/com/google/gwt/dev/jjs/ast/JMethod.java
Expand Up @@ -102,7 +102,7 @@ public void setJsMemberInfo(String namespace, String name, boolean exported) {
} }


public boolean isJsInteropEntryPoint() { public boolean isJsInteropEntryPoint() {
return exported && !needsDynamicDispatch(); return exported && !needsDynamicDispatch() && !isJsNative();
} }


public boolean canBeCalledExternally() { public boolean canBeCalledExternally() {
Expand Down Expand Up @@ -242,8 +242,9 @@ public boolean isOrOverridesJsFunctionMethod() {
return false; return false;
} }


@Override
public boolean isJsNative() { public boolean isJsNative() {
return enclosingType != null && enclosingType.isJsNative(); return body == null && jsName != null;
} }


public void setSyntheticAccidentalOverride() { public void setSyntheticAccidentalOverride() {
Expand Down
Expand Up @@ -2541,6 +2541,7 @@ private List<JsStatement> getGlobalStatements() {
*/ */
private static boolean doesNotHaveConcreteImplementation(JMethod method) { private static boolean doesNotHaveConcreteImplementation(JMethod method) {
return method.isAbstract() return method.isAbstract()
|| method.isJsNative()
|| JjsUtils.isUnnecessarySyntheticAccidentalOverride(method) || JjsUtils.isUnnecessarySyntheticAccidentalOverride(method)
|| (JProgram.isClinit(method) || (JProgram.isClinit(method)
&& method.getEnclosingType().getClinitTarget() != method.getEnclosingType()); && method.getEnclosingType().getClinitTarget() != method.getEnclosingType());
Expand Down
Expand Up @@ -3092,6 +3092,7 @@ private void processNativeMethod(MethodDeclaration x) {
JMethod method = curMethod.method; JMethod method = curMethod.method;
JsniMethod jsniMethod = jsniMethods.get(x); JsniMethod jsniMethod = jsniMethods.get(x);
if (jsniMethod == null) { if (jsniMethod == null) {
method.setBody(null);
return; return;
} }
SourceInfo info = method.getSourceInfo(); SourceInfo info = method.getSourceInfo();
Expand Down
Expand Up @@ -218,8 +218,8 @@ private boolean isDelegatingToConstructor(JConstructor ctor, JConstructor target
} }


private void checkField(JField x) { private void checkField(JField x) {
if (x.isJsNative()) { if (x.getEnclosingType().isJsNative()) {
checkNativeJsMember(x); checkMemberOfNativeJsType(x);
} }


checkUnusableByJs(x); checkUnusableByJs(x);
Expand All @@ -241,8 +241,8 @@ private void checkMethod(JMethod x) {
} }
currentProcessedMethods.addAll(x.getOverriddenMethods()); currentProcessedMethods.addAll(x.getOverriddenMethods());


if (x.isJsNative()) { if (x.getEnclosingType().isJsNative()) {
checkNativeJsMember(x); checkMemberOfNativeJsType(x);
} }


checkUnusableByJs(x); checkUnusableByJs(x);
Expand Down Expand Up @@ -281,7 +281,7 @@ private void checkJsPropertyType(String propertyName, String enclosingTypeName,
} }
} }


private void checkNativeJsMember(JMember member) { private void checkMemberOfNativeJsType(JMember member) {
if (member.isSynthetic()) { if (member.isSynthetic()) {
return; return;
} }
Expand Down
2 changes: 2 additions & 0 deletions user/test/com/google/gwt/core/CoreJsInteropSuite.java
Expand Up @@ -17,6 +17,7 @@


import com.google.gwt.core.client.interop.JsExportTest; import com.google.gwt.core.client.interop.JsExportTest;
import com.google.gwt.core.client.interop.JsFunctionTest; import com.google.gwt.core.client.interop.JsFunctionTest;
import com.google.gwt.core.client.interop.JsMethodTest;
import com.google.gwt.core.client.interop.JsPropertyTest; import com.google.gwt.core.client.interop.JsPropertyTest;
import com.google.gwt.core.client.interop.JsTypeArrayTest; import com.google.gwt.core.client.interop.JsTypeArrayTest;
import com.google.gwt.core.client.interop.JsTypeTest; import com.google.gwt.core.client.interop.JsTypeTest;
Expand All @@ -33,6 +34,7 @@ public static Test suite() {


suite.addTestSuite(JsTypeTest.class); suite.addTestSuite(JsTypeTest.class);
suite.addTestSuite(JsPropertyTest.class); suite.addTestSuite(JsPropertyTest.class);
suite.addTestSuite(JsMethodTest.class);
suite.addTestSuite(JsTypeArrayTest.class); suite.addTestSuite(JsTypeArrayTest.class);
suite.addTestSuite(JsExportTest.class); suite.addTestSuite(JsExportTest.class);
suite.addTestSuite(JsFunctionTest.class); suite.addTestSuite(JsFunctionTest.class);
Expand Down
60 changes: 60 additions & 0 deletions user/test/com/google/gwt/core/client/interop/JsMethodTest.java
@@ -0,0 +1,60 @@
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.core.client.interop;

import static jsinterop.annotations.JsPackage.GLOBAL;

import com.google.gwt.junit.client.GWTTestCase;

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsProperty;

/**
* Tests JsMethod functionality.
*/
public class JsMethodTest extends GWTTestCase {

@Override
public String getModuleName() {
return "com.google.gwt.core.Core";
}

class MyObject {
@JsProperty
public int mine;

@JsMethod
public native boolean hasOwnProperty(String name);
}

public void testNativeJsMethod() {
MyObject obj = new MyObject();
obj.mine = 0;
assertTrue(obj.hasOwnProperty("mine"));
assertFalse(obj.hasOwnProperty("toString"));
}

@JsMethod(namespace = GLOBAL)
private static native boolean isFinite(double d);

public void testStaticNativeJsMethod() {
assertFalse(isFinite(Double.POSITIVE_INFINITY));
assertFalse(isFinite(Double.NEGATIVE_INFINITY));
assertFalse(isFinite(Double.NaN));
assertTrue(isFinite(0));
assertTrue(isFinite(1));
}
}

0 comments on commit a631e79

Please sign in to comment.