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: 117 122 95.9 %
Date: 2016-07-19 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2016 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             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include "zend.h"
      24             : #include "zend_globals.h"
      25             : #include "zend_variables.h"
      26             : #include "zend_API.h"
      27             : #include "zend_interfaces.h"
      28             : #include "zend_exceptions.h"
      29             : 
      30     2386645 : ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce)
      31             : {
      32             :         zval *p, *end;
      33             : 
      34     2386645 :         GC_REFCOUNT(object) = 1;
      35     2386645 :         GC_TYPE_INFO(object) = IS_OBJECT;
      36     2386645 :         object->ce = ce;
      37     2386645 :         object->properties = NULL;
      38     2386645 :         zend_objects_store_put(object);
      39     2386645 :         p = object->properties_table;
      40     2386645 :         if (EXPECTED(ce->default_properties_count != 0)) {
      41      224493 :                 end = p + ce->default_properties_count;
      42             :                 do {
      43      380703 :                         ZVAL_UNDEF(p);
      44      380703 :                         p++;
      45      380703 :                 } while (p != end);
      46             :         }
      47     2386645 :         if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
      48         113 :                 GC_FLAGS(object) |= IS_OBJ_USE_GUARDS;
      49         113 :                 ZVAL_UNDEF(p);
      50             :         }
      51     2386645 : }
      52             : 
      53     2184983 : ZEND_API void zend_object_std_dtor(zend_object *object)
      54             : {
      55             :         zval *p, *end;
      56             : 
      57     2184983 :         if (object->properties) {
      58      629834 :                 if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
      59      629834 :                         if (EXPECTED(--GC_REFCOUNT(object->properties) == 0)) {
      60      629761 :                                 zend_array_destroy(object->properties);
      61             :                         }
      62             :                 }
      63             :         }
      64     2184983 :         p = object->properties_table;
      65     2184983 :         if (EXPECTED(object->ce->default_properties_count)) {
      66      224499 :                 end = p + object->ce->default_properties_count;
      67             :                 do {
      68             :                         i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
      69      380763 :                         p++;
      70      380763 :                 } while (p != end);
      71             :         }
      72     2184983 :         if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_HAS_GUARDS)) {
      73          90 :                 if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
      74          89 :                         zend_string_release(Z_STR_P(p));
      75             :                 } else {
      76             :                         HashTable *guards;
      77             : 
      78             :                         ZEND_ASSERT(Z_TYPE_P(p) == IS_ARRAY);
      79           1 :                         guards = Z_ARRVAL_P(p);
      80             :                         ZEND_ASSERT(guards != NULL);
      81           1 :                         zend_hash_destroy(guards);
      82           1 :                         FREE_HASHTABLE(guards);
      83             :                 }
      84             :         }
      85     2184983 : }
      86             : 
      87     1729355 : ZEND_API void zend_objects_destroy_object(zend_object *object)
      88             : {
      89     1729355 :         zend_function *destructor = object ? object->ce->destructor : NULL;
      90             : 
      91     1729355 :         if (destructor) {
      92             :                 zend_object *old_exception;
      93             :                 zval obj;
      94             :                 zend_class_entry *orig_fake_scope;
      95             : 
      96      198458 :                 if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
      97           8 :                         if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
      98             :                                 /* Ensure that if we're calling a private function, we're allowed to do so.
      99             :                                  */
     100           4 :                                 if (EG(current_execute_data)) {
     101           2 :                                         zend_class_entry *scope = zend_get_executed_scope();
     102             : 
     103           2 :                                         if (object->ce != scope) {
     104           4 :                                                 zend_throw_error(NULL,
     105             :                                                         "Call to private %s::__destruct() from context '%s'",
     106           2 :                                                         ZSTR_VAL(object->ce->name),
     107           0 :                                                         scope ? ZSTR_VAL(scope->name) : "");
     108           2 :                                                 return;
     109             :                                         }
     110             :                                 } else {
     111           2 :                                         zend_error(E_WARNING,
     112             :                                                 "Call to private %s::__destruct() from context '' during shutdown ignored",
     113           2 :                                                 ZSTR_VAL(object->ce->name));
     114           2 :                                         return;
     115             :                                 }
     116             :                         } else {
     117             :                                 /* Ensure that if we're calling a protected function, we're allowed to do so.
     118             :                                  */
     119           4 :                                 if (EG(current_execute_data)) {
     120           2 :                                         zend_class_entry *scope = zend_get_executed_scope();
     121             : 
     122           2 :                                         if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
     123           2 :                                                 zend_throw_error(NULL,
     124             :                                                         "Call to protected %s::__destruct() from context '%s'",
     125           1 :                                                         ZSTR_VAL(object->ce->name),
     126           0 :                                                         scope ? ZSTR_VAL(scope->name) : "");
     127           1 :                                                 return;
     128             :                                         }
     129             :                                 } else {
     130           2 :                                         zend_error(E_WARNING,
     131             :                                                 "Call to protected %s::__destruct() from context '' during shutdown ignored",
     132           2 :                                                 ZSTR_VAL(object->ce->name));
     133           2 :                                         return;
     134             :                                 }
     135             :                         }
     136             :                 }
     137             : 
     138      198451 :                 GC_REFCOUNT(object)++;
     139      198451 :                 ZVAL_OBJ(&obj, object);
     140             : 
     141             :                 /* Make sure that destructors are protected from previously thrown exceptions.
     142             :                  * For example, if an exception was thrown in a function and when the function's
     143             :                  * local variable destruction results in a destructor being called.
     144             :                  */
     145      198451 :                 old_exception = NULL;
     146      198451 :                 if (EG(exception)) {
     147          25 :                         if (EG(exception) == object) {
     148           0 :                                 zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
     149             :                         } else {
     150          25 :                                 old_exception = EG(exception);
     151          25 :                                 EG(exception) = NULL;
     152             :                         }
     153             :                 }
     154      198451 :                 orig_fake_scope = EG(fake_scope);
     155      198451 :                 EG(fake_scope) = NULL;
     156      198451 :                 zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
     157      198447 :                 if (old_exception) {
     158          25 :                         if (EG(exception)) {
     159           3 :                                 zend_exception_set_previous(EG(exception), old_exception);
     160             :                         } else {
     161          22 :                                 EG(exception) = old_exception;
     162             :                         }
     163             :                 }
     164      198447 :                 zval_ptr_dtor(&obj);
     165      198447 :                 EG(fake_scope) = orig_fake_scope;
     166             :         }
     167             : }
     168             : 
     169      934769 : ZEND_API zend_object *zend_objects_new(zend_class_entry *ce)
     170             : {
     171      934769 :         zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
     172             : 
     173      934769 :         zend_object_std_init(object, ce);
     174      934769 :         object->handlers = &std_object_handlers;
     175      934769 :         return object;
     176             : }
     177             : 
     178      174371 : ZEND_API void zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
     179             : {
     180      174371 :         if (old_object->ce->default_properties_count) {
     181          14 :                 zval *src = old_object->properties_table;
     182          14 :                 zval *dst = new_object->properties_table;
     183          14 :                 zval *end = src + old_object->ce->default_properties_count;
     184             : 
     185             :                 do {
     186             :                         i_zval_ptr_dtor(dst ZEND_FILE_LINE_CC);
     187          36 :                         ZVAL_COPY_VALUE(dst, src);
     188          36 :                         zval_add_ref(dst);
     189          36 :                         src++;
     190          36 :                         dst++;
     191          36 :                 } while (src != end);
     192      174357 :         } else if (old_object->properties && !old_object->ce->clone) {
     193             :                 /* fast copy */
     194          22 :                 if (EXPECTED(old_object->handlers == &std_object_handlers)) {
     195          13 :                         if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
     196          13 :                                 GC_REFCOUNT(old_object->properties)++;
     197             :                         }
     198          13 :                         new_object->properties = old_object->properties;
     199          13 :                         return;
     200             :                 }
     201             :         }
     202             : 
     203      174373 :         if (old_object->properties &&
     204          15 :             EXPECTED(zend_hash_num_elements(old_object->properties))) {
     205             :                 zval *prop, new_prop;
     206             :                 zend_ulong num_key;
     207             :                 zend_string *key;
     208             : 
     209          12 :                 if (!new_object->properties) {
     210          12 :                         ALLOC_HASHTABLE(new_object->properties);
     211          12 :                         zend_hash_init(new_object->properties, zend_hash_num_elements(old_object->properties), NULL, ZVAL_PTR_DTOR, 0);
     212          12 :                         zend_hash_real_init(new_object->properties, 0);
     213             :                 } else {
     214           0 :                         zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
     215             :                 }
     216             : 
     217          24 :                 new_object->properties->u.v.flags |=
     218          12 :                         old_object->properties->u.v.flags & HASH_FLAG_HAS_EMPTY_IND;
     219             : 
     220         114 :                 ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
     221          51 :                         if (Z_TYPE_P(prop) == IS_INDIRECT) {
     222          14 :                                 ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
     223             :                         } else {
     224          37 :                                 ZVAL_COPY_VALUE(&new_prop, prop);
     225          37 :                                 zval_add_ref(&new_prop);
     226             :                         }
     227          51 :                         if (EXPECTED(key)) {
     228          51 :                                 _zend_hash_append(new_object->properties, key, &new_prop);
     229             :                         } else {
     230           0 :                                 zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
     231             :                         }
     232             :                 } ZEND_HASH_FOREACH_END();
     233             :         }
     234             : 
     235      174358 :         if (old_object->ce->clone) {
     236             :                 zval new_obj;
     237             : 
     238          13 :                 ZVAL_OBJ(&new_obj, new_object);
     239             :                 Z_ADDREF(new_obj);
     240          13 :                 zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
     241          13 :                 zval_ptr_dtor(&new_obj);
     242             :         }
     243             : }
     244             : 
     245          43 : ZEND_API zend_object *zend_objects_clone_obj(zval *zobject)
     246             : {
     247             :         zend_object *old_object;
     248             :         zend_object *new_object;
     249             : 
     250             :         /* assume that create isn't overwritten, so when clone depends on the
     251             :          * overwritten one then it must itself be overwritten */
     252          43 :         old_object = Z_OBJ_P(zobject);
     253          43 :         new_object = zend_objects_new(old_object->ce);
     254             : 
     255          43 :         zend_objects_clone_members(new_object, old_object);
     256             : 
     257          43 :         return new_object;
     258             : }
     259             : 
     260             : /*
     261             :  * Local variables:
     262             :  * tab-width: 4
     263             :  * c-basic-offset: 4
     264             :  * indent-tabs-mode: t
     265             :  * End:
     266             :  */

Generated by: LCOV version 1.10

Generated at Wed, 20 Jul 2016 02:56:14 +0000 (5 days ago)

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