diff options
author | Koichi Sasada <ko1@atdot.net> | 2024-07-11 05:59:14 +0900 |
---|---|---|
committer | Koichi Sasada <ko1@atdot.net> | 2024-07-12 04:43:14 +0900 |
commit | 43aee3393d70f8893e312b38a9a30c1dba295c41 (patch) | |
tree | 0bff897526b61ff20fe6f978628b2ff1366e5225 /bootstraptest/test_ractor.rb | |
parent | ef563a696db983fe21a259dac38e5bec201d6b9a (diff) |
fix `defined?(@ivar)` with Ractors
`defined?(@ivar)` on the non main Ractor has two issues:
1. raising an exception
```ruby
class C
@iv1 = []
def self.defined_iv1 = defined?(@iv1)
end
Ractor.new{
p C.defined_iv1
#=> can not get unshareable values from instance variables of classes/modules from non-main Ractors (Ractor::IsolationError)
}.take
```
-> Do not raise an exception but return `"instance-variable"` because
it is defined.
2. returning `"instance-variable"` if there is not defined.
```
class C
# @iv2 is not defined
def self.defined_iv2 = defined?(@iv2)
end
Ractor.new{
p C.defined_iv2 #=> "instance-variable"
}.take
```
-> returns `nil`
Diffstat (limited to 'bootstraptest/test_ractor.rb')
-rw-r--r-- | bootstraptest/test_ractor.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 0390d38f9c..b6a4ef605e 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -1086,6 +1086,20 @@ assert_equal '333', %q{ a + b + c + d + e + f } +assert_equal '["instance-variable", "instance-variable", nil]', %q{ + class C + @iv1 = "" + @iv2 = 42 + def self.iv1 = defined?(@iv1) # "instance-variable" + def self.iv2 = defined?(@iv2) # "instance-variable" + def self.iv3 = defined?(@iv3) # nil + end + + Ractor.new{ + [C.iv1, C.iv2, C.iv3] + }.take +} + # moved objects have their shape properly set to original object's shape assert_equal '1234', %q{ class Obj |