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: 253 270 93.7 %
Date: 2014-09-27 Functions: 26 27 96.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 27 Sep 2014 16:43:04 +0000 (3 days ago)

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