Class: SyntaxTree::YARV::SetInstanceVariable

Inherits:
Instruction
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

‘setinstancevariable` pops a value off the top of the stack and then sets the instance variable associated with the instruction to that value.

This instruction has two forms, but both have the same structure. Before Ruby 3.2, the inline cache corresponded to both the get and set instructions and could be shared. Since Ruby 3.2, it uses object shapes instead so the caches are unique per instruction.

### Usage

~~~ruby ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #pushes, #side_effects?

Constructor Details

#initialize(name, cache) ⇒ SetInstanceVariable

Returns a new instance of SetInstanceVariable.



5117
5118
5119
5120
# File 'lib/syntax_tree/yarv/instructions.rb', line 5117

def initialize(name, cache)
  @name = name
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



5115
5116
5117
# File 'lib/syntax_tree/yarv/instructions.rb', line 5115

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



5115
5116
5117
# File 'lib/syntax_tree/yarv/instructions.rb', line 5115

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5137
5138
5139
5140
# File 'lib/syntax_tree/yarv/instructions.rb', line 5137

def ==(other)
  other.is_a?(SetInstanceVariable) && other.name == name &&
    other.cache == cache
end

#call(vm) ⇒ Object



5150
5151
5152
5153
# File 'lib/syntax_tree/yarv/instructions.rb', line 5150

def call(vm)
  method = Object.instance_method(:instance_variable_set)
  method.bind(vm.frame._self).call(name, vm.pop)
end

#deconstruct_keys(_keys) ⇒ Object



5133
5134
5135
# File 'lib/syntax_tree/yarv/instructions.rb', line 5133

def deconstruct_keys(_keys)
  { name: name, cache: cache }
end

#disasm(fmt) ⇒ Object



5122
5123
5124
5125
5126
5127
# File 'lib/syntax_tree/yarv/instructions.rb', line 5122

def disasm(fmt)
  fmt.instruction(
    "setinstancevariable",
    [fmt.object(name), fmt.inline_storage(cache)]
  )
end

#lengthObject



5142
5143
5144
# File 'lib/syntax_tree/yarv/instructions.rb', line 5142

def length
  3
end

#popsObject



5146
5147
5148
# File 'lib/syntax_tree/yarv/instructions.rb', line 5146

def pops
  1
end

#to_a(_iseq) ⇒ Object



5129
5130
5131
# File 'lib/syntax_tree/yarv/instructions.rb', line 5129

def to_a(_iseq)
  [:setinstancevariable, name, cache]
end