diff --git a/cont.c b/cont.c index 5f2815391e..e9ed5af4e3 100644 --- a/cont.c +++ b/cont.c @@ -2124,7 +2124,7 @@ rb_fiber_storage_set(VALUE self, VALUE value) static VALUE rb_fiber_storage_aref(VALUE class, VALUE key) { - Check_Type(key, T_SYMBOL); + key = rb_to_symbol(key); VALUE storage = fiber_storage_get(fiber_current(), FALSE); if (storage == Qnil) return Qnil; @@ -2145,7 +2145,7 @@ rb_fiber_storage_aref(VALUE class, VALUE key) static VALUE rb_fiber_storage_aset(VALUE class, VALUE key, VALUE value) { - Check_Type(key, T_SYMBOL); + key = rb_to_symbol(key); VALUE storage = fiber_storage_get(fiber_current(), value != Qnil); if (storage == Qnil) return Qnil; diff --git a/spec/ruby/core/fiber/storage_spec.rb b/spec/ruby/core/fiber/storage_spec.rb index 5c87ed5d41..83272b0930 100644 --- a/spec/ruby/core/fiber/storage_spec.rb +++ b/spec/ruby/core/fiber/storage_spec.rb @@ -90,7 +90,9 @@ ruby_version_is "3.2" do key = :"#{self.class.name}#.#{self.object_id}" Fiber.new { Fiber[key] = 42; Fiber[key] }.resume.should == 42 end + end + ruby_version_is "3.2.3"..."3.4" do it "can't use invalid keys" do invalid_keys = [Object.new, "Foo", 12] invalid_keys.each do |key| @@ -99,6 +101,15 @@ ruby_version_is "3.2" do end end + ruby_version_is "3.4" do + it "can use keys as strings" do + key = Object.new + def key.to_str; "Foo"; end + Fiber[key] = 42 + Fiber["Foo"].should == 42 + end + end + it "can access the storage of the parent fiber" do f = Fiber.new(storage: {life: 42}) do Fiber.new { Fiber[:life] }.resume diff --git a/test/ruby/test_fiber.rb b/test/ruby/test_fiber.rb index 45e5d12092..19cd52f7c8 100644 --- a/test/ruby/test_fiber.rb +++ b/test/ruby/test_fiber.rb @@ -252,6 +252,18 @@ class TestFiber < Test::Unit::TestCase assert_equal(nil, Thread.current[:v]); end + def test_fiber_variables + assert_equal "bar", Fiber.new {Fiber[:foo] = "bar"; Fiber[:foo]}.resume + + key = :"#{self.class.name}#.#{self.object_id}" + Fiber[key] = 42 + assert_equal 42, Fiber[key] + + key = Object.new + def key.to_str; "foo"; end + assert_equal "Bar", Fiber.new {Fiber[key] = "Bar"; Fiber[key]}.resume + end + def test_alive fib = Fiber.new{Fiber.yield} assert_equal(true, fib.alive?)