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: 260 277 93.9 %
Date: 2014-11-22 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 ((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_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 (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         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 (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          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(vm_stack);
     221             : 
     222          82 :         current_stack->top = EG(vm_stack_top);
     223             :         /* Create a clone of closure, because it may be destroyed */
     224          82 :         if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
     225           7 :                 zend_op_array *op_array_copy = (zend_op_array*)emalloc(sizeof(zend_op_array));
     226           7 :                 *op_array_copy = *op_array;
     227             : 
     228           7 :                 (*op_array->refcount)++;
     229           7 :                 op_array->run_time_cache = NULL;
     230           7 :                 if (op_array->static_variables) {
     231           5 :                         ALLOC_HASHTABLE(op_array_copy->static_variables);
     232           5 :                         zend_hash_init(
     233             :                                 op_array_copy->static_variables, 
     234             :                                 zend_hash_num_elements(op_array->static_variables),
     235             :                                 NULL, ZVAL_PTR_DTOR, 0
     236             :                         );
     237           5 :                         zend_hash_apply_with_arguments(
     238             :                                 op_array->static_variables TSRMLS_CC,
     239             :                                 copy_closure_static_var, 1, 
     240             :                                 op_array_copy->static_variables
     241             :                         );
     242             :                 }
     243             : 
     244           7 :                 op_array = op_array_copy;
     245             :         }
     246             :         
     247             :         /* Create new execution context. We have to back up and restore
     248             :          * EG(current_execute_data) here. */
     249          82 :         current_execute_data = EG(current_execute_data);
     250          82 :         execute_data = zend_create_generator_execute_data(call, op_array, return_value TSRMLS_CC);
     251          82 :         EG(current_execute_data) = current_execute_data;
     252             : 
     253          82 :         object_init_ex(return_value, zend_ce_generator);
     254             : 
     255          82 :         if (Z_OBJ(call->This)) {
     256           3 :                 Z_ADDREF(call->This);
     257             :         }
     258             : 
     259             :         /* Save execution context in generator object. */
     260          82 :         generator = (zend_generator *) Z_OBJ_P(return_value);
     261          82 :         execute_data->prev_execute_data = NULL;
     262          82 :         generator->execute_data = execute_data;
     263          82 :         generator->stack = EG(vm_stack);
     264          82 :         generator->stack->top = EG(vm_stack_top);
     265          82 :         EG(vm_stack_top) = current_stack->top;
     266          82 :         EG(vm_stack_end) = current_stack->end;
     267          82 :         EG(vm_stack) = current_stack;
     268             : 
     269             :         /* EX(return_value) keeps pointer to zend_object (not a real zval) */
     270          82 :         execute_data->return_value = (zval*)generator;
     271          82 : }
     272             : /* }}} */
     273             : 
     274           2 : static zend_function *zend_generator_get_constructor(zend_object *object TSRMLS_DC) /* {{{ */
     275             : {
     276           2 :         zend_error(E_RECOVERABLE_ERROR, "The \"Generator\" class is reserved for internal use and cannot be manually instantiated");
     277             : 
     278           0 :         return NULL;
     279             : }
     280             : /* }}} */
     281             : 
     282         209 : ZEND_API void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
     283             : {
     284             :         /* The generator is already closed, thus can't resume */
     285         209 :         if (!generator->execute_data) {
     286           1 :                 return;
     287             :         }
     288             : 
     289         208 :         if (generator->flags & ZEND_GENERATOR_CURRENTLY_RUNNING) {
     290           1 :                 zend_error(E_ERROR, "Cannot resume an already running generator");
     291             :         }
     292             : 
     293             :         /* Drop the AT_FIRST_YIELD flag */
     294         207 :         generator->flags &= ~ZEND_GENERATOR_AT_FIRST_YIELD;
     295             : 
     296             :         {
     297             :                 /* Backup executor globals */
     298         207 :                 zend_execute_data *original_execute_data = EG(current_execute_data);
     299         207 :                 zend_class_entry *original_scope = EG(scope);
     300         207 :                 zend_vm_stack original_stack = EG(vm_stack);
     301             : 
     302         207 :                 original_stack->top = EG(vm_stack_top);
     303             :                 /* Set executor globals */
     304         207 :                 EG(current_execute_data) = generator->execute_data;
     305         207 :                 EG(scope) = generator->execute_data->scope;
     306         207 :                 EG(vm_stack_top) = generator->stack->top;
     307         207 :                 EG(vm_stack_end) = generator->stack->end;
     308         207 :                 EG(vm_stack) = generator->stack;
     309             : 
     310             :                 /* We want the backtrace to look as if the generator function was
     311             :                  * called from whatever method we are current running (e.g. next()).
     312             :                  * So we have to link generator call frame with caller call frames */
     313             : 
     314         207 :                 generator->execute_data->prev_execute_data = original_execute_data;
     315             : 
     316             :                 /* Resume execution */
     317         207 :                 generator->flags |= ZEND_GENERATOR_CURRENTLY_RUNNING;
     318         207 :                 zend_execute_ex(generator->execute_data TSRMLS_CC);
     319         200 :                 generator->flags &= ~ZEND_GENERATOR_CURRENTLY_RUNNING;
     320             : 
     321             :                 /* Unlink generator call_frame from the caller */
     322         200 :                 if (generator->execute_data) {
     323         143 :                         generator->execute_data->prev_execute_data = NULL;
     324             :                 }
     325             : 
     326             :                 /* Restore executor globals */
     327         200 :                 EG(current_execute_data) = original_execute_data;
     328         200 :                 EG(scope) = original_scope;
     329         200 :                 EG(vm_stack_top) = original_stack->top;
     330         200 :                 EG(vm_stack_end) = original_stack->end;
     331         200 :                 EG(vm_stack) = original_stack;
     332             : 
     333             :                 /* If an exception was thrown in the generator we have to internally
     334             :                  * rethrow it in the parent scope. */
     335         200 :                 if (UNEXPECTED(EG(exception) != NULL)) {
     336           8 :                         zend_throw_exception_internal(NULL TSRMLS_CC);
     337             :                 }
     338             :         }
     339             : }
     340             : /* }}} */
     341             : 
     342         482 : static void zend_generator_ensure_initialized(zend_generator *generator TSRMLS_DC) /* {{{ */
     343             : {
     344         926 :         if (generator->execute_data && Z_TYPE(generator->value) == IS_UNDEF) {
     345          78 :                 zend_generator_resume(generator TSRMLS_CC);
     346          74 :                 generator->flags |= ZEND_GENERATOR_AT_FIRST_YIELD;
     347             :         }
     348         478 : }
     349             : /* }}} */
     350             : 
     351          53 : static void zend_generator_rewind(zend_generator *generator TSRMLS_DC) /* {{{ */
     352             : {
     353          53 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     354             : 
     355          50 :         if (!(generator->flags & ZEND_GENERATOR_AT_FIRST_YIELD)) {
     356           1 :                 zend_throw_exception(NULL, "Cannot rewind a generator that was already run", 0 TSRMLS_CC);
     357             :         }
     358          50 : }
     359             : /* }}} */
     360             : 
     361             : /* {{{ proto void Generator::rewind()
     362             :  * Rewind the generator */
     363          15 : ZEND_METHOD(Generator, rewind)
     364             : {
     365             :         zend_generator *generator;
     366             : 
     367          15 :         if (zend_parse_parameters_none() == FAILURE) {
     368           0 :                 return;
     369             :         }
     370             : 
     371          15 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     372             : 
     373          15 :         zend_generator_rewind(generator TSRMLS_CC);
     374             : }
     375             : /* }}} */
     376             : 
     377             : /* {{{ proto bool Generator::valid()
     378             :  * Check whether the generator is valid */
     379          10 : ZEND_METHOD(Generator, valid)
     380             : {
     381             :         zend_generator *generator;
     382             : 
     383          10 :         if (zend_parse_parameters_none() == FAILURE) {
     384           0 :                 return;
     385             :         }
     386             : 
     387          10 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     388             : 
     389          10 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     390             : 
     391          20 :         RETURN_BOOL(Z_TYPE(generator->value) != IS_UNDEF);
     392             : }
     393             : /* }}} */
     394             : 
     395             : /* {{{ proto mixed Generator::current()
     396             :  * Get the current value */
     397          22 : ZEND_METHOD(Generator, current)
     398             : {
     399             :         zend_generator *generator;
     400             : 
     401          22 :         if (zend_parse_parameters_none() == FAILURE) {
     402           0 :                 return;
     403             :         }
     404             : 
     405          22 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     406             : 
     407          22 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     408             : 
     409          42 :         if (Z_TYPE(generator->value) != IS_UNDEF) {
     410          76 :                 RETURN_ZVAL_FAST(&generator->value);
     411             :         }
     412             : }
     413             : /* }}} */
     414             : 
     415             : /* {{{ proto mixed Generator::key()
     416             :  * Get the current key */
     417           1 : ZEND_METHOD(Generator, key)
     418             : {
     419             :         zend_generator *generator;
     420             : 
     421           1 :         if (zend_parse_parameters_none() == FAILURE) {
     422           0 :                 return;
     423             :         }
     424             : 
     425           1 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     426             : 
     427           1 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     428             : 
     429           2 :         if (Z_TYPE(generator->key) != IS_UNDEF) {
     430           4 :                 RETURN_ZVAL_FAST(&generator->key);
     431             :         }
     432             : }
     433             : /* }}} */
     434             : 
     435             : /* {{{ proto void Generator::next()
     436             :  * Advances the generator */
     437          15 : ZEND_METHOD(Generator, next)
     438             : {
     439             :         zend_generator *generator;
     440             : 
     441          15 :         if (zend_parse_parameters_none() == FAILURE) {
     442           0 :                 return;
     443             :         }
     444             : 
     445          15 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     446             : 
     447          15 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     448             : 
     449          15 :         zend_generator_resume(generator TSRMLS_CC);
     450             : }
     451             : /* }}} */
     452             : 
     453             : /* {{{ proto mixed Generator::send(mixed $value)
     454             :  * Sends a value to the generator */
     455          14 : ZEND_METHOD(Generator, send)
     456             : {
     457             :         zval *value;
     458             :         zend_generator *generator;
     459             : 
     460          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) {
     461           0 :                 return;
     462             :         }
     463             : 
     464          14 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     465             : 
     466          14 :         zend_generator_ensure_initialized(generator TSRMLS_CC); 
     467             : 
     468             :         /* The generator is already closed, thus can't send anything */
     469          14 :         if (!generator->execute_data) {
     470           1 :                 return;
     471             :         }
     472             : 
     473             :         /* Put sent value in the target VAR slot, if it is used */
     474          13 :         if (generator->send_target) {
     475          12 :                 if (Z_REFCOUNTED_P(generator->send_target)) Z_DELREF_P(generator->send_target);
     476          12 :                 ZVAL_COPY(generator->send_target, value);
     477             :         }
     478             : 
     479          13 :         zend_generator_resume(generator TSRMLS_CC);
     480             : 
     481          24 :         if (Z_TYPE(generator->value) != IS_UNDEF) {
     482          24 :                 RETURN_ZVAL_FAST(&generator->value);
     483             :         }
     484             : }
     485             : /* }}} */
     486             : 
     487             : /* {{{ proto mixed Generator::throw(Exception $exception)
     488             :  * Throws an exception into the generator */
     489           5 : ZEND_METHOD(Generator, throw)
     490             : {
     491             :         zval *exception, exception_copy;
     492             :         zend_generator *generator;
     493             : 
     494           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &exception) == FAILURE) {
     495           0 :                 return;
     496             :         }
     497             : 
     498           5 :         ZVAL_DUP(&exception_copy, exception);
     499             : 
     500           5 :         generator = (zend_generator *) Z_OBJ_P(getThis());
     501             : 
     502           5 :         zend_generator_ensure_initialized(generator TSRMLS_CC); 
     503             : 
     504           5 :         if (generator->execute_data) {
     505             :                 /* Throw the exception in the context of the generator */
     506           4 :                 zend_execute_data *current_execute_data = EG(current_execute_data);
     507           4 :                 EG(current_execute_data) = generator->execute_data;
     508             : 
     509           4 :                 zend_throw_exception_object(&exception_copy TSRMLS_CC);
     510             : 
     511           3 :                 EG(current_execute_data) = current_execute_data;
     512             : 
     513           3 :                 zend_generator_resume(generator TSRMLS_CC);
     514             : 
     515           6 :                 if (Z_TYPE(generator->value) != IS_UNDEF) {
     516           4 :                         RETURN_ZVAL_FAST(&generator->value);
     517             :                 }
     518             :         } else {
     519             :                 /* If the generator is already closed throw the exception in the
     520             :                  * current context */
     521           1 :                 zend_throw_exception_object(&exception_copy TSRMLS_CC);
     522             :         }
     523             : }
     524             : /* }}} */
     525             : 
     526             : /* {{{ proto void Generator::__wakeup()
     527             :  * Throws an Exception as generators can't be serialized */
     528           0 : ZEND_METHOD(Generator, __wakeup)
     529             : {
     530             :         /* Just specifying the zend_class_unserialize_deny handler is not enough,
     531             :          * because it is only invoked for C unserialization. For O the error has
     532             :          * to be thrown in __wakeup. */
     533             : 
     534           0 :         if (zend_parse_parameters_none() == FAILURE) {
     535           0 :                 return;
     536             :         }
     537             : 
     538           0 :         zend_throw_exception(NULL, "Unserialization of 'Generator' is not allowed", 0 TSRMLS_CC);
     539             : }
     540             : /* }}} */
     541             : 
     542             : /* get_iterator implementation */
     543             : 
     544          38 : static void zend_generator_iterator_dtor(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
     545             : {
     546          38 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     547          38 :         generator->iterator = NULL;
     548          38 :         zval_ptr_dtor(&iterator->data);
     549          38 :         zend_iterator_dtor(iterator TSRMLS_CC);
     550          38 : }
     551             : /* }}} */
     552             : 
     553         129 : static int zend_generator_iterator_valid(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
     554             : {
     555         129 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     556             : 
     557         129 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     558             : 
     559         258 :         return Z_TYPE(generator->value) != IS_UNDEF ? SUCCESS : FAILURE;
     560             : }
     561             : /* }}} */
     562             : 
     563          98 : static zval *zend_generator_iterator_get_data(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
     564             : {
     565          98 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     566             : 
     567          98 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     568             : 
     569          98 :         return &generator->value;
     570             : }
     571             : /* }}} */
     572             : 
     573          38 : static void zend_generator_iterator_get_key(zend_object_iterator *iterator, zval *key TSRMLS_DC) /* {{{ */
     574             : {
     575          38 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     576             : 
     577          38 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     578             : 
     579          76 :         if (Z_TYPE(generator->key) != IS_UNDEF) {
     580          76 :                 ZVAL_ZVAL(key, &generator->key, 1, 0);
     581             :         } else {
     582           0 :                 ZVAL_NULL(key);
     583             :         }
     584          38 : }
     585             : /* }}} */
     586             : 
     587          97 : static void zend_generator_iterator_move_forward(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
     588             : {
     589          97 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     590             : 
     591          97 :         zend_generator_ensure_initialized(generator TSRMLS_CC);
     592             : 
     593          97 :         zend_generator_resume(generator TSRMLS_CC);
     594          96 : }
     595             : /* }}} */
     596             : 
     597          38 : static void zend_generator_iterator_rewind(zend_object_iterator *iterator TSRMLS_DC) /* {{{ */
     598             : {
     599          38 :         zend_generator *generator = (zend_generator*)Z_OBJ(iterator->data);
     600             : 
     601          38 :         zend_generator_rewind(generator TSRMLS_CC);
     602          37 : }
     603             : /* }}} */
     604             : 
     605             : static zend_object_iterator_funcs zend_generator_iterator_functions = {
     606             :         zend_generator_iterator_dtor,
     607             :         zend_generator_iterator_valid,
     608             :         zend_generator_iterator_get_data,
     609             :         zend_generator_iterator_get_key,
     610             :         zend_generator_iterator_move_forward,
     611             :         zend_generator_iterator_rewind
     612             : };
     613             : 
     614          40 : zend_object_iterator *zend_generator_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC) /* {{{ */
     615             : {
     616             :         zend_object_iterator *iterator;
     617          40 :         zend_generator *generator = (zend_generator*)Z_OBJ_P(object);
     618             : 
     619          40 :         if (!generator->execute_data) {
     620           1 :                 zend_throw_exception(NULL, "Cannot traverse an already closed generator", 0 TSRMLS_CC);
     621           1 :                 return NULL;
     622             :         }
     623             : 
     624          39 :         if (by_ref && !(generator->execute_data->func->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
     625           1 :                 zend_throw_exception(NULL, "You can only iterate a generator by-reference if it declared that it yields by-reference", 0 TSRMLS_CC);
     626           1 :                 return NULL;
     627             :         }
     628             : 
     629          38 :         iterator = generator->iterator = emalloc(sizeof(zend_object_iterator));
     630             :         
     631          38 :         zend_iterator_init(iterator TSRMLS_CC);
     632             : 
     633          38 :         iterator->funcs = &zend_generator_iterator_functions;
     634          38 :         ZVAL_COPY(&iterator->data, object);
     635             : 
     636          38 :         return iterator;
     637             : }
     638             : /* }}} */
     639             : 
     640             : ZEND_BEGIN_ARG_INFO(arginfo_generator_void, 0)
     641             : ZEND_END_ARG_INFO()
     642             : 
     643             : ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_send, 0, 0, 1)
     644             :         ZEND_ARG_INFO(0, value)
     645             : ZEND_END_ARG_INFO()
     646             : 
     647             : ZEND_BEGIN_ARG_INFO_EX(arginfo_generator_throw, 0, 0, 1)
     648             :         ZEND_ARG_INFO(0, exception)
     649             : ZEND_END_ARG_INFO()
     650             : 
     651             : static const zend_function_entry generator_functions[] = {
     652             :         ZEND_ME(Generator, rewind,   arginfo_generator_void, ZEND_ACC_PUBLIC)
     653             :         ZEND_ME(Generator, valid,    arginfo_generator_void, ZEND_ACC_PUBLIC)
     654             :         ZEND_ME(Generator, current,  arginfo_generator_void, ZEND_ACC_PUBLIC)
     655             :         ZEND_ME(Generator, key,      arginfo_generator_void, ZEND_ACC_PUBLIC)
     656             :         ZEND_ME(Generator, next,     arginfo_generator_void, ZEND_ACC_PUBLIC)
     657             :         ZEND_ME(Generator, send,     arginfo_generator_send, ZEND_ACC_PUBLIC)
     658             :         ZEND_ME(Generator, throw,    arginfo_generator_throw, ZEND_ACC_PUBLIC)
     659             :         ZEND_ME(Generator, __wakeup, arginfo_generator_void, ZEND_ACC_PUBLIC)
     660             :         ZEND_FE_END
     661             : };
     662             : 
     663       20507 : void zend_register_generator_ce(TSRMLS_D) /* {{{ */
     664             : {
     665             :         zend_class_entry ce;
     666             : 
     667       20507 :         INIT_CLASS_ENTRY(ce, "Generator", generator_functions);
     668       20507 :         zend_ce_generator = zend_register_internal_class(&ce TSRMLS_CC);
     669       20507 :         zend_ce_generator->ce_flags |= ZEND_ACC_FINAL_CLASS;
     670       20507 :         zend_ce_generator->create_object = zend_generator_create;
     671       20507 :         zend_ce_generator->serialize = zend_class_serialize_deny;
     672       20507 :         zend_ce_generator->unserialize = zend_class_unserialize_deny;
     673             : 
     674             :         /* get_iterator has to be assigned *after* implementing the inferface */
     675       20507 :         zend_class_implements(zend_ce_generator TSRMLS_CC, 1, zend_ce_iterator);
     676       20507 :         zend_ce_generator->get_iterator = zend_generator_get_iterator;
     677       20507 :         zend_ce_generator->iterator_funcs.funcs = &zend_generator_iterator_functions;
     678             : 
     679       20507 :         memcpy(&zend_generator_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
     680       20507 :         zend_generator_handlers.free_obj = zend_generator_free_storage;
     681       20507 :         zend_generator_handlers.dtor_obj = zend_generator_dtor_storage;
     682       20507 :         zend_generator_handlers.clone_obj = NULL;
     683       20507 :         zend_generator_handlers.get_constructor = zend_generator_get_constructor;
     684       20507 : }
     685             : /* }}} */
     686             : 
     687             : /*
     688             :  * Local variables:
     689             :  * tab-width: 4
     690             :  * c-basic-offset: 4
     691             :  * indent-tabs-mode: t
     692             :  * End:
     693             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:11 +0000 (3 days ago)

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