Skip to content

Commit adad97a

Browse files
committed
[Bug #20978] Stringize Fiber storage keys
1 parent a11bb36 commit adad97a

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

cont.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ rb_fiber_storage_set(VALUE self, VALUE value)
21242124
static VALUE
21252125
rb_fiber_storage_aref(VALUE class, VALUE key)
21262126
{
2127-
Check_Type(key, T_SYMBOL);
2127+
key = rb_to_symbol(key);
21282128

21292129
VALUE storage = fiber_storage_get(fiber_current(), FALSE);
21302130
if (storage == Qnil) return Qnil;
@@ -2145,7 +2145,7 @@ rb_fiber_storage_aref(VALUE class, VALUE key)
21452145
static VALUE
21462146
rb_fiber_storage_aset(VALUE class, VALUE key, VALUE value)
21472147
{
2148-
Check_Type(key, T_SYMBOL);
2148+
key = rb_to_symbol(key);
21492149

21502150
VALUE storage = fiber_storage_get(fiber_current(), value != Qnil);
21512151
if (storage == Qnil) return Qnil;

spec/ruby/core/fiber/storage_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@
9090
key = :"#{self.class.name}#.#{self.object_id}"
9191
Fiber.new { Fiber[key] = 42; Fiber[key] }.resume.should == 42
9292
end
93+
end
9394

95+
ruby_version_is "3.2.3"..."3.4" do
9496
it "can't use invalid keys" do
9597
invalid_keys = [Object.new, "Foo", 12]
9698
invalid_keys.each do |key|
@@ -99,6 +101,15 @@
99101
end
100102
end
101103

104+
ruby_version_is "3.4" do
105+
it "can use keys as strings" do
106+
key = Object.new
107+
def key.to_str; "Foo"; end
108+
Fiber[key] = 42
109+
Fiber["Foo"].should == 42
110+
end
111+
end
112+
102113
it "can access the storage of the parent fiber" do
103114
f = Fiber.new(storage: {life: 42}) do
104115
Fiber.new { Fiber[:life] }.resume

test/ruby/test_fiber.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,18 @@ def tvar(var, val)
252252
assert_equal(nil, Thread.current[:v]);
253253
end
254254

255+
def test_fiber_variables
256+
assert_equal "bar", Fiber.new {Fiber[:foo] = "bar"; Fiber[:foo]}.resume
257+
258+
key = :"#{self.class.name}#.#{self.object_id}"
259+
Fiber[key] = 42
260+
assert_equal 42, Fiber[key]
261+
262+
key = Object.new
263+
def key.to_str; "foo"; end
264+
assert_equal "Bar", Fiber.new {Fiber[key] = "Bar"; Fiber[key]}.resume
265+
end
266+
255267
def test_alive
256268
fib = Fiber.new{Fiber.yield}
257269
assert_equal(true, fib.alive?)

0 commit comments

Comments
 (0)