From f65a34222462fbb4be8e79a2d79ade3eeb364749 Mon Sep 17 00:00:00 2001 From: Knut Franke Date: Thu, 9 Oct 2014 01:03:42 +0200 Subject: [PATCH] Implement Fiber#raise in ext/fiber This allows raising exceptions in another fiber, similarly to Thread#raise. --- cont.c | 30 ++++++++++++++++++++++++++++++ test/ruby/test_fiber.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/cont.c b/cont.c index 08acf40..2b27aab 100644 --- a/cont.c +++ b/cont.c @@ -944,6 +944,8 @@ static VALUE make_passing_arg(int argc, const VALUE *argv) { switch (argc) { + case -1: + return argv[0]; case 0: return Qnil; case 1: @@ -1545,6 +1547,33 @@ rb_fiber_m_resume(int argc, VALUE *argv, VALUE fib) /* * call-seq: + * fiber.raise -> obj + * fiber.raise(string) -> obj + * fiber.raise(exception [, string [, array]]) -> obj + * + * Raises an exception in the fiber at the point at which the last + * Fiber.yield was called, or at the start if neither +resume+ + * nor +raise+ were called before. You need to require 'fiber' + * before using this method. + * + * With no arguments, raises a +RuntimeError+. With a single +String+ + * argument, raises a +RuntimeError+ with the string as a message. Otherwise, + * the first parameter should be the name of an +Exception+ class (or an + * object that returns an +Exception+ object when sent an +exception+ + * message). The optional second parameter sets the message associated with + * the exception, and the third parameter is an array of callback information. + * Exceptions are caught by the +rescue+ clause of begin...end + * blocks. + */ +static VALUE +rb_fiber_raise(int argc, VALUE *argv, VALUE fib) +{ + VALUE exc = rb_make_exception(argc, argv); + return rb_fiber_resume(fib, -1, &exc); +} + +/* + * call-seq: * fiber.transfer(args, ...) -> obj * * Transfer control to another fiber, resuming it from where it last @@ -1684,6 +1713,7 @@ ruby_Init_Fiber_as_Coroutine(void) { rb_define_method(rb_cFiber, "transfer", rb_fiber_m_transfer, -1); rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0); + rb_define_method(rb_cFiber, "raise", rb_fiber_raise, -1); rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0); } diff --git a/test/ruby/test_fiber.rb b/test/ruby/test_fiber.rb index ed071ac..d8bae1f 100644 --- a/test/ruby/test_fiber.rb +++ b/test/ruby/test_fiber.rb @@ -107,6 +107,15 @@ class TestFiber < Test::Unit::TestCase } fib.resume } + assert_raise(FiberError){ + fib = Fiber.new{} + fib.raise "raise in unborn fiber" + } + assert_raise(FiberError){ + fib = Fiber.new{} + fib.resume + fib.raise "raise in dead fiber" + } end def test_return @@ -125,6 +134,37 @@ class TestFiber < Test::Unit::TestCase } end + def test_raise + assert_raise(ZeroDivisionError){ + Fiber.new do + 1/0 + end.resume + } + assert_raise(RuntimeError){ + fib = Fiber.new{ Fiber.yield } + fib.raise "raise and propagate" + } + assert_nothing_raised{ + fib = Fiber.new do + begin + Fiber.yield + rescue + end + end + fib.resume + fib.raise "rescue in fiber" + } + fib = Fiber.new do + begin + Fiber.yield + rescue + Fiber.yield :ok + end + end + fib.resume + assert_equal(:ok, fib.raise) + end + def test_transfer ary = [] f2 = nil -- 1.9.1