1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2013 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
zend_object_std_init(zend_object * object,zend_class_entry * ce TSRMLS_DC)29 ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce TSRMLS_DC)
30 {
31 ALLOC_HASHTABLE(object->properties);
32 zend_hash_init(object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
33
34 object->ce = ce;
35 object->guards = NULL;
36 }
37
zend_object_std_dtor(zend_object * object TSRMLS_DC)38 ZEND_API void zend_object_std_dtor(zend_object *object TSRMLS_DC)
39 {
40 if (object->guards) {
41 zend_hash_destroy(object->guards);
42 FREE_HASHTABLE(object->guards);
43 }
44 if (object->properties) {
45 zend_hash_destroy(object->properties);
46 FREE_HASHTABLE(object->properties);
47 }
48 }
49
zend_objects_destroy_object(zend_object * object,zend_object_handle handle TSRMLS_DC)50 ZEND_API void zend_objects_destroy_object(zend_object *object, zend_object_handle handle TSRMLS_DC)
51 {
52 zend_function *destructor = object ? object->ce->destructor : NULL;
53
54 if (destructor) {
55 zval *old_exception;
56 zval *obj;
57 zend_object_store_bucket *obj_bucket;
58
59 if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
60 if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
61 /* Ensure that if we're calling a private function, we're allowed to do so.
62 */
63 if (object->ce != EG(scope)) {
64 zend_class_entry *ce = object->ce;
65
66 zend_error(EG(in_execution) ? E_ERROR : E_WARNING,
67 "Call to private %s::__destruct() from context '%s'%s",
68 ce->name,
69 EG(scope) ? EG(scope)->name : "",
70 EG(in_execution) ? "" : " during shutdown ignored");
71 return;
72 }
73 } else {
74 /* Ensure that if we're calling a protected function, we're allowed to do so.
75 */
76 if (!zend_check_protected(destructor->common.scope, EG(scope))) {
77 zend_class_entry *ce = object->ce;
78
79 zend_error(EG(in_execution) ? E_ERROR : E_WARNING,
80 "Call to protected %s::__destruct() from context '%s'%s",
81 ce->name,
82 EG(scope) ? EG(scope)->name : "",
83 EG(in_execution) ? "" : " during shutdown ignored");
84 return;
85 }
86 }
87 }
88
89 MAKE_STD_ZVAL(obj);
90 Z_TYPE_P(obj) = IS_OBJECT;
91 Z_OBJ_HANDLE_P(obj) = handle;
92 obj_bucket = &EG(objects_store).object_buckets[handle];
93 if (!obj_bucket->bucket.obj.handlers) {
94 obj_bucket->bucket.obj.handlers = &std_object_handlers;
95 }
96 Z_OBJ_HT_P(obj) = obj_bucket->bucket.obj.handlers;
97 zval_copy_ctor(obj);
98
99 /* Make sure that destructors are protected from previously thrown exceptions.
100 * For example, if an exception was thrown in a function and when the function's
101 * local variable destruction results in a destructor being called.
102 */
103 old_exception = NULL;
104 if (EG(exception)) {
105 if (Z_OBJ_HANDLE_P(EG(exception)) == handle) {
106 zend_error(E_ERROR, "Attempt to destruct pending exception");
107 } else {
108 old_exception = EG(exception);
109 EG(exception) = NULL;
110 }
111 }
112 zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
113 if (old_exception) {
114 if (EG(exception)) {
115 zend_exception_set_previous(EG(exception), old_exception TSRMLS_CC);
116 } else {
117 EG(exception) = old_exception;
118 }
119 }
120 zval_ptr_dtor(&obj);
121 }
122 }
123
zend_objects_free_object_storage(zend_object * object TSRMLS_DC)124 ZEND_API void zend_objects_free_object_storage(zend_object *object TSRMLS_DC)
125 {
126 zend_object_std_dtor(object TSRMLS_CC);
127 efree(object);
128 }
129
zend_objects_new(zend_object ** object,zend_class_entry * class_type TSRMLS_DC)130 ZEND_API zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type TSRMLS_DC)
131 {
132 zend_object_value retval;
133
134 *object = emalloc(sizeof(zend_object));
135 (*object)->ce = class_type;
136 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);
137 retval.handlers = &std_object_handlers;
138 (*object)->guards = NULL;
139 return retval;
140 }
141
zend_objects_get_address(const zval * zobject TSRMLS_DC)142 ZEND_API zend_object *zend_objects_get_address(const zval *zobject TSRMLS_DC)
143 {
144 return (zend_object *)zend_object_store_get_object(zobject TSRMLS_CC);
145 }
146
zend_objects_clone_members(zend_object * new_object,zend_object_value new_obj_val,zend_object * old_object,zend_object_handle handle TSRMLS_DC)147 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)
148 {
149 zend_hash_copy(new_object->properties, old_object->properties, zval_copy_property_ctor(old_object->ce), (void *) NULL /* Not used anymore */, sizeof(zval *));
150
151 if (old_object->ce->clone) {
152 zval *new_obj;
153
154 MAKE_STD_ZVAL(new_obj);
155 new_obj->type = IS_OBJECT;
156 new_obj->value.obj = new_obj_val;
157 zval_copy_ctor(new_obj);
158
159 zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
160
161 zval_ptr_dtor(&new_obj);
162 }
163 }
164
zend_objects_clone_obj(zval * zobject TSRMLS_DC)165 ZEND_API zend_object_value zend_objects_clone_obj(zval *zobject TSRMLS_DC)
166 {
167 zend_object_value new_obj_val;
168 zend_object *old_object;
169 zend_object *new_object;
170 zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
171
172 /* assume that create isn't overwritten, so when clone depends on the
173 * overwritten one then it must itself be overwritten */
174 old_object = zend_objects_get_address(zobject TSRMLS_CC);
175 new_obj_val = zend_objects_new(&new_object, old_object->ce TSRMLS_CC);
176
177 ALLOC_HASHTABLE(new_object->properties);
178 zend_hash_init(new_object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
179
180 zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
181
182 return new_obj_val;
183 }
184
185 /*
186 * Local variables:
187 * tab-width: 4
188 * c-basic-offset: 4
189 * indent-tabs-mode: t
190 * End:
191 */
192