mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
use FiberError in fiber exception raise
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user