~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to enumerator.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  enumerator.c - provides Enumerator class
4
4
 
5
 
  $Author: matz $
 
5
  $Author: nobu $
6
6
 
7
7
  Copyright (C) 2001-2003 Akinori MUSHA
8
8
 
9
9
  $Idaemons: /home/cvs/rb/enumerator/enumerator.c,v 1.1.1.1 2001/07/15 10:12:48 knu Exp $
10
10
  $RoughId: enumerator.c,v 1.6 2003/07/27 11:03:24 nobu Exp $
11
 
  $Id: enumerator.c 13279 2007-08-26 16:33:45Z matz $
 
11
  $Id: enumerator.c 14416 2007-12-21 07:33:31Z nobu $
12
12
 
13
13
************************************************/
14
14
 
22
22
 * object.
23
23
 */
24
24
static VALUE rb_cEnumerator;
25
 
static VALUE sym_each, sym_each_with_index, sym_each_slice, sym_each_cons, sym_call;
 
25
static VALUE sym_each, sym_call;
26
26
 
27
27
VALUE rb_eStopIteration;
28
28
 
35
35
    return rb_proc_call(proc, args);
36
36
}
37
37
 
38
 
struct enumerator;
39
 
typedef VALUE enum_iter(VALUE, struct enumerator *);
40
 
 
41
38
struct enumerator {
42
39
    VALUE method;
43
40
    VALUE proc;
44
41
    VALUE args;
45
 
    enum_iter *iter;
 
42
    rb_block_call_func *iter;
46
43
    VALUE fib;
47
44
    VALUE dst;
48
45
    VALUE no_next;
77
74
}
78
75
 
79
76
static VALUE
80
 
enumerator_iter_i(VALUE i, struct enumerator *e)
 
77
enumerator_iter_i(VALUE i, VALUE enum_obj, int argc, VALUE *argv)
81
78
{
 
79
    struct enumerator *e = (struct enumerator *)enum_obj;
82
80
    return rb_yield(proc_call(e->proc, i));
83
81
}
84
82
 
225
223
}
226
224
 
227
225
static VALUE
 
226
enumerator_each_i(VALUE v, VALUE enum_obj, int argc, VALUE *argv)
 
227
{
 
228
    return rb_yield_values2(argc, argv);
 
229
}
 
230
 
 
231
static VALUE
228
232
enumerator_init(VALUE enum_obj, VALUE obj, VALUE meth, int argc, VALUE *argv)
229
233
{
230
234
    struct enumerator *ptr = enumerator_ptr(enum_obj);
235
239
        ptr->iter = enumerator_iter_i;
236
240
    }
237
241
    else {
238
 
        ptr->iter = (enum_iter *)rb_yield;
 
242
        ptr->iter = enumerator_each_i;
239
243
    }
240
244
    if (argc) ptr->args = rb_ary_new4(argc, argv);
241
245
    ptr->fib = 0;
279
283
static VALUE
280
284
enumerator_init_copy(VALUE obj, VALUE orig)
281
285
{
282
 
    struct enumerator *ptr0 = enumerator_ptr(orig);
283
 
    struct enumerator *ptr1 = enumerator_ptr(obj);
 
286
    struct enumerator *ptr0, *ptr1;
 
287
 
 
288
    ptr0 = enumerator_ptr(orig);
 
289
    if (ptr0->fib) {
 
290
        /* Fibers cannot be copied */
 
291
        rb_raise(rb_eTypeError, "can't copy execution context");
 
292
    }
 
293
    ptr1 = enumerator_ptr(obj);
284
294
 
285
295
    ptr1->method = ptr0->method;
286
296
    ptr1->proc = ptr0->proc;
287
297
    ptr1->iter = ptr0->iter;
288
298
    ptr1->args = ptr0->args;
289
 
    ptr1->fib  = ptr0->fib;
 
299
    ptr1->fib  = 0;
290
300
 
291
301
    return obj;
292
302
}
354
364
                         enumerator_with_index_i, (VALUE)&memo);
355
365
}
356
366
 
357
 
/*
358
 
 * call-seq:
359
 
 *   e.to_splat   => array
360
 
 *
361
 
 * Convert this enumerator object to an array to splat.
362
 
 */
363
 
 
364
 
static VALUE
365
 
enumerator_to_splat(VALUE obj)
366
 
{
367
 
    return rb_convert_type(obj, T_ARRAY, "Array", "to_a");
368
 
}
369
 
 
370
367
static VALUE
371
368
next_ii(VALUE i, VALUE obj)
372
369
{
373
 
    struct enumerator *e = enumerator_ptr(obj);
374
370
    rb_fiber_yield(1, &i);
375
371
    return Qnil;
376
372
}
415
411
    VALUE curr, v;
416
412
    curr = rb_fiber_current();
417
413
 
418
 
    if (!e->fib) {
 
414
    if (!e->fib || !rb_fiber_alive_p(e->fib)) {
419
415
        next_init(obj, e);
420
416
    }
421
417
 
464
460
    rb_define_method(rb_cEnumerator, "initialize_copy", enumerator_init_copy, 1);
465
461
    rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
466
462
    rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0);
467
 
    rb_define_method(rb_cEnumerator, "to_splat", enumerator_to_splat, 0);
468
463
    rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
469
464
    rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
470
465
 
471
466
    rb_eStopIteration   = rb_define_class("StopIteration", rb_eIndexError);
472
467
 
473
468
    sym_each            = ID2SYM(rb_intern("each"));
474
 
    sym_each_with_index = ID2SYM(rb_intern("each_with_index"));
475
 
    sym_each_slice      = ID2SYM(rb_intern("each_slice"));
476
 
    sym_each_cons       = ID2SYM(rb_intern("each_cons"));
477
469
    sym_call            = ID2SYM(rb_intern("call"));
478
470
 
479
471
    rb_provide("enumerator.so");        /* for backward compatibility */