Class: SyntaxTree::YARV::GetClassVariable

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

Overview

### Summary

getclassvariable looks for a class variable in the current class and pushes its value onto the stack. It uses an inline cache to reduce the need to lookup the class variable in the class hierarchy every time.

### Usage

~~~ruby @@class_variable ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cache) ⇒ GetClassVariable

Returns a new instance of GetClassVariable.



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

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



1669
1670
1671
# File 'lib/syntax_tree/yarv/instructions.rb', line 1669

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



1669
1670
1671
# File 'lib/syntax_tree/yarv/instructions.rb', line 1669

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



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

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

#call(vm) ⇒ Object



1712
1713
1714
1715
1716
# File 'lib/syntax_tree/yarv/instructions.rb', line 1712

def call(vm)
  clazz = vm.frame._self
  clazz = clazz.class unless clazz.is_a?(Class)
  vm.push(clazz.class_variable_get(name))
end

#canonicalObject



1708
1709
1710
# File 'lib/syntax_tree/yarv/instructions.rb', line 1708

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



1687
1688
1689
# File 'lib/syntax_tree/yarv/instructions.rb', line 1687

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

#disasm(fmt) ⇒ Object



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

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

#lengthObject



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

def length
  3
end

#popsObject



1700
1701
1702
# File 'lib/syntax_tree/yarv/instructions.rb', line 1700

def pops
  0
end

#pushesObject



1704
1705
1706
# File 'lib/syntax_tree/yarv/instructions.rb', line 1704

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1683
1684
1685
# File 'lib/syntax_tree/yarv/instructions.rb', line 1683

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