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.



5566
5567
5568
# File 'lib/syntax_tree/yarv/instructions.rb', line 5566

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5564
5565
5566
# File 'lib/syntax_tree/yarv/instructions.rb', line 5564

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



5582
5583
5584
# File 'lib/syntax_tree/yarv/instructions.rb', line 5582

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

#call(vm) ⇒ Object



5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
# 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) ⇒ Object



5578
5579
5580
# File 'lib/syntax_tree/yarv/instructions.rb', line 5578

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



5570
5571
5572
# File 'lib/syntax_tree/yarv/instructions.rb', line 5570

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

#lengthObject



5586
5587
5588
# File 'lib/syntax_tree/yarv/instructions.rb', line 5586

def length
  2
end

#popsObject



5590
5591
5592
# File 'lib/syntax_tree/yarv/instructions.rb', line 5590

def pops
  1
end

#pushesObject



5594
5595
5596
# File 'lib/syntax_tree/yarv/instructions.rb', line 5594

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5574
5575
5576
# File 'lib/syntax_tree/yarv/instructions.rb', line 5574

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