Class: SyntaxTree::YARV::SetLocal

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

Overview

### Summary

setlocal sets the value of a local variable on a frame determined by the level and index arguments. The level is the number of frames back to look and the index is the index in the local table. It pops the value it is setting off the stack.

### Usage

~~~ruby value = 5 tap { tap { value = 10 } } ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, level) ⇒ SetLocal



5551
5552
5553
5554
# File 'lib/syntax_tree/yarv/instructions.rb', line 5551

def initialize(index, level)
  @index = index
  @level = level
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



5549
5550
5551
# File 'lib/syntax_tree/yarv/instructions.rb', line 5549

def index
  @index
end

#levelObject (readonly)

Returns the value of attribute level.



5549
5550
5551
# File 'lib/syntax_tree/yarv/instructions.rb', line 5549

def level
  @level
end

Instance Method Details

#==(other) ⇒ Object



5570
5571
5572
# File 'lib/syntax_tree/yarv/instructions.rb', line 5570

def ==(other)
  other.is_a?(SetLocal) && other.index == index && other.level == level
end

#call(vm) ⇒ Object



5590
5591
5592
# File 'lib/syntax_tree/yarv/instructions.rb', line 5590

def call(vm)
  vm.local_set(index, level, vm.pop)
end

#canonicalObject



5586
5587
5588
# File 'lib/syntax_tree/yarv/instructions.rb', line 5586

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



5566
5567
5568
# File 'lib/syntax_tree/yarv/instructions.rb', line 5566

def deconstruct_keys(_keys)
  { index: index, level: level }
end

#disasm(fmt) ⇒ Object



5556
5557
5558
# File 'lib/syntax_tree/yarv/instructions.rb', line 5556

def disasm(fmt)
  fmt.instruction("setlocal", [fmt.local(index, explicit: level)])
end

#lengthObject



5574
5575
5576
# File 'lib/syntax_tree/yarv/instructions.rb', line 5574

def length
  3
end

#popsObject



5578
5579
5580
# File 'lib/syntax_tree/yarv/instructions.rb', line 5578

def pops
  1
end

#pushesObject



5582
5583
5584
# File 'lib/syntax_tree/yarv/instructions.rb', line 5582

def pushes
  0
end

#to_a(iseq) ⇒ Object



5560
5561
5562
5563
5564
# File 'lib/syntax_tree/yarv/instructions.rb', line 5560

def to_a(iseq)
  current = iseq
  level.times { current = current.parent_iseq }
  [:setlocal, current.local_table.offset(index), level]
end