class RubyVM::MJIT::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::MJIT::CType::* }]

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

    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::MJIT::CType::* }]

# File lib/mjit/c_pointer.rb, line 91
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 lib/mjit/c_pointer.rb, line 103
def +(index)
  raise ArgumentError unless index.is_a?(Integer)
  self.class.new(@addr + index * @sizeof)
end
-(union) click to toggle source

Pointer diff

# File lib/mjit/c_pointer.rb, line 109
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 lib/mjit/c_pointer.rb, line 98
def to_i
  @addr
end