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.



5264
5265
5266
5267
# File 'lib/syntax_tree/yarv/instructions.rb', line 5264

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



5262
5263
5264
# File 'lib/syntax_tree/yarv/instructions.rb', line 5262

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



5262
5263
5264
# File 'lib/syntax_tree/yarv/instructions.rb', line 5262

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5284
5285
5286
5287
# File 'lib/syntax_tree/yarv/instructions.rb', line 5284

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

#call(vm) ⇒ Object



5297
5298
5299
5300
# File 'lib/syntax_tree/yarv/instructions.rb', line 5297

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

#deconstruct_keys(_keys) ⇒ Object



5280
5281
5282
# File 'lib/syntax_tree/yarv/instructions.rb', line 5280

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

#disasm(fmt) ⇒ Object



5269
5270
5271
5272
5273
5274
# File 'lib/syntax_tree/yarv/instructions.rb', line 5269

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

#lengthObject



5289
5290
5291
# File 'lib/syntax_tree/yarv/instructions.rb', line 5289

def length
  3
end

#popsObject



5293
5294
5295
# File 'lib/syntax_tree/yarv/instructions.rb', line 5293

def pops
  1
end

#to_a(_iseq) ⇒ Object



5276
5277
5278
# File 'lib/syntax_tree/yarv/instructions.rb', line 5276

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