Class: SyntaxTree::YARV::SetGlobal

Inherits:
Object
  • Object
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

Constructor Details

#initialize(name) ⇒ SetGlobal



5425
5426
5427
# File 'lib/syntax_tree/yarv/instructions.rb', line 5425

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5423
5424
5425
# File 'lib/syntax_tree/yarv/instructions.rb', line 5423

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5441
5442
5443
# File 'lib/syntax_tree/yarv/instructions.rb', line 5441

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

#call(vm) ⇒ Object



5461
5462
5463
5464
5465
# File 'lib/syntax_tree/yarv/instructions.rb', line 5461

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

#canonicalObject



5457
5458
5459
# File 'lib/syntax_tree/yarv/instructions.rb', line 5457

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



5437
5438
5439
# File 'lib/syntax_tree/yarv/instructions.rb', line 5437

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



5429
5430
5431
# File 'lib/syntax_tree/yarv/instructions.rb', line 5429

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

#lengthObject



5445
5446
5447
# File 'lib/syntax_tree/yarv/instructions.rb', line 5445

def length
  2
end

#popsObject



5449
5450
5451
# File 'lib/syntax_tree/yarv/instructions.rb', line 5449

def pops
  1
end

#pushesObject



5453
5454
5455
# File 'lib/syntax_tree/yarv/instructions.rb', line 5453

def pushes
  0
end

#to_a(_iseq) ⇒ Object



5433
5434
5435
# File 'lib/syntax_tree/yarv/instructions.rb', line 5433

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