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: 473 575 82.3 %
Date: 2016-04-27 Functions: 35 39 89.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 27 Apr 2016 15:51:35 +0000 (3 days ago)

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