diff --git a/ChangeLog b/ChangeLog index 0c841dea26..e49eba878f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15,6 +15,16 @@ Fri Aug 24 17:06:56 2007 Yukihiro Matsumoto * eval.c (rb_f_send_bang): abandon the name funcall for private aware method call. +Fri Aug 24 15:27:12 2007 Yukihiro Matsumoto + + * enumerator.c (enumerator_next): stop pre-fetching. + + * enumerator.c (Init_Enumerator): remove next? method. + +Fri Aug 24 15:14:57 2007 Yukihiro Matsumoto + + * eval.c (rb_f_loop): now handles StopIteration exception. + Thu Aug 23 20:31:31 2007 Koichi Sasada * compile.c: optimize simple massign. diff --git a/enumerator.c b/enumerator.c index 05405649b6..019179f55b 100644 --- a/enumerator.c +++ b/enumerator.c @@ -44,9 +44,8 @@ struct enumerator { VALUE args; enum_iter *iter; VALUE fib; - VALUE next; VALUE dst; - VALUE has_next; + VALUE no_next; }; static void @@ -57,7 +56,6 @@ enumerator_mark(void *p) rb_gc_mark(ptr->proc); rb_gc_mark(ptr->args); rb_gc_mark(ptr->fib); - rb_gc_mark(ptr->next); rb_gc_mark(ptr->dst); } @@ -241,8 +239,8 @@ enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv) } if (argc) ptr->args = rb_ary_new4(argc, argv); ptr->fib = 0; - ptr->next = ptr->dst = Qnil; - ptr->has_next = Qnil; + ptr->dst = Qnil; + ptr->no_next = Qfalse; return enum_obj; } @@ -373,13 +371,7 @@ static VALUE next_ii(VALUE i, VALUE obj) { struct enumerator *e = enumerator_ptr(obj); - VALUE tmp = e->next; - - e->next = i; - tmp = rb_fiber_yield(1, &tmp); - if (tmp != Qnil) { - e->dst = tmp; - } + rb_fiber_yield(1, &i); return Qnil; } @@ -387,11 +379,11 @@ static VALUE next_i(VALUE curr, VALUE obj) { struct enumerator *e = enumerator_ptr(obj); - e->dst = curr; + VALUE nil = Qnil; rb_block_call(obj, rb_intern("each"), 0, 0, next_ii, obj); - e->has_next = Qfalse; - return rb_fiber_yield(1, &e->next); + e->no_next = Qtrue; + return rb_fiber_yield(1, &nil); } static void @@ -400,8 +392,6 @@ next_init(VALUE obj, struct enumerator *e) VALUE curr = rb_fiber_current(); e->dst = curr; e->fib = rb_block_call(rb_cFiber, rb_intern("new"), 0, 0, next_i, obj); - e->has_next = Qtrue; - rb_fiber_resume(e->fib, 1, &curr); } /* @@ -429,33 +419,16 @@ enumerator_next(VALUE obj) next_init(obj, e); } - if (!e->has_next) { + v = rb_fiber_resume(e->fib, 1, &curr); + if (e->no_next) { e->fib = 0; - e->next = e->dst = Qnil; + e->dst = Qnil; + e->no_next = Qfalse; rb_raise(rb_eStopIteration, "Enumerator#each reached at end"); } - - v = rb_fiber_resume(e->fib, 1, &curr); return v; } -/* - * call-seq: - * e.next? => bool - * - * Returns true if this enumerator object has not reached the end yet. - */ - -static VALUE -enumerator_next_p(VALUE obj) -{ - struct enumerator *e = enumerator_ptr(obj); - if (!e->fib) { - next_init(obj, e); - } - return e->has_next; -} - /* * call-seq: * e.next? => e @@ -469,7 +442,8 @@ enumerator_rewind(VALUE obj) struct enumerator *e = enumerator_ptr(obj); e->fib = 0; - e->next = e->dst = Qnil; + e->dst = Qnil; + e->no_next = Qfalse; return obj; } @@ -492,7 +466,6 @@ Init_Enumerator(void) rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0); rb_define_method(rb_cEnumerator, "to_splat", enumerator_to_splat, 0); rb_define_method(rb_cEnumerator, "next", enumerator_next, 0); - rb_define_method(rb_cEnumerator, "next?", enumerator_next_p, 0); rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0); rb_eStopIteration = rb_define_class("StopIteration", rb_eIndexError); diff --git a/eval.c b/eval.c index 3780c19aa8..721ac6302c 100644 --- a/eval.c +++ b/eval.c @@ -951,6 +951,14 @@ rb_yield_splat(VALUE values) return v; } +static VALUE +loop_i() +{ + for (;;) { + rb_yield_0(0, 0); + } +} + /* * call-seq: * loop {|| block } @@ -968,9 +976,7 @@ rb_yield_splat(VALUE values) static VALUE rb_f_loop(void) { - for (;;) { - rb_yield_0(0, 0); - } + rb_rescue2(loop_i, (VALUE)0, 0, 0, rb_eStopIteration, (VALUE)0); return Qnil; /* dummy */ } diff --git a/test/ruby/test_iterator.rb b/test/ruby/test_iterator.rb index 45f1452629..9d99731a3c 100644 --- a/test/ruby/test_iterator.rb +++ b/test/ruby/test_iterator.rb @@ -496,13 +496,15 @@ class TestIterator < Test::Unit::TestCase e = [1,2,3].each assert_equal(1, e.next) - assert_equal(true, e.next?) assert_equal(2, e.next) assert_equal(3, e.next) assert_raises(StopIteration){e.next} e.rewind - assert_equal(true, e.next?) assert_equal(1, e.next) + e.rewind + a = [] + loop{a.push e.next} + assert_equal([1,2,3], a) assert_equal([[1, 8, 10], [2, 6, 11], [3, 4, 12]], (1..10).zip([8,6,4],(10..100)).to_a)