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.



5063
5064
5065
# File 'lib/syntax_tree/yarv/instructions.rb', line 5063

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5061
5062
5063
# File 'lib/syntax_tree/yarv/instructions.rb', line 5061

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5079
5080
5081
# File 'lib/syntax_tree/yarv/instructions.rb', line 5079

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

#call(vm) ⇒ Object



5091
5092
5093
5094
5095
# File 'lib/syntax_tree/yarv/instructions.rb', line 5091

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



5075
5076
5077
# File 'lib/syntax_tree/yarv/instructions.rb', line 5075

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



5067
5068
5069
# File 'lib/syntax_tree/yarv/instructions.rb', line 5067

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

#lengthObject



5083
5084
5085
# File 'lib/syntax_tree/yarv/instructions.rb', line 5083

def length
  2
end

#popsObject



5087
5088
5089
# File 'lib/syntax_tree/yarv/instructions.rb', line 5087

def pops
  1
end

#to_a(_iseq) ⇒ Object



5071
5072
5073
# File 'lib/syntax_tree/yarv/instructions.rb', line 5071

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