diff options
author | Alan Wu <XrXr@users.noreply.github.com> | 2021-03-11 14:46:19 -0500 |
---|---|---|
committer | Alan Wu <XrXr@users.noreply.github.com> | 2021-10-20 18:19:32 -0400 |
commit | 0cd9120f177b153126a093e4beabb5784cd0ab99 (patch) | |
tree | e10418049297791698f802c2581ea24e165a5e13 /bootstraptest | |
parent | db53decad6db5eedf9d9090e14f7d2216549f9d0 (diff) |
YJIT: hash specialization for opt_aref
Make it lazy and add a hash specialization in addition to the array
specialization.
Diffstat (limited to 'bootstraptest')
-rw-r--r-- | bootstraptest/test_yjit.rb | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/bootstraptest/test_yjit.rb b/bootstraptest/test_yjit.rb index 4750ac5fe8..44817313b4 100644 --- a/bootstraptest/test_yjit.rb +++ b/bootstraptest/test_yjit.rb @@ -361,3 +361,90 @@ assert_equal 'ok', %q{ subclasses.each { _1.new.get } parent.new.get } + +# Test polymorphic opt_aref. array -> hash +assert_equal '[42, :key]', %q{ + def index(obj, idx) + obj[idx] + end + + index([], 0) # get over compilation threshold + + [ + index([42], 0), + index({0=>:key}, 0), + ] +} + +# Test polymorphic opt_aref. hash -> array -> custom class +assert_equal '[nil, nil, :custom]', %q{ + def index(obj, idx) + obj[idx] + end + + custom = Object.new + def custom.[](_idx) + :custom + end + + index({}, 0) # get over compilation threshold + + [ + index({}, 0), + index([], 0), + index(custom, 0) + ] +} + +# Test polymorphic opt_aref. array -> custom class +assert_equal '[42, :custom]', %q{ + def index(obj, idx) + obj[idx] + end + + custom = Object.new + def custom.[](_idx) + :custom + end + + index([], 0) # get over compilation threshold + + [ + index([42], 0), + index(custom, 0) + ] +} + +# Test custom hash method with opt_aref +assert_equal '[nil, :ok]', %q{ + def index(obj, idx) + obj[idx] + end + + custom = Object.new + def custom.hash + 42 + end + + h = {custom => :ok} + + [ + index(h, 0), + index(h, custom) + ] +} + +# Test default value block for Hash with opt_aref +assert_equal '[42, :default]', %q{ + def index(obj, idx) + obj[idx] + end + + h = Hash.new { :default } + h[0] = 42 + + [ + index(h, 0), + index(h, 1) + ] +} |