Back to index

php5  5.3.10
zend_objects.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_objects.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "zend.h"
00023 #include "zend_globals.h"
00024 #include "zend_variables.h"
00025 #include "zend_API.h"
00026 #include "zend_interfaces.h"
00027 #include "zend_exceptions.h"
00028 
00029 ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce TSRMLS_DC)
00030 {
00031        ALLOC_HASHTABLE(object->properties);
00032        zend_hash_init(object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
00033 
00034        object->ce = ce;     
00035        object->guards = NULL;
00036 }
00037 
00038 ZEND_API void zend_object_std_dtor(zend_object *object TSRMLS_DC)
00039 {
00040        if (object->guards) {
00041               zend_hash_destroy(object->guards);
00042               FREE_HASHTABLE(object->guards);           
00043        }
00044        if (object->properties) {
00045               zend_hash_destroy(object->properties);
00046               FREE_HASHTABLE(object->properties);
00047        }
00048 }
00049 
00050 ZEND_API void zend_objects_destroy_object(zend_object *object, zend_object_handle handle TSRMLS_DC)
00051 {
00052        zend_function *destructor = object ? object->ce->destructor : NULL;
00053 
00054        if (destructor) {
00055               zval *old_exception;
00056               zval *obj;
00057               zend_object_store_bucket *obj_bucket;
00058 
00059               if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
00060                      if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
00061                             /* Ensure that if we're calling a private function, we're allowed to do so.
00062                              */
00063                             if (object->ce != EG(scope)) {
00064                                    zend_class_entry *ce = object->ce;
00065 
00066                                    zend_error(EG(in_execution) ? E_ERROR : E_WARNING, 
00067                                           "Call to private %s::__destruct() from context '%s'%s", 
00068                                           ce->name, 
00069                                           EG(scope) ? EG(scope)->name : "", 
00070                                           EG(in_execution) ? "" : " during shutdown ignored");
00071                                    return;
00072                             }
00073                      } else {
00074                             /* Ensure that if we're calling a protected function, we're allowed to do so.
00075                              */
00076                             if (!zend_check_protected(destructor->common.scope, EG(scope))) {
00077                                    zend_class_entry *ce = object->ce;
00078 
00079                                    zend_error(EG(in_execution) ? E_ERROR : E_WARNING, 
00080                                           "Call to protected %s::__destruct() from context '%s'%s", 
00081                                           ce->name, 
00082                                           EG(scope) ? EG(scope)->name : "", 
00083                                           EG(in_execution) ? "" : " during shutdown ignored");
00084                                    return;
00085                             }
00086                      }
00087               }
00088 
00089               MAKE_STD_ZVAL(obj);
00090               Z_TYPE_P(obj) = IS_OBJECT;
00091               Z_OBJ_HANDLE_P(obj) = handle;
00092               obj_bucket = &EG(objects_store).object_buckets[handle];
00093               if (!obj_bucket->bucket.obj.handlers) {
00094                      obj_bucket->bucket.obj.handlers = &std_object_handlers;
00095               }
00096               Z_OBJ_HT_P(obj) = obj_bucket->bucket.obj.handlers;
00097               zval_copy_ctor(obj);
00098 
00099               /* Make sure that destructors are protected from previously thrown exceptions.
00100                * For example, if an exception was thrown in a function and when the function's
00101                * local variable destruction results in a destructor being called.
00102                */
00103               old_exception = NULL;
00104               if (EG(exception)) {
00105                      if (Z_OBJ_HANDLE_P(EG(exception)) == handle) {
00106                             zend_error(E_ERROR, "Attempt to destruct pending exception");
00107                      } else {
00108                             old_exception = EG(exception);
00109                             EG(exception) = NULL;
00110                      }
00111               }
00112               zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
00113               if (old_exception) {
00114                      if (EG(exception)) {
00115                             zend_exception_set_previous(EG(exception), old_exception TSRMLS_CC);
00116                      } else {
00117                             EG(exception) = old_exception;
00118                      }
00119               }
00120               zval_ptr_dtor(&obj);
00121        }
00122 }
00123 
00124 ZEND_API void zend_objects_free_object_storage(zend_object *object TSRMLS_DC)
00125 {
00126        zend_object_std_dtor(object TSRMLS_CC);
00127        efree(object);
00128 }
00129 
00130 ZEND_API zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type TSRMLS_DC)
00131 {      
00132        zend_object_value retval;
00133 
00134        *object = emalloc(sizeof(zend_object));
00135        (*object)->ce = class_type;
00136        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);
00137        retval.handlers = &std_object_handlers;
00138        (*object)->guards = NULL;
00139        return retval;
00140 }
00141 
00142 ZEND_API zend_object *zend_objects_get_address(const zval *zobject TSRMLS_DC)
00143 {
00144        return (zend_object *)zend_object_store_get_object(zobject TSRMLS_CC);
00145 }
00146 
00147 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)
00148 {
00149        zend_hash_copy(new_object->properties, old_object->properties, zval_copy_property_ctor(old_object->ce), (void *) NULL /* Not used anymore */, sizeof(zval *));
00150 
00151        if (old_object->ce->clone) {
00152               zval *new_obj;
00153 
00154               MAKE_STD_ZVAL(new_obj);
00155               new_obj->type = IS_OBJECT;
00156               new_obj->value.obj = new_obj_val;
00157               zval_copy_ctor(new_obj);
00158 
00159               zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
00160 
00161               zval_ptr_dtor(&new_obj);
00162        }
00163 }
00164 
00165 ZEND_API zend_object_value zend_objects_clone_obj(zval *zobject TSRMLS_DC)
00166 {
00167        zend_object_value new_obj_val;
00168        zend_object *old_object;
00169        zend_object *new_object;
00170        zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
00171 
00172        /* assume that create isn't overwritten, so when clone depends on the 
00173         * overwritten one then it must itself be overwritten */
00174        old_object = zend_objects_get_address(zobject TSRMLS_CC);
00175        new_obj_val = zend_objects_new(&new_object, old_object->ce TSRMLS_CC);
00176 
00177        ALLOC_HASHTABLE(new_object->properties);
00178        zend_hash_init(new_object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
00179 
00180        zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
00181 
00182        return new_obj_val;
00183 }
00184 
00185 /*
00186  * Local variables:
00187  * tab-width: 4
00188  * c-basic-offset: 4
00189  * indent-tabs-mode: t
00190  * End:
00191  */