Class: SyntaxTree::YARV::GetGlobal

Inherits:
Instruction show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

‘getglobal` pushes the value of a global variables onto the stack.

### Usage

~~~ruby $$ ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #pops, #side_effects?

Constructor Details

#initialize(name) ⇒ GetGlobal

Returns a new instance of GetGlobal.



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

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



1660
1661
1662
# File 'lib/syntax_tree/yarv/instructions.rb', line 1660

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



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

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

#call(vm) ⇒ Object



1690
1691
1692
1693
1694
# File 'lib/syntax_tree/yarv/instructions.rb', line 1690

def call(vm)
  # Evaluating the name of the global variable because there isn't a
  # reflection API for global variables.
  vm.push(eval(name.to_s, binding, __FILE__, __LINE__))
end

#deconstruct_keys(_keys) ⇒ Object



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

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



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

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

#lengthObject



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

def length
  2
end

#pushesObject



1686
1687
1688
# File 'lib/syntax_tree/yarv/instructions.rb', line 1686

def pushes
  1
end

#to_a(_iseq) ⇒ Object



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

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