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