[前][次][番号順一覧][スレッド一覧]

ruby-changes:68159

From: rm155 <ko1@a...>
Date: Tue, 28 Sep 2021 19:15:59 +0900 (JST)
Subject: [ruby-changes:68159] cefa029573 (master): [ruby/ostruct] Allow properties to be accessed even when the object is moved to another Ractor (https://github.com/ruby/ostruct/pull/29)

https://git.ruby-lang.org/ruby.git/commit/?id=cefa029573

From cefa02957312620187cfd81219650304032ae787 Mon Sep 17 00:00:00 2001
From: rm155 <86454369+rm155@u...>
Date: Fri, 20 Aug 2021 08:12:28 -0400
Subject: [ruby/ostruct] Allow properties to be accessed even when the object
 is moved to another Ractor (https://github.com/ruby/ostruct/pull/29)

https://github.com/ruby/ostruct/commit/d85639f2f5
---
 lib/ostruct.rb               | 10 ++++++++--
 test/ostruct/test_ostruct.rb | 12 ++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index c03d912d6f..8680374499 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -221,8 +221,14 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L221
   #
   def new_ostruct_member!(name) # :nodoc:
     unless @table.key?(name) || is_method_protected!(name)
-      define_singleton_method!(name) { @table[name] }
-      define_singleton_method!("#{name}=") {|x| @table[name] = x}
+      getter_proc = Proc.new { @table[name] }
+      setter_proc = Proc.new {|x| @table[name] = x}
+      if defined?(::Ractor)
+        ::Ractor.make_shareable(getter_proc)
+        ::Ractor.make_shareable(setter_proc)
+      end
+      define_singleton_method!(name, &getter_proc) 
+      define_singleton_method!("#{name}=", &setter_proc)
     end
   end
   private :new_ostruct_member!
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 1fd7d87811..4ec4d43084 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -368,6 +368,18 @@ class TC_OpenStruct < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L368
     RUBY
   end if defined?(Ractor)
 
+  def test_access_methods_from_different_ractor
+    assert_ractor(<<~RUBY, require: 'ostruct')
+      os = OpenStruct.new
+      os.value = 100
+      r = Ractor.new(os) do |x|
+        v = x.value
+        Ractor.yield v
+      end
+      assert 100 == r.take
+    RUBY
+  end if defined?(Ractor)
+
   def test_legacy_yaml
     s = "--- !ruby/object:OpenStruct\ntable:\n  :foo: 42\n"
     o = YAML.safe_load(s, permitted_classes: [Symbol, OpenStruct])
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]