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.



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

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



1594
1595
1596
# File 'lib/syntax_tree/yarv/instructions.rb', line 1594

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



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

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

#call(vm) ⇒ Object

Raises:

  • (NameError)


1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
# File 'lib/syntax_tree/yarv/instructions.rb', line 1628

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



1608
1609
1610
# File 'lib/syntax_tree/yarv/instructions.rb', line 1608

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



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

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

#lengthObject



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

def length
  2
end

#popsObject



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

def pops
  2
end

#pushesObject



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

def pushes
  1
end

#to_a(_iseq) ⇒ Object



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

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