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