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