Project

General

Profile

Bug #16127

Delegates to BasicObject do not work

Added by jeremyevans0 (Jeremy Evans) about 2 months ago. Updated 5 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0dev (2019-08-25) [x86_64-openbsd6.5]
[ruby-core:94541]

Description

The delegate library does not currently handle BasicObject targets, since it assumes the target responds to respond_to?. The attached patch works around the issue by binding the Kernel respond_to? instance method to the target and then calling that.


Files

delegate-basicobject.patch (2.29 KB) delegate-basicobject.patch jeremyevans0 (Jeremy Evans), 08/25/2019 07:08 AM
delegate-basicobject-16127-v2.patch (2.53 KB) delegate-basicobject-16127-v2.patch jeremyevans0 (Jeremy Evans), 09/02/2019 05:11 AM

Associated revisions

Revision 2322c94d
Added by jeremyevans (Jeremy Evans) 5 days ago

Support delegates for BasicObject

For BasicObject, bind the Kernel respond_to? instance method to the
object and call it instead of calling the method directly.

Also, use bind_call(recv, ...) for better performance.

Fixes [Bug #16127]

History

Updated by decuplet (Nikita Shilnikov) about 2 months ago

With UnboundMethod#bind_call added in https://bugs.ruby-lang.org/projects/ruby-trunk/repository/git/revisions/83c6a1ef454c51ad1c0ca58e8a95fd67a033f710, we should be able to save some allocations by assigning ::Kernel.instance_method(:respond_to?) to a constant and calling with .bind_call. WDYT? Not a big deal, I just don't like the idea we allocate objects when merely calling a method.

#2

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

Sure, it would be fine to use #bind_call for this. At the time this patch was submitted, #bind_call wasn't available. I know I have a patch in #7833 where #bind_call could also be applied. We should probably do a sweep for other places in stdlib where #bind_call could be used.

Updated by alanwu (Alan Wu) about 1 month ago

It might be useful to have a test where the delegation happens successfully.

Updated by jeremyevans0 (Jeremy Evans) about 1 month ago

Here's an updated patch that uses #bind_call and tests successful delegation to a BasicObject target.

#5

Updated by jeremyevans (Jeremy Evans) 5 days ago

  • Status changed from Open to Closed

Applied in changeset git|2322c94dd65c0247b103e2f91411e37458e1466d.


Support delegates for BasicObject

For BasicObject, bind the Kernel respond_to? instance method to the
object and call it instead of calling the method directly.

Also, use bind_call(recv, ...) for better performance.

Fixes [Bug #16127]

Also available in: Atom PDF