Class: SyntaxTree::YARV::GetInstanceVariable

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

Overview

### Summary

getinstancevariable pushes the value of an instance variable onto the stack. It uses an inline cache to avoid having to look up the instance variable in the class hierarchy every time.

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) ⇒ GetInstanceVariable



1665
1666
1667
1668
# File 'lib/syntax_tree/yarv/instructions.rb', line 1665

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



1663
1664
1665
# File 'lib/syntax_tree/yarv/instructions.rb', line 1663

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



1663
1664
1665
# File 'lib/syntax_tree/yarv/instructions.rb', line 1663

def name
  @name
end

Instance Method Details

#call(vm) ⇒ Object



1697
1698
1699
1700
# File 'lib/syntax_tree/yarv/instructions.rb', line 1697

def call(vm)
  method = Object.instance_method(:instance_variable_get)
  vm.push(method.bind(vm.frame._self).call(name))
end

#canonicalObject



1693
1694
1695
# File 'lib/syntax_tree/yarv/instructions.rb', line 1693

def canonical
  self
end

#disasm(fmt) ⇒ Object



1670
1671
1672
1673
1674
1675
# File 'lib/syntax_tree/yarv/instructions.rb', line 1670

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

#lengthObject



1681
1682
1683
# File 'lib/syntax_tree/yarv/instructions.rb', line 1681

def length
  3
end

#popsObject



1685
1686
1687
# File 'lib/syntax_tree/yarv/instructions.rb', line 1685

def pops
  0
end

#pushesObject



1689
1690
1691
# File 'lib/syntax_tree/yarv/instructions.rb', line 1689

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1677
1678
1679
# File 'lib/syntax_tree/yarv/instructions.rb', line 1677

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