Class: SyntaxTree::YARV::Throw

Inherits:
Object
  • Object
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

Constructor Details

#initialize(type) ⇒ Throw



5984
5985
5986
# File 'lib/syntax_tree/yarv/instructions.rb', line 5984

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5982
5983
5984
# File 'lib/syntax_tree/yarv/instructions.rb', line 5982

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



6000
6001
6002
# File 'lib/syntax_tree/yarv/instructions.rb', line 6000

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

#call(vm) ⇒ Object



6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
# File 'lib/syntax_tree/yarv/instructions.rb', line 6020

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

#canonicalObject



6016
6017
6018
# File 'lib/syntax_tree/yarv/instructions.rb', line 6016

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



5996
5997
5998
# File 'lib/syntax_tree/yarv/instructions.rb', line 5996

def deconstruct_keys(_keys)
  { type: type }
end

#disasm(fmt) ⇒ Object



5988
5989
5990
# File 'lib/syntax_tree/yarv/instructions.rb', line 5988

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

#lengthObject



6004
6005
6006
# File 'lib/syntax_tree/yarv/instructions.rb', line 6004

def length
  2
end

#popsObject



6008
6009
6010
# File 'lib/syntax_tree/yarv/instructions.rb', line 6008

def pops
  1
end

#pushesObject



6012
6013
6014
# File 'lib/syntax_tree/yarv/instructions.rb', line 6012

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5992
5993
5994
# File 'lib/syntax_tree/yarv/instructions.rb', line 5992

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