Skip to content

Commit d1ce9c0

Browse files
committed
[GR-18163] Improve {Method,UnboundMethod}#inspect
PullRequest: truffleruby/2046
2 parents 2983bf9 + 1826937 commit d1ce9c0

File tree

8 files changed

+77
-34
lines changed

8 files changed

+77
-34
lines changed

spec/ruby/core/method/shared/to_s.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
describe :method_to_s, shared: true do
55
before :each do
66
@m = MethodSpecs::MySub.new.method :bar
7-
@string = @m.send(@method).sub(/0x\w+/, '0xXXXXXX')
7+
@string = @m.send(@method)
88
end
99

1010
it "returns a String" do
@@ -32,21 +32,29 @@
3232
@string.should =~ /MethodSpecs::MySub/
3333
end
3434

35+
it "returns a String including all details" do
36+
@string.should.start_with? "#<Method: MethodSpecs::MySub(MethodSpecs::MyMod)#bar"
37+
end
38+
39+
it "does not show the defining module if it is the same as the receiver class" do
40+
MethodSpecs::A.new.method(:baz).send(@method).should.start_with? "#<Method: MethodSpecs::A#baz"
41+
end
42+
3543
ruby_version_is '3.0' do
3644
it "returns a String containing the Module containing the method if object has a singleton class but method is not defined in the singleton class" do
3745
obj = MethodSpecs::MySub.new
3846
obj.singleton_class
3947
@m = obj.method(:bar)
40-
@string = @m.send(@method).sub(/0x\w+/, '0xXXXXXX')
41-
@string.should =~ /\A#<Method: MethodSpecs::MySub\(MethodSpecs::MyMod\)#bar\(\) /
48+
@string = @m.send(@method)
49+
@string.should.start_with? "#<Method: MethodSpecs::MySub(MethodSpecs::MyMod)#bar"
4250
end
4351
end
4452

4553
it "returns a String containing the singleton class if method is defined in the singleton class" do
4654
obj = MethodSpecs::MySub.new
4755
def obj.bar; end
4856
@m = obj.method(:bar)
49-
@string = @m.send(@method).sub(/0x\w+/, '0xXXXXXX')
50-
@string.should =~ /\A#<Method: #<MethodSpecs::MySub:0xXXXXXX>\.bar/
57+
@string = @m.send(@method).sub(/0x\h+/, '0xXXXXXX')
58+
@string.should.start_with? "#<Method: #<MethodSpecs::MySub:0xXXXXXX>.bar"
5159
end
5260
end

spec/ruby/core/unboundmethod/shared/to_s.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@
2222
@from_module.send(@method).should =~ /\bUnboundMethodSpecs::Mod\b/
2323
@from_method.send(@method).should =~ /\bUnboundMethodSpecs::Methods\b/
2424
end
25+
26+
it "returns a String including all details" do
27+
@from_module.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Methods(UnboundMethodSpecs::Mod)#from_mod"
28+
@from_method.send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::Methods(UnboundMethodSpecs::Mod)#from_mod"
29+
end
30+
31+
it "does not show the defining module if it is the same as the origin" do
32+
UnboundMethodSpecs::A.instance_method(:baz).send(@method).should.start_with? "#<UnboundMethod: UnboundMethodSpecs::A#baz"
33+
end
2534
end

spec/tags/core/method/inspect_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
11311131
"/core/class.rb",
11321132
"/core/binding.rb",
11331133
"/core/math.rb",
1134+
"/core/truffle/method_operations.rb",
11341135
"/core/method.rb",
11351136
"/core/unbound_method.rb",
11361137
"/core/warning.rb",

src/main/java/org/truffleruby/core/method/MethodNodes.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,22 +323,33 @@ protected int getCacheLimit() {
323323
public abstract static class MethodUnimplementNode extends PrimitiveArrayArgumentsNode {
324324

325325
@Specialization
326-
protected Object methodUnimplement(RubyMethod rubyMethod) {
327-
final InternalMethod method = rubyMethod.method;
328-
method.getDeclaringModule().fields.addMethod(
329-
getContext(),
330-
this,
331-
method.unimplemented());
326+
protected Object bound(RubyMethod rubyMethod) {
327+
unimplement(rubyMethod.method);
328+
return nil;
329+
}
330+
331+
@Specialization
332+
protected Object unbound(RubyUnboundMethod rubyMethod) {
333+
unimplement(rubyMethod.method);
332334
return nil;
333335
}
334336

337+
@TruffleBoundary
338+
private void unimplement(InternalMethod method) {
339+
method.getDeclaringModule().fields.addMethod(getContext(), this, method.unimplemented());
340+
}
335341
}
336342

337343
@Primitive(name = "method_unimplemented?")
338-
public abstract static class MethodUnimplementedQueryNode extends PrimitiveArrayArgumentsNode {
344+
public abstract static class MethodIsUnimplementedNode extends PrimitiveArrayArgumentsNode {
345+
346+
@Specialization
347+
protected boolean bound(RubyMethod rubyMethod) {
348+
return rubyMethod.method.isUnimplemented();
349+
}
339350

340351
@Specialization
341-
protected boolean isMethodUnimplemented(RubyMethod rubyMethod) {
352+
protected boolean unbound(RubyUnboundMethod rubyMethod) {
342353
return rubyMethod.method.isUnimplemented();
343354
}
344355

src/main/ruby/truffleruby/core/method.rb

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,10 @@
99
# GNU Lesser General Public License version 2.1.
1010

1111
class Method
12-
1312
def inspect
14-
extra = ''
15-
16-
if Primitive.method_unimplemented? self
17-
extra = ' (not-implemented)'
18-
else
19-
file, line = source_location
20-
21-
if file && line
22-
extra = " #{file}:#{line}"
23-
end
24-
end
25-
26-
"#<#{self.class}: #{receiver.class}(#{owner})##{name}#{extra}>"
13+
Truffle::MethodOperations.inspect_method(self, receiver.class, owner, receiver)
2714
end
15+
alias_method :to_s, :inspect
2816

2917
def curry(curried_arity = nil)
3018
self.to_proc.curry(curried_arity)
@@ -37,7 +25,4 @@ def >>(other)
3725
def <<(other)
3826
self.to_proc << other
3927
end
40-
41-
alias_method :to_s, :inspect
42-
4328
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
4+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
5+
# redistribute it and/or modify it under the terms of the:
6+
#
7+
# Eclipse Public License version 2.0, or
8+
# GNU General Public License version 2, or
9+
# GNU Lesser General Public License version 2.1.
10+
11+
module Truffle
12+
module MethodOperations
13+
def self.inspect_method(meth, origin, owner, receiver = undefined)
14+
extra = ''
15+
if Primitive.method_unimplemented? meth
16+
extra = ' (not-implemented)'
17+
else
18+
file, line = meth.source_location
19+
if file && line
20+
extra = " #{file}:#{line}"
21+
end
22+
end
23+
24+
if !Primitive.undefined?(receiver) && owner.singleton_class?
25+
"#<#{meth.class}: #{receiver.inspect}.#{meth.name}#{extra}>"
26+
else
27+
origin_owner = origin == owner ? origin : "#{origin}(#{owner})"
28+
"#<#{meth.class}: #{origin_owner}##{meth.name}#{extra}>"
29+
end
30+
end
31+
end
32+
end

src/main/ruby/truffleruby/core/unbound_method.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
# GNU Lesser General Public License version 2.1.
1010

1111
class UnboundMethod
12-
1312
def inspect
14-
"#<#{self.class}: #{origin}(#{owner})##{name}>"
13+
Truffle::MethodOperations.inspect_method(self, origin, owner)
1514
end
16-
1715
alias_method :to_s, :inspect
1816

1917
def bind_call(recv, *args, &block)

0 commit comments

Comments
 (0)