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

Returns a new instance of Throw.



5209
5210
5211
# File 'lib/syntax_tree/yarv/instructions.rb', line 5209

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5207
5208
5209
# File 'lib/syntax_tree/yarv/instructions.rb', line 5207

def type
  @type
end

Instance Method Details

#call(vm) ⇒ Object



5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
# File 'lib/syntax_tree/yarv/instructions.rb', line 5237

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



5233
5234
5235
# File 'lib/syntax_tree/yarv/instructions.rb', line 5233

def canonical
  self
end

#disasm(fmt) ⇒ Object



5213
5214
5215
# File 'lib/syntax_tree/yarv/instructions.rb', line 5213

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

#lengthObject



5221
5222
5223
# File 'lib/syntax_tree/yarv/instructions.rb', line 5221

def length
  2
end

#popsObject



5225
5226
5227
# File 'lib/syntax_tree/yarv/instructions.rb', line 5225

def pops
  1
end

#pushesObject



5229
5230
5231
# File 'lib/syntax_tree/yarv/instructions.rb', line 5229

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5217
5218
5219
# File 'lib/syntax_tree/yarv/instructions.rb', line 5217

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