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.



5210
5211
5212
# File 'lib/syntax_tree/yarv/instructions.rb', line 5210

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5208
5209
5210
# File 'lib/syntax_tree/yarv/instructions.rb', line 5208

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5226
5227
5228
# File 'lib/syntax_tree/yarv/instructions.rb', line 5226

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

#call(vm) ⇒ Object



5238
5239
5240
5241
5242
# File 'lib/syntax_tree/yarv/instructions.rb', line 5238

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



5222
5223
5224
# File 'lib/syntax_tree/yarv/instructions.rb', line 5222

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



5214
5215
5216
# File 'lib/syntax_tree/yarv/instructions.rb', line 5214

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

#lengthObject



5230
5231
5232
# File 'lib/syntax_tree/yarv/instructions.rb', line 5230

def length
  2
end

#popsObject



5234
5235
5236
# File 'lib/syntax_tree/yarv/instructions.rb', line 5234

def pops
  1
end

#to_a(_iseq) ⇒ Object



5218
5219
5220
# File 'lib/syntax_tree/yarv/instructions.rb', line 5218

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