class SyntaxTree::YARV::Throw

### Summary

‘throw` pops a value off the top of the stack and throws it. It is caught using the instruction sequence’s (or an ancestor’s) catch table. It pushes on the result of throwing the value.

### Usage

~~~ruby [1, 2, 3].map { break 2 } ~~~

Constants

RUBY_TAG_BREAK
RUBY_TAG_FATAL
RUBY_TAG_NEXT
RUBY_TAG_NONE
RUBY_TAG_RAISE
RUBY_TAG_REDO
RUBY_TAG_RETRY
RUBY_TAG_RETURN
RUBY_TAG_THROW
VM_THROW_NO_ESCAPE_FLAG
VM_THROW_STATE_MASK

Attributes

type[R]

Public Class Methods

new(type) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5566
def initialize(type)
  @type = type
end

Public Instance Methods

==(other) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5582
def ==(other)
  other.is_a?(Throw) && other.type == type
end
call(vm) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5598
def call(vm)
  state = type & VM_THROW_STATE_MASK
  value = vm.pop

  case state
  when RUBY_TAG_NONE
    case value
    when nil
      # do nothing
    when Exception
      raise value
    else
      raise NotImplementedError
    end
  when RUBY_TAG_RETURN
    raise VM::ReturnError.new(value, error_backtrace(vm))
  when RUBY_TAG_BREAK
    raise VM::BreakError.new(value, error_backtrace(vm))
  when RUBY_TAG_NEXT
    raise VM::NextError.new(value, error_backtrace(vm))
  else
    raise NotImplementedError, "Unknown throw kind #{state}"
  end
end
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5578
def deconstruct_keys(_keys)
  { type: type }
end
disasm(fmt) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5570
def disasm(fmt)
  fmt.instruction("throw", [fmt.object(type)])
end
length() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5586
def length
  2
end
pops() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5590
def pops
  1
end
pushes() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5594
def pushes
  1
end
to_a(_iseq) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 5574
def to_a(_iseq)
  [:throw, type]
end