1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 2.00 of the Zend license,     |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.zend.com/license/2_00.txt.                                |
11   | If you did not receive a copy of the Zend license and are unable to  |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@zend.com so we can mail you a copy immediately.              |
14   +----------------------------------------------------------------------+
15   | Authors: Nikita Popov <nikic@php.net>                                |
16   +----------------------------------------------------------------------+
17*/
18
19/* $Id$ */
20
21#include "zend.h"
22#include "zend_API.h"
23#include "zend_interfaces.h"
24#include "zend_exceptions.h"
25#include "zend_generators.h"
26
27ZEND_API zend_class_entry *zend_ce_generator;
28static zend_object_handlers zend_generator_handlers;
29
30static zend_object *zend_generator_create(zend_class_entry *class_type TSRMLS_DC);
31
32static void zend_generator_cleanup_unfinished_execution(zend_generator *generator TSRMLS_DC) /* {{{ */
33{
34    zend_execute_data *execute_data = generator->execute_data;
35    zend_op_array *op_array = &execute_data->func->op_array;
36
37    if (generator->send_target) {
38        if (Z_REFCOUNTED_P(generator->send_target)) Z_DELREF_P(generator->send_target);
39        generator->send_target = NULL;
40    }
41
42    /* Manually free loop variables, as execution couldn't reach their
43     * SWITCH_FREE / FREE opcodes. */
44    {
45        /* -1 required because we want the last run opcode, not the
46         * next to-be-run one. */
47        zend_uint op_num = execute_data->opline - op_array->opcodes - 1;
48
49        int i;
50        for (i = 0; i < op_array->last_brk_cont; ++i) {
51            zend_brk_cont_element *brk_cont = op_array->brk_cont_array + i;
52
53            if (brk_cont->start < 0) {
54                continue;
55            } else if (brk_cont->start > op_num) {
56                break;
57            } else if (brk_cont->brk > op_num) {
58                zend_op *brk_opline = op_array->opcodes + brk_cont->brk;
59
60                switch (brk_opline->opcode) {
61                    case ZEND_SWITCH_FREE:
62                        {
63                            zval *var = EX_VAR_2(execute_data, brk_opline->op1.var);
64                            zval_ptr_dtor(var);
65                        }
66                        break;
67                    case ZEND_FREE:
68                        {
69                            zval *var = EX_VAR_2(execute_data, brk_opline->op1.var);
70                            zval_dtor(var);
71                        }
72                        break;
73                }
74            }
75        }
76    }
77
78    /* If yield was used as a function argument there may be active
79     * method calls those objects need to be freed */
80    while (execute_data->call) {
81        if (execute_data->call->object) {
82            OBJ_RELEASE(execute_data->call->object);
83        }
84        execute_data->call = execute_data->call->prev_nested_call;
85    }
86}
87/* }}} */
88
89ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution TSRMLS_DC) /* {{{ */
90{
91    if (Z_TYPE(generator->value) != IS_UNDEF) {
92        zval_ptr_dtor(&generator->value);
93        ZVAL_UNDEF(&generator->value);
94    }
95
96    if (Z_TYPE(generator->key) != IS_UNDEF) {
97        zval_ptr_dtor(&generator->key);
98        ZVAL_UNDEF(&generator->key);
99    }
100
101    if (generator->execute_data) {
102        zend_execute_data *execute_data = generator->execute_data;
103        zend_op_array *op_array = &execute_data->func->op_array;
104
105        if (!execute_data->symbol_table) {
106            zend_free_compiled_variables(execute_data TSRMLS_CC);
107        } else {
108            zend_clean_and_cache_symbol_table(execute_data->symbol_table TSRMLS_CC);
109        }
110
111        if (execute_data->object) {
112            OBJ_RELEASE(execute_data->object);
113        }
114
115        /* A fatal error / die occurred during the generator execution. Trying to clean
116         * up the stack may not be safe in this case. */
117        if (CG(unclean_shutdown)) {
118            generator->execute_data = NULL;
119            return;
120        }
121
122        zend_vm_stack_free_extra_args(generator->execute_data TSRMLS_CC);
123
124        /* Some cleanups are only necessary if the generator was closued
125         * before it could finish execution (reach a return statement). */
126        if (!finished_execution) {
127            zend_generator_cleanup_unfinished_execution(generator TSRMLS_CC);
128        }
129
130        /* Free a clone of closure */
131        if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
132            destroy_op_array(op_array TSRMLS_CC);
133            efree(op_array);
134        }
135
136        efree(generator->stack);
137        generator->execute_data = NULL;
138    }
139}
140/* }}} */
141
142static void zend_generator_dtor_storage(zend_object *object TSRMLS_DC) /* {{{ */
143{
144    zend_generator *generator = (zend_generator*) object;
145    zend_execute_data *ex = generator->execute_data;
146    zend_uint op_num, finally_op_num;
147    int i;
148
149    if (!ex || !ex->func->op_array.has_finally_block) {
150        return;
151    }
152
153    /* -1 required because we want the last run opcode, not the
154     * next to-be-run one. */
155    op_num = ex->opline - ex->func->op_array.opcodes - 1;
156
157    /* Find next finally block */
158    finally_op_num = 0;
159    for (i = 0; i < ex->func->op_array.last_try_catch; i++) {
160        zend_try_catch_element *try_catch = &ex->func->op_array.try_catch_array[i];
161
162        if (op_num < try_catch->try_op) {
163            break;
164        }
165
166        if (op_num < try_catch->finally_op) {
167            finally_op_num = try_catch->finally_op;
168        }
169    }
170
171    /* If a finally block was found we jump directly to it and
172     * resume the generator. */
173    if (finally_op_num) {
174        ex->opline = &ex->func->op_array.opcodes[finally_op_num];
175        ex->fast_ret = NULL;
176        generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
177        zend_generator_resume(generator TSRMLS_CC);
178    }
179}
180/* }}} */
181
182static void zend_generator_free_storage(zend_object *object TSRMLS_DC) /* {{{ */
183{
184    zend_generator *generator = (zend_generator*) object;
185
186    zend_generator_close(generator, 0 TSRMLS_CC);
187
188    zend_object_std_dtor(&generator->std TSRMLS_CC);
189
190    if (generator->iterator) {
191        zend_iterator_dtor(generator->iterator TSRMLS_CC);
192    }
193}
194/* }}} */
195
196static zend_object *zend_generator_create(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
197{
198    zend_generator *generator;
199
200    generator = emalloc(sizeof(zend_generator));
201    memset(generator, 0, sizeof(zend_generator));
202
203    /* The key will be incremented on first use, so it'll start at 0 */
204    generator->largest_used_integer_key = -1;
205
206    zend_object_std_init(&generator->std, class_type TSRMLS_CC);
207    generator->std.handlers = &zend_generator_handlers;
208
209    return (zend_object*)generator;
210}
211/* }}} */
212
213static int copy_closure_static_var(zval *var TSRMLS_DC, int num_args, va_list args, zend_hash_key *key) /* {{{ */
214{
215    HashTable *target = va_arg(args, HashTable *);
216
217    ZVAL_MAKE_REF(var);
218    Z_ADDREF_P(var);
219    zend_hash_update(target, key->key, var);
220    return 0;
221}
222/* }}} */
223
224/* Requires globals EG(scope), EG(This) and EG(current_execute_data). */
225ZEND_API void zend_generator_create_zval(zend_execute_data *call, zend_op_array *op_array, zval *return_value TSRMLS_DC) /* {{{ */
226{
227    zend_generator *generator;
228    zend_execute_data *current_execute_data;
229    zend_execute_data *execute_data;
230    zend_vm_stack current_stack = EG(argument_stack);
231
232    /* Create a clone of closure, because it may be destroyed */
233    if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
234        zend_op_array *op_array_copy = (zend_op_array*)emalloc(sizeof(zend_op_array));
235        *op_array_copy = *op_array;
236
237        (*op_array->refcount)++;
238        op_array->run_time_cache = NULL;
239        if (op_array->static_variables) {
240            ALLOC_HASHTABLE(op_array_copy->static_variables);
241            zend_hash_init(
242                op_array_copy->static_variables,
243                zend_hash_num_elements(op_array->static_variables),
244                NULL, ZVAL_PTR_DTOR, 0
245            );
246            zend_hash_apply_with_arguments(
247                op_array->static_variables TSRMLS_CC,
248                copy_closure_static_var, 1,
249                op_array_copy->static_variables
250            );
251        }
252
253        op_array = op_array_copy;
254    }
255
256    /* Create new execution context. We have to back up and restore
257     * EG(current_execute_data) here. */
258    current_execute_data = EG(current_execute_data);
259    execute_data = zend_create_generator_execute_data(call, op_array, return_value TSRMLS_CC);
260    EG(current_execute_data) = current_execute_data;
261
262    object_init_ex(return_value, zend_ce_generator);
263
264    if (Z_OBJ(EG(This))) {
265        Z_ADDREF(EG(This));
266    }
267
268    /* Save execution context in generator object. */
269    generator = (zend_generator *) Z_OBJ_P(return_value);
270    execute_data->prev_execute_data = NULL;
271    generator->execute_data = execute_data;
272    generator->stack = EG(argument_stack);
273    EG(argument_stack) = current_stack;
274
275    /* EX(return_value) keeps pointer to zend_object (not a real zval) */
276    execute_data->return_value = (zval*)generator;
277}
278/* }}} */
279
280static zend_function *zend_generator_get_constructor(zend_object *object TSRMLS_DC) /* {{{ */
281{
282    zend_error(E_RECOVERABLE_ERROR, "The \"Generator\" class is reserved for internal use and cannot be manually instantiated");
283
284    return NULL;
285}
286/* }}} */
287
288ZEND_API void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
289{
290    /* The generator is already closed, thus can't resume */
291    if (!generator->execute_data) {
292        return;
293    }
294
295    if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
296        zend_error(E_ERROR, "Cannot resume an already running generator");
297    }
298
299    /* Drop the AT_FIRST_YIELD flag */
300    generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
301
302    {
303        /* Backup executor globals */
304        zend_execute_data *original_execute_data = EG(current_execute_data);
305        zend_object *original_This;
306        zend_class_entry *original_scope = EG(scope);
307        zend_vm_stack original_stack = EG(argument_stack);
308
309        original_This = Z_OBJ(EG(This));
310
311        /* Set executor globals */
312        EG(current_execute_data) = generator->execute_data;
313        Z_OBJ(EG(This)) = generator->execute_data->object;
314        EG(scope) = generator->execute_data->scope;
315        EG(argument_stack) = generator->stack;
316
317        /* We want the backtrace to look as if the generator function was
318         * called from whatever method we are current running (e.g. next()).
319         * So we have to link generator call frame with caller call frames */
320
321        generator->execute_data->prev_execute_data = original_execute_data;
322
323        /* Resume execution */
324        generator->flags |= ZEND_GENERATOR_CURRENTLY_RUNNING;
325        zend_execute_ex(generator->execute_data TSRMLS_CC);
326        generator->flags &= ~ZEND_GENERATOR_CURRENTLY_RUNNING;
327
328        /* Unlink generator call_frame from the caller */
329        if (generator->execute_data) {
330            generator->execute_data->prev_execute_data = NULL;
331        }
332
333        /* Restore executor globals */
334        EG(current_execute_data) = original_execute_data;
335        Z_OBJ(EG(This)) = original_This;
336        EG(scope) = original_scope;
337        EG(argument_stack) = original_stack;
338
339        /* If an exception was thrown in the generator we have to internally
340         * rethrow it in the parent scope. */
341        if (UNEXPECTED(EG(exception) != NULL)) {
342            zend_throw_exception_internal(NULL TSRMLS_CC);
343        }
344    }
345}
346/* }}} */
347
348static void zend_generator_ensure_initialized(zend_generator *generator TSRMLS_DC) /* {{{ */
349{
350    if (generator->execute_data && Z_TYPE(generator->value) == IS_UNDEF) {
351        zend_generator_resume(generator TSRMLS_CC);
352        generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
353    }
354}
355/* }}} */
356
357static void zend_generator_rewind(zend_generator *generator TSRMLS_DC) /* {{{ */
358{
359    zend_generator_ensure_initialized(generator TSRMLS_CC);
360
361    if (!(generator->flags & ZEND_GENERATOR_AT_FIRST_YIELD)) {
362        zend_throw_exception(NULL, "Cannot rewind a generator that was already run", 0 TSRMLS_CC);
363    }
364}
365/* }}} */
366
367/* {{{ proto void Generator::rewind()
368 * Rewind the generator */
369ZEND_METHOD(Generator, rewind)
370{
371    zend_generator *generator;
372
373    if (zend_parse_parameters_none() == FAILURE) {
374        return;
375    }
376
377    generator = (zend_generator *) Z_OBJ_P(getThis());
378
379    zend_generator_rewind(generator TSRMLS_CC);
380}
381/* }}} */
382
383/* {{{ proto bool Generator::valid()
384 * Check whether the generator is valid */
385ZEND_METHOD(Generator, valid)
386{
387    zend_generator *generator;
388
389    if (zend_parse_parameters_none() == FAILURE) {
390        return;
391    }
392
393    generator = (zend_generator *) Z_OBJ_P(getThis());
394
395    zend_generator_ensure_initialized(generator TSRMLS_CC);
396
397    RETURN_BOOL(Z_TYPE(generator->value) != IS_UNDEF);
398}
399/* }}} */
400
401/* {{{ proto mixed Generator::current()
402 * Get the current value */
403ZEND_METHOD(Generator, current)
404{
405    zend_generator *generator;
406
407    if (zend_parse_parameters_none() == FAILURE) {
408        return;
409    }
410
411    generator = (zend_generator *) Z_OBJ_P(getThis());
412
413    zend_generator_ensure_initialized(generator TSRMLS_CC);
414
415    if (Z_TYPE(generator->value) != IS_UNDEF) {
416        RETURN_ZVAL_FAST(&generator->value);
417    }
418}
419/* }}} */
420
421/* {{{ proto mixed Generator::key()
422 * Get the current key */
423ZEND_METHOD(Generator, key)
424{
425    zend_generator *generator;
426
427    if (zend_parse_parameters_none() == FAILURE) {
428        return;
429    }
430
431    generator = (zend_generator *) Z_OBJ_P(getThis());
432
433    zend_generator_ensure_initialized(generator TSRMLS_CC);
434
435    if (Z_TYPE(generator->key) != IS_UNDEF) {
436        RETURN_ZVAL_FAST(&generator->key);
437    }
438}
439/* }}} */
440
441/* {{{ proto void Generator::next()
442 * Advances the generator */
443ZEND_METHOD(Generator, next)
444{
445    zend_generator *generator;
446
447    if (zend_parse_parameters_none() == FAILURE) {
448        return;
449    }
450
451    generator = (zend_generator *) Z_OBJ_P(getThis());
452
453    zend_generator_ensure_initialized(generator TSRMLS_CC);
454
455    zend_generator_resume(generator TSRMLS_CC);
456}
457/* }}} */
458
459/* {{{ proto mixed Generator::send(mixed $value)
460 * Sends a value to the generator */
461ZEND_METHOD(Generator, send)
462{
463    zval *value;
464    zend_generator *generator;
465
466    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) {
467        return;
468    }
469
470    generator = (zend_generator *) Z_OBJ_P(getThis());
471
472    zend_generator_ensure_initialized(generator TSRMLS_CC);
473
474    /* The generator is already closed, thus can't send anything */
475    if (!generator->execute_data) {
476        return;
477    }
478
479    /* Put sent value in the target VAR slot, if it is used */
480    if (generator->send_target) {
481        if (Z_REFCOUNTED_P(generator->send_target)) Z_DELREF_P(generator->send_target);
482        ZVAL_COPY(generator->send_target, value);
483    }
484
485    zend_generator_resume(generator TSRMLS_CC);
486
487    if (Z_TYPE(generator->value) != IS_UNDEF) {
488        RETURN_ZVAL_FAST(&generator->value);
489    }
490}
491/* }}} */
492
493/* {{{ proto mixed Generator::throw(Exception $exception)
494 * Throws an exception into the generator */
495ZEND_METHOD(Generator, throw)
496{
497    zval *exception, exception_copy;
498    zend_generator *generator;
499
500    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception) == FAILURE) {
501        return;
502    }
503
504    ZVAL_DUP(&exception_copy, exception);
505
506    generator = (zend_generator *) Z_OBJ_P(getThis());
507
508    zend_generator_ensure_initialized(generator TSRMLS_CC);
509
510    if (generator->execute_data) {
511        /* Throw the exception in the context of the generator */
512        zend_execute_data *current_execute_data = EG(current_execute_data);
513        EG(current_execute_data) = generator->execute_data;
514
515        zend_throw_exception_object(&exception_copy TSRMLS_CC);
516
517        EG(current_execute_data) = current_execute_data;
518
519        zend_generator_resume(generator TSRMLS_CC);
520
521        if (Z_TYPE(generator->value) != IS_UNDEF) {
522            RETURN_ZVAL_FAST(&generator->value);
523        }
524    } else {
525        /* If the generator is already closed throw the exception in the
526         * current context */
527        zend_throw_exception_object(&exception_copy TSRMLS_CC);
528    }
529}
530/* }}} */
531
532/* {{{ proto void Generator::__wakeup()
533 * Throws an Exception as generators can't be serialized */
534ZEND_METHOD(Generator, __wakeup)
535{
536    /* Just specifying the zend_class_unserialize_deny handler is not enough,
537     * because it is only invoked for C unserialization. For O the error has
538     * to be thrown in __wakeup. */
539
540    if (zend_parse_parameters_none() == FAILURE) {
541        return;
542    }
543
544    zend_throw_exception(NULL, "Unserialization of 'Generator' is not allowed", 0 TSRMLS_CC);
545}
546/* }}} */
547
548/* get_iterator implementation */
549
550static void zend_generator_iterator_dtor(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
551{
552    zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
553    generator->iterator = NULL;
554    zval_ptr_dtor(&iterator->data);
555    zend_iterator_dtor(iterator TSRMLS_CC);
556}
557/* }}} */
558
559static int zend_generator_iterator_valid(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
560{
561    zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
562
563    zend_generator_ensure_initialized(generator TSRMLS_CC);
564
565    return Z_TYPE(generator->value) != IS_UNDEF ? SUCCESS : FAILURE;
566}
567/* }}} */
568
569static zval *zend_generator_iterator_get_data(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
570{
571    zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
572
573    zend_generator_ensure_initialized(generator TSRMLS_CC);
574
575    return &generator->value;
576}
577/* }}} */
578
579static void zend_generator_iterator_get_key(zend_object_iterator *iterator, zval *key TSRMLS_DC) /* {{{ */
580{
581    zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
582
583    zend_generator_ensure_initialized(generator TSRMLS_CC);
584
585    if (Z_TYPE(generator->key) != IS_UNDEF) {
586        ZVAL_ZVAL(key, &generator->key, 1, 0);
587    } else {
588        ZVAL_NULL(key);
589    }
590}
591/* }}} */
592
593static void zend_generator_iterator_move_forward(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
594{
595    zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
596
597    zend_generator_ensure_initialized(generator TSRMLS_CC);
598
599    zend_generator_resume(generator TSRMLS_CC);
600}
601/* }}} */
602
603static void zend_generator_iterator_rewind(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
604{
605    zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
606
607    zend_generator_rewind(generator TSRMLS_CC);
608}
609/* }}} */
610
611static zend_object_iterator_funcs zend_generator_iterator_functions = {
612    zend_generator_iterator_dtor,
613    zend_generator_iterator_valid,
614    zend_generator_iterator_get_data,
615    zend_generator_iterator_get_key,
616    zend_generator_iterator_move_forward,
617    zend_generator_iterator_rewind
618};
619
620zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
621{
622    zend_object_iterator *iterator;
623    zend_generator *generator = (zend_generator*)Z_OBJ_P(object);
624
625    if (!generator->execute_data) {
626        zend_throw_exception(NULL, "Cannot traverse an already closed generator", 0 TSRMLS_CC);
627        return NULL;
628    }
629
630    if (by_ref && !(generator->execute_data->func->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
631        zend_throw_exception(NULL, "You can only iterate a generator by-reference if it declared that it yields by-reference", 0 TSRMLS_CC);
632        return NULL;
633    }
634
635    iterator = generator->iterator = emalloc(sizeof(zend_object_iterator));
636
637    zend_iterator_init(iterator TSRMLS_CC);
638
639    iterator->funcs = &zend_generator_iterator_functions;
640    ZVAL_COPY(&iterator->data, object);
641
642    return iterator;
643}
644/* }}} */
645
646ZEND_BEGIN_ARG_INFO(arginfo_generator_void, 0)
647ZEND_END_ARG_INFO()
648
649ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_send, 0, 0, 1)
650    ZEND_ARG_INFO(0, value)
651ZEND_END_ARG_INFO()
652
653ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_throw, 0, 0, 1)
654    ZEND_ARG_INFO(0, exception)
655ZEND_END_ARG_INFO()
656
657static const zend_function_entry generator_functions[] = {
658    ZEND_ME(Generator, rewind,   arginfo_generator_void, ZEND_ACC_PUBLIC)
659    ZEND_ME(Generator, valid,    arginfo_generator_void, ZEND_ACC_PUBLIC)
660    ZEND_ME(Generator, current,  arginfo_generator_void, ZEND_ACC_PUBLIC)
661    ZEND_ME(Generator, key,      arginfo_generator_void, ZEND_ACC_PUBLIC)
662    ZEND_ME(Generator, next,     arginfo_generator_void, ZEND_ACC_PUBLIC)
663    ZEND_ME(Generator, send,     arginfo_generator_send, ZEND_ACC_PUBLIC)
664    ZEND_ME(Generator, throw,    arginfo_generator_throw, ZEND_ACC_PUBLIC)
665    ZEND_ME(Generator, __wakeup, arginfo_generator_void, ZEND_ACC_PUBLIC)
666    ZEND_FE_END
667};
668
669void zend_register_generator_ce(TSRMLS_D) /* {{{ */
670{
671    zend_class_entry ce;
672
673    INIT_CLASS_ENTRY(ce, "Generator", generator_functions);
674    zend_ce_generator = zend_register_internal_class(&ce TSRMLS_CC);
675    zend_ce_generator->ce_flags |= ZEND_ACC_FINAL_CLASS;
676    zend_ce_generator->create_object = zend_generator_create;
677    zend_ce_generator->serialize = zend_class_serialize_deny;
678    zend_ce_generator->unserialize = zend_class_unserialize_deny;
679
680    /* get_iterator has to be assigned *after* implementing the inferface */
681    zend_class_implements(zend_ce_generator TSRMLS_CC, 1, zend_ce_iterator);
682    zend_ce_generator->get_iterator = zend_generator_get_iterator;
683    zend_ce_generator->iterator_funcs.funcs = &zend_generator_iterator_functions;
684
685    memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
686    zend_generator_handlers.free_obj = zend_generator_free_storage;
687    zend_generator_handlers.dtor_obj = zend_generator_dtor_storage;
688    zend_generator_handlers.clone_obj = NULL;
689    zend_generator_handlers.get_constructor = zend_generator_get_constructor;
690}
691/* }}} */
692
693/*
694 * Local variables:
695 * tab-width: 4
696 * c-basic-offset: 4
697 * indent-tabs-mode: t
698 * End:
699 */
700