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_objects.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 110 112 98.2 %
Date: 2014-08-04 Functions: 8 8 100.0 %
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: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "zend.h"
      23             : #include "zend_globals.h"
      24             : #include "zend_variables.h"
      25             : #include "zend_API.h"
      26             : #include "zend_interfaces.h"
      27             : #include "zend_exceptions.h"
      28             : 
      29      959678 : ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce TSRMLS_DC)
      30             : {
      31      959678 :         object->ce = ce;
      32      959678 :         object->properties = NULL;
      33      959678 :         object->properties_table = NULL;
      34      959678 :         object->guards = NULL;
      35      959678 : }
      36             : 
      37     1761585 : ZEND_API void zend_object_std_dtor(zend_object *object TSRMLS_DC)
      38             : {
      39     1761585 :         if (object->guards) {
      40          69 :                 zend_hash_destroy(object->guards);
      41          69 :                 FREE_HASHTABLE(object->guards);
      42             :         }
      43     1761585 :         if (object->properties) {
      44      608979 :                 zend_hash_destroy(object->properties);
      45      608979 :                 FREE_HASHTABLE(object->properties);
      46      608979 :                 if (object->properties_table) {
      47       12262 :                         efree(object->properties_table);
      48             :                 }
      49     1152606 :         } else if (object->properties_table) {
      50             :                 int i;
      51             : 
      52      341328 :                 for (i = 0; i < object->ce->default_properties_count; i++) {
      53      241170 :                         if (object->properties_table[i]) {
      54      241139 :                                 zval_ptr_dtor(&object->properties_table[i]);
      55             :                         }
      56             :                 }
      57      100158 :                 efree(object->properties_table);
      58             :         }
      59     1761585 : }
      60             : 
      61     1356948 : ZEND_API void zend_objects_destroy_object(zend_object *object, zend_object_handle handle TSRMLS_DC)
      62             : {
      63     1356948 :         zend_function *destructor = object ? object->ce->destructor : NULL;
      64             : 
      65     1356948 :         if (destructor) {
      66             :                 zval *old_exception;
      67             :                 zval *obj;
      68             :                 zend_object_store_bucket *obj_bucket;
      69             : 
      70       98414 :                 if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
      71           8 :                         if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
      72             :                                 /* Ensure that if we're calling a private function, we're allowed to do so.
      73             :                                  */
      74           4 :                                 if (object->ce != EG(scope)) {
      75           4 :                                         zend_class_entry *ce = object->ce;
      76             : 
      77           8 :                                         zend_error(EG(in_execution) ? E_ERROR : E_WARNING,
      78             :                                                 "Call to private %s::__destruct() from context '%s'%s",
      79             :                                                 ce->name,
      80           4 :                                                 EG(scope) ? EG(scope)->name : "",
      81           4 :                                                 EG(in_execution) ? "" : " during shutdown ignored");
      82           2 :                                         return;
      83             :                                 }
      84             :                         } else {
      85             :                                 /* Ensure that if we're calling a protected function, we're allowed to do so.
      86             :                                  */
      87           4 :                                 if (!zend_check_protected(zend_get_function_root_class(destructor), EG(scope))) {
      88           3 :                                         zend_class_entry *ce = object->ce;
      89             : 
      90           6 :                                         zend_error(EG(in_execution) ? E_ERROR : E_WARNING,
      91             :                                                 "Call to protected %s::__destruct() from context '%s'%s",
      92             :                                                 ce->name,
      93           3 :                                                 EG(scope) ? EG(scope)->name : "",
      94           3 :                                                 EG(in_execution) ? "" : " during shutdown ignored");
      95           2 :                                         return;
      96             :                                 }
      97             :                         }
      98             :                 }
      99             : 
     100       98407 :                 MAKE_STD_ZVAL(obj);
     101       98407 :                 Z_TYPE_P(obj) = IS_OBJECT;
     102       98407 :                 Z_OBJ_HANDLE_P(obj) = handle;
     103       98407 :                 obj_bucket = &EG(objects_store).object_buckets[handle];
     104       98407 :                 if (!obj_bucket->bucket.obj.handlers) {
     105       97421 :                         obj_bucket->bucket.obj.handlers = &std_object_handlers;
     106             :                 }
     107       98407 :                 Z_OBJ_HT_P(obj) = obj_bucket->bucket.obj.handlers;
     108       98407 :                 zval_copy_ctor(obj);
     109             : 
     110             :                 /* Make sure that destructors are protected from previously thrown exceptions.
     111             :                  * For example, if an exception was thrown in a function and when the function's
     112             :                  * local variable destruction results in a destructor being called.
     113             :                  */
     114       98407 :                 old_exception = NULL;
     115       98407 :                 if (EG(exception)) {
     116          22 :                         if (Z_OBJ_HANDLE_P(EG(exception)) == handle) {
     117           0 :                                 zend_error(E_ERROR, "Attempt to destruct pending exception");
     118             :                         } else {
     119          22 :                                 old_exception = EG(exception);
     120          22 :                                 EG(exception) = NULL;
     121             :                         }
     122             :                 }
     123       98407 :                 zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
     124       98404 :                 if (old_exception) {
     125          22 :                         if (EG(exception)) {
     126           3 :                                 zend_exception_set_previous(EG(exception), old_exception TSRMLS_CC);
     127             :                         } else {
     128          19 :                                 EG(exception) = old_exception;
     129             :                         }
     130             :                 }
     131       98404 :                 zval_ptr_dtor(&obj);
     132             :         }
     133             : }
     134             : 
     135      805240 : ZEND_API void zend_objects_free_object_storage(zend_object *object TSRMLS_DC)
     136             : {
     137      805240 :         zend_object_std_dtor(object TSRMLS_CC);
     138      805240 :         efree(object);
     139      805240 : }
     140             : 
     141      801898 : ZEND_API zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type TSRMLS_DC)
     142             : {
     143             :         zend_object_value retval;
     144             : 
     145      801898 :         *object = emalloc(sizeof(zend_object));
     146      801898 :         (*object)->ce = class_type;
     147      801898 :         (*object)->properties = NULL;
     148      801898 :         (*object)->properties_table = NULL;
     149      801898 :         (*object)->guards = NULL;
     150      801898 :         retval.handle = zend_objects_store_put(*object, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
     151      801898 :         retval.handlers = &std_object_handlers;
     152      801898 :         return retval;
     153             : }
     154             : 
     155      137127 : ZEND_API zend_object *zend_objects_get_address(const zval *zobject TSRMLS_DC)
     156             : {
     157      137127 :         return (zend_object *)zend_object_store_get_object(zobject TSRMLS_CC);
     158             : }
     159             : 
     160      174341 : ZEND_API void zend_objects_clone_members(zend_object *new_object, zend_object_value new_obj_val, zend_object *old_object, zend_object_handle handle TSRMLS_DC)
     161             : {
     162             :         int i;
     163             : 
     164      174341 :         if (old_object->properties_table) {
     165          12 :                 if (!new_object->properties_table) {
     166           6 :                         new_object->properties_table = emalloc(sizeof(zval*) * old_object->ce->default_properties_count);
     167           6 :                         memset(new_object->properties_table, 0, sizeof(zval*) * old_object->ce->default_properties_count);
     168             :                 }
     169          46 :                 for (i = 0; i < old_object->ce->default_properties_count; i++) {
     170          34 :                         if (!new_object->properties) {
     171          34 :                                 if (new_object->properties_table[i]) {
     172          14 :                                         zval_ptr_dtor(&new_object->properties_table[i]);
     173             :                                 }
     174             :                         }
     175          34 :                         if (!old_object->properties) {
     176          20 :                                 new_object->properties_table[i] = old_object->properties_table[i];
     177          20 :                                 if (new_object->properties_table[i]) {
     178          16 :                                         Z_ADDREF_P(new_object->properties_table[i]);
     179             :                                 }
     180             :                         }
     181             :                 }
     182             :         }
     183      174341 :         if (old_object->properties) {
     184          16 :                 if (!new_object->properties) {
     185          14 :                         ALLOC_HASHTABLE(new_object->properties);
     186          14 :                         zend_hash_init(new_object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
     187             :                 }
     188          16 :                 zend_hash_copy(new_object->properties, old_object->properties, (copy_ctor_func_t) zval_add_ref, (void *) NULL /* Not used anymore */, sizeof(zval *));
     189          16 :                 if (old_object->properties_table) {
     190             :                         HashPosition pos;
     191             :                         zend_property_info *prop_info;
     192          24 :                         for (zend_hash_internal_pointer_reset_ex(&old_object->ce->properties_info, &pos);
     193          19 :                              zend_hash_get_current_data_ex(&old_object->ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
     194          14 :                              zend_hash_move_forward_ex(&old_object->ce->properties_info, &pos)) {
     195          14 :                                 if ((prop_info->flags & ZEND_ACC_STATIC) == 0) {
     196          14 :                                         if (zend_hash_quick_find(new_object->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&new_object->properties_table[prop_info->offset]) == FAILURE) {
     197           0 :                                                 new_object->properties_table[prop_info->offset] = NULL;
     198             :                                         }
     199             :                                 }
     200             :                         }
     201             :                 }
     202             :         }
     203             : 
     204      174341 :         if (old_object->ce->clone) {
     205             :                 zval *new_obj;
     206             : 
     207          11 :                 MAKE_STD_ZVAL(new_obj);
     208          11 :                 new_obj->type = IS_OBJECT;
     209          11 :                 new_obj->value.obj = new_obj_val;
     210          11 :                 zval_copy_ctor(new_obj);
     211             : 
     212          11 :                 zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
     213             : 
     214          11 :                 zval_ptr_dtor(&new_obj);
     215             :         }
     216      174341 : }
     217             : 
     218          28 : ZEND_API zend_object_value zend_objects_clone_obj(zval *zobject TSRMLS_DC)
     219             : {
     220             :         zend_object_value new_obj_val;
     221             :         zend_object *old_object;
     222             :         zend_object *new_object;
     223          28 :         zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
     224             : 
     225             :         /* assume that create isn't overwritten, so when clone depends on the
     226             :          * overwritten one then it must itself be overwritten */
     227          28 :         old_object = zend_objects_get_address(zobject TSRMLS_CC);
     228          28 :         new_obj_val = zend_objects_new(&new_object, old_object->ce TSRMLS_CC);
     229             : 
     230          28 :         zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
     231             : 
     232          28 :         return new_obj_val;
     233             : }
     234             : 
     235             : /*
     236             :  * Local variables:
     237             :  * tab-width: 4
     238             :  * c-basic-offset: 4
     239             :  * indent-tabs-mode: t
     240             :  * End:
     241             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:01 +0000 (28 days ago)

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