Skip to content

Commit a882af1

Browse files
author
Nicolas Laurent
committed
implement two-arg version of {public,private,protected}_method_defined?
1 parent a5afb7a commit a882af1

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
@@ -39,6 +39,7 @@ Compatibility:
3939
* Switched to the io-console C extension from C ruby for better performance and compatibility in `irb`.
4040
* Coerce the message to a `String` for `BasicSocket#send` (#2209, @HoneyryderChuck).
4141
* Support buffer argument for `UDPSocket#recvfrom_nonblock` (#2209, @HoneyryderChuck).
42+
* Support the `inherit` parameter for `Module#{private, protected, public}_method_defined?`.
4243

4344
Performance:
4445

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
@@ -1687,6 +1687,7 @@ public PrivateInstanceMethodsNode() {
16871687

16881688
@NodeChild(value = "module", type = RubyNode.class)
16891689
@NodeChild(value = "name", type = RubyNode.class)
1690+
@NodeChild(value = "inherit", type = RubyNode.class)
16901691
protected abstract static class AbstractMethodDefinedNode extends CoreMethodNode {
16911692

16921693
final Visibility visibility;
@@ -1700,39 +1701,47 @@ protected RubyNode coerceToString(RubyNode name) {
17001701
return NameToJavaStringNode.create(name);
17011702
}
17021703

1703-
@Specialization
1704-
protected boolean isMethodDefined(RubyModule module, String name) {
1705-
// TODO (pitr-ch 30-Mar-2016): cache lookup
1706-
return ModuleOperations.lookupMethod(module, name, visibility) != null;
1704+
@CreateCast("inherit")
1705+
protected RubyNode coerceToBoolean(RubyNode inherit) {
1706+
return BooleanCastWithDefaultNodeGen.create(true, inherit);
1707+
}
1708+
1709+
// TODO (pitr-ch 30-Mar-2016): cache lookup
1710+
1711+
@Specialization(guards = "inherit")
1712+
protected boolean isMethodDefinedInherit(RubyModule module, String name, boolean inherit) {
1713+
final InternalMethod method = ModuleOperations.lookupMethodUncached(module, name, null);
1714+
return method != null && !method.isUndefined() && !method.isUnimplemented() &&
1715+
method.getVisibility() == visibility;
17071716
}
17081717

1718+
@Specialization(guards = "!inherit")
1719+
protected boolean isMethodDefinedDontInherit(RubyModule module, String name, boolean inherit) {
1720+
final InternalMethod method = module.fields.getMethod(name);
1721+
return method != null && !method.isUndefined() && !method.isUnimplemented() &&
1722+
method.getVisibility() == visibility;
1723+
}
17091724
}
17101725

1711-
@CoreMethod(names = "public_method_defined?", required = 1)
1726+
@CoreMethod(names = "public_method_defined?", required = 1, optional = 1)
17121727
public abstract static class PublicMethodDefinedNode extends AbstractMethodDefinedNode {
1713-
17141728
public PublicMethodDefinedNode() {
17151729
super(Visibility.PUBLIC);
17161730
}
1717-
17181731
}
17191732

1720-
@CoreMethod(names = "protected_method_defined?", required = 1)
1733+
@CoreMethod(names = "protected_method_defined?", required = 1, optional = 1)
17211734
public abstract static class ProtectedMethodDefinedNode extends AbstractMethodDefinedNode {
1722-
17231735
public ProtectedMethodDefinedNode() {
17241736
super(Visibility.PROTECTED);
17251737
}
1726-
17271738
}
17281739

1729-
@CoreMethod(names = "private_method_defined?", required = 1)
1740+
@CoreMethod(names = "private_method_defined?", required = 1, optional = 1)
17301741
public abstract static class PrivateMethodDefinedNode extends AbstractMethodDefinedNode {
1731-
17321742
public PrivateMethodDefinedNode() {
17331743
super(Visibility.PRIVATE);
17341744
}
1735-
17361745
}
17371746

17381747
@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;
@@ -483,14 +482,6 @@ private static InternalMethod lookupMethodUncached(RubyModule module, RubyModule
483482
return null;
484483
}
485484

486-
public static InternalMethod lookupMethod(RubyModule module, String name, Visibility visibility) {
487-
final InternalMethod method = lookupMethodUncached(module, name, null);
488-
if (method == null || method.isUndefined()) {
489-
return null;
490-
}
491-
return method.getVisibility() == visibility ? method : null;
492-
}
493-
494485
public static MethodLookupResult lookupSuperMethod(InternalMethod currentMethod, RubyModule objectMetaClass) {
495486
final String name = currentMethod.getSharedMethodInfo().getMethodNameForNotBlock(); // use the original name
496487

0 commit comments

Comments
 (0)