diff options
| author | take_cheeze <[email protected]> | 2014-03-20 04:38:34 +0900 |
|---|---|---|
| committer | take_cheeze <[email protected]> | 2014-03-20 04:38:34 +0900 |
| commit | 272fe3d86489adaef7211f506612c65a343466f9 (patch) | |
| tree | 747bccd8bfbf00b66bc9d4579286b8a28aa925b7 | |
| parent | 07d81a113daeb29f154ad50caadf38e4f7d1ee1f (diff) | |
| download | mruby-272fe3d86489adaef7211f506612c65a343466f9.tar.gz mruby-272fe3d86489adaef7211f506612c65a343466f9.zip | |
use FiberError in fiber exception raise
| -rw-r--r-- | include/mruby.h | 1 | ||||
| -rw-r--r-- | mrbgems/mruby-fiber/src/fiber.c | 22 | ||||
| -rw-r--r-- | mrbgems/mruby-fiber/test/fiber.rb | 12 |
3 files changed, 21 insertions, 14 deletions
diff --git a/include/mruby.h b/include/mruby.h index ca75d2984..3e0e796d2 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -389,6 +389,7 @@ mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c) /* fiber functions (you need to link mruby-fiber mrbgem to use) */ mrb_value mrb_fiber_yield(mrb_state *mrb, int argc, mrb_value *argv); +#define E_FIBER_ERROR (mrb_class_get(mrb, "FiberError")) /* memory pool implementation */ typedef struct mrb_pool mrb_pool; diff --git a/mrbgems/mruby-fiber/src/fiber.c b/mrbgems/mruby-fiber/src/fiber.c index 0a6e0cdd5..7bc157cc5 100644 --- a/mrbgems/mruby-fiber/src/fiber.c +++ b/mrbgems/mruby-fiber/src/fiber.c @@ -36,7 +36,7 @@ * * 1 * 2 - * resuming dead fiber (RuntimeError) + * resuming dead fiber (FiberError) * * The <code>Fiber#resume</code> method accepts an arbitrary number of * parameters, if it is the first call to <code>resume</code> then they @@ -57,7 +57,7 @@ * * 12 * 14 - * resuming dead fiber (RuntimeError) + * resuming dead fiber (FiberError) * */ static mrb_value @@ -73,11 +73,11 @@ fiber_init(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "&", &blk); if (mrb_nil_p(blk)) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Fiber object without a block"); + mrb_raise(mrb, E_FIBER_ERROR, "tried to create Fiber object without a block"); } p = mrb_proc_ptr(blk); if (MRB_PROC_CFUNC_P(p)) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Fiber from C defined method"); + mrb_raise(mrb, E_FIBER_ERROR, "tried to create Fiber from C defined method"); } f->cxt = (struct mrb_context*)mrb_malloc(mrb, sizeof(struct mrb_context)); @@ -120,7 +120,7 @@ fiber_check(mrb_state *mrb, mrb_value fib) mrb_assert(f->tt == MRB_TT_FIBER); if (!f->cxt) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized Fiber"); + mrb_raise(mrb, E_FIBER_ERROR, "uninitialized Fiber"); } return f->cxt; } @@ -161,14 +161,14 @@ fiber_resume(mrb_state *mrb, mrb_value self) for (ci = c->ci; ci >= c->cibase; ci--) { if (ci->acc < 0) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "can't cross C function boundary"); + mrb_raise(mrb, E_FIBER_ERROR, "can't cross C function boundary"); } } if (c->status == MRB_FIBER_RUNNING || c->status == MRB_FIBER_RESUMING) { - mrb_raise(mrb, E_RUNTIME_ERROR, "double resume"); + mrb_raise(mrb, E_FIBER_ERROR, "double resume"); } if (c->status == MRB_FIBER_TERMINATED) { - mrb_raise(mrb, E_RUNTIME_ERROR, "resuming dead fiber"); + mrb_raise(mrb, E_FIBER_ERROR, "resuming dead fiber"); } mrb_get_args(mrb, "*", &a, &len); mrb->c->status = MRB_FIBER_RESUMING; @@ -235,11 +235,11 @@ mrb_fiber_yield(mrb_state *mrb, int len, mrb_value *a) for (ci = c->ci; ci >= c->cibase; ci--) { if (ci->acc < 0) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "can't cross C function boundary"); + mrb_raise(mrb, E_FIBER_ERROR, "can't cross C function boundary"); } } if (!c->prev) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "can't yield from root fiber"); + mrb_raise(mrb, E_FIBER_ERROR, "can't yield from root fiber"); } c->prev->status = MRB_FIBER_RUNNING; @@ -305,6 +305,8 @@ mrb_mruby_fiber_gem_init(mrb_state* mrb) mrb_define_class_method(mrb, c, "yield", fiber_yield, MRB_ARGS_ANY()); mrb_define_class_method(mrb, c, "current", fiber_current, MRB_ARGS_NONE()); + + mrb_define_class(mrb, "FiberError", mrb->eStandardError_class); } void diff --git a/mrbgems/mruby-fiber/test/fiber.rb b/mrbgems/mruby-fiber/test/fiber.rb index c09b49925..9ae4785d7 100644 --- a/mrbgems/mruby-fiber/test/fiber.rb +++ b/mrbgems/mruby-fiber/test/fiber.rb @@ -35,6 +35,10 @@ assert('Fiber.yield') { f.resume(3) } +assert('FiberError') do + assert_equal StandardError, FiberError.superclass +end + assert('Fiber iteration') { f1 = Fiber.new{ [1,2,3].each{|x| Fiber.yield(x)} @@ -80,7 +84,7 @@ assert('Double resume of Fiber') do f1 = Fiber.new {} f2 = Fiber.new { f1.resume - assert_raise(RuntimeError) { f2.resume } + assert_raise(FiberError) { f2.resume } Fiber.yield 0 } assert_equal 0, f2.resume @@ -91,7 +95,7 @@ end assert('Recursive resume of Fiber') do f1, f2 = nil, nil - f1 = Fiber.new { assert_raise(RuntimeError) { f2.resume } } + f1 = Fiber.new { assert_raise(FiberError) { f2.resume } } f2 = Fiber.new { f1.resume Fiber.yield 0 @@ -108,9 +112,9 @@ end assert('Root fiber resume') do root = Fiber.current - assert_raise(RuntimeError) { root.resume } + assert_raise(FiberError) { root.resume } f = Fiber.new { - assert_raise(RuntimeError) { root.resume } + assert_raise(FiberError) { root.resume } } f.resume assert_false f.alive? |
