class SyntaxTree::YARV::GetConstant

### 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 ~~~

Attributes

name[R]

Public Class Methods

new(name) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1654
def initialize(name)
  @name = name
end

Public Instance Methods

==(other) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1670
def ==(other)
  other.is_a?(GetConstant) && other.name == name
end
call(vm) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1686
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) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1666
def deconstruct_keys(_keys)
  { name: name }
end
disasm(fmt) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1658
def disasm(fmt)
  fmt.instruction("getconstant", [fmt.object(name)])
end
length() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1674
def length
  2
end
pops() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1678
def pops
  2
end
pushes() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1682
def pushes
  1
end
to_a(_iseq) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 1662
def to_a(_iseq)
  [:getconstant, name]
end