Skip to content

Commit 77abd77

Browse files
committed
Share logic for {Method,UnboundMethod}#inspect and avoid duplication when origin == owner
1 parent 6dca58c commit 77abd77

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

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
@@ -320,22 +320,33 @@ protected int getCacheLimit() {
320320
public abstract static class MethodUnimplementNode extends PrimitiveArrayArgumentsNode {
321321

322322
@Specialization
323-
protected Object methodUnimplement(RubyMethod rubyMethod) {
324-
final InternalMethod method = rubyMethod.method;
325-
method.getDeclaringModule().fields.addMethod(
326-
getContext(),
327-
this,
328-
method.unimplemented());
323+
protected Object bound(RubyMethod rubyMethod) {
324+
unimplement(rubyMethod.method);
325+
return nil;
326+
}
327+
328+
@Specialization
329+
protected Object unbound(RubyUnboundMethod rubyMethod) {
330+
unimplement(rubyMethod.method);
329331
return nil;
330332
}
331333

334+
@TruffleBoundary
335+
private void unimplement(InternalMethod method) {
336+
method.getDeclaringModule().fields.addMethod(getContext(), this, method.unimplemented());
337+
}
332338
}
333339

334340
@Primitive(name = "method_unimplemented?")
335-
public abstract static class MethodUnimplementedQueryNode extends PrimitiveArrayArgumentsNode {
341+
public abstract static class MethodIsUnimplementedNode extends PrimitiveArrayArgumentsNode {
342+
343+
@Specialization
344+
protected boolean bound(RubyMethod rubyMethod) {
345+
return rubyMethod.method.isUnimplemented();
346+
}
336347

337348
@Specialization
338-
protected boolean isMethodUnimplemented(RubyMethod rubyMethod) {
349+
protected boolean unbound(RubyUnboundMethod rubyMethod) {
339350
return rubyMethod.method.isUnimplemented();
340351
}
341352

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)
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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)
14+
extra = ''
15+
if Primitive.method_unimplemented? meth
16+
extra = ' (not-implemented)'
17+
else
18+
file, line = meth.source_location
19+
20+
if file && line
21+
extra = " #{file}:#{line}"
22+
end
23+
end
24+
25+
origin_owner = origin == owner ? origin : "#{origin}(#{owner})"
26+
"#<#{meth.class}: #{origin_owner}##{meth.name}#{extra}>"
27+
end
28+
end
29+
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)