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.



1654
1655
1656
# File 'lib/syntax_tree/yarv/instructions.rb', line 1654

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



1652
1653
1654
# File 'lib/syntax_tree/yarv/instructions.rb', line 1652

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



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

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

#call(vm) ⇒ Object

Raises:

  • (NameError)


1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
# 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) ⇒ Object



1666
1667
1668
# File 'lib/syntax_tree/yarv/instructions.rb', line 1666

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



1658
1659
1660
# File 'lib/syntax_tree/yarv/instructions.rb', line 1658

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

#lengthObject



1674
1675
1676
# File 'lib/syntax_tree/yarv/instructions.rb', line 1674

def length
  2
end

#popsObject



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

def pops
  2
end

#pushesObject



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

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1662
1663
1664
# File 'lib/syntax_tree/yarv/instructions.rb', line 1662

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