Class: SyntaxTree::YARV::AnyToString

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

Overview

### Summary

‘anytostring` ensures that the value on top of the stack is a string.

It pops two values off the stack. If the first value is a string it pushes it back on the stack. If the first value is not a string, it uses Ruby’s built in string coercion to coerce the second value to a string and then pushes that back on the stack.

This is used in conjunction with ‘objtostring` as a fallback for when an object’s ‘to_s` method does not return a string.

### Usage

~~~ruby “#5” ~~~

Instance Method Summary collapse

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #length, #side_effects?

Instance Method Details

#==(other) ⇒ Object



136
137
138
# File 'lib/syntax_tree/yarv/instructions.rb', line 136

def ==(other)
  other.is_a?(AnyToString)
end

#call(vm) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/syntax_tree/yarv/instructions.rb', line 148

def call(vm)
  original, value = vm.pop(2)

  if value.is_a?(String)
    vm.push(value)
  else
    vm.push("#<#{original.class.name}:0000>")
  end
end

#deconstruct_keys(_keys) ⇒ Object



132
133
134
# File 'lib/syntax_tree/yarv/instructions.rb', line 132

def deconstruct_keys(_keys)
  {}
end

#disasm(fmt) ⇒ Object



124
125
126
# File 'lib/syntax_tree/yarv/instructions.rb', line 124

def disasm(fmt)
  fmt.instruction("anytostring")
end

#popsObject



140
141
142
# File 'lib/syntax_tree/yarv/instructions.rb', line 140

def pops
  2
end

#pushesObject



144
145
146
# File 'lib/syntax_tree/yarv/instructions.rb', line 144

def pushes
  1
end

#to_a(_iseq) ⇒ Object



128
129
130
# File 'lib/syntax_tree/yarv/instructions.rb', line 128

def to_a(_iseq)
  [:anytostring]
end