Class: SyntaxTree::YARV::SetConstant

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

Overview

### Summary

‘setconstant` pops two values off the stack: the value to set the constant to and the constant base to set it in.

### Usage

~~~ruby Constant = 1 ~~~

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) ⇒ SetConstant

Returns a new instance of SetConstant.



5015
5016
5017
# File 'lib/syntax_tree/yarv/instructions.rb', line 5015

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5013
5014
5015
# File 'lib/syntax_tree/yarv/instructions.rb', line 5013

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5031
5032
5033
# File 'lib/syntax_tree/yarv/instructions.rb', line 5031

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

#call(vm) ⇒ Object



5043
5044
5045
5046
# File 'lib/syntax_tree/yarv/instructions.rb', line 5043

def call(vm)
  value, parent = vm.pop(2)
  parent.const_set(name, value)
end

#deconstruct_keys(_keys) ⇒ Object



5027
5028
5029
# File 'lib/syntax_tree/yarv/instructions.rb', line 5027

def deconstruct_keys(_keys)
  { name: name }
end

#disasm(fmt) ⇒ Object



5019
5020
5021
# File 'lib/syntax_tree/yarv/instructions.rb', line 5019

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

#lengthObject



5035
5036
5037
# File 'lib/syntax_tree/yarv/instructions.rb', line 5035

def length
  2
end

#popsObject



5039
5040
5041
# File 'lib/syntax_tree/yarv/instructions.rb', line 5039

def pops
  2
end

#to_a(_iseq) ⇒ Object



5023
5024
5025
# File 'lib/syntax_tree/yarv/instructions.rb', line 5023

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