summaryrefslogtreecommitdiff
path: root/lib/ruby_vm/rjit/c_pointer.rb
diff options
authorTakashi Kokubun <[email protected]>2023-12-22 11:20:45 -0800
committerTakashi Kokubun <[email protected]>2023-12-22 11:24:04 -0800
commit19d082dcfa393e8a5d0282b872ace587ca5cd49a (patch)
tree749210d710ef8a261f304e556e0709b97f45ea1f /lib/ruby_vm/rjit/c_pointer.rb
parent40e3f782dd92749a1fa593ef905d1b75fdd61904 (diff)
RJIT: Distinguish Pointer with Array
This is more convenient for accessing those fields.
Diffstat (limited to 'lib/ruby_vm/rjit/c_pointer.rb')
-rw-r--r--lib/ruby_vm/rjit/c_pointer.rb34
1 files changed, 34 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