[Bug #20978] Stringize Fiber storage keys

This commit is contained in:
Nobuyoshi Nakada 2024-12-23 18:16:28 +09:00
parent a11bb36316
commit adad97a031
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2024-12-23 11:13:09 +00:00
3 changed files with 25 additions and 2 deletions

4
cont.c
View File

@ -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;

View File

@ -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

View File

@ -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?)