PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_generators.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 469 518 90.5 %
Date: 2015-08-25 Functions: 35 37 94.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 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             :    |          Bob Weinand <bobwei9@hotmail.com>                           |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "zend.h"
      23             : #include "zend_API.h"
      24             : #include "zend_interfaces.h"
      25             : #include "zend_exceptions.h"
      26             : #include "zend_generators.h"
      27             : 
      28             : ZEND_API zend_class_entry *zend_ce_generator;
      29             : ZEND_API zend_class_entry *zend_ce_ClosedGeneratorException;
      30             : static zend_object_handlers zend_generator_handlers;
      31             : 
      32             : static zend_object *zend_generator_create(zend_class_entry *class_type);
      33             : 
      34         255 : static void zend_generator_cleanup_unfinished_execution(zend_generator *generator) /* {{{ */
      35             : {
      36         255 :         zend_execute_data *execute_data = generator->execute_data;
      37             : 
      38         255 :         if (generator->send_target) {
      39          86 :                 Z_TRY_DELREF_P(generator->send_target);
      40          86 :                 generator->send_target = NULL;
      41             :         }
      42             : 
      43         255 :         if (execute_data->opline != execute_data->func->op_array.opcodes) {
      44             :                 /* -1 required because we want the last run opcode, not the next to-be-run one. */
      45         244 :                 uint32_t op_num = execute_data->opline - execute_data->func->op_array.opcodes - 1;
      46             : 
      47             :                 /* There may be calls to zend_vm_stack_free_call_frame(), which modifies the VM stack
      48             :                  * globals, so need to load/restore those. */
      49         244 :                 zend_vm_stack original_stack = EG(vm_stack);
      50         244 :                 original_stack->top = EG(vm_stack_top);
      51         244 :                 EG(vm_stack_top) = generator->stack->top;
      52         244 :                 EG(vm_stack_end) = generator->stack->end;
      53         244 :                 EG(vm_stack) = generator->stack;
      54             : 
      55         244 :                 zend_cleanup_unfinished_execution(execute_data, op_num, 0);
      56             : 
      57         244 :                 generator->stack = EG(vm_stack);
      58         244 :                 generator->stack->top = EG(vm_stack_top);
      59         244 :                 EG(vm_stack_top) = original_stack->top;
      60         244 :                 EG(vm_stack_end) = original_stack->end;
      61         244 :                 EG(vm_stack) = original_stack;
      62             :         }
      63         255 : }
      64             : /* }}} */
      65             : 
      66      100554 : ZEND_API void zend_generator_close(zend_generator *generator, zend_bool finished_execution) /* {{{ */
      67             : {
      68      201108 :         if (Z_TYPE(generator->value) != IS_UNDEF) {
      69         274 :                 zval_ptr_dtor(&generator->value);
      70         274 :                 ZVAL_UNDEF(&generator->value);
      71             :         }
      72             : 
      73      201108 :         if (Z_TYPE(generator->key) != IS_UNDEF) {
      74         274 :                 zval_ptr_dtor(&generator->key);
      75         274 :                 ZVAL_UNDEF(&generator->key);
      76             :         }
      77             : 
      78      201108 :         if (Z_TYPE(generator->values) != IS_UNDEF) {
      79           0 :                 zval_ptr_dtor(&generator->values);
      80           0 :                 ZVAL_UNDEF(&generator->values);
      81             :         }
      82             : 
      83      100554 :         if (generator->execute_data) {
      84       50396 :                 zend_execute_data *execute_data = generator->execute_data;
      85             : 
      86       50396 :                 if (execute_data->symbol_table) {
      87           2 :                         zend_clean_and_cache_symbol_table(execute_data->symbol_table);
      88             :                 }
      89             :                 /* always free the CV's, in the symtable are only not-free'd IS_INDIRECT's */
      90       50396 :                 zend_free_compiled_variables(execute_data);
      91             : 
      92       50396 :                 if (Z_OBJ(execute_data->This)) {
      93           5 :                         OBJ_RELEASE(Z_OBJ(execute_data->This));
      94             :                 }
      95             : 
      96             :                 /* A fatal error / die occurred during the generator execution. Trying to clean
      97             :                  * up the stack may not be safe in this case. */
      98       50396 :                 if (CG(unclean_shutdown)) {
      99           7 :                         generator->execute_data = NULL;
     100           7 :                         return;
     101             :                 }
     102             : 
     103       50389 :                 zend_vm_stack_free_extra_args(generator->execute_data);
     104             : 
     105             :                 /* Some cleanups are only necessary if the generator was closued
     106             :                  * before it could finish execution (reach a return statement). */
     107       50389 :                 if (!finished_execution) {
     108         255 :                         zend_generator_cleanup_unfinished_execution(generator);
     109             :                 }
     110             : 
     111             :                 /* Free closure object */
     112       50389 :                 if (EX_CALL_INFO() & ZEND_CALL_CLOSURE) {
     113          15 :                         OBJ_RELEASE((zend_object *) EX(func)->common.prototype);
     114             :                 }
     115             : 
     116       50389 :                 efree(generator->stack);
     117       50389 :                 generator->execute_data = NULL;
     118             :         }
     119             : }
     120             : /* }}} */
     121             : 
     122       50386 : static void zend_generator_dtor_storage(zend_object *object) /* {{{ */
     123             : {
     124       50386 :         zend_generator *generator = (zend_generator*) object;
     125       50386 :         zend_execute_data *ex = generator->execute_data;
     126             :         uint32_t op_num, finally_op_num, finally_op_end;
     127             :         int i;
     128             : 
     129       50386 :         if (!ex || !(ex->func->op_array.fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
     130       50382 :                 return;
     131             :         }
     132             : 
     133             :         /* -1 required because we want the last run opcode, not the
     134             :          * next to-be-run one. */
     135           4 :         op_num = ex->opline - ex->func->op_array.opcodes - 1;
     136             : 
     137             :         /* Find next finally block */
     138           4 :         finally_op_num = 0;
     139           4 :         finally_op_end = 0;
     140           9 :         for (i = 0; i < ex->func->op_array.last_try_catch; i++) {
     141           5 :                 zend_try_catch_element *try_catch = &ex->func->op_array.try_catch_array[i];
     142             : 
     143           5 :                 if (op_num < try_catch->try_op) {
     144           0 :                         break;
     145             :                 }
     146             : 
     147           5 :                 if (op_num < try_catch->finally_op) {
     148           5 :                         finally_op_num = try_catch->finally_op;
     149           5 :                         finally_op_end = try_catch->finally_end;
     150             :                 }
     151             :         }
     152             : 
     153             :         /* If a finally block was found we jump directly to it and
     154             :          * resume the generator. */
     155           4 :         if (finally_op_num) {
     156           4 :                 zval *fast_call = ZEND_CALL_VAR(ex, ex->func->op_array.opcodes[finally_op_end].op1.var);
     157             : 
     158           4 :                 Z_OBJ_P(fast_call) = EG(exception);
     159           4 :                 EG(exception) = NULL;
     160           4 :                 fast_call->u2.lineno = (uint32_t)-1;
     161             : 
     162           4 :                 ex->opline = &ex->func->op_array.opcodes[finally_op_num];
     163           4 :                 generator->flags |= ZEND_GENERATOR_FORCED_CLOSE;
     164           4 :                 zend_generator_resume(generator);
     165             :         }
     166             : }
     167             : /* }}} */
     168             : 
     169             : static zend_generator *zend_generator_get_child(zend_generator_node *node, zend_generator *leaf);
     170             : 
     171       50398 : static void zend_generator_free_storage(zend_object *object) /* {{{ */
     172             : {
     173       50398 :         zend_generator *generator = (zend_generator*) object;
     174             : 
     175       50398 :         zend_generator_close(generator, 0);
     176             : 
     177      100796 :         if (!Z_ISUNDEF(generator->retval)) {
     178       50104 :                 zval_ptr_dtor(&generator->retval);
     179             :         }
     180             : 
     181       50398 :         if (generator->node.children > 4) {
     182          29 :                 zend_hash_destroy(&generator->node.child.ht);
     183             :         }
     184             : 
     185       50398 :         zend_object_std_dtor(&generator->std);
     186             : 
     187       50398 :         if (generator->iterator) {
     188           0 :                 zend_iterator_dtor(generator->iterator);
     189             :         }
     190             : 
     191       50398 :         if (generator->node.children == 0) {
     192         314 :                 zend_generator *root = generator->node.ptr.root, *next;
     193         904 :                 while (root != generator) {
     194         276 :                         next = zend_generator_get_child(&root->node, generator);
     195         276 :                         OBJ_RELEASE(&root->std);
     196         276 :                         root = next;
     197             :                 }
     198             :         }
     199       50398 : }
     200             : /* }}} */
     201             : 
     202       50398 : static zend_object *zend_generator_create(zend_class_entry *class_type) /* {{{ */
     203             : {
     204             :         zend_generator *generator;
     205             : 
     206       50398 :         generator = emalloc(sizeof(zend_generator));
     207       50398 :         memset(generator, 0, sizeof(zend_generator));
     208             : 
     209             :         /* The key will be incremented on first use, so it'll start at 0 */
     210       50398 :         generator->largest_used_integer_key = -1;
     211             : 
     212       50398 :         ZVAL_UNDEF(&generator->retval);
     213       50398 :         ZVAL_UNDEF(&generator->values);
     214             : 
     215             :         /* By default we have a tree of only one node */
     216       50398 :         generator->node.parent = NULL;
     217       50398 :         generator->node.children = 0;
     218       50398 :         generator->node.ptr.root = generator;
     219             : 
     220       50398 :         zend_object_std_init(&generator->std, class_type);
     221       50398 :         generator->std.handlers = &zend_generator_handlers;
     222             : 
     223       50398 :         return (zend_object*)generator;
     224             : }
     225             : /* }}} */
     226             : 
     227             : /* Requires globals EG(scope), EG(This) and EG(current_execute_data). */
     228       50396 : ZEND_API void zend_generator_create_zval(zend_execute_data *call, zend_op_array *op_array, zval *return_value) /* {{{ */
     229             : {
     230             :         zend_generator *generator;
     231             :         zend_execute_data *current_execute_data;
     232             :         zend_execute_data *execute_data;
     233       50396 :         zend_vm_stack current_stack = EG(vm_stack);
     234             : 
     235       50396 :         current_stack->top = EG(vm_stack_top);
     236             : 
     237             :         /* Create new execution context. We have to back up and restore
     238             :          * EG(current_execute_data) here. */
     239       50396 :         current_execute_data = EG(current_execute_data);
     240       50396 :         execute_data = zend_create_generator_execute_data(call, op_array, return_value);
     241       50396 :         EG(current_execute_data) = current_execute_data;
     242             : 
     243       50396 :         object_init_ex(return_value, zend_ce_generator);
     244             : 
     245       50396 :         if (Z_OBJ(call->This)) {
     246           5 :                 Z_ADDREF(call->This);
     247             :         }
     248             : 
     249             :         /* Save execution context in generator object. */
     250       50396 :         generator = (zend_generator *) Z_OBJ_P(return_value);
     251       50396 :         generator->execute_data = execute_data;
     252       50396 :         generator->stack = EG(vm_stack);
     253       50396 :         generator->stack->top = EG(vm_stack_top);
     254       50396 :         EG(vm_stack_top) = current_stack->top;
     255       50396 :         EG(vm_stack_end) = current_stack->end;
     256       50396 :         EG(vm_stack) = current_stack;
     257             : 
     258             :         /* EX(return_value) keeps pointer to zend_object (not a real zval) */
     259       50396 :         execute_data->return_value = (zval*)generator;
     260             : 
     261       50396 :         memset(&generator->execute_fake, 0, sizeof(zend_execute_data));
     262       50396 :         Z_OBJ(generator->execute_fake.This) = (zend_object *) generator;
     263       50396 : }
     264             : /* }}} */
     265             : 
     266           2 : static ZEND_COLD zend_function *zend_generator_get_constructor(zend_object *object) /* {{{ */
     267             : {
     268           2 :         zend_throw_error(NULL, "The \"Generator\" class is reserved for internal use and cannot be manually instantiated");
     269             : 
     270           2 :         return NULL;
     271             : }
     272             : /* }}} */
     273             : 
     274        3597 : ZEND_API zend_execute_data *zend_generator_check_placeholder_frame(zend_execute_data *ptr)
     275             : {
     276        3597 :         if (!ptr->func && Z_OBJ(ptr->This)) {
     277          16 :                 if (Z_OBJCE(ptr->This) == zend_ce_generator) {
     278           7 :                         zend_generator *generator = (zend_generator *) Z_OBJ(ptr->This);
     279           7 :                         zend_generator *root = (generator->node.children < 1 ? generator : generator->node.ptr.leaf)->node.ptr.root;
     280           7 :                         zend_execute_data *prev = ptr->prev_execute_data;
     281           7 :                         if (generator->node.parent != root) {
     282             :                                 do {
     283           3 :                                         generator->execute_data->prev_execute_data = prev;
     284           3 :                                         prev = generator->execute_data;
     285           3 :                                         generator = generator->node.parent;
     286           3 :                                 } while (generator->node.parent != root);
     287             :                         }
     288           7 :                         generator->execute_data->prev_execute_data = prev;
     289           7 :                         ptr = generator->execute_data;
     290             :                 }
     291             :         }
     292        3597 :         return ptr;
     293             : }
     294             : 
     295          16 : static void zend_generator_throw_exception(zend_generator *generator, zval *exception)
     296             : {
     297             :         /* Throw the exception in the context of the generator. Decrementing the opline
     298             :          * to pretend the exception happened during the YIELD opcode. */
     299          16 :         zend_execute_data *original_execute_data = EG(current_execute_data);
     300          16 :         EG(current_execute_data) = generator->execute_data;
     301          16 :         generator->execute_data->opline--;
     302          16 :         if (exception) {
     303          10 :                 zend_throw_exception_object(exception);
     304             :         } else {
     305           6 :                 zend_throw_exception_internal(NULL);
     306             :         }
     307          16 :         generator->execute_data->opline++;
     308          16 :         EG(current_execute_data) = original_execute_data;
     309          16 : }
     310             : 
     311       50305 : static zend_generator *zend_generator_get_child(zend_generator_node *node, zend_generator *leaf)
     312             : {
     313       50305 :         switch (node->children) {
     314             :                 case 0:
     315           0 :                         return NULL;
     316             :                 case 1:
     317           0 :                         return node->child.array[0].child;
     318             : 
     319             : #define ZEND_GEN_GET_CHILD(x) \
     320             :                         if (node->child.array[x].leaf == leaf) { \
     321             :                                 return node->child.array[x].child; \
     322             :                         }
     323             :                 case 4:
     324           0 :                         ZEND_GEN_GET_CHILD(3)
     325             :                 case 3:
     326          76 :                         ZEND_GEN_GET_CHILD(2)
     327             :                 case 2:
     328       50055 :                         ZEND_GEN_GET_CHILD(1)
     329           0 :                         ZEND_GEN_GET_CHILD(0)
     330             :                         ZEND_ASSERT(0); // we never should have no matching child
     331             :         }
     332             : 
     333         424 :         return zend_hash_index_find_ptr(&node->child.ht, (zend_ulong) leaf);
     334             : }
     335             : 
     336         124 : static zend_generator_node *zend_generator_search_multi_children_node(zend_generator_node *node)
     337             : {
     338         248 :         while (node->children == 1) {
     339           0 :                 node = &node->child.array[0].child->node;
     340             :         }
     341         124 :         return node->children > 1 ? node : NULL;
     342             : }
     343             : 
     344      100543 : static void zend_generator_add_single_child(zend_generator_node *node, zend_generator *child, zend_generator *leaf)
     345             : {
     346      100543 :         if (node->children < 4) {
     347      100264 :                 node->child.array[node->children].leaf = leaf;
     348      100264 :                 node->child.array[node->children].child = child;
     349         279 :         } else if (node->children > 4) {
     350         250 :                 zend_hash_index_add_ptr(&node->child.ht, (zend_ulong) leaf, child);
     351             :         } else {
     352             :                 struct {
     353             :                         zend_generator *leaf;
     354             :                         zend_generator *child;
     355             :                 } array[4];
     356             :                 int i;
     357             : 
     358          29 :                 memcpy(&array, &node->child.array, sizeof(array));
     359          29 :                 zend_hash_init(&node->child.ht, 5, sigh, NULL, 0);
     360         145 :                 for (i = 0; i < 4; i++) {
     361         116 :                         zend_hash_index_add_ptr(&node->child.ht, (zend_ulong) array[i].leaf, array[i].child);
     362             :                 }
     363          29 :                 zend_hash_index_add_ptr(&node->child.ht, (zend_ulong) leaf, child);
     364             :         }
     365             : 
     366      100543 :         node->children++;
     367      100543 : }
     368             : 
     369           0 : static void zend_generator_merge_child_nodes(zend_generator_node *dest, zend_generator_node *src, zend_generator *child)
     370             : {
     371           0 :         if (src->children <= 4) {
     372           0 :                 int i = src->children;
     373           0 :                 while (i--) {
     374           0 :                         zend_generator_add_single_child(dest, child, src->child.array[i].leaf);
     375             :                 }
     376             :         } else {
     377             :                 zend_ulong leaf;
     378           0 :                 ZEND_HASH_FOREACH_NUM_KEY(&src->child.ht, leaf) {
     379           0 :                         zend_generator_add_single_child(dest, child, (zend_generator *) leaf);
     380             :                 } ZEND_HASH_FOREACH_END();
     381             :         }
     382           0 : }
     383             : 
     384             : /* Make attention so that the root of each subtree of the Generators tree is referenced once per leaf */
     385       50150 : static void zend_generator_add_child(zend_generator *generator, zend_generator *child)
     386             : {
     387       50150 :         zend_generator *leaf = child->node.children ? child->node.ptr.leaf : child;
     388             :         zend_generator_node *multi_children_node;
     389       50150 :         zend_bool was_leaf = generator->node.children == 0;
     390             : 
     391       50150 :         if (was_leaf) {
     392       50084 :                 zend_generator *next = generator->node.parent;
     393       50084 :                 leaf->node.ptr.root = generator->node.ptr.root;
     394       50084 :                 ++GC_REFCOUNT(&generator->std); /* we need to increment the generator refcount here as it became integrated into the tree (no leaf), but we must not increment the refcount of the *whole* path in tree */
     395       50084 :                 generator->node.ptr.leaf = leaf;
     396             : 
     397      100322 :                 while (next) {
     398         154 :                         if (next->node.children > 1) {
     399         154 :                                 if (next->node.children > 4) {
     400         250 :                                         zend_generator *child = zend_hash_index_find_ptr(&next->node.child.ht, (zend_ulong) generator);
     401         125 :                                         zend_hash_index_del(&next->node.child.ht, (zend_ulong) generator);
     402         125 :                                         zend_hash_index_add_ptr(&next->node.child.ht, (zend_ulong) leaf, child);
     403             :                                 } else {
     404          29 :                                         switch (next->node.children) {
     405             : #define ZEND_GEN_UPDATE_CHILD(x) \
     406             :                                                         if (next->node.child.array[x].leaf == generator) { \
     407             :                                                                 next->node.child.array[x].leaf = leaf; \
     408             :                                                                 break; \
     409             :                                                         }
     410             :                                                 case 4:
     411           0 :                                                         ZEND_GEN_UPDATE_CHILD(3)
     412             :                                                 case 3:
     413          29 :                                                         ZEND_GEN_UPDATE_CHILD(2)
     414             :                                                 case 2:
     415          29 :                                                         ZEND_GEN_UPDATE_CHILD(1)
     416           0 :                                                         ZEND_GEN_UPDATE_CHILD(0)
     417             :                                                         ZEND_ASSERT(0); // we never should have no matching child
     418             :                                         }
     419             :                                 }
     420             :                         }
     421             : 
     422         154 :                         next->node.ptr.leaf = leaf;
     423         154 :                         next = next->node.parent;
     424             :                 }
     425             : 
     426       50084 :                 zend_generator_add_single_child(&generator->node, child, leaf);
     427          66 :         } else if (generator->node.children == 1) {
     428           0 :                 multi_children_node = zend_generator_search_multi_children_node(&generator->node);
     429           0 :                 if (multi_children_node) {
     430           0 :                         generator->node.children = 0;
     431           0 :                         zend_generator_merge_child_nodes(&generator->node, multi_children_node, generator->node.child.array[0].child);
     432             :                 }
     433             :         }
     434             : 
     435       50150 :         if (!was_leaf) {
     436          66 :                 multi_children_node = zend_generator_search_multi_children_node(&child->node);
     437             :         } else {
     438       50084 :                 multi_children_node = (zend_generator_node *) 0x1;
     439             :         }
     440             : 
     441             :         {
     442       50150 :                 zend_generator *parent = generator->node.parent, *cur = generator;
     443             : 
     444       50150 :                 if (multi_children_node > (zend_generator_node *) 0x1) {
     445           0 :                         zend_generator_merge_child_nodes(&generator->node, multi_children_node, child);
     446             :                 } else {
     447       50150 :                         zend_generator_add_single_child(&generator->node, child, leaf);
     448             :                 }
     449      100609 :                 while (parent) {
     450         309 :                         if (parent->node.children > 1) {
     451         309 :                                 if (multi_children_node == (zend_generator_node *) 0x1) {
     452          58 :                                         multi_children_node = zend_generator_search_multi_children_node(&child->node);
     453             :                                 }
     454         309 :                                 if (multi_children_node) {
     455           0 :                                         zend_generator_merge_child_nodes(&parent->node, multi_children_node, cur);
     456             :                                 } else {
     457         309 :                                         zend_generator_add_single_child(&parent->node, cur, leaf);
     458             :                                 }
     459             :                         }
     460         309 :                         cur = parent;
     461         309 :                         parent = parent->node.parent;
     462             :                 }
     463             :         }
     464       50150 : }
     465             : 
     466       50150 : void zend_generator_yield_from(zend_generator *generator, zend_generator *from)
     467             : {
     468       50150 :         zend_generator_add_child(from, generator);
     469             : 
     470       50150 :         generator->node.parent = from;
     471       50150 :         zend_generator_get_current(generator);
     472       50150 :         --GC_REFCOUNT(&from->std);
     473       50150 : }
     474             : 
     475     7256071 : ZEND_API zend_generator *zend_generator_get_current(zend_generator *generator)
     476             : {
     477             :         zend_generator *leaf;
     478             :         zend_generator *root, *old_root;
     479             : 
     480     7256071 :         if (generator->node.parent == NULL) {
     481             :                 /* we're not in yield from mode */
     482     7104842 :                 return generator;
     483             :         }
     484             : 
     485      151229 :         leaf = generator->node.children ? generator->node.ptr.leaf : generator;
     486      151229 :         root = leaf->node.ptr.root;
     487             : 
     488      151229 :         if (root->execute_data && root->node.parent == NULL) {
     489             :                 /* generator still running */
     490      101134 :                 return root;
     491             :         }
     492             : 
     493             :         /* generator at the root had stopped */
     494       50095 :         if (root != generator) {
     495       50029 :                 old_root = root;
     496       50029 :                 root = zend_generator_get_child(&root->node, leaf);
     497             :         } else {
     498          66 :                 old_root = NULL;
     499             :         }
     500             : 
     501      100190 :         while (!root->execute_data && root != generator) {
     502           0 :                 OBJ_RELEASE(&old_root->std);
     503           0 :                 old_root = root;
     504             : 
     505           0 :                 root = zend_generator_get_child(&root->node, leaf);
     506             :         }
     507             : 
     508       50095 :         if (root->node.parent) {
     509       50094 :                 if (root->node.parent->execute_data == NULL) {
     510       50028 :                         if (EXPECTED(EG(exception) == NULL)) {
     511       50022 :                                 zend_op *yield_from = (zend_op *) root->execute_data->opline - 1;
     512             : 
     513       50022 :                                 if (yield_from->opcode == ZEND_YIELD_FROM && !(yield_from->result_type & EXT_TYPE_UNUSED)) {
     514      100044 :                                         if (Z_ISUNDEF(root->node.parent->retval)) {
     515             :                                                 /* Throw the exception in the context of the generator */
     516           2 :                                                 zend_execute_data *original_execute_data = EG(current_execute_data);
     517           2 :                                                 EG(current_execute_data) = root->execute_data;
     518             : 
     519           2 :                                                 if (root == generator) {
     520           2 :                                                         root->execute_data->prev_execute_data = original_execute_data;
     521             :                                                 } else {
     522           0 :                                                         root->execute_data->prev_execute_data = &generator->execute_fake;
     523           0 :                                                         generator->execute_fake.prev_execute_data = original_execute_data;
     524             :                                                 }
     525             : 
     526           2 :                                                 zend_throw_exception(zend_ce_ClosedGeneratorException, "Generator yielded from aborted, no return value available", 0);
     527             : 
     528           2 :                                                 EG(current_execute_data) = original_execute_data;
     529             :                                         } else {
     530       50020 :                                                 ZVAL_COPY(ZEND_CALL_VAR(root->execute_data, yield_from->result.var), &root->node.parent->retval);
     531             :                                         }
     532             :                                 }
     533             :                         }
     534             : 
     535       50028 :                         root->node.parent = NULL;
     536             :                 } else {
     537             :                         do {
     538         221 :                                 root = root->node.parent;
     539         221 :                                 ++GC_REFCOUNT(&root->std);
     540         221 :                         } while (root->node.parent);
     541             :                 }
     542             :         }
     543             : 
     544       50095 :         if (old_root) {
     545       50029 :                 OBJ_RELEASE(&old_root->std);
     546             :         }
     547             : 
     548       50095 :         return leaf->node.ptr.root = root;
     549             : }
     550             : 
     551           8 : static int zend_generator_get_next_delegated_value(zend_generator *generator) /* {{{ */
     552             : {
     553             :         zval *value;
     554          16 :         if (Z_TYPE(generator->values) == IS_ARRAY) {
     555           4 :                 HashTable *ht = Z_ARR(generator->values);
     556           4 :                 HashPosition pos = Z_FE_POS(generator->values);
     557             : 
     558             :                 Bucket *p;
     559             :                 do {
     560           4 :                         if (UNEXPECTED(pos >= ht->nNumUsed)) {
     561             :                                 /* Reached end of array */
     562           2 :                                 goto failure;
     563             :                         }
     564             : 
     565           2 :                         p = &ht->arData[pos];
     566           2 :                         value = &p->val;
     567           2 :                         if (Z_TYPE_P(value) == IS_INDIRECT) {
     568           0 :                                 value = Z_INDIRECT_P(value);
     569             :                         }
     570           2 :                         pos++;
     571           2 :                 } while (Z_ISUNDEF_P(value));
     572             : 
     573           2 :                 zval_ptr_dtor(&generator->value);
     574           2 :                 ZVAL_COPY(&generator->value, value);
     575             : 
     576           2 :                 zval_ptr_dtor(&generator->key);
     577           2 :                 if (p->key) {
     578           0 :                         ZVAL_STR_COPY(&generator->key, p->key);
     579             :                 } else {
     580           2 :                         ZVAL_LONG(&generator->key, p->h);
     581             :                 }
     582             : 
     583           2 :                 Z_FE_POS(generator->values) = pos;
     584             :         } else {
     585           4 :                 zend_object_iterator *iter = (zend_object_iterator *) Z_OBJ(generator->values);
     586             : 
     587           4 :                 if (iter->index++ > 0) {
     588           3 :                         iter->funcs->move_forward(iter);
     589           3 :                         if (UNEXPECTED(EG(exception) != NULL)) {
     590           0 :                                 goto failure;
     591             :                         }
     592             :                 }
     593             : 
     594           4 :                 if (iter->funcs->valid(iter) == FAILURE) {
     595             :                         /* reached end of iteration */
     596           1 :                         goto failure;
     597             :                 }
     598             : 
     599           3 :                 value = iter->funcs->get_current_data(iter);
     600           3 :                 if (UNEXPECTED(EG(exception) != NULL || !value)) {
     601             :                         goto failure;
     602             :                 }
     603             : 
     604           3 :                 zval_ptr_dtor(&generator->value);
     605           3 :                 ZVAL_COPY(&generator->value, value);
     606             : 
     607           3 :                 zval_ptr_dtor(&generator->key);
     608           3 :                 if (iter->funcs->get_current_key) {
     609           3 :                         iter->funcs->get_current_key(iter, &generator->key);
     610           3 :                         if (UNEXPECTED(EG(exception) != NULL)) {
     611           0 :                                 ZVAL_UNDEF(&generator->key);
     612           0 :                                 goto failure;
     613             :                         }
     614             :                 } else {
     615           0 :                         ZVAL_LONG(&generator->key, iter->index);
     616             :                 }
     617             :         }
     618           5 :         return SUCCESS;
     619             : 
     620             : failure:
     621           3 :         zval_ptr_dtor(&generator->values);
     622           3 :         ZVAL_UNDEF(&generator->values);
     623           3 :         return FAILURE;
     624             : }
     625             : /* }}} */
     626             : 
     627     2352019 : ZEND_API void zend_generator_resume(zend_generator *orig_generator) /* {{{ */
     628             : {
     629             :         zend_generator *generator;
     630             : 
     631             :         /* The generator is already closed, thus can't resume */
     632     2352019 :         if (!orig_generator->execute_data) {
     633           2 :                 return;
     634             :         }
     635             : 
     636     2352017 :         generator = zend_generator_get_current(orig_generator);
     637             : 
     638             : try_again:
     639     2452195 :         if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
     640           2 :                 zend_throw_error(NULL, "Cannot resume an already running generator");
     641           2 :                 return;
     642             :         }
     643             : 
     644     4904386 :         if (!Z_ISUNDEF(generator->values)) {
     645           8 :                 if (zend_generator_get_next_delegated_value(generator) == SUCCESS) {
     646           5 :                         return;
     647             :                 }
     648             :                 /* If there are no more deletegated values, resume the generator
     649             :                  * after the "yield from" expression. */
     650             :         }
     651             : 
     652     2502607 :         if ((orig_generator->flags & ZEND_GENERATOR_DO_INIT) && !Z_ISUNDEF(generator->value)) {
     653             :                 /* We must not advance Generator if we yield from a Generator being currently run */
     654          64 :                 return;
     655             :         }
     656             : 
     657             :         /* Drop the AT_FIRST_YIELD flag */
     658     2452124 :         orig_generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
     659             : 
     660             :         {
     661             :                 /* Backup executor globals */
     662     2452124 :                 zend_execute_data *original_execute_data = EG(current_execute_data);
     663     2452124 :                 zend_class_entry *original_scope = EG(scope);
     664     2452124 :                 zend_vm_stack original_stack = EG(vm_stack);
     665     2452124 :                 original_stack->top = EG(vm_stack_top);
     666             : 
     667             :                 /* Set executor globals */
     668     2452124 :                 EG(current_execute_data) = generator->execute_data;
     669     2452124 :                 EG(scope) = generator->execute_data->func->common.scope;
     670     2452124 :                 EG(vm_stack_top) = generator->stack->top;
     671     2452124 :                 EG(vm_stack_end) = generator->stack->end;
     672     2452124 :                 EG(vm_stack) = generator->stack;
     673             : 
     674             :                 /* We want the backtrace to look as if the generator function was
     675             :                  * called from whatever method we are current running (e.g. next()).
     676             :                  * So we have to link generator call frame with caller call frame. */
     677     2452124 :                 if (generator == orig_generator) {
     678     2351814 :                         generator->execute_data->prev_execute_data = original_execute_data;
     679             :                 } else {
     680             :                         /* We need some execute_data placeholder in stacktrace to be replaced
     681             :                          * by the real stack trace when needed */
     682      100310 :                         generator->execute_data->prev_execute_data = &orig_generator->execute_fake;
     683      100310 :                         orig_generator->execute_fake.prev_execute_data = original_execute_data;
     684             :                 }
     685             : 
     686             :                 /* Resume execution */
     687     2452124 :                 generator->flags |= ZEND_GENERATOR_CURRENTLY_RUNNING;
     688     2452124 :                 zend_execute_ex(generator->execute_data);
     689     2452121 :                 generator->flags &= ~ZEND_GENERATOR_CURRENTLY_RUNNING;
     690             : 
     691             :                 /* Unlink generator call_frame from the caller and backup vm_stack_top */
     692     2452121 :                 if (generator->execute_data) {
     693     2401987 :                         generator->stack = EG(vm_stack);
     694     2401987 :                         generator->stack->top = EG(vm_stack_top);
     695             :                 }
     696             : 
     697             :                 /* Restore executor globals */
     698     2452121 :                 EG(current_execute_data) = original_execute_data;
     699     2452121 :                 EG(scope) = original_scope;
     700     2452121 :                 EG(vm_stack_top) = original_stack->top;
     701     2452121 :                 EG(vm_stack_end) = original_stack->end;
     702     2452121 :                 EG(vm_stack) = original_stack;
     703             : 
     704             :                 /* If an exception was thrown in the generator we have to internally
     705             :                  * rethrow it in the parent scope.
     706             :                  * In case we did yield from, the Exception must be rethrown into
     707             :                  * its calling frame (see above in if (check_yield_from). */
     708     2452121 :                 if (UNEXPECTED(EG(exception) != NULL)) {
     709          28 :                         if (generator == orig_generator) {
     710          22 :                                 zend_generator_close(generator, 0);
     711          22 :                                 zend_throw_exception_internal(NULL);
     712             :                         } else {
     713           6 :                                 generator = zend_generator_get_current(orig_generator);
     714           6 :                                 zend_generator_throw_exception(generator, NULL);
     715           6 :                                 goto try_again;
     716             :                         }
     717             :                 }
     718             : 
     719             :                 /* yield from was used, try another resume. */
     720     2552419 :                 if ((generator != orig_generator && !Z_ISUNDEF(generator->retval)) || (generator->execute_data && (generator->execute_data->opline - 1)->opcode == ZEND_YIELD_FROM)) {
     721      100172 :                         generator = zend_generator_get_current(orig_generator);
     722      100172 :                         goto try_again;
     723             :                 }
     724             :         }
     725             : }
     726             : /* }}} */
     727             : 
     728     2352865 : static void zend_generator_ensure_initialized(zend_generator *generator) /* {{{ */
     729             : {
     730     4705661 :         if (generator->execute_data && Z_TYPE(generator->value) == IS_UNDEF && generator->node.parent == NULL) {
     731         347 :                 generator->flags |= ZEND_GENERATOR_DO_INIT;
     732         347 :                 zend_generator_resume(generator);
     733         344 :                 generator->flags &= ~ZEND_GENERATOR_DO_INIT;
     734         344 :                 generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
     735             :         }
     736     2352862 : }
     737             : /* }}} */
     738             : 
     739          71 : static void zend_generator_rewind(zend_generator *generator) /* {{{ */
     740             : {
     741          71 :         zend_generator_ensure_initialized(generator);
     742             : 
     743          69 :         if (!(generator->flags & ZEND_GENERATOR_AT_FIRST_YIELD)) {
     744           1 :                 zend_throw_exception(NULL, "Cannot rewind a generator that was already run", 0);
     745             :         }
     746          69 : }
     747             : /* }}} */
     748             : 
     749             : /* {{{ proto void Generator::rewind()
     750             :  * Rewind the generator */
     751          19 : ZEND_METHOD(Generator, rewind)
     752             : {
     753             :         zend_generator *generator;
     754             : 
     755          19 :         if (zend_parse_parameters_none() == FAILURE) {
     756           0 :                 return;
     757             :         }
     758             : 
     759          19 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     760             : 
     761          19 :         zend_generator_rewind(generator);
     762             : }
     763             : /* }}} */
     764             : 
     765             : /* {{{ proto bool Generator::valid()
     766             :  * Check whether the generator is valid */
     767         331 : ZEND_METHOD(Generator, valid)
     768             : {
     769             :         zend_generator *generator;
     770             : 
     771         331 :         if (zend_parse_parameters_none() == FAILURE) {
     772           0 :                 return;
     773             :         }
     774             : 
     775         331 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     776             : 
     777         331 :         zend_generator_ensure_initialized(generator);
     778             : 
     779         331 :         zend_generator_get_current(generator);
     780             : 
     781         662 :         RETURN_BOOL(Z_TYPE(generator->value) != IS_UNDEF || generator->node.parent != NULL);
     782             : }
     783             : /* }}} */
     784             : 
     785             : /* {{{ proto mixed Generator::current()
     786             :  * Get the current value */
     787         407 : ZEND_METHOD(Generator, current)
     788             : {
     789             :         zend_generator *generator, *root;
     790             : 
     791         407 :         if (zend_parse_parameters_none() == FAILURE) {
     792           0 :                 return;
     793             :         }
     794             : 
     795         407 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     796             : 
     797         407 :         zend_generator_ensure_initialized(generator);
     798             : 
     799         406 :         root = zend_generator_get_current(generator);
     800         812 :         if (Z_TYPE(root->value) != IS_UNDEF) {
     801         402 :                 zval *value = &root->value;
     802             : 
     803         402 :                 ZVAL_DEREF(value);
     804         402 :                 ZVAL_COPY(return_value, value);
     805             :         }
     806             : }
     807             : /* }}} */
     808             : 
     809             : /* {{{ proto mixed Generator::key()
     810             :  * Get the current key */
     811          20 : ZEND_METHOD(Generator, key)
     812             : {
     813             :         zend_generator *generator, *root;
     814             : 
     815          20 :         if (zend_parse_parameters_none() == FAILURE) {
     816           0 :                 return;
     817             :         }
     818             : 
     819          20 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     820             : 
     821          20 :         zend_generator_ensure_initialized(generator);
     822             : 
     823          20 :         root = zend_generator_get_current(generator);
     824          40 :         if (Z_TYPE(root->key) != IS_UNDEF) {
     825          20 :                 zval *key = &root->key;
     826             : 
     827          20 :                 ZVAL_DEREF(key);
     828          20 :                 ZVAL_COPY(return_value, key);
     829             :         }
     830             : }
     831             : /* }}} */
     832             : 
     833             : /* {{{ proto void Generator::next()
     834             :  * Advances the generator */
     835         310 : ZEND_METHOD(Generator, next)
     836             : {
     837             :         zend_generator *generator;
     838             : 
     839         310 :         if (zend_parse_parameters_none() == FAILURE) {
     840           0 :                 return;
     841             :         }
     842             : 
     843         310 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     844             : 
     845         310 :         zend_generator_ensure_initialized(generator);
     846             : 
     847         310 :         zend_generator_resume(generator);
     848             : }
     849             : /* }}} */
     850             : 
     851             : /* {{{ proto mixed Generator::send(mixed value)
     852             :  * Sends a value to the generator */
     853     2351221 : ZEND_METHOD(Generator, send)
     854             : {
     855             :         zval *value;
     856             :         zend_generator *generator, *root;
     857             : 
     858     2351221 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
     859           0 :                 return;
     860             :         }
     861             : 
     862     2351221 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     863             : 
     864     2351221 :         zend_generator_ensure_initialized(generator);
     865             : 
     866             :         /* The generator is already closed, thus can't send anything */
     867     2351221 :         if (!generator->execute_data) {
     868           1 :                 return;
     869             :         }
     870             : 
     871     2351220 :         root = zend_generator_get_current(generator);
     872             :         /* Put sent value in the target VAR slot, if it is used */
     873     2351220 :         if (root->send_target) {
     874     2351215 :                 if (Z_REFCOUNTED_P(root->send_target)) Z_DELREF_P(root->send_target);
     875     2351215 :                 ZVAL_COPY(root->send_target, value);
     876             :         }
     877             : 
     878     2351220 :         zend_generator_resume(generator);
     879             : 
     880     2351220 :         root = zend_generator_get_current(generator);
     881     4702440 :         if (Z_TYPE(root->value) != IS_UNDEF) {
     882     2351209 :                 zval *value = &root->value;
     883             : 
     884     2351209 :                 ZVAL_DEREF(value);
     885     2351209 :                 ZVAL_COPY(return_value, value);
     886             :         }
     887             : }
     888             : /* }}} */
     889             : 
     890             : /* {{{ proto mixed Generator::throw(Exception exception)
     891             :  * Throws an exception into the generator */
     892          11 : ZEND_METHOD(Generator, throw)
     893             : {
     894             :         zval *exception, exception_copy;
     895             :         zend_generator *generator;
     896             : 
     897          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &exception) == FAILURE) {
     898           0 :                 return;
     899             :         }
     900             : 
     901          11 :         ZVAL_DUP(&exception_copy, exception);
     902             : 
     903          11 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     904             : 
     905          11 :         zend_generator_ensure_initialized(generator);
     906             : 
     907          11 :         if (generator->execute_data) {
     908          10 :                 zend_generator *root = zend_generator_get_current(generator);
     909             : 
     910          10 :                 zend_generator_throw_exception(root, &exception_copy);
     911             : 
     912          10 :                 zend_generator_resume(generator);
     913             : 
     914          10 :                 root = zend_generator_get_current(generator);
     915          20 :                 if (Z_TYPE(root->value) != IS_UNDEF) {
     916           5 :                         zval *value = &root->value;
     917             : 
     918           5 :                         ZVAL_DEREF(value);
     919           5 :                         ZVAL_COPY(return_value, value);
     920             :                 }
     921             :         } else {
     922             :                 /* If the generator is already closed throw the exception in the
     923             :                  * current context */
     924           1 :                 zend_throw_exception_object(&exception_copy);
     925             :         }
     926             : }
     927             : /* }}} */
     928             : 
     929             : /* {{{ proto mixed Generator::getReturn()
     930             :  * Retrieves the return value of the generator */
     931          19 : ZEND_METHOD(Generator, getReturn)
     932             : {
     933             :         zend_generator *generator;
     934             : 
     935          19 :         if (zend_parse_parameters_none() == FAILURE) {
     936           0 :                 return;
     937             :         }
     938             : 
     939          19 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     940             : 
     941          19 :         zend_generator_ensure_initialized(generator);
     942          19 :         if (EG(exception)) {
     943           2 :                 return;
     944             :         }
     945             : 
     946          34 :         if (Z_ISUNDEF(generator->retval)) {
     947             :                 /* Generator hasn't returned yet -> error! */
     948           4 :                 zend_throw_exception(NULL,
     949             :                         "Cannot get return value of a generator that hasn't returned", 0);
     950           4 :                 return;
     951             :         }
     952             : 
     953          13 :         ZVAL_COPY(return_value, &generator->retval);
     954             : }
     955             : /* }}} */
     956             : 
     957             : /* {{{ proto void Generator::__wakeup()
     958             :  * Throws an Exception as generators can't be serialized */
     959           0 : ZEND_METHOD(Generator, __wakeup)
     960             : {
     961             :         /* Just specifying the zend_class_unserialize_deny handler is not enough,
     962             :          * because it is only invoked for C unserialization. For O the error has
     963             :          * to be thrown in __wakeup. */
     964             : 
     965           0 :         if (zend_parse_parameters_none() == FAILURE) {
     966           0 :                 return;
     967             :         }
     968             : 
     969           0 :         zend_throw_exception(NULL, "Unserialization of 'Generator' is not allowed", 0);
     970             : }
     971             : /* }}} */
     972             : 
     973             : /* get_iterator implementation */
     974             : 
     975          53 : static void zend_generator_iterator_dtor(zend_object_iterator *iterator) /* {{{ */
     976             : {
     977          53 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     978          53 :         generator->iterator = NULL;
     979          53 :         zval_ptr_dtor(&iterator->data);
     980          53 :         zend_iterator_dtor(iterator);
     981          53 : }
     982             : /* }}} */
     983             : 
     984         173 : static int zend_generator_iterator_valid(zend_object_iterator *iterator) /* {{{ */
     985             : {
     986         173 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     987             : 
     988         173 :         zend_generator_ensure_initialized(generator);
     989             : 
     990         173 :         zend_generator_get_current(generator);
     991             : 
     992         346 :         return Z_TYPE(generator->value) != IS_UNDEF || generator->node.parent != NULL ? SUCCESS : FAILURE;
     993             : }
     994             : /* }}} */
     995             : 
     996         132 : static zval *zend_generator_iterator_get_data(zend_object_iterator *iterator) /* {{{ */
     997             : {
     998         132 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data), *root;
     999             : 
    1000         132 :         zend_generator_ensure_initialized(generator);
    1001             : 
    1002         132 :         root = zend_generator_get_current(generator);
    1003             : 
    1004         132 :         return &root->value;
    1005             : }
    1006             : /* }}} */
    1007             : 
    1008          42 : static void zend_generator_iterator_get_key(zend_object_iterator *iterator, zval *key) /* {{{ */
    1009             : {
    1010          42 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data), *root;
    1011             : 
    1012          42 :         zend_generator_ensure_initialized(generator);
    1013             : 
    1014          42 :         root = zend_generator_get_current(generator);
    1015             : 
    1016          84 :         if (Z_TYPE(root->key) != IS_UNDEF) {
    1017          42 :                 zval *zv = &root->key;
    1018             : 
    1019          42 :                 ZVAL_DEREF(zv);
    1020          42 :                 ZVAL_COPY(key, zv);
    1021             :         } else {
    1022           0 :                 ZVAL_NULL(key);
    1023             :         }
    1024          42 : }
    1025             : /* }}} */
    1026             : 
    1027         128 : static void zend_generator_iterator_move_forward(zend_object_iterator *iterator) /* {{{ */
    1028             : {
    1029         128 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
    1030             : 
    1031         128 :         zend_generator_ensure_initialized(generator);
    1032             : 
    1033         128 :         zend_generator_resume(generator);
    1034         128 : }
    1035             : /* }}} */
    1036             : 
    1037          52 : static void zend_generator_iterator_rewind(zend_object_iterator *iterator) /* {{{ */
    1038             : {
    1039          52 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
    1040             : 
    1041          52 :         zend_generator_rewind(generator);
    1042          52 : }
    1043             : /* }}} */
    1044             : 
    1045             : static zend_object_iterator_funcs zend_generator_iterator_functions = {
    1046             :         zend_generator_iterator_dtor,
    1047             :         zend_generator_iterator_valid,
    1048             :         zend_generator_iterator_get_data,
    1049             :         zend_generator_iterator_get_key,
    1050             :         zend_generator_iterator_move_forward,
    1051             :         zend_generator_iterator_rewind
    1052             : };
    1053             : 
    1054          55 : zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
    1055             : {
    1056             :         zend_object_iterator *iterator;
    1057          55 :         zend_generator *generator = (zend_generator*)Z_OBJ_P(object);
    1058             : 
    1059          55 :         if (!generator->execute_data) {
    1060           1 :                 zend_throw_exception(NULL, "Cannot traverse an already closed generator", 0);
    1061           1 :                 return NULL;
    1062             :         }
    1063             : 
    1064          54 :         if (by_ref && !(generator->execute_data->func->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
    1065           1 :                 zend_throw_exception(NULL, "You can only iterate a generator by-reference if it declared that it yields by-reference", 0);
    1066           1 :                 return NULL;
    1067             :         }
    1068             : 
    1069          53 :         iterator = generator->iterator = emalloc(sizeof(zend_object_iterator));
    1070             : 
    1071          53 :         zend_iterator_init(iterator);
    1072             : 
    1073          53 :         iterator->funcs = &zend_generator_iterator_functions;
    1074          53 :         ZVAL_COPY(&iterator->data, object);
    1075             : 
    1076          53 :         return iterator;
    1077             : }
    1078             : /* }}} */
    1079             : 
    1080             : ZEND_BEGIN_ARG_INFO(arginfo_generator_void, 0)
    1081             : ZEND_END_ARG_INFO()
    1082             : 
    1083             : ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_send, 0, 0, 1)
    1084             :         ZEND_ARG_INFO(0, value)
    1085             : ZEND_END_ARG_INFO()
    1086             : 
    1087             : ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_throw, 0, 0, 1)
    1088             :         ZEND_ARG_INFO(0, exception)
    1089             : ZEND_END_ARG_INFO()
    1090             : 
    1091             : static const zend_function_entry generator_functions[] = {
    1092             :         ZEND_ME(Generator, rewind,   arginfo_generator_void, ZEND_ACC_PUBLIC)
    1093             :         ZEND_ME(Generator, valid,    arginfo_generator_void, ZEND_ACC_PUBLIC)
    1094             :         ZEND_ME(Generator, current,  arginfo_generator_void, ZEND_ACC_PUBLIC)
    1095             :         ZEND_ME(Generator, key,      arginfo_generator_void, ZEND_ACC_PUBLIC)
    1096             :         ZEND_ME(Generator, next,     arginfo_generator_void, ZEND_ACC_PUBLIC)
    1097             :         ZEND_ME(Generator, send,     arginfo_generator_send, ZEND_ACC_PUBLIC)
    1098             :         ZEND_ME(Generator, throw,    arginfo_generator_throw, ZEND_ACC_PUBLIC)
    1099             :         ZEND_ME(Generator, getReturn,arginfo_generator_void, ZEND_ACC_PUBLIC)
    1100             :         ZEND_ME(Generator, __wakeup, arginfo_generator_void, ZEND_ACC_PUBLIC)
    1101             :         ZEND_FE_END
    1102             : };
    1103             : 
    1104       21285 : void zend_register_generator_ce(void) /* {{{ */
    1105             : {
    1106             :         zend_class_entry ce;
    1107             : 
    1108       21285 :         INIT_CLASS_ENTRY(ce, "Generator", generator_functions);
    1109       21285 :         zend_ce_generator = zend_register_internal_class(&ce);
    1110       21285 :         zend_ce_generator->ce_flags |= ZEND_ACC_FINAL;
    1111       21285 :         zend_ce_generator->create_object = zend_generator_create;
    1112       21285 :         zend_ce_generator->serialize = zend_class_serialize_deny;
    1113       21285 :         zend_ce_generator->unserialize = zend_class_unserialize_deny;
    1114             : 
    1115             :         /* get_iterator has to be assigned *after* implementing the inferface */
    1116       21285 :         zend_class_implements(zend_ce_generator, 1, zend_ce_iterator);
    1117       21285 :         zend_ce_generator->get_iterator = zend_generator_get_iterator;
    1118       21285 :         zend_ce_generator->iterator_funcs.funcs = &zend_generator_iterator_functions;
    1119             : 
    1120       21285 :         memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    1121       21285 :         zend_generator_handlers.free_obj = zend_generator_free_storage;
    1122       21285 :         zend_generator_handlers.dtor_obj = zend_generator_dtor_storage;
    1123       21285 :         zend_generator_handlers.clone_obj = NULL;
    1124       21285 :         zend_generator_handlers.get_constructor = zend_generator_get_constructor;
    1125             : 
    1126       21285 :         INIT_CLASS_ENTRY(ce, "ClosedGeneratorException", NULL);
    1127       21285 :         zend_ce_ClosedGeneratorException = zend_register_internal_class_ex(&ce, zend_ce_exception);
    1128       21285 : }
    1129             : /* }}} */
    1130             : 
    1131             : /*
    1132             :  * Local variables:
    1133             :  * tab-width: 4
    1134             :  * c-basic-offset: 4
    1135             :  * indent-tabs-mode: t
    1136             :  * End:
    1137             :  */

Generated by: LCOV version 1.10

Generated at Tue, 25 Aug 2015 07:24:13 +0000 (2 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.