diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2023-12-22 11:20:45 -0800 |
---|---|---|
committer | Takashi Kokubun <takashikkbn@gmail.com> | 2023-12-22 11:24:04 -0800 |
commit | 19d082dcfa393e8a5d0282b872ace587ca5cd49a (patch) | |
tree | 749210d710ef8a261f304e556e0709b97f45ea1f /lib | |
parent | 40e3f782dd92749a1fa593ef905d1b75fdd61904 (diff) |
RJIT: Distinguish Pointer with Array
This is more convenient for accessing those fields.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ruby_vm/rjit/c_pointer.rb | 34 | ||||
-rw-r--r-- | lib/ruby_vm/rjit/c_type.rb | 8 |
2 files changed, 42 insertions, 0 deletions
diff --git a/lib/ruby_vm/rjit/c_pointer.rb b/lib/ruby_vm/rjit/c_pointer.rb index 1a84896822..db00c4cd11 100644 --- a/lib/ruby_vm/rjit/c_pointer.rb +++ b/lib/ruby_vm/rjit/c_pointer.rb @@ -238,6 +238,40 @@ module RubyVM::RJIT end end + # Basically Immediate but without #* to skip auto-dereference of structs. + class Array + attr_reader :type + + # @param addr [Integer] + # @param type [Class] RubyVM::RJIT::CType::* + def initialize(addr, type) + @addr = addr + @type = type + end + + # Array access + def [](index) + @type.new(@addr)[index] + end + + # Array set + # @param index [Integer] + # @param value [Integer, RubyVM::RJIT::CPointer::Struct] an address itself or an object that return an address with to_i + def []=(index, value) + @type.new(@addr)[index] = value + end + + private + + def self.define(block) + Class.new(self) do + define_method(:initialize) do |addr| + super(addr, block.call) + end + end + end + end + class Pointer attr_reader :type diff --git a/lib/ruby_vm/rjit/c_type.rb b/lib/ruby_vm/rjit/c_type.rb index bec7e21c38..3b313a658b 100644 --- a/lib/ruby_vm/rjit/c_type.rb +++ b/lib/ruby_vm/rjit/c_type.rb @@ -62,6 +62,14 @@ module RubyVM::RJIT end end + class Array + def self.new(&block) + CPointer.with_class_name('Array', block.object_id.to_s) do + CPointer::Array.define(block) + end + end + end + class Pointer # This takes a block to avoid "stack level too deep" on a cyclic reference # @param block [Proc] |