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



1548
1549
1550
# File 'lib/syntax_tree/yarv/instructions.rb', line 1548

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



1546
1547
1548
# File 'lib/syntax_tree/yarv/instructions.rb', line 1546

def name
  @name
end

Instance Method Details

#call(vm) ⇒ Object

Raises:

  • (NameError)


1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
# File 'lib/syntax_tree/yarv/instructions.rb', line 1576

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



1572
1573
1574
# File 'lib/syntax_tree/yarv/instructions.rb', line 1572

def canonical
  self
end

#disasm(fmt) ⇒ Object



1552
1553
1554
# File 'lib/syntax_tree/yarv/instructions.rb', line 1552

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

#lengthObject



1560
1561
1562
# File 'lib/syntax_tree/yarv/instructions.rb', line 1560

def length
  2
end

#popsObject



1564
1565
1566
# File 'lib/syntax_tree/yarv/instructions.rb', line 1564

def pops
  2
end

#pushesObject



1568
1569
1570
# File 'lib/syntax_tree/yarv/instructions.rb', line 1568

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1556
1557
1558
# File 'lib/syntax_tree/yarv/instructions.rb', line 1556

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