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.



5554
5555
5556
# File 'lib/syntax_tree/yarv/instructions.rb', line 5554

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5552
5553
5554
# File 'lib/syntax_tree/yarv/instructions.rb', line 5552

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



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

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

#call(vm) ⇒ Object



5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
# File 'lib/syntax_tree/yarv/instructions.rb', line 5586

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



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

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



5558
5559
5560
# File 'lib/syntax_tree/yarv/instructions.rb', line 5558

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

#lengthObject



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

def length
  2
end

#popsObject



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

def pops
  1
end

#pushesObject



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

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5562
5563
5564
# File 'lib/syntax_tree/yarv/instructions.rb', line 5562

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