2006-05-21 05:02:44 +08:00
|
|
|
/*
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:04:26 +08:00
|
|
|
| Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
|
2003-02-01 09:49:15 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2003-02-01 09:49:15 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2001-08-07 11:17:33 +08:00
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_globals.h"
|
2001-08-12 02:04:07 +08:00
|
|
|
#include "zend_variables.h"
|
2001-12-27 03:54:20 +08:00
|
|
|
#include "zend_API.h"
|
2003-12-28 04:33:14 +08:00
|
|
|
#include "zend_interfaces.h"
|
2006-05-21 05:02:44 +08:00
|
|
|
#include "zend_exceptions.h"
|
2001-08-07 11:17:33 +08:00
|
|
|
|
2006-03-29 22:28:43 +08:00
|
|
|
ZEND_API void zend_object_std_init(zend_object *object, zend_class_entry *ce TSRMLS_DC)
|
|
|
|
{
|
2012-05-13 13:12:48 +08:00
|
|
|
object->ce = ce;
|
2010-05-24 22:11:39 +08:00
|
|
|
object->properties = NULL;
|
|
|
|
object->properties_table = NULL;
|
2006-03-29 22:28:43 +08:00
|
|
|
object->guards = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API void zend_object_std_dtor(zend_object *object TSRMLS_DC)
|
|
|
|
{
|
|
|
|
if (object->guards) {
|
|
|
|
zend_hash_destroy(object->guards);
|
2012-05-13 13:12:48 +08:00
|
|
|
FREE_HASHTABLE(object->guards);
|
2006-03-29 22:28:43 +08:00
|
|
|
}
|
|
|
|
if (object->properties) {
|
|
|
|
zend_hash_destroy(object->properties);
|
|
|
|
FREE_HASHTABLE(object->properties);
|
2010-05-24 22:11:39 +08:00
|
|
|
if (object->properties_table) {
|
|
|
|
efree(object->properties_table);
|
|
|
|
}
|
|
|
|
} else if (object->properties_table) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < object->ce->default_properties_count; i++) {
|
|
|
|
if (object->properties_table[i]) {
|
|
|
|
zval_ptr_dtor(&object->properties_table[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
efree(object->properties_table);
|
2006-03-29 22:28:43 +08:00
|
|
|
}
|
|
|
|
}
|
2003-07-03 07:53:53 +08:00
|
|
|
|
2005-02-24 10:35:59 +08:00
|
|
|
ZEND_API void zend_objects_destroy_object(zend_object *object, zend_object_handle handle TSRMLS_DC)
|
2001-12-27 22:35:09 +08:00
|
|
|
{
|
2009-10-13 01:09:11 +08:00
|
|
|
zend_function *destructor = object ? object->ce->destructor : NULL;
|
2003-07-02 03:13:50 +08:00
|
|
|
|
|
|
|
if (destructor) {
|
2010-05-12 00:09:43 +08:00
|
|
|
zval *old_exception;
|
2006-07-26 23:29:27 +08:00
|
|
|
zval *obj;
|
2008-08-25 00:49:19 +08:00
|
|
|
zend_object_store_bucket *obj_bucket;
|
2006-05-21 05:02:44 +08:00
|
|
|
|
2003-07-03 07:53:53 +08:00
|
|
|
if (destructor->op_array.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
|
|
|
|
if (destructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
|
|
|
|
/* Ensure that if we're calling a private function, we're allowed to do so.
|
|
|
|
*/
|
|
|
|
if (object->ce != EG(scope)) {
|
2003-08-09 22:32:33 +08:00
|
|
|
zend_class_entry *ce = object->ce;
|
|
|
|
|
2012-05-13 13:12:48 +08:00
|
|
|
zend_error(EG(in_execution) ? E_ERROR : E_WARNING,
|
|
|
|
"Call to private %s::__destruct() from context '%s'%s",
|
|
|
|
ce->name,
|
|
|
|
EG(scope) ? EG(scope)->name : "",
|
2003-08-09 22:32:33 +08:00
|
|
|
EG(in_execution) ? "" : " during shutdown ignored");
|
2003-07-03 07:53:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Ensure that if we're calling a protected function, we're allowed to do so.
|
|
|
|
*/
|
2012-05-13 13:12:48 +08:00
|
|
|
if (!zend_check_protected(zend_get_function_root_class(destructor), EG(scope))) {
|
2003-08-09 22:32:33 +08:00
|
|
|
zend_class_entry *ce = object->ce;
|
|
|
|
|
2012-05-13 13:12:48 +08:00
|
|
|
zend_error(EG(in_execution) ? E_ERROR : E_WARNING,
|
|
|
|
"Call to protected %s::__destruct() from context '%s'%s",
|
|
|
|
ce->name,
|
|
|
|
EG(scope) ? EG(scope)->name : "",
|
2003-08-09 22:32:33 +08:00
|
|
|
EG(in_execution) ? "" : " during shutdown ignored");
|
2003-07-03 07:53:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-26 23:29:27 +08:00
|
|
|
MAKE_STD_ZVAL(obj);
|
|
|
|
Z_TYPE_P(obj) = IS_OBJECT;
|
|
|
|
Z_OBJ_HANDLE_P(obj) = handle;
|
2008-08-25 00:49:19 +08:00
|
|
|
obj_bucket = &EG(objects_store).object_buckets[handle];
|
|
|
|
if (!obj_bucket->bucket.obj.handlers) {
|
|
|
|
obj_bucket->bucket.obj.handlers = &std_object_handlers;
|
|
|
|
}
|
|
|
|
Z_OBJ_HT_P(obj) = obj_bucket->bucket.obj.handlers;
|
2006-07-26 23:29:27 +08:00
|
|
|
zval_copy_ctor(obj);
|
2001-12-27 22:35:09 +08:00
|
|
|
|
2004-03-02 16:13:15 +08:00
|
|
|
/* Make sure that destructors are protected from previously thrown exceptions.
|
|
|
|
* For example, if an exception was thrown in a function and when the function's
|
|
|
|
* local variable destruction results in a destructor being called.
|
|
|
|
*/
|
2010-05-12 00:09:43 +08:00
|
|
|
old_exception = NULL;
|
|
|
|
if (EG(exception)) {
|
|
|
|
if (Z_OBJ_HANDLE_P(EG(exception)) == handle) {
|
|
|
|
zend_error(E_ERROR, "Attempt to destruct pending exception");
|
|
|
|
} else {
|
|
|
|
old_exception = EG(exception);
|
2010-08-16 17:20:46 +08:00
|
|
|
EG(exception) = NULL;
|
2010-05-12 00:09:43 +08:00
|
|
|
}
|
2008-08-14 18:24:52 +08:00
|
|
|
}
|
2006-05-21 05:02:44 +08:00
|
|
|
zend_call_method_with_0_params(&obj, object->ce, &destructor, ZEND_DESTRUCTOR_FUNC_NAME, NULL);
|
2010-05-12 00:09:43 +08:00
|
|
|
if (old_exception) {
|
|
|
|
if (EG(exception)) {
|
2010-08-16 17:20:46 +08:00
|
|
|
zend_exception_set_previous(EG(exception), old_exception TSRMLS_CC);
|
2010-05-12 00:09:43 +08:00
|
|
|
} else {
|
|
|
|
EG(exception) = old_exception;
|
|
|
|
}
|
|
|
|
}
|
2006-07-26 23:29:27 +08:00
|
|
|
zval_ptr_dtor(&obj);
|
2001-12-27 22:35:09 +08:00
|
|
|
}
|
2004-02-04 17:56:20 +08:00
|
|
|
}
|
|
|
|
|
2005-02-24 10:35:59 +08:00
|
|
|
ZEND_API void zend_objects_free_object_storage(zend_object *object TSRMLS_DC)
|
2004-02-04 17:56:20 +08:00
|
|
|
{
|
2006-03-29 22:28:43 +08:00
|
|
|
zend_object_std_dtor(object TSRMLS_CC);
|
2004-02-04 19:56:07 +08:00
|
|
|
efree(object);
|
2001-12-27 22:35:09 +08:00
|
|
|
}
|
|
|
|
|
2003-03-26 14:32:53 +08:00
|
|
|
ZEND_API zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type TSRMLS_DC)
|
2012-05-13 13:12:48 +08:00
|
|
|
{
|
2001-08-07 11:17:33 +08:00
|
|
|
zend_object_value retval;
|
|
|
|
|
2002-05-31 20:09:19 +08:00
|
|
|
*object = emalloc(sizeof(zend_object));
|
2001-08-12 02:04:07 +08:00
|
|
|
(*object)->ce = class_type;
|
2010-05-24 22:11:39 +08:00
|
|
|
(*object)->properties = NULL;
|
|
|
|
(*object)->properties_table = NULL;
|
|
|
|
(*object)->guards = NULL;
|
2004-02-04 17:56:20 +08:00
|
|
|
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);
|
2002-02-07 22:08:43 +08:00
|
|
|
retval.handlers = &std_object_handlers;
|
2001-08-07 11:17:33 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-08-13 01:20:25 +08:00
|
|
|
ZEND_API zend_object *zend_objects_get_address(const zval *zobject TSRMLS_DC)
|
2001-08-07 11:17:33 +08:00
|
|
|
{
|
2002-05-31 22:32:19 +08:00
|
|
|
return (zend_object *)zend_object_store_get_object(zobject TSRMLS_CC);
|
2001-08-07 11:17:33 +08:00
|
|
|
}
|
2001-08-16 22:04:04 +08:00
|
|
|
|
2003-07-19 17:47:00 +08:00
|
|
|
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)
|
2001-08-16 22:04:04 +08:00
|
|
|
{
|
2010-05-24 22:11:39 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (old_object->properties_table) {
|
|
|
|
if (!new_object->properties_table) {
|
|
|
|
new_object->properties_table = emalloc(sizeof(zval*) * old_object->ce->default_properties_count);
|
|
|
|
memset(new_object->properties_table, 0, sizeof(zval*) * old_object->ce->default_properties_count);
|
|
|
|
}
|
|
|
|
for (i = 0; i < old_object->ce->default_properties_count; i++) {
|
|
|
|
if (!new_object->properties) {
|
|
|
|
if (new_object->properties_table[i]) {
|
|
|
|
zval_ptr_dtor(&new_object->properties_table[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!old_object->properties) {
|
|
|
|
new_object->properties_table[i] = old_object->properties_table[i];
|
|
|
|
if (new_object->properties_table[i]) {
|
|
|
|
Z_ADDREF_P(new_object->properties_table[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (old_object->properties) {
|
|
|
|
if (!new_object->properties) {
|
|
|
|
ALLOC_HASHTABLE(new_object->properties);
|
|
|
|
zend_hash_init(new_object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
|
|
|
|
}
|
|
|
|
zend_hash_copy(new_object->properties, old_object->properties, (copy_ctor_func_t) zval_add_ref, (void *) NULL /* Not used anymore */, sizeof(zval *));
|
|
|
|
if (old_object->properties_table) {
|
|
|
|
HashPosition pos;
|
|
|
|
zend_property_info *prop_info;
|
|
|
|
for (zend_hash_internal_pointer_reset_ex(&old_object->ce->properties_info, &pos);
|
|
|
|
zend_hash_get_current_data_ex(&old_object->ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
|
|
|
|
zend_hash_move_forward_ex(&old_object->ce->properties_info, &pos)) {
|
|
|
|
if ((prop_info->flags & ZEND_ACC_STATIC) == 0) {
|
|
|
|
if (zend_hash_quick_find(new_object->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&new_object->properties_table[prop_info->offset]) == FAILURE) {
|
|
|
|
new_object->properties_table[prop_info->offset] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-18 22:10:45 +08:00
|
|
|
|
2001-12-27 03:54:20 +08:00
|
|
|
if (old_object->ce->clone) {
|
|
|
|
zval *new_obj;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(new_obj);
|
|
|
|
new_obj->type = IS_OBJECT;
|
2003-07-19 17:47:00 +08:00
|
|
|
new_obj->value.obj = new_obj_val;
|
2001-12-27 03:54:20 +08:00
|
|
|
zval_copy_ctor(new_obj);
|
|
|
|
|
2004-09-29 06:55:22 +08:00
|
|
|
zend_call_method_with_0_params(&new_obj, old_object->ce, &old_object->ce->clone, ZEND_CLONE_FUNC_NAME, NULL);
|
2001-12-27 03:54:20 +08:00
|
|
|
|
|
|
|
zval_ptr_dtor(&new_obj);
|
|
|
|
}
|
2003-07-19 17:47:00 +08:00
|
|
|
}
|
2001-08-16 22:04:04 +08:00
|
|
|
|
2003-07-19 17:47:00 +08:00
|
|
|
ZEND_API zend_object_value zend_objects_clone_obj(zval *zobject TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zend_object_value new_obj_val;
|
|
|
|
zend_object *old_object;
|
|
|
|
zend_object *new_object;
|
|
|
|
zend_object_handle handle = Z_OBJ_HANDLE_P(zobject);
|
|
|
|
|
2012-05-13 13:12:48 +08:00
|
|
|
/* assume that create isn't overwritten, so when clone depends on the
|
2003-07-19 17:47:00 +08:00
|
|
|
* overwritten one then it must itself be overwritten */
|
|
|
|
old_object = zend_objects_get_address(zobject TSRMLS_CC);
|
|
|
|
new_obj_val = zend_objects_new(&new_object, old_object->ce TSRMLS_CC);
|
|
|
|
|
|
|
|
zend_objects_clone_members(new_object, new_obj_val, old_object, handle TSRMLS_CC);
|
|
|
|
|
|
|
|
return new_obj_val;
|
2001-08-16 22:04:04 +08:00
|
|
|
}
|
2003-02-01 09:49:15 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|