Class: SyntaxTree::YARV::Throw

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

Overview

### 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 } ~~~

Constant Summary collapse

RUBY_TAG_NONE =
0x0
RUBY_TAG_RETURN =
0x1
RUBY_TAG_BREAK =
0x2
RUBY_TAG_NEXT =
0x3
RUBY_TAG_RETRY =
0x4
RUBY_TAG_REDO =
0x5
RUBY_TAG_RAISE =
0x6
RUBY_TAG_THROW =
0x7
RUBY_TAG_FATAL =
0x8
VM_THROW_NO_ESCAPE_FLAG =
0x8000
VM_THROW_STATE_MASK =
0xff

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(type) ⇒ Throw

Returns a new instance of Throw.



5713
5714
5715
# File 'lib/syntax_tree/yarv/instructions.rb', line 5713

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5711
5712
5713
# File 'lib/syntax_tree/yarv/instructions.rb', line 5711

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



5729
5730
5731
# File 'lib/syntax_tree/yarv/instructions.rb', line 5729

def ==(other)
  other.is_a?(Throw) && other.type == type
end

#call(vm) ⇒ Object



5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
# File 'lib/syntax_tree/yarv/instructions.rb', line 5745

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



5725
5726
5727
# File 'lib/syntax_tree/yarv/instructions.rb', line 5725

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



5717
5718
5719
# File 'lib/syntax_tree/yarv/instructions.rb', line 5717

def disasm(fmt)
  fmt.instruction("throw", [fmt.object(type)])
end

#lengthObject



5733
5734
5735
# File 'lib/syntax_tree/yarv/instructions.rb', line 5733

def length
  2
end

#popsObject



5737
5738
5739
# File 'lib/syntax_tree/yarv/instructions.rb', line 5737

def pops
  1
end

#pushesObject



5741
5742
5743
# File 'lib/syntax_tree/yarv/instructions.rb', line 5741

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5721
5722
5723
# File 'lib/syntax_tree/yarv/instructions.rb', line 5721

def to_a(_iseq)
  [:throw, type]
end