summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-catch/mrblib/catch.rb
blob: c69cd125eef91152fd87717f278dd51c4125c99b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class UncaughtThrowError < ArgumentError
  attr_reader :_tag, :_val
  def initialize(tag, val)
    @_tag = tag
    @_val = val
    super("uncaught throw #{tag.inspect}")
  end
end

module Kernel
  def catch(tag=Object.new, &block)
    # A double closure is required to make the nested `catch` distinguishable
    # and because `break` goes back to `proc->upper`.
    -> { -> { block.call(tag) }.call }.call
  end
  def throw(tag, val=nil)
    __throw(tag, val)
    raise UncaughtThrowError.new(tag, val)
  end

  __preserve_catch_method
end