Skip to content

Commit 18a93b8

Browse files
committed
Don't use ReadCallerFrameNode in ModuleNodes.InstanceMethodNode class
1 parent 0b3843d commit 18a93b8

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

spec/ruby/core/module/instance_method_spec.rb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,36 @@
5151
@mod_um.inspect.should =~ /\bModuleSpecs::InstanceMethChild\b/
5252
end
5353

54-
it "raises a TypeError if not passed a symbol" do
55-
-> { Object.instance_method([]) }.should raise_error(TypeError)
56-
-> { Object.instance_method(0) }.should raise_error(TypeError)
54+
it "raises a TypeError if the given name is not a String/Symbol" do
55+
-> { Object.instance_method([]) }.should raise_error(TypeError, /is not a symbol nor a string/)
56+
-> { Object.instance_method(0) }.should raise_error(TypeError, /is not a symbol nor a string/)
57+
-> { Object.instance_method(nil) }.should raise_error(TypeError, /is not a symbol nor a string/)
58+
-> { Object.instance_method(mock('x')) }.should raise_error(TypeError, /is not a symbol nor a string/)
5759
end
5860

59-
it "raises a TypeError if the given name is not a string/symbol" do
60-
-> { Object.instance_method(nil) }.should raise_error(TypeError)
61-
-> { Object.instance_method(mock('x')) }.should raise_error(TypeError)
61+
it "accepts String name argument" do
62+
method = ModuleSpecs::InstanceMeth.instance_method(:foo)
63+
method.should be_kind_of(UnboundMethod)
64+
end
65+
66+
it "accepts Symbol name argument" do
67+
method = ModuleSpecs::InstanceMeth.instance_method("foo")
68+
method.should be_kind_of(UnboundMethod)
69+
end
70+
71+
it "converts non-String name by calling #to_str method" do
72+
obj = Object.new
73+
def obj.to_str() "foo" end
74+
75+
method = ModuleSpecs::InstanceMeth.instance_method(obj)
76+
method.should be_kind_of(UnboundMethod)
77+
end
78+
79+
it "raises TypeError when passed non-String name and #to_str returns non-String value" do
80+
obj = Object.new
81+
def obj.to_str() [] end
82+
83+
-> { ModuleSpecs::InstanceMeth.instance_method(obj) }.should raise_error(TypeError, /can't convert Object to String/)
6284
end
6385

6486
it "raises a NameError if the method has been undefined" do

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
import org.truffleruby.language.WarningNode.UncachedWarningNode;
9090
import org.truffleruby.language.arguments.ArgumentsDescriptor;
9191
import org.truffleruby.language.arguments.EmptyArgumentsDescriptor;
92-
import org.truffleruby.language.arguments.ReadCallerFrameNode;
9392
import org.truffleruby.language.arguments.RubyArguments;
9493
import org.truffleruby.language.backtrace.BacktraceFormatter;
9594
import org.truffleruby.language.constants.ConstantEntry;
@@ -1948,22 +1947,18 @@ protected RubyArray instanceMethods(RubyModule module, boolean includeAncestors)
19481947
}
19491948
}
19501949

1951-
@CoreMethod(names = "instance_method", required = 1)
1952-
@NodeChild(value = "module", type = RubyNode.class)
1953-
@NodeChild(value = "name", type = RubyBaseNodeWithExecute.class)
1954-
public abstract static class InstanceMethodNode extends CoreMethodNode {
1955-
@Child private ReadCallerFrameNode readCallerFrame = ReadCallerFrameNode.create();
1956-
1957-
@CreateCast("name")
1958-
protected RubyBaseNodeWithExecute coerceToString(RubyBaseNodeWithExecute name) {
1959-
return NameToJavaStringNode.create(name);
1960-
}
1950+
@GenerateUncached
1951+
@CoreMethod(names = "instance_method", required = 1, alwaysInlined = true)
1952+
public abstract static class InstanceMethodNode extends AlwaysInlinedMethodNode {
19611953

19621954
@Specialization
1963-
protected RubyUnboundMethod instanceMethod(VirtualFrame frame, RubyModule module, String name,
1955+
protected RubyUnboundMethod instanceMethod(
1956+
Frame callerFrame, RubyModule module, Object[] rubyArgs, RootCallTarget target,
1957+
@Cached NameToJavaStringNode nameToJavaStringNode,
19641958
@Cached BranchProfile errorProfile) {
1965-
final Frame callerFrame = readCallerFrame.execute(frame);
1959+
needCallerFrame(callerFrame, target);
19661960
final DeclarationContext declarationContext = RubyArguments.getDeclarationContext(callerFrame);
1961+
final String name = nameToJavaStringNode.execute(RubyArguments.getArgument(rubyArgs, 0));
19671962

19681963
// TODO(CS, 11-Jan-15) cache this lookup
19691964
final InternalMethod method = ModuleOperations.lookupMethodUncached(module, name, declarationContext);

0 commit comments

Comments
 (0)