php-src/Zend/zend_variables.c

373 lines
8.2 KiB
C
Raw Normal View History

1999-04-08 02:10:10 +08:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
2014-01-03 11:08:10 +08:00
| Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
1999-04-08 02:10:10 +08:00
+----------------------------------------------------------------------+
2001-12-11 23:16:21 +08:00
| This source file is subject to version 2.00 of the Zend license, |
1999-07-16 22:58:16 +08:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2001-12-11 23:16:21 +08:00
| http://www.zend.com/license/2_00.txt. |
1999-07-16 22:58:16 +08:00
| 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. |
1999-04-08 02:10:10 +08:00
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
1999-07-16 22:58:16 +08:00
1999-04-08 02:10:10 +08:00
#include <stdio.h>
#include "zend.h"
#include "zend_API.h"
#include "zend_ast.h"
1999-04-08 02:10:10 +08:00
#include "zend_globals.h"
#include "zend_constants.h"
#include "zend_list.h"
2014-03-19 21:00:28 +08:00
ZEND_API void _zval_dtor_func(zend_refcounted *p ZEND_FILE_LINE_DC)
1999-04-08 02:10:10 +08:00
{
switch (GC_TYPE(p)) {
1999-04-08 02:10:10 +08:00
case IS_STRING:
2014-03-19 21:00:28 +08:00
case IS_CONSTANT: {
zend_string *str = (zend_string*)p;
2014-04-15 19:40:40 +08:00
CHECK_ZVAL_STRING_REL(str);
2014-03-19 21:00:28 +08:00
STR_RELEASE(str);
break;
2014-03-19 21:00:28 +08:00
}
case IS_ARRAY:
case IS_CONSTANT_ARRAY: {
zend_array *arr =(zend_array*)p;
TSRMLS_FETCH();
1999-04-08 02:10:10 +08:00
2014-03-19 21:00:28 +08:00
if (arr != &EG(symbol_table)) {
2012-10-18 20:10:35 +08:00
/* break possible cycles */
GC_TYPE(arr) = IS_NULL;
2014-03-19 21:00:28 +08:00
zend_hash_destroy(&arr->ht);
GC_REMOVE_FROM_BUFFER(arr);
efree(arr);
}
break;
1999-04-08 02:10:10 +08:00
}
2014-03-19 21:00:28 +08:00
case IS_CONSTANT_AST: {
zend_ast_ref *ast =(zend_ast_ref*)p;
2014-03-19 21:00:28 +08:00
zend_ast_destroy(ast->ast);
efree(ast);
break;
2014-03-19 21:00:28 +08:00
}
case IS_OBJECT: {
zend_object *obj = (zend_object*)p;
2002-03-16 00:26:17 +08:00
TSRMLS_FETCH();
2014-03-19 21:00:28 +08:00
OBJ_RELEASE(obj);
break;
2002-03-16 00:26:17 +08:00
}
2014-03-19 21:00:28 +08:00
case IS_RESOURCE: {
zend_resource *res = (zend_resource*)p;
2001-07-30 12:54:16 +08:00
TSRMLS_FETCH();
if (--GC_REFCOUNT(res) == 0) {
/* destroy resource */
2014-03-19 21:00:28 +08:00
zend_list_delete(res);
}
break;
2001-07-30 12:54:16 +08:00
}
2014-03-19 21:00:28 +08:00
case IS_REFERENCE: {
zend_reference *ref = (zend_reference*)p;
if (--GC_REFCOUNT(ref) == 0) {
zval_ptr_dtor(&ref->val);
efree(ref);
2014-03-19 21:00:28 +08:00
}
break;
}
1999-04-08 02:10:10 +08:00
default:
break;
1999-04-08 02:10:10 +08:00
}
}
2014-03-19 21:00:28 +08:00
ZEND_API void _zval_dtor_func_for_ptr(zend_refcounted *p ZEND_FILE_LINE_DC)
{
switch (GC_TYPE(p)) {
case IS_STRING:
2014-03-19 21:00:28 +08:00
case IS_CONSTANT: {
zend_string *str = (zend_string*)p;
2014-04-15 19:40:40 +08:00
CHECK_ZVAL_STRING_REL(str);
2014-03-19 21:00:28 +08:00
STR_FREE(str);
break;
2014-03-19 21:00:28 +08:00
}
case IS_ARRAY:
case IS_CONSTANT_ARRAY: {
2014-03-19 21:00:28 +08:00
zend_array *arr =(zend_array*)p;
TSRMLS_FETCH();
2014-03-19 21:00:28 +08:00
if (arr != &EG(symbol_table)) {
/* break possible cycles */
GC_TYPE(arr) = IS_NULL;
2014-03-19 21:00:28 +08:00
zend_hash_destroy(&arr->ht);
GC_REMOVE_FROM_BUFFER(arr);
efree(arr);
}
break;
}
2014-03-19 21:00:28 +08:00
case IS_CONSTANT_AST: {
zend_ast_ref *ast =(zend_ast_ref*)p;
zend_ast_destroy(ast->ast);
efree(ast);
break;
2014-03-19 21:00:28 +08:00
}
case IS_OBJECT: {
zend_object *obj = (zend_object*)p;
TSRMLS_FETCH();
2014-03-19 21:00:28 +08:00
zend_objects_store_del(obj TSRMLS_CC);
break;
}
2014-03-19 21:00:28 +08:00
case IS_RESOURCE: {
zend_resource *res = (zend_resource*)p;
TSRMLS_FETCH();
/* destroy resource */
2014-03-19 21:00:28 +08:00
zend_list_delete(res);
break;
2014-03-19 21:00:28 +08:00
}
case IS_REFERENCE: {
zend_reference *ref = (zend_reference*)p;
zval_ptr_dtor(&ref->val);
efree(ref);
break;
}
default:
break;
}
}
1999-04-08 02:10:10 +08:00
ZEND_API void _zval_internal_dtor(zval *zvalue ZEND_FILE_LINE_DC)
{
switch (Z_TYPE_P(zvalue)) {
case IS_STRING:
case IS_CONSTANT:
2014-04-15 19:40:40 +08:00
CHECK_ZVAL_STRING_REL(Z_STR_P(zvalue));
STR_RELEASE(Z_STR_P(zvalue));
break;
case IS_ARRAY:
case IS_CONSTANT_ARRAY:
2013-11-07 02:21:07 +08:00
case IS_CONSTANT_AST:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
break;
case IS_REFERENCE: {
zend_reference *ref = (zend_reference*)Z_REF_P(zvalue);
zval_internal_ptr_dtor(&ref->val);
free(ref);
break;
}
case IS_LONG:
case IS_DOUBLE:
case IS_FALSE:
case IS_TRUE:
case IS_NULL:
default:
break;
}
}
ZEND_API void _zval_internal_dtor_for_ptr(zval *zvalue ZEND_FILE_LINE_DC)
{
switch (Z_TYPE_P(zvalue)) {
case IS_STRING:
case IS_CONSTANT:
2014-04-15 19:40:40 +08:00
CHECK_ZVAL_STRING_REL(Z_STR_P(zvalue));
STR_FREE(Z_STR_P(zvalue));
break;
case IS_ARRAY:
case IS_CONSTANT_ARRAY:
case IS_CONSTANT_AST:
case IS_OBJECT:
case IS_RESOURCE:
zend_error(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
break;
case IS_REFERENCE: {
zend_reference *ref = (zend_reference*)Z_REF_P(zvalue);
zval_internal_ptr_dtor(&ref->val);
free(ref);
break;
}
case IS_LONG:
case IS_DOUBLE:
case IS_FALSE:
case IS_TRUE:
case IS_NULL:
default:
break;
}
}
ZEND_API void zval_add_ref(zval *p)
1999-04-08 02:10:10 +08:00
{
if (Z_REFCOUNTED_P(p)) {
if (Z_ISREF_P(p) && Z_REFCOUNT_P(p) == 1) {
ZVAL_DUP(p, Z_REFVAL_P(p));
} else {
Z_ADDREF_P(p);
}
}
1999-04-08 02:10:10 +08:00
}
ZEND_API void zval_add_ref_unref(zval *p)
{
if (Z_REFCOUNTED_P(p)) {
if (Z_ISREF_P(p)) {
ZVAL_DUP(p, Z_REFVAL_P(p));
} else {
Z_ADDREF_P(p);
}
}
}
1999-04-08 02:10:10 +08:00
ZEND_API void _zval_copy_ctor_func(zval *zvalue ZEND_FILE_LINE_DC)
1999-04-08 02:10:10 +08:00
{
switch (Z_TYPE_P(zvalue)) {
case IS_CONSTANT:
1999-04-08 02:10:10 +08:00
case IS_STRING:
2014-04-15 19:40:40 +08:00
CHECK_ZVAL_STRING_REL(Z_STR_P(zvalue));
Z_STR_P(zvalue) = STR_DUP(Z_STR_P(zvalue), 0);
1999-04-08 02:10:10 +08:00
break;
case IS_ARRAY:
case IS_CONSTANT_ARRAY: {
HashTable *ht;
TSRMLS_FETCH();
1999-04-08 02:10:10 +08:00
2014-03-20 15:37:42 +08:00
if (Z_ARR_P(zvalue) == &EG(symbol_table)) {
return; /* do nothing */
1999-04-08 02:10:10 +08:00
}
ht = Z_ARRVAL_P(zvalue);
ZVAL_NEW_ARR(zvalue);
zend_hash_init(Z_ARRVAL_P(zvalue), zend_hash_num_elements(ht), NULL, ZVAL_PTR_DTOR, 0);
zend_hash_copy(Z_ARRVAL_P(zvalue), ht, zval_add_ref);
1999-04-08 02:10:10 +08:00
}
break;
case IS_CONSTANT_AST: {
zend_ast_ref *ast = emalloc(sizeof(zend_ast_ref));
GC_REFCOUNT(ast) = 1;
GC_TYPE_INFO(ast) = IS_CONSTANT_AST;
ast->ast = zend_ast_copy(Z_ASTVAL_P(zvalue));
Z_AST_P(zvalue) = ast;
}
break;
case IS_OBJECT:
case IS_RESOURCE:
case IS_REFERENCE:
Z_ADDREF_P(zvalue);
1999-04-08 02:10:10 +08:00
break;
}
}
ZEND_API int zend_print_variable(zval *var TSRMLS_DC)
1999-04-08 02:10:10 +08:00
{
return zend_print_zval(var, 0 TSRMLS_CC);
1999-04-08 02:10:10 +08:00
}
ZEND_API void _zval_dtor_wrapper(zval *zvalue)
1999-08-28 03:17:19 +08:00
{
zval_dtor(zvalue);
1999-08-28 03:17:19 +08:00
}
#if ZEND_DEBUG
ZEND_API void _zval_copy_ctor_wrapper(zval *zvalue)
{
zval_copy_ctor(zvalue);
}
ZEND_API void _zval_internal_dtor_wrapper(zval *zvalue)
{
zval_internal_dtor(zvalue);
}
1999-08-28 03:17:19 +08:00
ZEND_API void _zval_ptr_dtor_wrapper(zval *zval_ptr)
1999-08-28 03:17:19 +08:00
{
zval_ptr_dtor(zval_ptr);
1999-08-28 03:17:19 +08:00
}
ZEND_API void _zval_internal_ptr_dtor_wrapper(zval *zval_ptr)
{
zval_internal_ptr_dtor(zval_ptr);
}
1999-08-28 03:17:19 +08:00
#endif
ZEND_API int zval_copy_static_var(zval *p TSRMLS_DC, int num_args, va_list args, zend_hash_key *key) /* {{{ */
{
HashTable *target = va_arg(args, HashTable*);
zend_bool is_ref;
zval tmp;
if (Z_CONST_FLAGS_P(p) & (IS_LEXICAL_VAR|IS_LEXICAL_REF)) {
is_ref = Z_CONST_FLAGS_P(p) & IS_LEXICAL_REF;
if (!EG(active_symbol_table)) {
zend_rebuild_symbol_table(TSRMLS_C);
}
p = zend_hash_find(&EG(active_symbol_table)->ht, key->key);
if (!p) {
p = &tmp;
ZVAL_NULL(&tmp);
if (is_ref) {
ZVAL_NEW_REF(&tmp, &tmp);
zend_hash_add(&EG(active_symbol_table)->ht, key->key, &tmp);
2014-03-22 01:34:15 +08:00
Z_ADDREF_P(p);
} else {
zend_error(E_NOTICE,"Undefined variable: %s", key->key->val);
}
} else {
if (Z_TYPE_P(p) == IS_INDIRECT) {
p = Z_INDIRECT_P(p);
if (Z_TYPE_P(p) == IS_UNDEF) {
if (!is_ref) {
zend_error(E_NOTICE,"Undefined variable: %s", key->key->val);
p = &tmp;
ZVAL_NULL(&tmp);
} else {
ZVAL_NULL(p);
}
}
}
if (is_ref) {
SEPARATE_ZVAL_TO_MAKE_IS_REF(p);
2014-03-22 01:34:15 +08:00
Z_ADDREF_P(p);
} else if (Z_ISREF_P(p)) {
2014-03-22 01:34:15 +08:00
ZVAL_DUP(&tmp, Z_REFVAL_P(p));
p = &tmp;
2014-03-22 01:34:15 +08:00
} else if (Z_REFCOUNTED_P(p)) {
Z_ADDREF_P(p);
}
}
2014-03-22 01:34:15 +08:00
} else if (Z_REFCOUNTED_P(p)) {
Z_ADDREF_P(p);
}
zend_hash_add(target, key->key, p);
return ZEND_HASH_APPLY_KEEP;
}
/* }}} */
1999-04-08 02:10:10 +08:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
1999-04-08 02:10:10 +08:00
* End:
*/