Class: SyntaxTree::YARV::SetInstanceVariable

Inherits:
Object
  • 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

Constructor Details

#initialize(name, cache) ⇒ SetInstanceVariable

Returns a new instance of SetInstanceVariable.



4777
4778
4779
4780
# File 'lib/syntax_tree/yarv/instructions.rb', line 4777

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



4775
4776
4777
# File 'lib/syntax_tree/yarv/instructions.rb', line 4775

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



4775
4776
4777
# File 'lib/syntax_tree/yarv/instructions.rb', line 4775

def name
  @name
end

Instance Method Details

#call(vm) ⇒ Object



4809
4810
4811
4812
# File 'lib/syntax_tree/yarv/instructions.rb', line 4809

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

#canonicalObject



4805
4806
4807
# File 'lib/syntax_tree/yarv/instructions.rb', line 4805

def canonical
  self
end

#disasm(fmt) ⇒ Object



4782
4783
4784
4785
4786
4787
# File 'lib/syntax_tree/yarv/instructions.rb', line 4782

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

#lengthObject



4793
4794
4795
# File 'lib/syntax_tree/yarv/instructions.rb', line 4793

def length
  3
end

#popsObject



4797
4798
4799
# File 'lib/syntax_tree/yarv/instructions.rb', line 4797

def pops
  1
end

#pushesObject



4801
4802
4803
# File 'lib/syntax_tree/yarv/instructions.rb', line 4801

def pushes
  0
end

#to_a(_iseq) ⇒ Object



4789
4790
4791
# File 'lib/syntax_tree/yarv/instructions.rb', line 4789

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