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: 106 109 97.2 %
Date: 2015-06-27 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     1469671 : ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce)
      30             : {
      31             :         zval *p, *end;
      32             : 
      33     1469671 :         GC_REFCOUNT(object) = 1;
      34     1469671 :         GC_TYPE_INFO(object) = IS_OBJECT;
      35     1469671 :         object->ce = ce;
      36     1469671 :         object->properties = NULL;
      37     1469671 :         zend_objects_store_put(object);
      38     1469671 :         p = object->properties_table;
      39     1469671 :         if (EXPECTED(ce->default_properties_count != 0)) {
      40      113220 :                 end = p + ce->default_properties_count;
      41             :                 do {
      42      267332 :                         ZVAL_UNDEF(p);
      43      267332 :                         p++;
      44      267332 :                 } while (p != end);
      45             :         }
      46     1469671 :         if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
      47          98 :                 GC_FLAGS(object) |= IS_OBJ_USE_GUARDS;
      48          98 :                 Z_PTR_P(p) = NULL;
      49          98 :                 ZVAL_UNDEF(p);
      50             :         }
      51     1469671 : }
      52             : 
      53     1468064 : ZEND_API void zend_object_std_dtor(zend_object *object)
      54             : {
      55             :         zval *p, *end;
      56             : 
      57     1468064 :         if (object->properties) {
      58      236706 :                 if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
      59      236706 :                         if (EXPECTED(--GC_REFCOUNT(object->properties) == 0)) {
      60      236698 :                                 zend_array_destroy(object->properties);
      61             :                         }
      62             :                 }
      63             :         }
      64     1468064 :         p = object->properties_table;
      65     1468064 :         if (EXPECTED(object->ce->default_properties_count)) {
      66      113226 :                 end = p + object->ce->default_properties_count;
      67             :                 do {
      68             :                         i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
      69      267392 :                         p++;
      70      267392 :                 } while (p != end);
      71             :         }
      72     1468064 :         if (UNEXPECTED(GC_FLAGS(object) & IS_OBJ_HAS_GUARDS)) {
      73          75 :                 HashTable *guards = Z_PTR_P(p);
      74             : 
      75             :                 ZEND_ASSERT(guards != NULL);
      76          75 :                 zend_hash_destroy(guards);
      77          75 :                 FREE_HASHTABLE(guards);
      78             :         }
      79     1468064 : }
      80             : 
      81     1012628 : ZEND_API void zend_objects_destroy_object(zend_object *object)
      82             : {
      83     1012628 :         zend_function *destructor = object ? object->ce->destructor : NULL;
      84             : 
      85     1012628 :         if (destructor) {
      86             :                 zend_object *old_exception;
      87             :                 zval obj;
      88             : 
      89       98410 :                 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          12 :                                         zend_error(EG(current_execute_data) ? E_EXCEPTION | E_ERROR : E_WARNING,
      97             :                                                 "Call to private %s::__destruct() from context '%s'%s",
      98           4 :                                                 ce->name->val,
      99           4 :                                                 EG(scope) ? EG(scope)->name->val : "",
     100           4 :                                                 EG(current_execute_data) ? "" : " during shutdown ignored");
     101           4 :                                         return;
     102             :                                 }
     103             :                         } else {
     104             :                                 /* Ensure that if we're calling a protected function, we're allowed to do so.
     105             :                                  */
     106           4 :                                 if (!zend_check_protected(zend_get_function_root_class(destructor), EG(scope))) {
     107           3 :                                         zend_class_entry *ce = object->ce;
     108             : 
     109           9 :                                         zend_error(EG(current_execute_data) ? E_EXCEPTION | E_ERROR : E_WARNING,
     110             :                                                 "Call to protected %s::__destruct() from context '%s'%s",
     111           3 :                                                 ce->name->val,
     112           3 :                                                 EG(scope) ? EG(scope)->name->val : "",
     113           3 :                                                 EG(current_execute_data) ? "" : " during shutdown ignored");
     114           3 :                                         return;
     115             :                                 }
     116             :                         }
     117             :                 }
     118             : 
     119       98403 :                 ZVAL_OBJ(&obj, object);
     120             :                 Z_ADDREF(obj);
     121             : 
     122             :                 /* Make sure that destructors are protected from previously thrown exceptions.
     123             :                  * For example, if an exception was thrown in a function and when the function's
     124             :                  * local variable destruction results in a destructor being called.
     125             :                  */
     126       98403 :                 old_exception = NULL;
     127       98403 :                 if (EG(exception)) {
     128          22 :                         if (EG(exception) == object) {
     129           0 :                                 zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
     130             :                         } else {
     131          22 :                                 old_exception = EG(exception);
     132          22 :                                 EG(exception) = NULL;
     133             :                         }
     134             :                 }
     135       98403 :                 zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
     136       98399 :                 if (old_exception) {
     137          22 :                         if (EG(exception)) {
     138           3 :                                 zend_exception_set_previous(EG(exception), old_exception);
     139             :                         } else {
     140          19 :                                 EG(exception) = old_exception;
     141             :                         }
     142             :                 }
     143       98399 :                 zval_ptr_dtor(&obj);
     144             :         }
     145             : }
     146             : 
     147      431014 : ZEND_API zend_object *zend_objects_new(zend_class_entry *ce)
     148             : {
     149      431014 :         zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
     150             : 
     151      431014 :         zend_object_std_init(object, ce);
     152      431014 :         object->handlers = &std_object_handlers;
     153      431014 :         return object;
     154             : }
     155             : 
     156      174350 : ZEND_API void zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
     157             : {
     158      174350 :         if (old_object->ce->default_properties_count) {
     159          14 :                 zval *src = old_object->properties_table;
     160          14 :                 zval *dst = new_object->properties_table;
     161          14 :                 zval *end = src + old_object->ce->default_properties_count;
     162             : 
     163             :                 do {
     164             :                         i_zval_ptr_dtor(dst ZEND_FILE_LINE_CC);
     165          36 :                         ZVAL_COPY_VALUE(dst, src);
     166          36 :                         zval_add_ref(dst);
     167          36 :                         src++;
     168          36 :                         dst++;
     169          36 :                 } while (src != end);
     170      174336 :         } else if (old_object->properties && !old_object->ce->clone) {
     171             :                 /* fast copy */
     172          17 :                 if (EXPECTED(old_object->handlers == &std_object_handlers)) {
     173           8 :                         if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
     174           8 :                                 GC_REFCOUNT(old_object->properties)++;
     175             :                         }
     176           8 :                         new_object->properties = old_object->properties;
     177           8 :                         return;
     178             :                 }
     179             :         }
     180             : 
     181      174357 :         if (old_object->properties &&
     182          15 :             EXPECTED(zend_hash_num_elements(old_object->properties))) {
     183             :                 zval *prop, new_prop;
     184             :                 zend_ulong num_key;
     185             :                 zend_string *key;
     186             : 
     187          12 :                 if (!new_object->properties) {
     188          12 :                         ALLOC_HASHTABLE(new_object->properties);
     189          12 :                         zend_hash_init(new_object->properties, zend_hash_num_elements(old_object->properties), NULL, ZVAL_PTR_DTOR, 0);
     190          12 :                         zend_hash_real_init(new_object->properties, 0);
     191             :                 } else {
     192           0 :                         zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
     193             :                 }
     194             : 
     195         114 :                 ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
     196          51 :                         if (Z_TYPE_P(prop) == IS_INDIRECT) {
     197          14 :                                 ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
     198             :                         } else {
     199          37 :                                 ZVAL_COPY_VALUE(&new_prop, prop);
     200          37 :                                 zval_add_ref(&new_prop);
     201             :                         }
     202          51 :                         if (EXPECTED(key)) {
     203          51 :                                 _zend_hash_append(new_object->properties, key, &new_prop);
     204             :                         } else {
     205           0 :                                 zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
     206             :                         }
     207             :                 } ZEND_HASH_FOREACH_END();
     208             :         }
     209             : 
     210      174342 :         if (old_object->ce->clone) {
     211             :                 zval new_obj;
     212             : 
     213          11 :                 ZVAL_OBJ(&new_obj, new_object);
     214             :                 zval_copy_ctor(&new_obj);
     215          11 :                 zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
     216          11 :                 zval_ptr_dtor(&new_obj);
     217             :         }
     218             : }
     219             : 
     220          37 : ZEND_API zend_object *zend_objects_clone_obj(zval *zobject)
     221             : {
     222             :         zend_object *old_object;
     223             :         zend_object *new_object;
     224             : 
     225             :         /* assume that create isn't overwritten, so when clone depends on the
     226             :          * overwritten one then it must itself be overwritten */
     227          37 :         old_object = Z_OBJ_P(zobject);
     228          37 :         new_object = zend_objects_new(old_object->ce);
     229             : 
     230          37 :         zend_objects_clone_members(new_object, old_object);
     231             : 
     232          37 :         return new_object;
     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 Sat, 27 Jun 2015 09:41:07 +0000 (5 days ago)

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