Skip to content

Commit 3fb6552

Browse files
author
Nicolas Laurent
committed
[GR-28849] Implement two-arg version of {public,private,protected}_method_defined?
PullRequest: truffleruby/2344
2 parents 2378f64 + a882af1 commit 3fb6552

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Compatibility:
4040
* Coerce the message to a `String` for `BasicSocket#send` (#2209, @HoneyryderChuck).
4141
* Support buffer argument for `UDPSocket#recvfrom_nonblock` (#2209, @HoneyryderChuck).
4242
* Fixed `Integer#digits` implementation to handle more bases (#2224, #2225).
43+
* Support the `inherit` parameter for `Module#{private, protected, public}_method_defined?`.
4344

4445
Performance:
4546

spec/tags/core/module/private_method_defined_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/tags/core/module/protected_method_defined_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,7 @@ public PrivateInstanceMethodsNode() {
16971697

16981698
@NodeChild(value = "module", type = RubyNode.class)
16991699
@NodeChild(value = "name", type = RubyNode.class)
1700+
@NodeChild(value = "inherit", type = RubyNode.class)
17001701
protected abstract static class AbstractMethodDefinedNode extends CoreMethodNode {
17011702

17021703
final Visibility visibility;
@@ -1710,39 +1711,47 @@ protected RubyNode coerceToString(RubyNode name) {
17101711
return NameToJavaStringNode.create(name);
17111712
}
17121713

1713-
@Specialization
1714-
protected boolean isMethodDefined(RubyModule module, String name) {
1715-
// TODO (pitr-ch 30-Mar-2016): cache lookup
1716-
return ModuleOperations.lookupMethod(module, name, visibility) != null;
1714+
@CreateCast("inherit")
1715+
protected RubyNode coerceToBoolean(RubyNode inherit) {
1716+
return BooleanCastWithDefaultNodeGen.create(true, inherit);
1717+
}
1718+
1719+
// TODO (pitr-ch 30-Mar-2016): cache lookup
1720+
1721+
@Specialization(guards = "inherit")
1722+
protected boolean isMethodDefinedInherit(RubyModule module, String name, boolean inherit) {
1723+
final InternalMethod method = ModuleOperations.lookupMethodUncached(module, name, null);
1724+
return method != null && !method.isUndefined() && !method.isUnimplemented() &&
1725+
method.getVisibility() == visibility;
17171726
}
17181727

1728+
@Specialization(guards = "!inherit")
1729+
protected boolean isMethodDefinedDontInherit(RubyModule module, String name, boolean inherit) {
1730+
final InternalMethod method = module.fields.getMethod(name);
1731+
return method != null && !method.isUndefined() && !method.isUnimplemented() &&
1732+
method.getVisibility() == visibility;
1733+
}
17191734
}
17201735

1721-
@CoreMethod(names = "public_method_defined?", required = 1)
1736+
@CoreMethod(names = "public_method_defined?", required = 1, optional = 1)
17221737
public abstract static class PublicMethodDefinedNode extends AbstractMethodDefinedNode {
1723-
17241738
public PublicMethodDefinedNode() {
17251739
super(Visibility.PUBLIC);
17261740
}
1727-
17281741
}
17291742

1730-
@CoreMethod(names = "protected_method_defined?", required = 1)
1743+
@CoreMethod(names = "protected_method_defined?", required = 1, optional = 1)
17311744
public abstract static class ProtectedMethodDefinedNode extends AbstractMethodDefinedNode {
1732-
17331745
public ProtectedMethodDefinedNode() {
17341746
super(Visibility.PROTECTED);
17351747
}
1736-
17371748
}
17381749

1739-
@CoreMethod(names = "private_method_defined?", required = 1)
1750+
@CoreMethod(names = "private_method_defined?", required = 1, optional = 1)
17401751
public abstract static class PrivateMethodDefinedNode extends AbstractMethodDefinedNode {
1741-
17421752
public PrivateMethodDefinedNode() {
17431753
super(Visibility.PRIVATE);
17441754
}
1745-
17461755
}
17471756

17481757
@CoreMethod(names = "instance_methods", optional = 1)

src/main/java/org/truffleruby/core/module/ModuleOperations.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.truffleruby.core.string.StringUtils;
2525
import org.truffleruby.language.LexicalScope;
2626
import org.truffleruby.language.RubyConstant;
27-
import org.truffleruby.language.Visibility;
2827
import org.truffleruby.language.control.RaiseException;
2928
import org.truffleruby.language.methods.DeclarationContext;
3029
import org.truffleruby.language.methods.InternalMethod;
@@ -492,14 +491,6 @@ private static InternalMethod lookupMethodUncached(RubyModule module, RubyModule
492491
return null;
493492
}
494493

495-
public static InternalMethod lookupMethod(RubyModule module, String name, Visibility visibility) {
496-
final InternalMethod method = lookupMethodUncached(module, name, null);
497-
if (method == null || method.isUndefined()) {
498-
return null;
499-
}
500-
return method.getVisibility() == visibility ? method : null;
501-
}
502-
503494
public static MethodLookupResult lookupSuperMethod(InternalMethod currentMethod, RubyModule objectMetaClass) {
504495
final String name = currentMethod.getSharedMethodInfo().getMethodNameForNotBlock(); // use the original name
505496

0 commit comments

Comments
 (0)