summaryrefslogtreecommitdiff
path: root/Zend/zend_variables.c
blob: 6d5452c94ea465e6a976987140b9d36839d25f3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski                  |
   +----------------------------------------------------------------------+
   | This source file is subject to version 0.91 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        | 
   | available at through the world-wide-web at                           |
   | http://www.zend.com/license/0_91.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>                                |
   +----------------------------------------------------------------------+
*/


#include <stdio.h>
#include "zend.h"
#include "zend_API.h"
#include "zend_globals.h"
#include "zend_constants.h"
#include "zend_variables.h"
#include "zend_list.h"

ZEND_API char *empty_string = "";	/* in order to save emalloc() and efree() time for
									 * empty strings (usually used to denote empty
									 * return values in failed functions).
									 * The macro STR_FREE() will not efree() it.
									 */

ZEND_API char *undefined_variable_string = "\0";

/* this function MUST set the value for the variable to an empty string */
/* and empty strings must be evaluated as FALSE */
ZEND_API inline void var_reset(zval *var)
{
	var->type = IS_STRING;
	var->value.str.val = empty_string;
	var->value.str.len = 0;
}

ZEND_API inline void var_uninit(zval *var)
{
	var->type = IS_STRING;
	var->value.str.val = undefined_variable_string;
	var->value.str.len = 0;
}
		

ZEND_API int _zval_dtor(zval *zvalue ZEND_FILE_LINE_DC)
{
	if (zvalue->type==IS_LONG) {
		return 1;
	}
	switch(zvalue->type) {
		case IS_STRING:
		case IS_CONSTANT:
			STR_FREE_REL(zvalue->value.str.val);
			break;
		case IS_ARRAY: {
				ELS_FETCH();

				if (zvalue->value.ht && (zvalue->value.ht != &EG(symbol_table))) {
					zend_hash_destroy(zvalue->value.ht);
					efree_rel(zvalue->value.ht);
				}
			}
			break;
		case IS_OBJECT:
			zend_hash_destroy(zvalue->value.obj.properties);
			efree_rel(zvalue->value.obj.properties);
			break;
		case IS_RESOURCE:
			/* destroy resource */
			zend_list_delete(zvalue->value.lval);
			break;
		case IS_LONG:
		case IS_DOUBLE:
		case IS_BOOL:
		default:
			return 1;
			break;
	}
	return 1;
}


void zval_del_ref(zval **p)
{
	(*p)->refcount--;
	if ((*p)->refcount==0) {
		zval_dtor(*p);
		efree(*p);
	}
}                                                                                                                               


void zval_add_ref(zval **p)
{
	(*p)->refcount++;
}


ZEND_API int _zval_copy_ctor(zval *zvalue ZEND_FILE_LINE_DC)
{
	switch (zvalue->type) {
		case IS_RESOURCE:
			zend_list_addref(zvalue->value.lval);
			break;
		case IS_BOOL:
		case IS_LONG:
			break;
		case IS_STRING:
			if (zvalue->value.str.val) {
				if (zvalue->value.str.len==0) {
					if (zvalue->value.str.val==undefined_variable_string) {
						zvalue->value.str.val = undefined_variable_string;
					} else {
						zvalue->value.str.val = empty_string;
					}
					return SUCCESS;
				}
			}
			zvalue->value.str.val = (char *) estrndup_rel(zvalue->value.str.val, zvalue->value.str.len);
			break;
		case IS_ARRAY: {
				zval *tmp;
				HashTable *original_ht = zvalue->value.ht;
				ELS_FETCH();

				if (!zvalue->value.ht) {
					var_reset(zvalue);
					return FAILURE;
				} else if (zvalue->value.ht==&EG(symbol_table)) {
					return SUCCESS; /* do nothing */
				}
				zvalue->value.ht = (HashTable *) emalloc_rel(sizeof(HashTable));
				zend_hash_init(zvalue->value.ht, 0, NULL, PVAL_PTR_DTOR, 0);
				zend_hash_copy(zvalue->value.ht, original_ht, (void (*)(void *)) zval_add_ref, (void *) &tmp, sizeof(zval *));
			}
			break;
		case IS_OBJECT: {
				zval *tmp;
				HashTable *original_ht = zvalue->value.obj.properties;

				zvalue->value.obj.properties = (HashTable *) emalloc_rel(sizeof(HashTable));
				zend_hash_init(zvalue->value.obj.properties, 0, NULL, PVAL_PTR_DTOR, 0);
				zend_hash_copy(zvalue->value.obj.properties, original_ht, (void (*)(void *)) zval_add_ref, (void *) &tmp, sizeof(zval *));
			}
			break;
	}
	return SUCCESS;
}


ZEND_API int zend_print_variable(zval *var) 
{
	return zend_print_zval(var, 0);
}


#if ZEND_DEBUG
ZEND_API int _zval_copy_ctor_wrapper(zval *zvalue)
{
	return zval_copy_ctor(zvalue);
}


ZEND_API int _zval_dtor_wrapper(zval *zvalue)
{
	return zval_dtor(zvalue);
}



ZEND_API int _zval_ptr_dtor_wrapper(zval **zval_ptr)
{
	return zval_ptr_dtor(zval_ptr);
}
#endif


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */