Class: SyntaxTree::YARV::GetConstant

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

Overview

### Summary

‘getconstant` performs a constant lookup and pushes the value of the constant onto the stack. It pops both the class it should look in and whether or not it should look globally as well.

### Usage

~~~ruby Constant ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(name) ⇒ GetConstant

Returns a new instance of GetConstant.



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

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



1723
1724
1725
# File 'lib/syntax_tree/yarv/instructions.rb', line 1723

def ==(other)
  other.is_a?(GetConstant) && other.name == name
end

#call(vm) ⇒ Object

Raises:

  • (NameError)


1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
# File 'lib/syntax_tree/yarv/instructions.rb', line 1739

def call(vm)
  const_base, allow_nil = vm.pop(2)

  if const_base
    if const_base.const_defined?(name)
      vm.push(const_base.const_get(name))
      return
    end
  elsif const_base.nil? && allow_nil
    vm.frame.nesting.reverse_each do |clazz|
      if clazz.const_defined?(name)
        vm.push(clazz.const_get(name))
        return
      end
    end
  end

  raise NameError, "uninitialized constant #{name}"
end

#deconstruct_keys(_keys) ⇒ Object



1719
1720
1721
# File 'lib/syntax_tree/yarv/instructions.rb', line 1719

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



1711
1712
1713
# File 'lib/syntax_tree/yarv/instructions.rb', line 1711

def disasm(fmt)
  fmt.instruction("getconstant", [fmt.object(name)])
end

#lengthObject



1727
1728
1729
# File 'lib/syntax_tree/yarv/instructions.rb', line 1727

def length
  2
end

#popsObject



1731
1732
1733
# File 'lib/syntax_tree/yarv/instructions.rb', line 1731

def pops
  2
end

#pushesObject



1735
1736
1737
# File 'lib/syntax_tree/yarv/instructions.rb', line 1735

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1715
1716
1717
# File 'lib/syntax_tree/yarv/instructions.rb', line 1715

def to_a(_iseq)
  [:getconstant, name]
end