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: 114 117 97.4 %
Date: 2015-08-29 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 2.00 of the Zend license,     |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.zend.com/license/2_00.txt.                                |
      11             :    | If you did not receive a copy of the Zend license and are unable to  |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@zend.com so we can mail you a copy immediately.              |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: 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     1469931 : ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce)
      30             : {
      31             :         zval *p, *end;
      32             : 
      33     1469931 :         GC_REFCOUNT(object) = 1;
      34     1469931 :         GC_TYPE_INFO(object) = IS_OBJECT;
      35     1469931 :         object->ce = ce;
      36     1469931 :         object->properties = NULL;
      37     1469931 :         zend_objects_store_put(object);
      38     1469931 :         p = object->properties_table;
      39     1469931 :         if (EXPECTED(ce->default_properties_count != 0)) {
      40      113312 :                 end = p + ce->default_properties_count;
      41             :                 do {
      42      267844 :                         ZVAL_UNDEF(p);
      43      267844 :                         p++;
      44      267844 :                 } while (p != end);
      45             :         }
      46     1469931 :         if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
      47         107 :                 GC_FLAGS(object) |= IS_OBJ_USE_GUARDS;
      48         107 :                 Z_PTR_P(p) = NULL;
      49         107 :                 ZVAL_UNDEF(p);
      50             :         }
      51     1469931 : }
      52             : 
      53     1468314 : ZEND_API void zend_object_std_dtor(zend_object *object)
      54             : {
      55             :         zval *p, *end;
      56             : 
      57     1468314 :         if (object->properties) {
      58      236731 :                 if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
      59      236731 :                         if (EXPECTED(--GC_REFCOUNT(object->properties) == 0)) {
      60      236657 :                                 zend_array_destroy(object->properties);
      61             :                         }
      62             :                 }
      63             :         }
      64     1468314 :         p = object->properties_table;
      65     1468314 :         if (EXPECTED(object->ce->default_properties_count)) {
      66      113318 :                 end = p + object->ce->default_properties_count;
      67             :                 do {
      68             :                         i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
      69      267904 :                         p++;
      70      267904 :                 } while (p != end);
      71             :         }
      72     1468314 :         if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_HAS_GUARDS)) {
      73          84 :                 HashTable *guards = Z_PTR_P(p);
      74             : 
      75             :                 ZEND_ASSERT(guards != NULL);
      76          84 :                 zend_hash_destroy(guards);
      77          84 :                 FREE_HASHTABLE(guards);
      78             :         }
      79     1468314 : }
      80             : 
      81     1012862 : ZEND_API void zend_objects_destroy_object(zend_object *object)
      82             : {
      83     1012862 :         zend_function *destructor = object ? object->ce->destructor : NULL;
      84             : 
      85     1012862 :         if (destructor) {
      86             :                 zend_object *old_exception;
      87             :                 zval obj;
      88             : 
      89       98414 :                 if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
      90           8 :                         if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
      91             :                                 /* Ensure that if we're calling a private function, we're allowed to do so.
      92             :                                  */
      93           4 :                                 if (object->ce != EG(scope)) {
      94           4 :                                         zend_class_entry *ce = object->ce;
      95             : 
      96           4 :                                         if (EG(current_execute_data)) {
      97           4 :                                                 zend_throw_error(NULL,
      98             :                                                         "Call to private %s::__destruct() from context '%s'",
      99           2 :                                                         ZSTR_VAL(ce->name),
     100           2 :                                                         EG(scope) ? ZSTR_VAL(EG(scope)->name) : "");
     101             :                                         } else {
     102           4 :                                                 zend_error(E_WARNING,
     103             :                                                         "Call to private %s::__destruct() from context '%s' during shutdown ignored",
     104           2 :                                                         ZSTR_VAL(ce->name),
     105           2 :                                                         EG(scope) ? ZSTR_VAL(EG(scope)->name) : "");
     106             :                                         }
     107           4 :                                         return;
     108             :                                 }
     109             :                         } else {
     110             :                                 /* Ensure that if we're calling a protected function, we're allowed to do so.
     111             :                                  */
     112           4 :                                 if (!zend_check_protected(zend_get_function_root_class(destructor), EG(scope))) {
     113           3 :                                         zend_class_entry *ce = object->ce;
     114             : 
     115           3 :                                         if (EG(current_execute_data)) {
     116           2 :                                                 zend_throw_error(NULL,
     117             :                                                         "Call to protected %s::__destruct() from context '%s'",
     118           1 :                                                         ZSTR_VAL(ce->name),
     119           1 :                                                         EG(scope) ? ZSTR_VAL(EG(scope)->name) : "");
     120             :                                         } else {
     121           4 :                                                 zend_error(E_WARNING,
     122             :                                                         "Call to protected %s::__destruct() from context '%s' during shutdown ignored",
     123           2 :                                                         ZSTR_VAL(ce->name),
     124           2 :                                                         EG(scope) ? ZSTR_VAL(EG(scope)->name) : "");
     125             :                                         }
     126           3 :                                         return;
     127             :                                 }
     128             :                         }
     129             :                 }
     130             : 
     131       98407 :                 ZVAL_OBJ(&obj, object);
     132             :                 Z_ADDREF(obj);
     133             : 
     134             :                 /* Make sure that destructors are protected from previously thrown exceptions.
     135             :                  * For example, if an exception was thrown in a function and when the function's
     136             :                  * local variable destruction results in a destructor being called.
     137             :                  */
     138       98407 :                 old_exception = NULL;
     139       98407 :                 if (EG(exception)) {
     140          22 :                         if (EG(exception) == object) {
     141           0 :                                 zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
     142             :                         } else {
     143          22 :                                 old_exception = EG(exception);
     144          22 :                                 EG(exception) = NULL;
     145             :                         }
     146             :                 }
     147       98407 :                 zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
     148       98403 :                 if (old_exception) {
     149          22 :                         if (EG(exception)) {
     150           3 :                                 zend_exception_set_previous(EG(exception), old_exception);
     151             :                         } else {
     152          19 :                                 EG(exception) = old_exception;
     153             :                         }
     154             :                 }
     155       98403 :                 zval_ptr_dtor(&obj);
     156             :         }
     157             : }
     158             : 
     159      431141 : ZEND_API zend_object *zend_objects_new(zend_class_entry *ce)
     160             : {
     161      431141 :         zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
     162             : 
     163      431141 :         zend_object_std_init(object, ce);
     164      431141 :         object->handlers = &std_object_handlers;
     165      431141 :         return object;
     166             : }
     167             : 
     168      174355 : ZEND_API void zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
     169             : {
     170      174355 :         if (old_object->ce->default_properties_count) {
     171          14 :                 zval *src = old_object->properties_table;
     172          14 :                 zval *dst = new_object->properties_table;
     173          14 :                 zval *end = src + old_object->ce->default_properties_count;
     174             : 
     175             :                 do {
     176             :                         i_zval_ptr_dtor(dst ZEND_FILE_LINE_CC);
     177          36 :                         ZVAL_COPY_VALUE(dst, src);
     178          36 :                         zval_add_ref(dst);
     179          36 :                         src++;
     180          36 :                         dst++;
     181          36 :                 } while (src != end);
     182      174341 :         } else if (old_object->properties && !old_object->ce->clone) {
     183             :                 /* fast copy */
     184          22 :                 if (EXPECTED(old_object->handlers == &std_object_handlers)) {
     185          13 :                         if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
     186          13 :                                 GC_REFCOUNT(old_object->properties)++;
     187             :                         }
     188          13 :                         new_object->properties = old_object->properties;
     189          13 :                         return;
     190             :                 }
     191             :         }
     192             : 
     193      174357 :         if (old_object->properties &&
     194          15 :             EXPECTED(zend_hash_num_elements(old_object->properties))) {
     195             :                 zval *prop, new_prop;
     196             :                 zend_ulong num_key;
     197             :                 zend_string *key;
     198             : 
     199          12 :                 if (!new_object->properties) {
     200          12 :                         ALLOC_HASHTABLE(new_object->properties);
     201          12 :                         zend_hash_init(new_object->properties, zend_hash_num_elements(old_object->properties), NULL, ZVAL_PTR_DTOR, 0);
     202          12 :                         zend_hash_real_init(new_object->properties, 0);
     203             :                 } else {
     204           0 :                         zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
     205             :                 }
     206             : 
     207          24 :                 new_object->properties->u.v.flags |=
     208          12 :                         old_object->properties->u.v.flags & HASH_FLAG_HAS_EMPTY_IND;
     209             : 
     210         114 :                 ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
     211          51 :                         if (Z_TYPE_P(prop) == IS_INDIRECT) {
     212          14 :                                 ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
     213             :                         } else {
     214          37 :                                 ZVAL_COPY_VALUE(&new_prop, prop);
     215          37 :                                 zval_add_ref(&new_prop);
     216             :                         }
     217          51 :                         if (EXPECTED(key)) {
     218          51 :                                 _zend_hash_append(new_object->properties, key, &new_prop);
     219             :                         } else {
     220           0 :                                 zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
     221             :                         }
     222             :                 } ZEND_HASH_FOREACH_END();
     223             :         }
     224             : 
     225      174342 :         if (old_object->ce->clone) {
     226             :                 zval new_obj;
     227             : 
     228          11 :                 ZVAL_OBJ(&new_obj, new_object);
     229             :                 zval_copy_ctor(&new_obj);
     230          11 :                 zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
     231          11 :                 zval_ptr_dtor(&new_obj);
     232             :         }
     233             : }
     234             : 
     235          42 : ZEND_API zend_object *zend_objects_clone_obj(zval *zobject)
     236             : {
     237             :         zend_object *old_object;
     238             :         zend_object *new_object;
     239             : 
     240             :         /* assume that create isn't overwritten, so when clone depends on the
     241             :          * overwritten one then it must itself be overwritten */
     242          42 :         old_object = Z_OBJ_P(zobject);
     243          42 :         new_object = zend_objects_new(old_object->ce);
     244             : 
     245          42 :         zend_objects_clone_members(new_object, old_object);
     246             : 
     247          42 :         return new_object;
     248             : }
     249             : 
     250             : /*
     251             :  * Local variables:
     252             :  * tab-width: 4
     253             :  * c-basic-offset: 4
     254             :  * indent-tabs-mode: t
     255             :  * End:
     256             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:12 +0000 (2 hours ago)

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