Class: SyntaxTree::YARV::GetClassVariable

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

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #pops, #side_effects?

Constructor Details

#initialize(name, cache) ⇒ GetClassVariable

Returns a new instance of GetClassVariable.



1599
1600
1601
1602
# File 'lib/syntax_tree/yarv/instructions.rb', line 1599

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



1597
1598
1599
# File 'lib/syntax_tree/yarv/instructions.rb', line 1597

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



1597
1598
1599
# File 'lib/syntax_tree/yarv/instructions.rb', line 1597

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



1619
1620
1621
1622
# File 'lib/syntax_tree/yarv/instructions.rb', line 1619

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

#call(vm) ⇒ Object



1632
1633
1634
1635
1636
# File 'lib/syntax_tree/yarv/instructions.rb', line 1632

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

#deconstruct_keys(_keys) ⇒ Object



1615
1616
1617
# File 'lib/syntax_tree/yarv/instructions.rb', line 1615

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

#disasm(fmt) ⇒ Object



1604
1605
1606
1607
1608
1609
# File 'lib/syntax_tree/yarv/instructions.rb', line 1604

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

#lengthObject



1624
1625
1626
# File 'lib/syntax_tree/yarv/instructions.rb', line 1624

def length
  3
end

#pushesObject



1628
1629
1630
# File 'lib/syntax_tree/yarv/instructions.rb', line 1628

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1611
1612
1613
# File 'lib/syntax_tree/yarv/instructions.rb', line 1611

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