summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-catch/mrblib/catch.rb
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-catch/mrblib/catch.rb')
-rw-r--r--mrbgems/mruby-catch/mrblib/catch.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/mrbgems/mruby-catch/mrblib/catch.rb b/mrbgems/mruby-catch/mrblib/catch.rb
new file mode 100644
index 000000000..89eedf66a
--- /dev/null
+++ b/mrbgems/mruby-catch/mrblib/catch.rb
@@ -0,0 +1,27 @@
+class ThrowCatchJump < Exception
+ def initialize(tag, val)
+ @tag = tag
+ @val = val
+ super("uncaught throw :#{tag}")
+ end
+ def _tag
+ @tag
+ end
+ def _val
+ @val
+ end
+end
+
+module Kernel
+ def catch(tag, &block)
+ block.call(tag)
+ rescue ThrowCatchJump => e
+ unless e._tag == tag
+ raise e
+ end
+ return e._val
+ end
+ def throw(tag, val=nil)
+ raise ThrowCatchJump.new(tag, val)
+ end
+end