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