1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/ostruct] Proc's self should be shareable.

To fix the issue https://bugs.ruby-lang.org/issues/18243
we need to make sure the Proc's self is shareable.
These procs are used by `define_method` and it doesn't use
Proc's self, so `nil` is enough.
This commit is contained in:
Koichi Sasada 2021-12-09 04:06:53 +09:00 committed by Marc-André Lafortune
parent 9a2ecddf32
commit 9c26931635

View file

@ -221,11 +221,14 @@ class OpenStruct
#
def new_ostruct_member!(name) # :nodoc:
unless @table.key?(name) || is_method_protected!(name)
getter_proc = Proc.new { @table[name] }
setter_proc = Proc.new {|x| @table[name] = x}
if defined?(::Ractor)
getter_proc = nil.instance_eval{ Proc.new { @table[name] } }
setter_proc = nil.instance_eval{ Proc.new {|x| @table[name] = x} }
::Ractor.make_shareable(getter_proc)
::Ractor.make_shareable(setter_proc)
else
getter_proc = Proc.new { @table[name] }
setter_proc = Proc.new {|x| @table[name] = x}
end
define_singleton_method!(name, &getter_proc)
define_singleton_method!("#{name}=", &setter_proc)