Class: JIT::Struct::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/jit/struct.rb

Overview

An abstraction for an instance of a JIT::Struct.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(struct, ptr) ⇒ Instance

Wrap an existing structure.

struct

The JIT::Struct type to wrap.

ptr A pointer to the first element of the structure.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jit/struct.rb', line 109

def initialize(struct, ptr)
  @struct = struct
  @function = ptr.function
  @ptr = ptr

  mod = Module.new do
    struct.members.each do |name|
      define_method("#{name}") do
        self[name] # return
      end

      define_method("#{name}=") do |value|
        self[name] = value # return
      end
    end
  end

  extend(mod)
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



102
103
104
# File 'lib/jit/struct.rb', line 102

def ptr
  @ptr
end

Instance Method Details

#[](name) ⇒ Object

Generate JIT code to retrieve the element with the given name.

name

The name of the desired element.



133
134
135
136
137
138
# File 'lib/jit/struct.rb', line 133

def [](name)
  @function.insn_load_relative(
      @ptr,
      @struct.offset_of(name),
      @struct.type_of(name))
end

#[]=(name, value) ⇒ Object

Generate JIT code to assign to the element with the given name.

name

The name of the desired element.

value

The JIT::Value to assign to the element.



145
146
147
148
149
150
# File 'lib/jit/struct.rb', line 145

def []=(name, value)
  @function.insn_store_relative(
      @ptr,
      @struct.offset_of(name),
      value)
end

#membersObject



152
153
154
# File 'lib/jit/struct.rb', line 152

def members
  return @struct.members
end