class RubyVM::RJIT::CPointer::Union

Note: We’d like to avoid alphabetic method names to avoid a conflict with member methods. to_i is considered an exception.

Public Class Methods

define(sizeof, members) click to toggle source

@param sizeof [Integer] @param members [Hash{ Symbol => RubyVM::RJIT::CType::* }]

Calls superclass method
# File ruby_vm/rjit/c_pointer.rb, line 132
def self.define(sizeof, members)
  Class.new(self) do
    # Return the size of this type
    define_singleton_method(:sizeof) { sizeof }

    # Part of Struct's offsetof implementation
    define_singleton_method(:offsetof) do |field, *fields|
      member = members.fetch(field)
      offset = 0
      unless fields.empty?
        offset += member.offsetof(*fields)
      end
      offset
    end

    define_method(:initialize) do |addr|
      super(addr, sizeof, members)
    end

    members.each do |member, type|
      # Intelligent API that does automatic dereference
      define_method(member) do
        value = type.new(@addr)
        if value.respond_to?(:*)
          value = value.*
        end
        value
      end
    end
  end
end
new(addr, sizeof, members) click to toggle source

@param _name [String] To be used when it starts defining a union pointer class @param sizeof [Integer] @param members [Hash{ Symbol => RubyVM::RJIT::CType::* }]

# File ruby_vm/rjit/c_pointer.rb, line 107
def initialize(addr, sizeof, members)
  @addr = addr
  @sizeof = sizeof
  @members = members
end

Public Instance Methods

+(index) click to toggle source

Move addr to access this pointer like an array

# File ruby_vm/rjit/c_pointer.rb, line 119
def +(index)
  raise ArgumentError unless index.is_a?(Integer)
  self.class.new(@addr + index * @sizeof)
end
-(union) click to toggle source

Pointer diff

# File ruby_vm/rjit/c_pointer.rb, line 125
def -(union)
  raise ArgumentError if self.class != union.class
  (@addr - union.instance_variable_get(:@addr)) / @sizeof
end
to_i() click to toggle source

Get a raw address

# File ruby_vm/rjit/c_pointer.rb, line 114
def to_i
  @addr
end