Class: SyntaxTree::YARV::GetConstant

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

Constructor Details

#initialize(name) ⇒ GetConstant



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

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



1750
1751
1752
# File 'lib/syntax_tree/yarv/instructions.rb', line 1750

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

#call(vm) ⇒ Object

Raises:

  • (NameError)


1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
# File 'lib/syntax_tree/yarv/instructions.rb', line 1770

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

#canonicalObject



1766
1767
1768
# File 'lib/syntax_tree/yarv/instructions.rb', line 1766

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



1746
1747
1748
# File 'lib/syntax_tree/yarv/instructions.rb', line 1746

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



1738
1739
1740
# File 'lib/syntax_tree/yarv/instructions.rb', line 1738

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

#lengthObject



1754
1755
1756
# File 'lib/syntax_tree/yarv/instructions.rb', line 1754

def length
  2
end

#popsObject



1758
1759
1760
# File 'lib/syntax_tree/yarv/instructions.rb', line 1758

def pops
  2
end

#pushesObject



1762
1763
1764
# File 'lib/syntax_tree/yarv/instructions.rb', line 1762

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1742
1743
1744
# File 'lib/syntax_tree/yarv/instructions.rb', line 1742

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