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

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:40 +0000 (3 days ago)

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