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.



5612
5613
5614
# File 'lib/syntax_tree/yarv/instructions.rb', line 5612

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5610
5611
5612
# File 'lib/syntax_tree/yarv/instructions.rb', line 5610

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



5628
5629
5630
# File 'lib/syntax_tree/yarv/instructions.rb', line 5628

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

#call(vm) ⇒ Object



5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
# File 'lib/syntax_tree/yarv/instructions.rb', line 5644

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



5624
5625
5626
# File 'lib/syntax_tree/yarv/instructions.rb', line 5624

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



5616
5617
5618
# File 'lib/syntax_tree/yarv/instructions.rb', line 5616

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

#lengthObject



5632
5633
5634
# File 'lib/syntax_tree/yarv/instructions.rb', line 5632

def length
  2
end

#popsObject



5636
5637
5638
# File 'lib/syntax_tree/yarv/instructions.rb', line 5636

def pops
  1
end

#pushesObject



5640
5641
5642
# File 'lib/syntax_tree/yarv/instructions.rb', line 5640

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5620
5621
5622
# File 'lib/syntax_tree/yarv/instructions.rb', line 5620

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