summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-catch
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2020-10-15 18:31:06 +0900
committerGitHub <[email protected]>2020-10-15 18:31:06 +0900
commit9cebddf9fe83ae0acde6f64f291fa3c9fc22880f (patch)
tree6f9ca4f2941c3da48a504c937719adca36e4cdfe /mrbgems/mruby-catch
parent8c276f95be2f4e9deed73f08125a23a6746cb517 (diff)
parent21e07d61138a87891dc780efaa28e6c76a39378f (diff)
downloadmruby-9cebddf9fe83ae0acde6f64f291fa3c9fc22880f.tar.gz
mruby-9cebddf9fe83ae0acde6f64f291fa3c9fc22880f.zip
Merge pull request #5084 from mruby/mruby3
Mruby3
Diffstat (limited to 'mrbgems/mruby-catch')
-rw-r--r--mrbgems/mruby-catch/mrbgem.rake5
-rw-r--r--mrbgems/mruby-catch/mrblib/catch.rb27
2 files changed, 32 insertions, 0 deletions
diff --git a/mrbgems/mruby-catch/mrbgem.rake b/mrbgems/mruby-catch/mrbgem.rake
new file mode 100644
index 000000000..c714d443d
--- /dev/null
+++ b/mrbgems/mruby-catch/mrbgem.rake
@@ -0,0 +1,5 @@
+MRuby::Gem::Specification.new('mruby-catch') do |spec|
+ spec.license = 'MIT'
+ spec.author = 'mruby developers'
+ spec.summary = 'Catch / Throw non-local Jump'
+end
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