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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:58:52 +0000 (41 hours ago)

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