Project

General

Profile

Feature #10344 ยป 0001-Implement-Fiber-raise.patch

updated patch with some doc corrections - nome (Knut Franke), 10/18/2014 01:16 PM

View differences:

cont.c
944 944
make_passing_arg(int argc, const VALUE *argv)
945 945
{
946 946
    switch (argc) {
947
      case -1:
948
	return argv[0];
947 949
      case 0:
948 950
	return Qnil;
949 951
      case 1:
......
1545 1547

  
1546 1548
/*
1547 1549
 *  call-seq:
1550
 *     fiber.raise                                 -> obj
1551
 *     fiber.raise(string)                         -> obj
1552
 *     fiber.raise(exception [, string [, array]]) -> obj
1553
 *
1554
 *  Raises an exception in the fiber at the point at which the last
1555
 *  <code>Fiber.yield</code> was called, or at the start if neither +resume+
1556
 *  nor +raise+ were called before.
1557
 *
1558
 *  With no arguments, raises a +RuntimeError+. With a single +String+
1559
 *  argument, raises a +RuntimeError+ with the string as a message.  Otherwise,
1560
 *  the first parameter should be the name of an +Exception+ class (or an
1561
 *  object that returns an +Exception+ object when sent an +exception+
1562
 *  message). The optional second parameter sets the message associated with
1563
 *  the exception, and the third parameter is an array of callback information.
1564
 *  Exceptions are caught by the +rescue+ clause of <code>begin...end</code>
1565
 *  blocks.
1566
 */
1567
static VALUE
1568
rb_fiber_raise(int argc, VALUE *argv, VALUE fib)
1569
{
1570
    VALUE exc = rb_make_exception(argc, argv);
1571
    return rb_fiber_resume(fib, -1, &exc);
1572
}
1573

  
1574
/*
1575
 *  call-seq:
1548 1576
 *     fiber.transfer(args, ...) -> obj
1549 1577
 *
1550 1578
 *  Transfer control to another fiber, resuming it from where it last
......
1664 1692
    rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
1665 1693
    rb_define_method(rb_cFiber, "initialize", rb_fiber_init, 0);
1666 1694
    rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
1695
    rb_define_method(rb_cFiber, "raise", rb_fiber_raise, -1);
1667 1696
}
1668 1697

  
1669 1698
RUBY_SYMBOL_EXPORT_BEGIN
test/ruby/test_fiber.rb
107 107
      }
108 108
      fib.resume
109 109
    }
110
    assert_raise(FiberError){
111
      fib = Fiber.new{}
112
      fib.raise "raise in unborn fiber"
113
    }
114
    assert_raise(FiberError){
115
      fib = Fiber.new{}
116
      fib.resume
117
      fib.raise "raise in dead fiber"
118
    }
110 119
  end
111 120

  
112 121
  def test_return
......
125 134
    }
126 135
  end
127 136

  
137
  def test_raise
138
    assert_raise(ZeroDivisionError){
139
      Fiber.new do
140
        1/0
141
      end.resume
142
    }
143
    assert_raise(RuntimeError){
144
      fib = Fiber.new{ Fiber.yield }
145
      fib.raise "raise and propagate"
146
    }
147
    assert_nothing_raised{
148
      fib = Fiber.new do
149
        begin
150
          Fiber.yield
151
        rescue
152
        end
153
      end
154
      fib.resume
155
      fib.raise "rescue in fiber"
156
    }
157
    fib = Fiber.new do
158
      begin
159
        Fiber.yield
160
      rescue
161
        Fiber.yield :ok
162
      end
163
    end
164
    fib.resume
165
    assert_equal(:ok, fib.raise)
166
  end
167

  
128 168
  def test_transfer
129 169
    ary = []
130 170
    f2 = nil
131
-