summaryrefslogtreecommitdiff
path: root/ext/spl/spl_array.c
blob: 7c68d3c06f17ca978e76e6f335719452a3d3e150 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Marcus Boerger <helly@php.net>                              |
   +----------------------------------------------------------------------+
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_compile.h"
#include "zend_execute_locks.h"

#include "php_spl.h"
#include "spl_functions.h"
#include "spl_engine.h"
#include "spl_array.h"

#define DELETE_ZVAL(z) \
	if ((z)->refcount < 2) { \
		zval_dtor(z); \
		FREE_ZVAL(z); /* maybe safe_free_zval_ptr is needed for the uninitialised things */ \
	}

#define DELETE_RET_ZVAL(z) \
	if ((z)->refcount < 3) { \
		zval_dtor(z); \
		FREE_ZVAL(z); /* maybe safe_free_zval_ptr is needed for the uninitialised things */ \
	}

#define AI_PTR_2_PTR_PTR(ai) \
	(ai).ptr_ptr = &((ai).ptr)

/* {{{ spl_array_writer_default stuff */
typedef struct {
	zval *obj;
	zval *idx;
} spl_array_writer_object;

static zend_class_entry *spl_array_writer_default_get_class(zval *object TSRMLS_DC)
{
#ifdef SPL_ARRAY_WRITE
	return spl_ce_array_writer_default;
#else
	return (zend_class_entry *)1; /* force an error here: this ensures not equal */
#endif
}

static zend_object_handlers spl_array_writer_default_handlers = {
	ZEND_OBJECTS_STORE_HANDLERS,
	
	NULL,     /* read_property */
	NULL,     /* write_property */
	NULL,     /* get_property_ptr */
	NULL,     /* get_property_zval_ptr */
	NULL,     /* get */
	NULL,     /* set */
	NULL,     /* has_property */
	NULL,     /* unset_property */
	NULL,     /* get_properties */
	NULL,     /* get_method */
	NULL,     /* call_method */
	NULL,     /* get_constructor */
	spl_array_writer_default_get_class,     /* get_class_entry */
	NULL,     /* get_class_name */
	NULL      /* compare_objects */
};
/* }}} */

/* {{{ spl_array_writer_dtor */
void spl_array_writer_default_dtor(void *object, zend_object_handle handle TSRMLS_DC)
{
	spl_array_writer_object *writer = (spl_array_writer_object*) object;

	if (writer->obj)
	{
		writer->obj->refcount--;
/*		DELETE_ZVAL(writer->obj); */
	}
	if (writer->idx)
	{
		writer->idx->refcount--;
		DELETE_ZVAL(writer->idx);
	}
	efree(writer);
}
/* }}} */

/* {{{ spl_array_writer_default_create */
zend_object_value spl_array_writer_default_create(zend_class_entry *class_type TSRMLS_DC)
{
	zend_object_value retval;
	spl_array_writer_object *intern;

	intern = ecalloc(sizeof(spl_array_writer_object), 1);

	retval.handle = zend_objects_store_put(intern, spl_array_writer_default_dtor, NULL TSRMLS_CC);
	retval.handlers = &spl_array_writer_default_handlers;

	return retval;
}
/* }}} */

/* {{{ spl_array_writer_default_set */
void spl_array_writer_default_set(zval *object, zval *newval, zval **retval TSRMLS_DC)
{
	zval *obj, *idx;
	spl_array_writer_object *writer;

	writer = (spl_array_writer_object *) zend_object_store_get_object(object TSRMLS_CC);
	obj = writer->obj;
	idx = writer->idx;
	spl_begin_method_call_arg_ex2(&obj, "set", retval, &idx, &newval, 0, NULL TSRMLS_CC);
}
/* }}} */

/* {{{ SPL_CLASS_FUNCTION(array_writer_default, __construct) */
SPL_CLASS_FUNCTION(array_writer_default, __construct)
{
	zval *object = getThis();
	zval *obj, *idx;
	spl_array_writer_object *writer;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &obj, &idx) == FAILURE) {
		php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to parse parameters");
		return;
	}
	writer = (spl_array_writer_object *) zend_object_store_get_object(object TSRMLS_CC);
	writer->obj = obj; obj->refcount++;
	writer->idx = idx; idx->refcount++;

}
/* }}} */

/* {{{ SPL_CLASS_FUNCTION(array_writer_default, set) */
SPL_CLASS_FUNCTION(array_writer_default, set)
{
	zval *object = getThis();
	zval *newval;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &newval) == FAILURE) {
		php_error_docref(NULL TSRMLS_CC, E_ERROR, "Failed to parse parameters");
		return;
	}
	spl_array_writer_default_set(object, newval, &return_value TSRMLS_CC);
}
/* }}} */

/* {{{ spl_fetch_dimension_address */
int spl_fetch_dimension_address(znode *result, znode *op1, znode *op2, temp_variable *Ts, int type TSRMLS_DC)
{
	zval **container_ptr = spl_get_zval_ptr_ptr(op1, Ts TSRMLS_CC);

	if (spl_is_instance_of(container_ptr, spl_ce_array_read TSRMLS_CC)) {
		zval **retval = &(T(result->u.var).var.ptr);
		zval *dim = spl_get_zval_ptr(op2, Ts, &EG(free_op2) TSRMLS_CC);
		zval *exists;

		/*ALLOC_ZVAL(exists); not needed */
		spl_begin_method_call_arg_ex1(container_ptr, "exists", &exists, &dim, 0, NULL TSRMLS_CC);
		if (!i_zend_is_true(exists)) {
			if (type == BP_VAR_R || type == BP_VAR_RW) {
				SEPARATE_ZVAL(&dim);
				convert_to_string_ex(&dim);
				zend_error(E_NOTICE,"Undefined index:  %s", Z_STRVAL_P(dim));
				DELETE_ZVAL(dim);
			}
			if (type == BP_VAR_R || type == BP_VAR_IS) {
				DELETE_RET_ZVAL(exists);
				*retval = &EG(error_zval);
				(*retval)->refcount++;
				FREE_OP(Ts, op2, EG(free_op2));
				SELECTIVE_PZVAL_LOCK(*retval, result);
				return 0;
			}
		}
		DELETE_RET_ZVAL(exists);
		if (type == BP_VAR_R || type == BP_VAR_IS) {
			spl_begin_method_call_arg_ex1(container_ptr, "get", retval, &dim, 0, NULL TSRMLS_CC);
			(*retval)->refcount--;
		} else 
#ifdef SPL_ARRAY_WRITE
		if (spl_is_instance_of(container_ptr, spl_ce_array_access_ex TSRMLS_CC)) {
			/* array_access_ex instaces have their own way of creating an access_writer */
			spl_begin_method_call_arg_ex1(container_ptr, "new_writer", retval, &dim, 0, NULL TSRMLS_CC);
			T(result->u.var).var.ptr = *retval;
			AI_PTR_2_PTR_PTR(T(result->u.var).var);
			SELECTIVE_PZVAL_LOCK(*retval, result);
		} else if (spl_is_instance_of(container_ptr, spl_ce_array_access TSRMLS_CC)) {
			/* array_access instances create the default array_writer: array_write */
			spl_array_writer_object *writer;
			spl_instanciate(spl_ce_array_writer_default, retval TSRMLS_CC);
			T(result->u.var).var.ptr = *retval;
			AI_PTR_2_PTR_PTR(T(result->u.var).var);
			writer = (spl_array_writer_object *) zend_object_store_get_object(*retval TSRMLS_CC);
			writer->obj = *container_ptr; writer->obj->refcount++;
			writer->idx = dim;            writer->idx->refcount++;
			SELECTIVE_PZVAL_LOCK(*retval, result);
		} else {
			zend_error(E_ERROR, "Object must implement spl::array_access for write access");
			retval = &EG(error_zval_ptr);
		}
		SELECTIVE_PZVAL_LOCK(*retval, result);
#else
		zend_error(E_ERROR, "SPL compiled withut array write hook");
#endif
		FREE_OP(Ts, op2, EG(free_op2));
		return 0;
	}
	return 1;
}
/* }}} */

/* {{{ ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_R) */
#ifdef SPL_ARRAY_READ
ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_R)
{
	if (!spl_fetch_dimension_address(&EX(opline)->result, &EX(opline)->op1, &EX(opline)->op2, EX(Ts), BP_VAR_R TSRMLS_CC))
	{
		if (EX(opline)->extended_value == ZEND_FETCH_ADD_LOCK) {
			PZVAL_LOCK(*EX_T(EX(opline)->op1.u.var).var.ptr_ptr);
		}
		spl_unlock_zval_ptr_ptr(&EX(opline)->op1, EX(Ts) TSRMLS_CC);

		AI_PTR_2_PTR_PTR(EX_T(EX(opline)->result.u.var).var);
		NEXT_OPCODE();
	}
	ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_FETCH_DIM_R);
}
#endif
/* }}} */

/* {{{ ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_W) */
#ifdef SPL_ARRAY_READ
ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_W)
{
	if (!spl_fetch_dimension_address(&EX(opline)->result, &EX(opline)->op1, &EX(opline)->op2, EX(Ts), BP_VAR_W TSRMLS_CC))
	{
		spl_unlock_zval_ptr_ptr(&EX(opline)->op1, EX(Ts) TSRMLS_CC);

		NEXT_OPCODE();
	}
	ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_FETCH_DIM_W);
}
#endif
/* }}} */

/* {{{ ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_RW) */
#ifdef SPL_ARRAY_READ
ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_RW)
{
	if (!spl_fetch_dimension_address(&EX(opline)->result, &EX(opline)->op1, &EX(opline)->op2, EX(Ts), BP_VAR_RW TSRMLS_CC))
	{
		spl_unlock_zval_ptr_ptr(&EX(opline)->op1, EX(Ts) TSRMLS_CC);

		NEXT_OPCODE();
	}
	ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_FETCH_DIM_RW);
}
#endif
/* }}} */

/* {{{ ZEND_EXECUTE_HOOK_FUNCTION(ZEND_ASSIGN) */
#ifdef SPL_ARRAY_WRITE
ZEND_EXECUTE_HOOK_FUNCTION(ZEND_ASSIGN)
{
	zval **writer = spl_get_zval_ptr_ptr(&EX(opline)->op1, EX(Ts) TSRMLS_CC);
	zval *newval, *retval, *target;
	znode *result;

	if (writer && *writer && Z_TYPE_PP(writer) == IS_OBJECT) {
		/* optimization: do pre checks and only test for handlers in case of
		 * spl::array_writer_default, for spl::array_writer we must use the 
		 * long way of calling spl_instance
		 * if (spl_is_instance_of(writer, spl_ce_array_writer_default TSRMLS_CC))
		 */
		if ((*writer)->value.obj.handlers == &spl_array_writer_default_handlers) {
			newval = spl_get_zval_ptr(&EX(opline)->op2, EX(Ts), &EG(free_op2) TSRMLS_CC);
			spl_array_writer_default_set(*writer, newval, &retval TSRMLS_CC);
		} else if (spl_is_instance_of(writer, spl_ce_array_writer TSRMLS_CC)) {
			newval = spl_get_zval_ptr(&EX(opline)->op2, EX(Ts), &EG(free_op2) TSRMLS_CC);
			spl_begin_method_call_arg_ex1(writer, "set", &retval, &newval, 0, NULL TSRMLS_CC);
		} else {
			ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_ASSIGN);
		}
	} else {
		ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_ASSIGN);
	}
	spl_unlock_zval_ptr_ptr(&EX(opline)->op1, EX(Ts) TSRMLS_CC);

	result = &EX(opline)->result;
	if (result) {
		if (retval->refcount<2) {
			if ((*writer)->value.obj.handlers == &spl_array_writer_default_handlers) {
				spl_array_writer_object *object = (spl_array_writer_object *) zend_object_store_get_object(*writer TSRMLS_CC);
				target = object->obj;
			} else {
				target = *writer;
			}
			zend_error(E_WARNING, "Method %s::set() did not return a value, using NULL", Z_OBJCE_P(target)->name);
			DELETE_ZVAL(retval);
			DELETE_ZVAL(newval);
			/* Unfortunately it doesn't work when trying to return newval.
			 * But anyhow it wouldn't make sense...and confuse reference counting and such.
			 */
			retval = &EG(uninitialized_zval);
		} else {
			retval->refcount--;
		}
		EX_T(EX(opline)->result.u.var).var.ptr = retval;
		AI_PTR_2_PTR_PTR(EX_T(EX(opline)->result.u.var).var);
		SELECTIVE_PZVAL_LOCK(retval, result);
	} else {
		retval->refcount = 1;
		DELETE_ZVAL(retval);
	}

	(*writer)->refcount = 1;
	DELETE_ZVAL(*writer);
	FREE_OP(EX(Ts), &EX(opline)->op2, EG(free_op2));

	NEXT_OPCODE();
}
#endif
/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: fdm=marker
 * vim: noet sw=4 ts=4
 */