summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2021-12-09 04:06:53 +0900
committerMarc-André Lafortune <github@marc-andre.ca>2021-12-08 14:47:46 -0500
commit9c269316357a1a5b6ef27794f3c9412b2e33a6cd (patch)
treede3baa4722547b95fe595355f1d07e68ba22e251
parent9a2ecddf32528286c81ca87db1d17c85aca4e754 (diff)
[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.
-rw-r--r--lib/ostruct.rb7
1 files changed, 5 insertions, 2 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 4f0a4a7dbe..ec44a525d9 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -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)