php-src/Zend/zend_objects.c

301 lines
8.9 KiB
C
Raw Normal View History

2006-05-20 21:02:44 +00:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
2018-01-02 04:57:58 +00:00
| Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| 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 |
| available through the world-wide-web at the following url: |
| 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> |
| Dmitry Stogov <dmitry@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "zend.h"
#include "zend_globals.h"
#include "zend_variables.h"
2001-12-26 19:54:20 +00:00
#include "zend_API.h"
2003-12-27 20:33:14 +00:00
#include "zend_interfaces.h"
2006-05-20 21:02:44 +00:00
#include "zend_exceptions.h"
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
{
GC_SET_REFCOUNT(object, 1);
GC_TYPE_INFO(object) = IS_OBJECT | (GC_COLLECTABLE << GC_FLAGS_SHIFT);
object->ce = ce;
object->properties = NULL;
2014-12-13 22:06:14 +00:00
zend_objects_store_put(object);
2015-04-27 15:30:33 +00:00
if (UNEXPECTED(ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
ZVAL_UNDEF(object->properties_table + object->ce->default_properties_count);
}
}
2014-12-13 22:06:14 +00:00
ZEND_API void zend_object_std_dtor(zend_object *object)
{
2015-04-27 15:30:33 +00:00
zval *p, *end;
if (object->properties) {
if (EXPECTED(!(GC_FLAGS(object->properties) & IS_ARRAY_IMMUTABLE))) {
if (EXPECTED(GC_DELREF(object->properties) == 0)) {
zend_array_destroy(object->properties);
}
}
}
2015-04-27 15:30:33 +00:00
p = object->properties_table;
if (EXPECTED(object->ce->default_properties_count)) {
end = p + object->ce->default_properties_count;
do {
i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
p++;
} while (p != end);
}
if (UNEXPECTED(object->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
if (EXPECTED(Z_TYPE_P(p) == IS_STRING)) {
zend_string_release_ex(Z_STR_P(p), 0);
} else if (Z_TYPE_P(p) == IS_ARRAY) {
HashTable *guards;
guards = Z_ARRVAL_P(p);
ZEND_ASSERT(guards != NULL);
zend_hash_destroy(guards);
FREE_HASHTABLE(guards);
}
}
}
2014-12-13 22:06:14 +00:00
ZEND_API void zend_objects_destroy_object(zend_object *object)
{
zend_function *destructor = object->ce->destructor;
2003-07-01 19:13:50 +00:00
if (destructor) {
zend_object *old_exception;
zend_class_entry *orig_fake_scope;
zend_fcall_info fci;
zend_fcall_info_cache fcic;
zval ret;
2006-05-20 21:02:44 +00: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 (EG(current_execute_data)) {
zend_class_entry *scope = zend_get_executed_scope();
2003-08-09 14:32:33 +00:00
if (object->ce != scope) {
zend_throw_error(NULL,
2015-07-03 21:04:33 +00:00
"Call to private %s::__destruct() from context '%s'",
ZSTR_VAL(object->ce->name),
scope ? ZSTR_VAL(scope->name) : "");
return;
2015-07-03 21:04:33 +00:00
}
} else {
zend_error(E_WARNING,
"Call to private %s::__destruct() from context '' during shutdown ignored",
ZSTR_VAL(object->ce->name));
return;
}
} else {
/* Ensure that if we're calling a protected function, we're allowed to do so.
*/
if (EG(current_execute_data)) {
zend_class_entry *scope = zend_get_executed_scope();
2003-08-09 14:32:33 +00:00
if (!zend_check_protected(zend_get_function_root_class(destructor), scope)) {
zend_throw_error(NULL,
2015-07-03 21:04:33 +00:00
"Call to protected %s::__destruct() from context '%s'",
ZSTR_VAL(object->ce->name),
scope ? ZSTR_VAL(scope->name) : "");
return;
2015-07-03 21:04:33 +00:00
}
} else {
zend_error(E_WARNING,
"Call to protected %s::__destruct() from context '' during shutdown ignored",
ZSTR_VAL(object->ce->name));
return;
}
}
}
GC_ADDREF(object);
/* 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.
*/
old_exception = NULL;
if (EG(exception)) {
if (EG(exception) == object) {
zend_error_noreturn(E_CORE_ERROR, "Attempt to destruct pending exception");
} else {
old_exception = EG(exception);
EG(exception) = NULL;
}
2008-08-14 10:24:52 +00:00
}
orig_fake_scope = EG(fake_scope);
EG(fake_scope) = NULL;
ZVAL_UNDEF(&ret);
fci.size = sizeof(fci);
fci.object = object;
fci.retval = &ret;
fci.param_count = 0;
fci.params = NULL;
fci.no_separation = 1;
ZVAL_UNDEF(&fci.function_name); /* Unused */
fcic.function_handler = destructor;
fcic.called_scope = object->ce;
fcic.object = object;
zend_call_function(&fci, &fcic);
zval_ptr_dtor(&ret);
if (old_exception) {
if (EG(exception)) {
2014-12-13 22:06:14 +00:00
zend_exception_set_previous(EG(exception), old_exception);
} else {
EG(exception) = old_exception;
}
}
OBJ_RELEASE(object);
EG(fake_scope) = orig_fake_scope;
}
}
ZEND_API zend_object* ZEND_FASTCALL zend_objects_new(zend_class_entry *ce)
{
zend_object *object = emalloc(sizeof(zend_object) + zend_object_properties_size(ce));
2014-12-13 22:06:14 +00:00
zend_object_std_init(object, ce);
object->handlers = &std_object_handlers;
return object;
}
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
{
if (old_object->ce->default_properties_count) {
2015-04-28 16:11:45 +00:00
zval *src = old_object->properties_table;
zval *dst = new_object->properties_table;
zval *end = src + old_object->ce->default_properties_count;
do {
i_zval_ptr_dtor(dst ZEND_FILE_LINE_CC);
ZVAL_COPY_VALUE(dst, src);
zval_add_ref(dst);
src++;
dst++;
} while (src != end);
} else if (old_object->properties && !old_object->ce->clone) {
/* fast copy */
if (EXPECTED(old_object->handlers == &std_object_handlers)) {
if (EXPECTED(!(GC_FLAGS(old_object->properties) & IS_ARRAY_IMMUTABLE))) {
GC_ADDREF(old_object->properties);
}
new_object->properties = old_object->properties;
return;
}
}
2015-05-19 10:23:13 +00:00
if (old_object->properties &&
EXPECTED(zend_hash_num_elements(old_object->properties))) {
zval *prop, new_prop;
2014-08-25 17:24:55 +00:00
zend_ulong num_key;
zend_string *key;
if (!new_object->properties) {
new_object->properties = zend_new_array(zend_hash_num_elements(old_object->properties));
2018-03-22 21:13:45 +00:00
zend_hash_real_init_mixed(new_object->properties);
2015-05-19 10:23:13 +00:00
} else {
zend_hash_extend(new_object->properties, new_object->properties->nNumUsed + zend_hash_num_elements(old_object->properties), 0);
}
2018-01-22 11:58:16 +00:00
HT_FLAGS(new_object->properties) |=
HT_FLAGS(old_object->properties) & HASH_FLAG_HAS_EMPTY_IND;
ZEND_HASH_FOREACH_KEY_VAL(old_object->properties, num_key, key, prop) {
if (Z_TYPE_P(prop) == IS_INDIRECT) {
ZVAL_INDIRECT(&new_prop, new_object->properties_table + (Z_INDIRECT_P(prop) - old_object->properties_table));
} else {
ZVAL_COPY_VALUE(&new_prop, prop);
zval_add_ref(&new_prop);
}
2015-05-19 10:23:13 +00:00
if (EXPECTED(key)) {
_zend_hash_append(new_object->properties, key, &new_prop);
} else {
2014-12-15 11:43:16 +00:00
zend_hash_index_add_new(new_object->properties, num_key, &new_prop);
}
} ZEND_HASH_FOREACH_END();
}
2001-12-26 19:54:20 +00:00
if (old_object->ce->clone) {
zend_fcall_info fci;
zend_fcall_info_cache fcic;
zval ret;
GC_ADDREF(new_object);
ZVAL_UNDEF(&ret);
fci.size = sizeof(fci);
fci.object = new_object;
fci.retval = &ret;
fci.param_count = 0;
fci.params = NULL;
fci.no_separation = 1;
ZVAL_UNDEF(&fci.function_name); /* Unused */
fcic.function_handler = new_object->ce->clone;
fcic.called_scope = new_object->ce;
fcic.object = new_object;
2001-12-26 19:54:20 +00:00
zend_call_function(&fci, &fcic);
zval_ptr_dtor(&ret);
OBJ_RELEASE(new_object);
2001-12-26 19:54:20 +00:00
}
}
2014-12-13 22:06:14 +00:00
ZEND_API zend_object *zend_objects_clone_obj(zval *zobject)
{
zend_object *old_object;
zend_object *new_object;
/* assume that create isn't overwritten, so when clone depends on the
* overwritten one then it must itself be overwritten */
old_object = Z_OBJ_P(zobject);
2014-12-13 22:06:14 +00:00
new_object = zend_objects_new(old_object->ce);
/* zend_objects_clone_members() expect the properties to be initialized. */
if (new_object->ce->default_properties_count) {
zval *p = new_object->properties_table;
zval *end = p + new_object->ce->default_properties_count;
do {
ZVAL_UNDEF(p);
p++;
} while (p != end);
}
2014-12-13 22:06:14 +00:00
zend_objects_clone_members(new_object, old_object);
return new_object;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
2017-07-04 16:12:45 +00:00
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/