Commit 272fe3d8 authored by take_cheeze's avatar take_cheeze

use FiberError in fiber exception raise

parent 07d81a11
...@@ -389,6 +389,7 @@ mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c) ...@@ -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) */ /* fiber functions (you need to link mruby-fiber mrbgem to use) */
mrb_value mrb_fiber_yield(mrb_state *mrb, int argc, mrb_value *argv); 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 */ /* memory pool implementation */
typedef struct mrb_pool mrb_pool; typedef struct mrb_pool mrb_pool;
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
* *
* 1 * 1
* 2 * 2
* resuming dead fiber (RuntimeError) * resuming dead fiber (FiberError)
* *
* The <code>Fiber#resume</code> method accepts an arbitrary number of * The <code>Fiber#resume</code> method accepts an arbitrary number of
* parameters, if it is the first call to <code>resume</code> then they * parameters, if it is the first call to <code>resume</code> then they
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
* *
* 12 * 12
* 14 * 14
* resuming dead fiber (RuntimeError) * resuming dead fiber (FiberError)
* *
*/ */
static mrb_value static mrb_value
...@@ -73,11 +73,11 @@ fiber_init(mrb_state *mrb, mrb_value self) ...@@ -73,11 +73,11 @@ fiber_init(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "&", &blk); mrb_get_args(mrb, "&", &blk);
if (mrb_nil_p(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); p = mrb_proc_ptr(blk);
if (MRB_PROC_CFUNC_P(p)) { 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)); f->cxt = (struct mrb_context*)mrb_malloc(mrb, sizeof(struct mrb_context));
...@@ -120,7 +120,7 @@ fiber_check(mrb_state *mrb, mrb_value fib) ...@@ -120,7 +120,7 @@ fiber_check(mrb_state *mrb, mrb_value fib)
mrb_assert(f->tt == MRB_TT_FIBER); mrb_assert(f->tt == MRB_TT_FIBER);
if (!f->cxt) { if (!f->cxt) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized Fiber"); mrb_raise(mrb, E_FIBER_ERROR, "uninitialized Fiber");
} }
return f->cxt; return f->cxt;
} }
...@@ -161,14 +161,14 @@ fiber_resume(mrb_state *mrb, mrb_value self) ...@@ -161,14 +161,14 @@ fiber_resume(mrb_state *mrb, mrb_value self)
for (ci = c->ci; ci >= c->cibase; ci--) { for (ci = c->ci; ci >= c->cibase; ci--) {
if (ci->acc < 0) { 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) { 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) { 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_get_args(mrb, "*", &a, &len);
mrb->c->status = MRB_FIBER_RESUMING; mrb->c->status = MRB_FIBER_RESUMING;
...@@ -235,11 +235,11 @@ mrb_fiber_yield(mrb_state *mrb, int len, mrb_value *a) ...@@ -235,11 +235,11 @@ mrb_fiber_yield(mrb_state *mrb, int len, mrb_value *a)
for (ci = c->ci; ci >= c->cibase; ci--) { for (ci = c->ci; ci >= c->cibase; ci--) {
if (ci->acc < 0) { 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) { 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; c->prev->status = MRB_FIBER_RUNNING;
...@@ -305,6 +305,8 @@ mrb_mruby_fiber_gem_init(mrb_state* mrb) ...@@ -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, "yield", fiber_yield, MRB_ARGS_ANY());
mrb_define_class_method(mrb, c, "current", fiber_current, MRB_ARGS_NONE()); mrb_define_class_method(mrb, c, "current", fiber_current, MRB_ARGS_NONE());
mrb_define_class(mrb, "FiberError", mrb->eStandardError_class);
} }
void void
......
...@@ -35,6 +35,10 @@ assert('Fiber.yield') { ...@@ -35,6 +35,10 @@ assert('Fiber.yield') {
f.resume(3) f.resume(3)
} }
assert('FiberError') do
assert_equal StandardError, FiberError.superclass
end
assert('Fiber iteration') { assert('Fiber iteration') {
f1 = Fiber.new{ f1 = Fiber.new{
[1,2,3].each{|x| Fiber.yield(x)} [1,2,3].each{|x| Fiber.yield(x)}
...@@ -80,7 +84,7 @@ assert('Double resume of Fiber') do ...@@ -80,7 +84,7 @@ assert('Double resume of Fiber') do
f1 = Fiber.new {} f1 = Fiber.new {}
f2 = Fiber.new { f2 = Fiber.new {
f1.resume f1.resume
assert_raise(RuntimeError) { f2.resume } assert_raise(FiberError) { f2.resume }
Fiber.yield 0 Fiber.yield 0
} }
assert_equal 0, f2.resume assert_equal 0, f2.resume
...@@ -91,7 +95,7 @@ end ...@@ -91,7 +95,7 @@ end
assert('Recursive resume of Fiber') do assert('Recursive resume of Fiber') do
f1, f2 = nil, nil f1, f2 = nil, nil
f1 = Fiber.new { assert_raise(RuntimeError) { f2.resume } } f1 = Fiber.new { assert_raise(FiberError) { f2.resume } }
f2 = Fiber.new { f2 = Fiber.new {
f1.resume f1.resume
Fiber.yield 0 Fiber.yield 0
...@@ -108,9 +112,9 @@ end ...@@ -108,9 +112,9 @@ end
assert('Root fiber resume') do assert('Root fiber resume') do
root = Fiber.current root = Fiber.current
assert_raise(RuntimeError) { root.resume } assert_raise(FiberError) { root.resume }
f = Fiber.new { f = Fiber.new {
assert_raise(RuntimeError) { root.resume } assert_raise(FiberError) { root.resume }
} }
f.resume f.resume
assert_false f.alive? assert_false f.alive?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment