Class: SyntaxTree::YARV::SetGlobal

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

Overview

### Summary

‘setglobal` sets the value of a global variable to a value popped off the top of the stack.

### Usage

~~~ruby $global = 5 ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(name) ⇒ SetGlobal

Returns a new instance of SetGlobal.



5109
5110
5111
# File 'lib/syntax_tree/yarv/instructions.rb', line 5109

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5107
5108
5109
# File 'lib/syntax_tree/yarv/instructions.rb', line 5107

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5125
5126
5127
# File 'lib/syntax_tree/yarv/instructions.rb', line 5125

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

#call(vm) ⇒ Object



5137
5138
5139
5140
5141
# File 'lib/syntax_tree/yarv/instructions.rb', line 5137

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

#deconstruct_keys(_keys) ⇒ Object



5121
5122
5123
# File 'lib/syntax_tree/yarv/instructions.rb', line 5121

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



5113
5114
5115
# File 'lib/syntax_tree/yarv/instructions.rb', line 5113

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

#lengthObject



5129
5130
5131
# File 'lib/syntax_tree/yarv/instructions.rb', line 5129

def length
  2
end

#popsObject



5133
5134
5135
# File 'lib/syntax_tree/yarv/instructions.rb', line 5133

def pops
  1
end

#to_a(_iseq) ⇒ Object



5117
5118
5119
# File 'lib/syntax_tree/yarv/instructions.rb', line 5117

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