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



1867
1868
1869
1870
# File 'lib/syntax_tree/yarv/instructions.rb', line 1867

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



1865
1866
1867
# File 'lib/syntax_tree/yarv/instructions.rb', line 1865

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



1865
1866
1867
# File 'lib/syntax_tree/yarv/instructions.rb', line 1865

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



1887
1888
1889
1890
# File 'lib/syntax_tree/yarv/instructions.rb', line 1887

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

#call(vm) ⇒ Object



1908
1909
1910
1911
# File 'lib/syntax_tree/yarv/instructions.rb', line 1908

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

#canonicalObject



1904
1905
1906
# File 'lib/syntax_tree/yarv/instructions.rb', line 1904

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



1883
1884
1885
# File 'lib/syntax_tree/yarv/instructions.rb', line 1883

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

#disasm(fmt) ⇒ Object



1872
1873
1874
1875
1876
1877
# File 'lib/syntax_tree/yarv/instructions.rb', line 1872

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

#lengthObject



1892
1893
1894
# File 'lib/syntax_tree/yarv/instructions.rb', line 1892

def length
  3
end

#popsObject



1896
1897
1898
# File 'lib/syntax_tree/yarv/instructions.rb', line 1896

def pops
  0
end

#pushesObject



1900
1901
1902
# File 'lib/syntax_tree/yarv/instructions.rb', line 1900

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1879
1880
1881
# File 'lib/syntax_tree/yarv/instructions.rb', line 1879

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