Project

General

Profile

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

variant adding Fiber#raise only in ext/fiber - 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. You need to <code>require 'fiber'</code>
1557
 *  before using this method.
1558
 *
1559
 *  With no arguments, raises a +RuntimeError+. With a single +String+
1560
 *  argument, raises a +RuntimeError+ with the string as a message.  Otherwise,
1561
 *  the first parameter should be the name of an +Exception+ class (or an
1562
 *  object that returns an +Exception+ object when sent an +exception+
1563
 *  message). The optional second parameter sets the message associated with
1564
 *  the exception, and the third parameter is an array of callback information.
1565
 *  Exceptions are caught by the +rescue+ clause of <code>begin...end</code>
1566
 *  blocks.
1567
 */
1568
static VALUE
1569
rb_fiber_raise(int argc, VALUE *argv, VALUE fib)
1570
{
1571
    VALUE exc = rb_make_exception(argc, argv);
1572
    return rb_fiber_resume(fib, -1, &exc);
1573
}
1574

  
1575
/*
1576
 *  call-seq:
1548 1577
 *     fiber.transfer(args, ...) -> obj
1549 1578
 *
1550 1579
 *  Transfer control to another fiber, resuming it from where it last
......
1684 1713
{
1685 1714
    rb_define_method(rb_cFiber, "transfer", rb_fiber_m_transfer, -1);
1686 1715
    rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0);
1716
    rb_define_method(rb_cFiber, "raise", rb_fiber_raise, -1);
1687 1717
    rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0);
1688 1718
}
1689 1719

  
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
-