1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2015 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Marcus Boerger <helly@php.net>                              |
16   +----------------------------------------------------------------------+
17 */
18
19/* $Id$ */
20
21#ifdef HAVE_CONFIG_H
22# include "config.h"
23#endif
24
25#include "php.h"
26#include "php_ini.h"
27#include "ext/standard/info.h"
28#include "ext/standard/php_var.h"
29#include "zend_smart_str.h"
30#include "zend_interfaces.h"
31#include "zend_exceptions.h"
32
33#include "php_spl.h"
34#include "spl_functions.h"
35#include "spl_engine.h"
36#include "spl_iterators.h"
37#include "spl_array.h"
38#include "spl_exceptions.h"
39
40zend_object_handlers spl_handler_ArrayObject;
41PHPAPI zend_class_entry  *spl_ce_ArrayObject;
42
43zend_object_handlers spl_handler_ArrayIterator;
44PHPAPI zend_class_entry  *spl_ce_ArrayIterator;
45PHPAPI zend_class_entry  *spl_ce_RecursiveArrayIterator;
46
47#define SPL_ARRAY_STD_PROP_LIST      0x00000001
48#define SPL_ARRAY_ARRAY_AS_PROPS     0x00000002
49#define SPL_ARRAY_CHILD_ARRAYS_ONLY  0x00000004
50#define SPL_ARRAY_OVERLOADED_REWIND  0x00010000
51#define SPL_ARRAY_OVERLOADED_VALID   0x00020000
52#define SPL_ARRAY_OVERLOADED_KEY     0x00040000
53#define SPL_ARRAY_OVERLOADED_CURRENT 0x00080000
54#define SPL_ARRAY_OVERLOADED_NEXT    0x00100000
55#define SPL_ARRAY_IS_SELF            0x01000000
56#define SPL_ARRAY_USE_OTHER          0x02000000
57#define SPL_ARRAY_INT_MASK           0xFFFF0000
58#define SPL_ARRAY_CLONE_MASK         0x0100FFFF
59
60#define SPL_ARRAY_METHOD_NO_ARG             0
61#define SPL_ARRAY_METHOD_USE_ARG            1
62#define SPL_ARRAY_METHOD_MAY_USER_ARG       2
63
64typedef struct _spl_array_object {
65    zval              array;
66    uint32_t          ht_iter;
67    int               ar_flags;
68    unsigned char     nApplyCount;
69    zend_function     *fptr_offset_get;
70    zend_function     *fptr_offset_set;
71    zend_function     *fptr_offset_has;
72    zend_function     *fptr_offset_del;
73    zend_function     *fptr_count;
74    zend_class_entry* ce_get_iterator;
75    zend_object       std;
76} spl_array_object;
77
78static inline spl_array_object *spl_array_from_obj(zend_object *obj) /* {{{ */ {
79    return (spl_array_object*)((char*)(obj) - XtOffsetOf(spl_array_object, std));
80}
81/* }}} */
82
83#define Z_SPLARRAY_P(zv)  spl_array_from_obj(Z_OBJ_P((zv)))
84
85static inline HashTable *spl_array_get_hash_table(spl_array_object* intern, int check_std_props) { /* {{{ */
86    if (intern->ar_flags & SPL_ARRAY_IS_SELF
87        || (check_std_props && (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST))
88    ) {
89        if (!intern->std.properties) {
90            rebuild_object_properties(&intern->std);
91        }
92        return intern->std.properties;
93    } else if (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
94        spl_array_object *other = Z_SPLARRAY_P(&intern->array);
95        return spl_array_get_hash_table(other, check_std_props);
96    } else {
97        return HASH_OF(&intern->array);
98    }
99} /* }}} */
100
101static inline zend_bool spl_array_is_object(spl_array_object *intern) /* {{{ */
102{
103    while (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
104        intern = Z_SPLARRAY_P(&intern->array);
105    }
106    return (intern->ar_flags & SPL_ARRAY_IS_SELF) || Z_TYPE(intern->array) == IS_OBJECT;
107}
108/* }}} */
109
110static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht);
111
112static zend_never_inline void spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern) /* {{{ */
113{
114    intern->ht_iter = zend_hash_iterator_add(ht, ht->nInternalPointer);
115    zend_hash_internal_pointer_reset_ex(ht, &EG(ht_iterators)[intern->ht_iter].pos);
116    spl_array_skip_protected(intern, ht);
117}
118/* }}} */
119
120static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
121{
122    if (UNEXPECTED(intern->ht_iter == (uint32_t)-1)) {
123        spl_array_create_ht_iter(ht, intern);
124    }
125    return &EG(ht_iterators)[intern->ht_iter].pos;
126}
127/* }}} */
128
129/* {{{ spl_array_object_free_storage */
130static void spl_array_object_free_storage(zend_object *object)
131{
132    spl_array_object *intern = spl_array_from_obj(object);
133
134    zend_object_std_dtor(&intern->std);
135
136    zval_ptr_dtor(&intern->array);
137}
138/* }}} */
139
140zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
141
142/* {{{ spl_array_object_new_ex */
143static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig)
144{
145    spl_array_object *intern;
146    zend_class_entry *parent = class_type;
147    int inherited = 0;
148
149    intern = ecalloc(1, sizeof(spl_array_object) + zend_object_properties_size(parent));
150
151    zend_object_std_init(&intern->std, class_type);
152    object_properties_init(&intern->std, class_type);
153
154    intern->ar_flags = 0;
155    intern->ce_get_iterator = spl_ce_ArrayIterator;
156    if (orig) {
157        spl_array_object *other = Z_SPLARRAY_P(orig);
158
159        intern->ar_flags &= ~ SPL_ARRAY_CLONE_MASK;
160        intern->ar_flags |= (other->ar_flags & SPL_ARRAY_CLONE_MASK);
161        intern->ce_get_iterator = other->ce_get_iterator;
162        if (clone_orig) {
163            if (other->ar_flags & SPL_ARRAY_IS_SELF) {
164                ZVAL_UNDEF(&intern->array);
165            } else if (Z_OBJ_HT_P(orig) == &spl_handler_ArrayObject) {
166                ZVAL_ARR(&intern->array, zend_array_dup(HASH_OF(&other->array)));
167            } else {
168                ZEND_ASSERT(Z_OBJ_HT_P(orig) == &spl_handler_ArrayIterator);
169                ZVAL_COPY(&intern->array, &other->array);
170            }
171        } else {
172            ZVAL_COPY(&intern->array, orig);
173            intern->ar_flags |= SPL_ARRAY_USE_OTHER;
174        }
175    } else {
176        array_init(&intern->array);
177    }
178
179    while (parent) {
180        if (parent == spl_ce_ArrayIterator || parent == spl_ce_RecursiveArrayIterator) {
181            intern->std.handlers = &spl_handler_ArrayIterator;
182            class_type->get_iterator = spl_array_get_iterator;
183            break;
184        } else if (parent == spl_ce_ArrayObject) {
185            intern->std.handlers = &spl_handler_ArrayObject;
186            break;
187        }
188        parent = parent->parent;
189        inherited = 1;
190    }
191    if (!parent) { /* this must never happen */
192        php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of ArrayObject or ArrayIterator");
193    }
194    if (inherited) {
195        intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
196        if (intern->fptr_offset_get->common.scope == parent) {
197            intern->fptr_offset_get = NULL;
198        }
199        intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
200        if (intern->fptr_offset_set->common.scope == parent) {
201            intern->fptr_offset_set = NULL;
202        }
203        intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
204        if (intern->fptr_offset_has->common.scope == parent) {
205            intern->fptr_offset_has = NULL;
206        }
207        intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset",  sizeof("offsetunset") - 1);
208        if (intern->fptr_offset_del->common.scope == parent) {
209            intern->fptr_offset_del = NULL;
210        }
211        intern->fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
212        if (intern->fptr_count->common.scope == parent) {
213            intern->fptr_count = NULL;
214        }
215    }
216    /* Cache iterator functions if ArrayIterator or derived. Check current's */
217    /* cache since only current is always required */
218    if (intern->std.handlers == &spl_handler_ArrayIterator) {
219        if (!class_type->iterator_funcs.zf_current) {
220            class_type->iterator_funcs.zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
221            class_type->iterator_funcs.zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
222            class_type->iterator_funcs.zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
223            class_type->iterator_funcs.zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
224            class_type->iterator_funcs.zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
225        }
226        if (inherited) {
227            if (class_type->iterator_funcs.zf_rewind->common.scope  != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_REWIND;
228            if (class_type->iterator_funcs.zf_valid->common.scope   != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_VALID;
229            if (class_type->iterator_funcs.zf_key->common.scope     != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_KEY;
230            if (class_type->iterator_funcs.zf_current->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_CURRENT;
231            if (class_type->iterator_funcs.zf_next->common.scope    != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_NEXT;
232        }
233    }
234
235    intern->ht_iter = (uint32_t)-1;
236    return &intern->std;
237}
238/* }}} */
239
240/* {{{ spl_array_object_new */
241static zend_object *spl_array_object_new(zend_class_entry *class_type)
242{
243    return spl_array_object_new_ex(class_type, NULL, 0);
244}
245/* }}} */
246
247/* {{{ spl_array_object_clone */
248static zend_object *spl_array_object_clone(zval *zobject)
249{
250    zend_object *old_object;
251    zend_object *new_object;
252
253    old_object = Z_OBJ_P(zobject);
254    new_object = spl_array_object_new_ex(old_object->ce, zobject, 1);
255
256    zend_objects_clone_members(new_object, old_object);
257
258    return new_object;
259}
260/* }}} */
261
262static zval *spl_array_get_dimension_ptr(int check_inherited, zval *object, zval *offset, int type) /* {{{ */
263{
264    zval *retval;
265    zend_long index;
266    zend_string *offset_key;
267    spl_array_object *intern = Z_SPLARRAY_P(object);
268    HashTable *ht = spl_array_get_hash_table(intern, 0);
269
270    if (!offset || Z_ISUNDEF_P(offset)) {
271        return &EG(uninitialized_zval);
272    }
273
274    if ((type == BP_VAR_W || type == BP_VAR_RW) && (ht->u.v.nApplyCount > 0)) {
275        zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
276        return &EG(error_zval);;
277    }
278
279    switch (Z_TYPE_P(offset)) {
280    case IS_NULL:
281       offset_key = ZSTR_EMPTY_ALLOC();
282       goto fetch_dim_string;
283    case IS_STRING:
284       offset_key = Z_STR_P(offset);
285fetch_dim_string:
286        retval = zend_symtable_find(ht, offset_key);
287        if (retval) {
288            if (Z_TYPE_P(retval) == IS_INDIRECT) {
289                retval = Z_INDIRECT_P(retval);
290                if (Z_TYPE_P(retval) == IS_UNDEF) {
291                    switch (type) {
292                        case BP_VAR_R:
293                            zend_error(E_NOTICE, "Undefined index: %s", ZSTR_VAL(offset_key));
294                        case BP_VAR_UNSET:
295                        case BP_VAR_IS:
296                            retval = &EG(uninitialized_zval);
297                            break;
298                        case BP_VAR_RW:
299                            zend_error(E_NOTICE,"Undefined index: %s", ZSTR_VAL(offset_key));
300                        case BP_VAR_W: {
301                            ZVAL_NULL(retval);
302                        }
303                    }
304                }
305            }
306        } else {
307            switch (type) {
308                case BP_VAR_R:
309                    zend_error(E_NOTICE, "Undefined index: %s", ZSTR_VAL(offset_key));
310                case BP_VAR_UNSET:
311                case BP_VAR_IS:
312                    retval = &EG(uninitialized_zval);
313                    break;
314                case BP_VAR_RW:
315                    zend_error(E_NOTICE,"Undefined index: %s", ZSTR_VAL(offset_key));
316                case BP_VAR_W: {
317                    zval value;
318                    ZVAL_NULL(&value);
319                    retval = zend_symtable_update(ht, offset_key, &value);
320                }
321            }
322        }
323        return retval;
324    case IS_RESOURCE:
325        zend_error(E_NOTICE, "Resource ID#%pd used as offset, casting to integer (%pd)", Z_RES_P(offset)->handle, Z_RES_P(offset)->handle);
326        index = Z_RES_P(offset)->handle;
327        goto num_index;
328    case IS_DOUBLE:
329        index = (zend_long)Z_DVAL_P(offset);
330        goto num_index;
331    case IS_FALSE:
332        index = 0;
333        goto num_index;
334    case IS_TRUE:
335        index = 1;
336        goto num_index;
337    case IS_LONG:
338        index = Z_LVAL_P(offset);
339num_index:
340        if ((retval = zend_hash_index_find(ht, index)) == NULL) {
341            switch (type) {
342                case BP_VAR_R:
343                    zend_error(E_NOTICE, "Undefined offset: %pd", index);
344                case BP_VAR_UNSET:
345                case BP_VAR_IS:
346                    retval = &EG(uninitialized_zval);
347                    break;
348                case BP_VAR_RW:
349                    zend_error(E_NOTICE, "Undefined offset: %pd", index);
350                case BP_VAR_W: {
351                    zval value;
352                    ZVAL_UNDEF(&value);
353                    retval = zend_hash_index_update(ht, index, &value);
354               }
355            }
356        }
357        return retval;
358    default:
359        zend_error(E_WARNING, "Illegal offset type");
360        return (type == BP_VAR_W || type == BP_VAR_RW) ?
361            &EG(error_zval) : &EG(uninitialized_zval);
362    }
363} /* }}} */
364
365static zval *spl_array_read_dimension_ex(int check_inherited, zval *object, zval *offset, int type, zval *rv) /* {{{ */
366{
367    zval *ret;
368
369    if (check_inherited) {
370        spl_array_object *intern = Z_SPLARRAY_P(object);
371        if (intern->fptr_offset_get) {
372            zval tmp;
373            if (!offset) {
374                ZVAL_UNDEF(&tmp);
375                offset = &tmp;
376            } else {
377                SEPARATE_ARG_IF_REF(offset);
378            }
379            zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_get, "offsetGet", rv, offset);
380            zval_ptr_dtor(offset);
381
382            if (!Z_ISUNDEF_P(rv)) {
383                return rv;
384            }
385            return &EG(uninitialized_zval);
386        }
387    }
388    ret = spl_array_get_dimension_ptr(check_inherited, object, offset, type);
389    //!!! FIXME?
390    //  ZVAL_COPY(result, ret);
391
392    /* When in a write context,
393     * ZE has to be fooled into thinking this is in a reference set
394     * by separating (if necessary) and returning as an is_ref=1 zval (even if refcount == 1)
395     */
396
397    if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) &&
398        !Z_ISREF_P(ret) &&
399        EXPECTED(ret != &EG(uninitialized_zval))) {
400        ZVAL_NEW_REF(ret, ret);
401    }
402
403    return ret;
404} /* }}} */
405
406static zval *spl_array_read_dimension(zval *object, zval *offset, int type, zval *rv) /* {{{ */
407{
408    return spl_array_read_dimension_ex(1, object, offset, type, rv);
409} /* }}} */
410
411static void spl_array_write_dimension_ex(int check_inherited, zval *object, zval *offset, zval *value) /* {{{ */
412{
413    spl_array_object *intern = Z_SPLARRAY_P(object);
414    zend_long index;
415    HashTable *ht;
416
417    if (check_inherited && intern->fptr_offset_set) {
418        zval tmp;
419
420        if (!offset) {
421            ZVAL_NULL(&tmp);
422            offset = &tmp;
423        } else {
424            SEPARATE_ARG_IF_REF(offset);
425        }
426        zend_call_method_with_2_params(object, Z_OBJCE_P(object), &intern->fptr_offset_set, "offsetSet", NULL, offset, value);
427        zval_ptr_dtor(offset);
428        return;
429    }
430
431    if (!offset) {
432        ht = spl_array_get_hash_table(intern, 0);
433        if (ht->u.v.nApplyCount > 0) {
434            zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
435            return;
436        }
437        if (Z_REFCOUNTED_P(value)) {
438            Z_ADDREF_P(value);
439        }
440        zend_hash_next_index_insert(ht, value);
441        return;
442    }
443
444    if (Z_REFCOUNTED_P(value)) {
445        Z_ADDREF_P(value);
446    }
447    switch (Z_TYPE_P(offset)) {
448        case IS_STRING:
449            ht = spl_array_get_hash_table(intern, 0);
450            if (ht->u.v.nApplyCount > 0) {
451                zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
452                return;
453            }
454            zend_symtable_update_ind(ht, Z_STR_P(offset), value);
455            return;
456        case IS_DOUBLE:
457            index = (zend_long)Z_DVAL_P(offset);
458            goto num_index;
459        case IS_RESOURCE:
460            index = Z_RES_HANDLE_P(offset);
461            goto num_index;
462        case IS_FALSE:
463            index = 0;
464            goto num_index;
465        case IS_TRUE:
466            index = 1;
467            goto num_index;
468        case IS_LONG:
469            index = Z_LVAL_P(offset);
470num_index:
471            ht = spl_array_get_hash_table(intern, 0);
472            if (ht->u.v.nApplyCount > 0) {
473                zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
474                return;
475            }
476            zend_hash_index_update(ht, index, value);
477            return;
478        case IS_NULL:
479            ht = spl_array_get_hash_table(intern, 0);
480            if (ht->u.v.nApplyCount > 0) {
481                zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
482                return;
483            }
484            zend_hash_next_index_insert(ht, value);
485            return;
486        default:
487            zend_error(E_WARNING, "Illegal offset type");
488            return;
489    }
490} /* }}} */
491
492static void spl_array_write_dimension(zval *object, zval *offset, zval *value) /* {{{ */
493{
494    spl_array_write_dimension_ex(1, object, offset, value);
495} /* }}} */
496
497static void spl_array_unset_dimension_ex(int check_inherited, zval *object, zval *offset) /* {{{ */
498{
499    zend_long index;
500    HashTable *ht;
501    spl_array_object *intern = Z_SPLARRAY_P(object);
502
503    if (check_inherited && intern->fptr_offset_del) {
504        SEPARATE_ARG_IF_REF(offset);
505        zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_del, "offsetUnset", NULL, offset);
506        zval_ptr_dtor(offset);
507        return;
508    }
509
510    switch(Z_TYPE_P(offset)) {
511    case IS_STRING:
512        ht = spl_array_get_hash_table(intern, 0);
513        if (ht->u.v.nApplyCount > 0) {
514            zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
515            return;
516        }
517        if (ht == &EG(symbol_table)) {
518            if (zend_delete_global_variable(Z_STR_P(offset))) {
519                zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
520            }
521        } else {
522            zval *data = zend_symtable_find(ht, Z_STR_P(offset));
523
524            if (data) {
525                if (Z_TYPE_P(data) == IS_INDIRECT) {
526                    data = Z_INDIRECT_P(data);
527                    if (Z_TYPE_P(data) == IS_UNDEF) {
528                        zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
529                    } else {
530                        zval_ptr_dtor(data);
531                        ZVAL_UNDEF(data);
532                        zend_hash_move_forward_ex(ht, spl_array_get_pos_ptr(ht, intern));
533                        if (spl_array_is_object(intern)) {
534                            spl_array_skip_protected(intern, ht);
535                        }
536                    }
537                } else if (zend_symtable_del(ht, Z_STR_P(offset)) == FAILURE) {
538                    zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
539                }
540            } else {
541                zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
542            }
543        }
544        break;
545    case IS_DOUBLE:
546        index = (zend_long)Z_DVAL_P(offset);
547        goto num_index;
548    case IS_RESOURCE:
549        index = Z_RES_HANDLE_P(offset);
550        goto num_index;
551    case IS_FALSE:
552        index = 0;
553        goto num_index;
554    case IS_TRUE:
555        index = 1;
556        goto num_index;
557    case IS_LONG:
558        index = Z_LVAL_P(offset);
559num_index:
560        ht = spl_array_get_hash_table(intern, 0);
561        if (ht->u.v.nApplyCount > 0) {
562            zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
563            return;
564        }
565        if (zend_hash_index_del(ht, index) == FAILURE) {
566            zend_error(E_NOTICE,"Undefined offset: %pd", index);
567        }
568        break;
569    default:
570        zend_error(E_WARNING, "Illegal offset type");
571        return;
572    }
573} /* }}} */
574
575static void spl_array_unset_dimension(zval *object, zval *offset) /* {{{ */
576{
577    spl_array_unset_dimension_ex(1, object, offset);
578} /* }}} */
579
580static int spl_array_has_dimension_ex(int check_inherited, zval *object, zval *offset, int check_empty) /* {{{ */
581{
582    spl_array_object *intern = Z_SPLARRAY_P(object);
583    zend_long index;
584    zval rv, *value = NULL, *tmp;
585
586    if (check_inherited && intern->fptr_offset_has) {
587//???       zval offset_tmp;
588//???       ZVAL_COPY_VALUE(&offset_tmp, offset);
589//???       SEPARATE_ARG_IF_REF(&offset_tmp);
590//???       zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_has, "offsetExists", &rv, &offset_tmp);
591//???       zval_ptr_dtor(&offset_tmp);
592        SEPARATE_ARG_IF_REF(offset);
593        zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_has, "offsetExists", &rv, offset);
594        zval_ptr_dtor(offset);
595
596        if (!Z_ISUNDEF(rv) && zend_is_true(&rv)) {
597            zval_ptr_dtor(&rv);
598            if (check_empty != 1) {
599                return 1;
600            } else if (intern->fptr_offset_get) {
601                value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
602            }
603        } else {
604            zval_ptr_dtor(&rv);
605            return 0;
606        }
607    }
608
609    if (!value) {
610        HashTable *ht = spl_array_get_hash_table(intern, 0);
611
612        switch(Z_TYPE_P(offset)) {
613            case IS_STRING:
614                if ((tmp = zend_symtable_find(ht, Z_STR_P(offset))) != NULL) {
615                    if (check_empty == 2) {
616                        return 1;
617                    }
618                } else {
619                    return 0;
620                }
621                break;
622
623            case IS_DOUBLE:
624                index = (zend_long)Z_DVAL_P(offset);
625                goto num_index;
626            case IS_RESOURCE:
627                index = Z_RES_HANDLE_P(offset);
628                goto num_index;
629            case IS_FALSE:
630                index = 0;
631                goto num_index;
632            case IS_TRUE:
633                index = 1;
634                goto num_index;
635            case IS_LONG:
636                index = Z_LVAL_P(offset);
637num_index:
638                if ((tmp = zend_hash_index_find(ht, index)) != NULL) {
639                    if (check_empty == 2) {
640                        return 1;
641                    }
642                } else {
643                    return 0;
644                }
645                break;
646
647            default:
648                zend_error(E_WARNING, "Illegal offset type");
649                return 0;
650        }
651
652        if (check_empty && check_inherited && intern->fptr_offset_get) {
653            value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
654        } else {
655            value = tmp;
656        }
657    }
658
659    {
660        zend_bool result = check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
661        if (value == &rv) {
662            zval_ptr_dtor(&rv);
663        }
664        return result;
665    }
666} /* }}} */
667
668static int spl_array_has_dimension(zval *object, zval *offset, int check_empty) /* {{{ */
669{
670    return spl_array_has_dimension_ex(1, object, offset, check_empty);
671} /* }}} */
672
673/* {{{ spl_array_object_verify_pos_ex */
674static inline int spl_array_object_verify_pos_ex(spl_array_object *object, HashTable *ht, const char *msg_prefix)
675{
676    if (!ht) {
677        php_error_docref(NULL, E_NOTICE, "%sArray was modified outside object and is no longer an array", msg_prefix);
678        return FAILURE;
679    }
680
681    return SUCCESS;
682} /* }}} */
683
684/* {{{ spl_array_object_verify_pos */
685static inline int spl_array_object_verify_pos(spl_array_object *object, HashTable *ht)
686{
687    return spl_array_object_verify_pos_ex(object, ht, "");
688} /* }}} */
689
690/* {{{ proto bool ArrayObject::offsetExists(mixed $index)
691       proto bool ArrayIterator::offsetExists(mixed $index)
692   Returns whether the requested $index exists. */
693SPL_METHOD(Array, offsetExists)
694{
695    zval *index;
696    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
697        return;
698    }
699    RETURN_BOOL(spl_array_has_dimension_ex(0, getThis(), index, 2));
700} /* }}} */
701
702/* {{{ proto mixed ArrayObject::offsetGet(mixed $index)
703       proto mixed ArrayIterator::offsetGet(mixed $index)
704   Returns the value at the specified $index. */
705SPL_METHOD(Array, offsetGet)
706{
707    zval *value, *index;
708    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
709        return;
710    }
711    value = spl_array_read_dimension_ex(0, getThis(), index, BP_VAR_R, return_value);
712    if (value != return_value) {
713        ZVAL_DEREF(value);
714        ZVAL_COPY(return_value, value);
715    }
716} /* }}} */
717
718/* {{{ proto void ArrayObject::offsetSet(mixed $index, mixed $newval)
719       proto void ArrayIterator::offsetSet(mixed $index, mixed $newval)
720   Sets the value at the specified $index to $newval. */
721SPL_METHOD(Array, offsetSet)
722{
723    zval *index, *value;
724    if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
725        return;
726    }
727    spl_array_write_dimension_ex(0, getThis(), index, value);
728} /* }}} */
729
730void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
731{
732    spl_array_object *intern = Z_SPLARRAY_P(object);
733    HashTable *aht = spl_array_get_hash_table(intern, 0);
734
735    if (!aht) {
736        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
737        return;
738    }
739
740    if (spl_array_is_object(intern)) {
741        php_error_docref(NULL, E_RECOVERABLE_ERROR, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
742        return;
743    }
744
745    spl_array_write_dimension(object, NULL, append_value);
746} /* }}} */
747
748/* {{{ proto void ArrayObject::append(mixed $newval)
749       proto void ArrayIterator::append(mixed $newval)
750   Appends the value (cannot be called for objects). */
751SPL_METHOD(Array, append)
752{
753    zval *value;
754
755    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
756        return;
757    }
758    spl_array_iterator_append(getThis(), value);
759} /* }}} */
760
761/* {{{ proto void ArrayObject::offsetUnset(mixed $index)
762       proto void ArrayIterator::offsetUnset(mixed $index)
763   Unsets the value at the specified $index. */
764SPL_METHOD(Array, offsetUnset)
765{
766    zval *index;
767    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
768        return;
769    }
770    spl_array_unset_dimension_ex(0, getThis(), index);
771} /* }}} */
772
773/* {{{ proto array ArrayObject::getArrayCopy()
774      proto array ArrayIterator::getArrayCopy()
775   Return a copy of the contained array */
776SPL_METHOD(Array, getArrayCopy)
777{
778    zval *object = getThis();
779    spl_array_object *intern = Z_SPLARRAY_P(object);
780
781    RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern, 0)));
782} /* }}} */
783
784static HashTable *spl_array_get_properties(zval *object) /* {{{ */
785{
786    spl_array_object *intern = Z_SPLARRAY_P(object);
787    HashTable *result;
788
789    if (intern->nApplyCount > 1) {
790        php_error_docref(NULL, E_ERROR, "Nesting level too deep - recursive dependency?");
791    }
792
793    intern->nApplyCount++;
794    result = spl_array_get_hash_table(intern, 1);
795    intern->nApplyCount--;
796    return result;
797} /* }}} */
798
799static HashTable* spl_array_get_debug_info(zval *obj, int *is_temp) /* {{{ */
800{
801    zval *storage;
802    zend_string *zname;
803    zend_class_entry *base;
804    spl_array_object *intern = Z_SPLARRAY_P(obj);
805
806    if (!intern->std.properties) {
807        rebuild_object_properties(&intern->std);
808    }
809
810    if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
811        *is_temp = 0;
812        return intern->std.properties;
813    } else {
814        HashTable *debug_info;
815        *is_temp = 1;
816
817        ALLOC_HASHTABLE(debug_info);
818        ZEND_INIT_SYMTABLE_EX(debug_info, zend_hash_num_elements(intern->std.properties) + 1, 0);
819        zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
820
821        storage = &intern->array;
822        Z_TRY_ADDREF_P(storage);
823
824        base = Z_OBJ_HT_P(obj) == &spl_handler_ArrayIterator
825            ? spl_ce_ArrayIterator : spl_ce_ArrayObject;
826        zname = spl_gen_private_prop_name(base, "storage", sizeof("storage")-1);
827        zend_symtable_update(debug_info, zname, storage);
828        zend_string_release(zname);
829
830        return debug_info;
831    }
832}
833/* }}} */
834
835static HashTable *spl_array_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
836{
837    spl_array_object *intern = Z_SPLARRAY_P(obj);
838    *gc_data = &intern->array;
839    *gc_data_count = 1;
840    return zend_std_get_properties(obj);
841}
842/* }}} */
843
844static zval *spl_array_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
845{
846    spl_array_object *intern = Z_SPLARRAY_P(object);
847
848    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
849        && !std_object_handlers.has_property(object, member, 2, NULL)) {
850        return spl_array_read_dimension(object, member, type, rv);
851    }
852    return std_object_handlers.read_property(object, member, type, cache_slot, rv);
853} /* }}} */
854
855static void spl_array_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
856{
857    spl_array_object *intern = Z_SPLARRAY_P(object);
858
859    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
860    && !std_object_handlers.has_property(object, member, 2, NULL)) {
861        spl_array_write_dimension(object, member, value);
862        return;
863    }
864    std_object_handlers.write_property(object, member, value, cache_slot);
865} /* }}} */
866
867static zval *spl_array_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
868{
869    spl_array_object *intern = Z_SPLARRAY_P(object);
870
871    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
872        && !std_object_handlers.has_property(object, member, 2, NULL)) {
873        return spl_array_get_dimension_ptr(1, object, member, type);
874    }
875    //!!! FIXME
876    //return std_object_handlers.get_property_ptr_ptr(object, member, type, key);
877    return NULL;
878} /* }}} */
879
880static int spl_array_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
881{
882    spl_array_object *intern = Z_SPLARRAY_P(object);
883
884    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
885        && !std_object_handlers.has_property(object, member, 2, NULL)) {
886        return spl_array_has_dimension(object, member, has_set_exists);
887    }
888    return std_object_handlers.has_property(object, member, has_set_exists, cache_slot);
889} /* }}} */
890
891static void spl_array_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
892{
893    spl_array_object *intern = Z_SPLARRAY_P(object);
894
895    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
896        && !std_object_handlers.has_property(object, member, 2, NULL)) {
897        spl_array_unset_dimension(object, member);
898        return;
899    }
900    std_object_handlers.unset_property(object, member, cache_slot);
901} /* }}} */
902
903static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
904{
905    HashTable           *ht1,
906                        *ht2;
907    spl_array_object    *intern1,
908                        *intern2;
909    int                 result  = 0;
910
911    intern1 = Z_SPLARRAY_P(o1);
912    intern2 = Z_SPLARRAY_P(o2);
913    ht1     = spl_array_get_hash_table(intern1, 0);
914    ht2     = spl_array_get_hash_table(intern2, 0);
915
916    result = zend_compare_symbol_tables(ht1, ht2);
917    /* if we just compared std.properties, don't do it again */
918    if (result == 0 &&
919            !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
920        result = std_object_handlers.compare_objects(o1, o2);
921    }
922    return result;
923} /* }}} */
924
925static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
926{
927    zend_string *string_key;
928    zend_ulong num_key;
929    zval *data;
930
931    if (spl_array_is_object(intern)) {
932        uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
933
934        do {
935            if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
936                data = zend_hash_get_current_data_ex(aht, pos_ptr);
937                if (data && Z_TYPE_P(data) == IS_INDIRECT &&
938                    Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
939                    /* skip */
940                } else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
941                    return SUCCESS;
942                }
943            } else {
944                return SUCCESS;
945            }
946            if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
947                return FAILURE;
948            }
949            zend_hash_move_forward_ex(aht, pos_ptr);
950        } while (1);
951    }
952    return FAILURE;
953} /* }}} */
954
955static int spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
956{
957    uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
958
959    zend_hash_move_forward_ex(aht, pos_ptr);
960    if (spl_array_is_object(intern)) {
961        return spl_array_skip_protected(intern, aht);
962    } else {
963        return zend_hash_has_more_elements_ex(aht, pos_ptr);
964    }
965} /* }}} */
966
967static int spl_array_next(spl_array_object *intern) /* {{{ */
968{
969    HashTable *aht = spl_array_get_hash_table(intern, 0);
970
971    return spl_array_next_ex(intern, aht);
972
973} /* }}} */
974
975static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
976{
977    zend_user_it_invalidate_current(iter);
978    zval_ptr_dtor(&iter->data);
979}
980/* }}} */
981
982static int spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
983{
984    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
985    HashTable *aht = spl_array_get_hash_table(object, 0);
986
987    if (object->ar_flags & SPL_ARRAY_OVERLOADED_VALID) {
988        return zend_user_it_valid(iter);
989    } else {
990        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::valid(): ") == FAILURE) {
991            return FAILURE;
992        }
993
994        return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
995    }
996}
997/* }}} */
998
999static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1000{
1001    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1002    HashTable *aht = spl_array_get_hash_table(object, 0);
1003
1004    if (object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT) {
1005        return zend_user_it_get_current_data(iter);
1006    } else {
1007        zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1008        if (Z_TYPE_P(data) == IS_INDIRECT) {
1009            data = Z_INDIRECT_P(data);
1010        }
1011        return data;
1012    }
1013}
1014/* }}} */
1015
1016static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1017{
1018    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1019    HashTable *aht = spl_array_get_hash_table(object, 0);
1020
1021    if (object->ar_flags & SPL_ARRAY_OVERLOADED_KEY) {
1022        zend_user_it_get_current_key(iter, key);
1023    } else {
1024        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::current(): ") == FAILURE) {
1025            ZVAL_NULL(key);
1026        } else {
1027            zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1028        }
1029    }
1030}
1031/* }}} */
1032
1033static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1034{
1035    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1036    HashTable *aht = spl_array_get_hash_table(object, 0);
1037
1038    if (object->ar_flags & SPL_ARRAY_OVERLOADED_NEXT) {
1039        zend_user_it_move_forward(iter);
1040    } else {
1041        zend_user_it_invalidate_current(iter);
1042        if (!aht) {
1043            php_error_docref(NULL, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array");
1044            return;
1045        }
1046
1047        spl_array_next_ex(object, aht);
1048    }
1049}
1050/* }}} */
1051
1052static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1053{
1054    HashTable *aht = spl_array_get_hash_table(intern, 0);
1055
1056    if (!aht) {
1057        php_error_docref(NULL, E_NOTICE, "ArrayIterator::rewind(): Array was modified outside object and is no longer an array");
1058        return;
1059    }
1060
1061    if (intern->ht_iter == (uint32_t)-1) {
1062        spl_array_get_pos_ptr(aht, intern);
1063    } else {
1064        zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1065        spl_array_skip_protected(intern, aht);
1066    }
1067}
1068/* }}} */
1069
1070static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1071{
1072    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1073
1074    if (object->ar_flags & SPL_ARRAY_OVERLOADED_REWIND) {
1075        zend_user_it_rewind(iter);
1076    } else {
1077        zend_user_it_invalidate_current(iter);
1078        spl_array_rewind(object);
1079    }
1080}
1081/* }}} */
1082
1083/* {{{ spl_array_set_array */
1084static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
1085    if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
1086        zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object, using empty array instead", 0);
1087        return;
1088    }
1089
1090    zval_ptr_dtor(&intern->array);
1091
1092    if (Z_TYPE_P(array) == IS_ARRAY) {
1093        //??? TODO: try to avoid array duplication
1094        ZVAL_DUP(&intern->array, array);
1095    } else {
1096        if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject || Z_OBJ_HT_P(array) == &spl_handler_ArrayIterator) {
1097            if (just_array) {
1098                spl_array_object *other = Z_SPLARRAY_P(array);
1099                ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
1100            }
1101            if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
1102                ar_flags |= SPL_ARRAY_IS_SELF;
1103                ZVAL_UNDEF(&intern->array);
1104            } else {
1105                ar_flags |= SPL_ARRAY_USE_OTHER;
1106                ZVAL_COPY(&intern->array, array);
1107            }
1108        } else {
1109            zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
1110            ZVAL_COPY(&intern->array, array);
1111            if (handler != std_object_handlers.get_properties
1112                || !spl_array_get_hash_table(intern, 0)) {
1113                zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Overloaded object of type %s is not compatible with %s", Z_OBJCE_P(array)->name, intern->std.ce->name);
1114            }
1115        }
1116    }
1117
1118    intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
1119    intern->ar_flags |= ar_flags;
1120    intern->ht_iter = (uint32_t)-1;
1121}
1122/* }}} */
1123
1124/* iterator handler table */
1125zend_object_iterator_funcs spl_array_it_funcs = {
1126    spl_array_it_dtor,
1127    spl_array_it_valid,
1128    spl_array_it_get_current_data,
1129    spl_array_it_get_current_key,
1130    spl_array_it_move_forward,
1131    spl_array_it_rewind
1132};
1133
1134zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1135{
1136    zend_user_iterator *iterator;
1137    spl_array_object *array_object = Z_SPLARRAY_P(object);
1138
1139    if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
1140        zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
1141    }
1142
1143    iterator = emalloc(sizeof(zend_user_iterator));
1144
1145    zend_iterator_init(&iterator->it);
1146
1147    ZVAL_COPY(&iterator->it.data, object);
1148    iterator->it.funcs = &spl_array_it_funcs;
1149    iterator->ce = ce;
1150    ZVAL_UNDEF(&iterator->value);
1151
1152    return &iterator->it;
1153}
1154/* }}} */
1155
1156/* {{{ proto void ArrayObject::__construct(array|object ar = array() [, int flags = 0 [, string iterator_class = "ArrayIterator"]])
1157       proto void ArrayIterator::__construct(array|object ar = array() [, int flags = 0])
1158   Constructs a new array iterator from a path. */
1159SPL_METHOD(Array, __construct)
1160{
1161    zval *object = getThis();
1162    spl_array_object *intern;
1163    zval *array;
1164    zend_long ar_flags = 0;
1165    zend_class_entry *ce_get_iterator = spl_ce_Iterator;
1166
1167    if (ZEND_NUM_ARGS() == 0) {
1168        return; /* nothing to do */
1169    }
1170
1171    if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|lC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1172        return;
1173    }
1174
1175    intern = Z_SPLARRAY_P(object);
1176
1177    if (ZEND_NUM_ARGS() > 2) {
1178        intern->ce_get_iterator = ce_get_iterator;
1179    }
1180
1181    ar_flags &= ~SPL_ARRAY_INT_MASK;
1182
1183    spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1184}
1185 /* }}} */
1186
1187/* {{{ proto void ArrayObject::setIteratorClass(string iterator_class)
1188   Set the class used in getIterator. */
1189SPL_METHOD(Array, setIteratorClass)
1190{
1191    zval *object = getThis();
1192    spl_array_object *intern = Z_SPLARRAY_P(object);
1193    zend_class_entry * ce_get_iterator = spl_ce_Iterator;
1194
1195#ifndef FAST_ZPP
1196    if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &ce_get_iterator) == FAILURE) {
1197        return;
1198    }
1199#else
1200    ZEND_PARSE_PARAMETERS_START(1, 1)
1201        Z_PARAM_CLASS(ce_get_iterator)
1202    ZEND_PARSE_PARAMETERS_END();
1203#endif
1204
1205    intern->ce_get_iterator = ce_get_iterator;
1206}
1207/* }}} */
1208
1209/* {{{ proto string ArrayObject::getIteratorClass()
1210   Get the class used in getIterator. */
1211SPL_METHOD(Array, getIteratorClass)
1212{
1213    zval *object = getThis();
1214    spl_array_object *intern = Z_SPLARRAY_P(object);
1215
1216    if (zend_parse_parameters_none() == FAILURE) {
1217        return;
1218    }
1219
1220    zend_string_addref(intern->ce_get_iterator->name);
1221    RETURN_STR(intern->ce_get_iterator->name);
1222}
1223/* }}} */
1224
1225/* {{{ proto int ArrayObject::getFlags()
1226   Get flags */
1227SPL_METHOD(Array, getFlags)
1228{
1229    zval *object = getThis();
1230    spl_array_object *intern = Z_SPLARRAY_P(object);
1231
1232    if (zend_parse_parameters_none() == FAILURE) {
1233        return;
1234    }
1235
1236    RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1237}
1238/* }}} */
1239
1240/* {{{ proto void ArrayObject::setFlags(int flags)
1241   Set flags */
1242SPL_METHOD(Array, setFlags)
1243{
1244    zval *object = getThis();
1245    spl_array_object *intern = Z_SPLARRAY_P(object);
1246    zend_long ar_flags = 0;
1247
1248    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1249        return;
1250    }
1251
1252    intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1253}
1254/* }}} */
1255
1256/* {{{ proto Array|Object ArrayObject::exchangeArray(Array|Object ar = array())
1257   Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
1258SPL_METHOD(Array, exchangeArray)
1259{
1260    zval *object = getThis(), *array;
1261    spl_array_object *intern = Z_SPLARRAY_P(object);
1262
1263    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &array) == FAILURE) {
1264        return;
1265    }
1266
1267    RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern, 0)));
1268    spl_array_set_array(object, intern, array, 0L, 1);
1269}
1270/* }}} */
1271
1272/* {{{ proto ArrayIterator ArrayObject::getIterator()
1273   Create a new iterator from a ArrayObject instance */
1274SPL_METHOD(Array, getIterator)
1275{
1276    zval *object = getThis();
1277    spl_array_object *intern = Z_SPLARRAY_P(object);
1278    HashTable *aht = spl_array_get_hash_table(intern, 0);
1279
1280    if (zend_parse_parameters_none() == FAILURE) {
1281        return;
1282    }
1283
1284    if (!aht) {
1285        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1286        return;
1287    }
1288
1289    ZVAL_OBJ(return_value, spl_array_object_new_ex(intern->ce_get_iterator, object, 0));
1290}
1291/* }}} */
1292
1293/* {{{ proto void ArrayIterator::rewind()
1294   Rewind array back to the start */
1295SPL_METHOD(Array, rewind)
1296{
1297    zval *object = getThis();
1298    spl_array_object *intern = Z_SPLARRAY_P(object);
1299
1300    if (zend_parse_parameters_none() == FAILURE) {
1301        return;
1302    }
1303
1304    spl_array_rewind(intern);
1305}
1306/* }}} */
1307
1308/* {{{ proto void ArrayIterator::seek(int $position)
1309   Seek to position. */
1310SPL_METHOD(Array, seek)
1311{
1312    zend_long opos, position;
1313    zval *object = getThis();
1314    spl_array_object *intern = Z_SPLARRAY_P(object);
1315    HashTable *aht = spl_array_get_hash_table(intern, 0);
1316    int result;
1317
1318    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1319        return;
1320    }
1321
1322    if (!aht) {
1323        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1324        return;
1325    }
1326
1327    opos = position;
1328
1329    if (position >= 0) { /* negative values are not supported */
1330        spl_array_rewind(intern);
1331        result = SUCCESS;
1332
1333        while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1334
1335        if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1336            return; /* ok */
1337        }
1338    }
1339    zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position %pd is out of range", opos);
1340} /* }}} */
1341
1342int static spl_array_object_count_elements_helper(spl_array_object *intern, zend_long *count) /* {{{ */
1343{
1344    HashTable *aht = spl_array_get_hash_table(intern, 0);
1345    HashPosition pos, *pos_ptr;
1346
1347    if (!aht) {
1348        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1349        *count = 0;
1350        return FAILURE;
1351    }
1352
1353    if (spl_array_is_object(intern)) {
1354        /* We need to store the 'pos' since we'll modify it in the functions
1355         * we're going to call and which do not support 'pos' as parameter. */
1356        pos_ptr = spl_array_get_pos_ptr(aht, intern);
1357        pos = *pos_ptr;
1358        *count = 0;
1359        spl_array_rewind(intern);
1360        while (*pos_ptr != HT_INVALID_IDX && spl_array_next(intern) == SUCCESS) {
1361            (*count)++;
1362        }
1363        *pos_ptr = pos;
1364        return SUCCESS;
1365    } else {
1366        *count = zend_hash_num_elements(aht);
1367        return SUCCESS;
1368    }
1369} /* }}} */
1370
1371int spl_array_object_count_elements(zval *object, zend_long *count) /* {{{ */
1372{
1373    spl_array_object *intern = Z_SPLARRAY_P(object);
1374
1375    if (intern->fptr_count) {
1376        zval rv;
1377        zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1378        if (Z_TYPE(rv) != IS_UNDEF) {
1379            *count = zval_get_long(&rv);
1380            zval_ptr_dtor(&rv);
1381            return SUCCESS;
1382        }
1383        *count = 0;
1384        return FAILURE;
1385    }
1386    return spl_array_object_count_elements_helper(intern, count);
1387} /* }}} */
1388
1389/* {{{ proto int ArrayObject::count()
1390       proto int ArrayIterator::count()
1391   Return the number of elements in the Iterator. */
1392SPL_METHOD(Array, count)
1393{
1394    zend_long count;
1395    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1396
1397    if (zend_parse_parameters_none() == FAILURE) {
1398        return;
1399    }
1400
1401    spl_array_object_count_elements_helper(intern, &count);
1402
1403    RETURN_LONG(count);
1404} /* }}} */
1405
1406static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) /* {{{ */
1407{
1408    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1409    HashTable *aht = spl_array_get_hash_table(intern, 0);
1410    zval function_name, params[2], *arg = NULL;
1411    uint32_t old_refcount;
1412
1413    ZVAL_STRINGL(&function_name, fname, fname_len);
1414
1415    /* A tricky way to pass "aht" by reference, reset refcount */
1416    //??? It may be not safe, if user comparison handler accesses "aht"
1417    old_refcount = GC_REFCOUNT(aht);
1418    GC_REFCOUNT(aht) = 1;
1419    ZVAL_NEW_EMPTY_REF(&params[0]);
1420    ZVAL_ARR(Z_REFVAL(params[0]), aht);
1421
1422    if (!use_arg) {
1423        aht->u.v.nApplyCount++;
1424        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, 1, params, 1, NULL);
1425        aht->u.v.nApplyCount--;
1426    } else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
1427        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
1428            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
1429            goto exit;
1430        }
1431        if (arg) {
1432            ZVAL_COPY_VALUE(&params[1], arg);
1433        }
1434        aht->u.v.nApplyCount++;
1435        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, arg ? 2 : 1, params, 1, NULL);
1436        aht->u.v.nApplyCount--;
1437    } else {
1438        if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1439            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
1440            goto exit;
1441        }
1442        ZVAL_COPY_VALUE(&params[1], arg);
1443        aht->u.v.nApplyCount++;
1444        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, 2, params, 1, NULL);
1445        aht->u.v.nApplyCount--;
1446    }
1447
1448exit:
1449    /* A tricky way to pass "aht" by reference, copy back and cleanup */
1450    GC_REFCOUNT(aht) = old_refcount;
1451    efree(Z_REF(params[0]));
1452    zend_string_free(Z_STR(function_name));
1453} /* }}} */
1454
1455#define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1456SPL_METHOD(cname, fname) \
1457{ \
1458    spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1459}
1460
1461/* {{{ proto int ArrayObject::asort([int $sort_flags = SORT_REGULAR ])
1462       proto int ArrayIterator::asort([int $sort_flags = SORT_REGULAR ])
1463   Sort the entries by values. */
1464SPL_ARRAY_METHOD(Array, asort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1465
1466/* {{{ proto int ArrayObject::ksort([int $sort_flags = SORT_REGULAR ])
1467       proto int ArrayIterator::ksort([int $sort_flags = SORT_REGULAR ])
1468   Sort the entries by key. */
1469SPL_ARRAY_METHOD(Array, ksort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1470
1471/* {{{ proto int ArrayObject::uasort(callback cmp_function)
1472       proto int ArrayIterator::uasort(callback cmp_function)
1473   Sort the entries by values user defined function. */
1474SPL_ARRAY_METHOD(Array, uasort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1475
1476/* {{{ proto int ArrayObject::uksort(callback cmp_function)
1477       proto int ArrayIterator::uksort(callback cmp_function)
1478   Sort the entries by key using user defined function. */
1479SPL_ARRAY_METHOD(Array, uksort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1480
1481/* {{{ proto int ArrayObject::natsort()
1482       proto int ArrayIterator::natsort()
1483   Sort the entries by values using "natural order" algorithm. */
1484SPL_ARRAY_METHOD(Array, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1485
1486/* {{{ proto int ArrayObject::natcasesort()
1487       proto int ArrayIterator::natcasesort()
1488   Sort the entries by key using case insensitive "natural order" algorithm. */
1489SPL_ARRAY_METHOD(Array, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1490
1491/* {{{ proto mixed|NULL ArrayIterator::current()
1492   Return current array entry */
1493SPL_METHOD(Array, current)
1494{
1495    zval *object = getThis();
1496    spl_array_object *intern = Z_SPLARRAY_P(object);
1497    zval *entry;
1498    HashTable *aht = spl_array_get_hash_table(intern, 0);
1499
1500    if (zend_parse_parameters_none() == FAILURE) {
1501        return;
1502    }
1503
1504    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1505        return;
1506    }
1507
1508    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1509        return;
1510    }
1511    if (Z_TYPE_P(entry) == IS_INDIRECT) {
1512        entry = Z_INDIRECT_P(entry);
1513        if (Z_TYPE_P(entry) == IS_UNDEF) {
1514            return;
1515        }
1516    }
1517    ZVAL_DEREF(entry);
1518    ZVAL_COPY(return_value, entry);
1519}
1520/* }}} */
1521
1522/* {{{ proto mixed|NULL ArrayIterator::key()
1523   Return current array key */
1524SPL_METHOD(Array, key)
1525{
1526    if (zend_parse_parameters_none() == FAILURE) {
1527        return;
1528    }
1529
1530    spl_array_iterator_key(getThis(), return_value);
1531} /* }}} */
1532
1533void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1534{
1535    spl_array_object *intern = Z_SPLARRAY_P(object);
1536    HashTable *aht = spl_array_get_hash_table(intern, 0);
1537
1538    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1539        return;
1540    }
1541
1542    zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1543}
1544/* }}} */
1545
1546/* {{{ proto void ArrayIterator::next()
1547   Move to next entry */
1548SPL_METHOD(Array, next)
1549{
1550    zval *object = getThis();
1551    spl_array_object *intern = Z_SPLARRAY_P(object);
1552    HashTable *aht = spl_array_get_hash_table(intern, 0);
1553
1554    if (zend_parse_parameters_none() == FAILURE) {
1555        return;
1556    }
1557
1558    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1559        return;
1560    }
1561
1562    spl_array_next_ex(intern, aht);
1563}
1564/* }}} */
1565
1566/* {{{ proto bool ArrayIterator::valid()
1567   Check whether array contains more entries */
1568SPL_METHOD(Array, valid)
1569{
1570    zval *object = getThis();
1571    spl_array_object *intern = Z_SPLARRAY_P(object);
1572    HashTable *aht = spl_array_get_hash_table(intern, 0);
1573
1574    if (zend_parse_parameters_none() == FAILURE) {
1575        return;
1576    }
1577
1578    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1579        RETURN_FALSE;
1580    } else {
1581        RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1582    }
1583}
1584/* }}} */
1585
1586/* {{{ proto bool RecursiveArrayIterator::hasChildren()
1587   Check whether current element has children (e.g. is an array) */
1588SPL_METHOD(Array, hasChildren)
1589{
1590    zval *object = getThis(), *entry;
1591    spl_array_object *intern = Z_SPLARRAY_P(object);
1592    HashTable *aht = spl_array_get_hash_table(intern, 0);
1593
1594    if (zend_parse_parameters_none() == FAILURE) {
1595        return;
1596    }
1597
1598    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1599        RETURN_FALSE;
1600    }
1601
1602    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1603        RETURN_FALSE;
1604    }
1605
1606    RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1607}
1608/* }}} */
1609
1610/* {{{ proto object RecursiveArrayIterator::getChildren()
1611   Create a sub iterator for the current element (same class as $this) */
1612SPL_METHOD(Array, getChildren)
1613{
1614    zval *object = getThis(), *entry, flags;
1615    spl_array_object *intern = Z_SPLARRAY_P(object);
1616    HashTable *aht = spl_array_get_hash_table(intern, 0);
1617
1618    if (zend_parse_parameters_none() == FAILURE) {
1619        return;
1620    }
1621
1622    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1623        return;
1624    }
1625
1626    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1627        return;
1628    }
1629
1630    if (Z_TYPE_P(entry) == IS_OBJECT) {
1631        if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1632            return;
1633        }
1634        if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(getThis()))) {
1635            ZVAL_OBJ(return_value, Z_OBJ_P(entry));
1636            Z_ADDREF_P(return_value);
1637            return;
1638        }
1639    }
1640
1641    ZVAL_LONG(&flags, intern->ar_flags);
1642    spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), return_value, entry, &flags);
1643}
1644/* }}} */
1645
1646/* {{{ proto string ArrayObject::serialize()
1647   Serialize the object */
1648SPL_METHOD(Array, serialize)
1649{
1650    zval *object = getThis();
1651    spl_array_object *intern = Z_SPLARRAY_P(object);
1652    HashTable *aht = spl_array_get_hash_table(intern, 0);
1653    zval members, flags;
1654    php_serialize_data_t var_hash;
1655    smart_str buf = {0};
1656
1657    if (zend_parse_parameters_none() == FAILURE) {
1658        return;
1659    }
1660
1661    if (!aht) {
1662        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1663        return;
1664    }
1665
1666    PHP_VAR_SERIALIZE_INIT(var_hash);
1667
1668    ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1669
1670    /* storage */
1671    smart_str_appendl(&buf, "x:", 2);
1672    //!!! php_var_serialize need to be modified
1673    php_var_serialize(&buf, &flags, &var_hash);
1674
1675    if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1676        php_var_serialize(&buf, &intern->array, &var_hash);
1677        smart_str_appendc(&buf, ';');
1678    }
1679
1680    /* members */
1681    smart_str_appendl(&buf, "m:", 2);
1682    if (!intern->std.properties) {
1683        rebuild_object_properties(&intern->std);
1684    }
1685
1686    ZVAL_ARR(&members, zend_array_dup(intern->std.properties));
1687
1688    php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1689
1690    zval_ptr_dtor(&members);
1691
1692    /* done */
1693    PHP_VAR_SERIALIZE_DESTROY(var_hash);
1694
1695    if (buf.s) {
1696        RETURN_NEW_STR(buf.s);
1697    }
1698
1699    RETURN_NULL();
1700} /* }}} */
1701
1702/* {{{ proto void ArrayObject::unserialize(string serialized)
1703 * unserialize the object
1704 */
1705SPL_METHOD(Array, unserialize)
1706{
1707    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1708
1709    char *buf;
1710    size_t buf_len;
1711    const unsigned char *p, *s;
1712    php_unserialize_data_t var_hash;
1713    zval members, zflags;
1714    HashTable *aht;
1715    zend_long flags;
1716
1717    if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1718        return;
1719    }
1720
1721    if (buf_len == 0) {
1722        return;
1723    }
1724
1725    aht = spl_array_get_hash_table(intern, 0);
1726    if (aht->u.v.nApplyCount > 0) {
1727        zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
1728        return;
1729    }
1730
1731    /* storage */
1732    s = p = (const unsigned char*)buf;
1733    PHP_VAR_UNSERIALIZE_INIT(var_hash);
1734
1735    if (*p!= 'x' || *++p != ':') {
1736        goto outexcept;
1737    }
1738    ++p;
1739
1740    if (!php_var_unserialize(&zflags, &p, s + buf_len, &var_hash) || Z_TYPE(zflags) != IS_LONG) {
1741        goto outexcept;
1742    }
1743
1744    --p; /* for ';' */
1745    flags = Z_LVAL(zflags);
1746    /* flags needs to be verified and we also need to verify whether the next
1747     * thing we get is ';'. After that we require an 'm' or somethign else
1748     * where 'm' stands for members and anything else should be an array. If
1749     * neither 'a' or 'm' follows we have an error. */
1750
1751    if (*p != ';') {
1752        goto outexcept;
1753    }
1754    ++p;
1755
1756    if (*p!='m') {
1757        if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1758            goto outexcept;
1759        }
1760        intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1761        intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1762        zval_ptr_dtor(&intern->array);
1763        if (!php_var_unserialize(&intern->array, &p, s + buf_len, &var_hash)) {
1764            goto outexcept;
1765        }
1766    }
1767    if (*p != ';') {
1768        goto outexcept;
1769    }
1770    ++p;
1771
1772    /* members */
1773    if (*p!= 'm' || *++p != ':') {
1774        goto outexcept;
1775    }
1776    ++p;
1777
1778    ZVAL_UNDEF(&members);
1779    if (!php_var_unserialize(&members, &p, s + buf_len, &var_hash) || Z_TYPE(members) != IS_ARRAY) {
1780        zval_ptr_dtor(&members);
1781        goto outexcept;
1782    }
1783
1784    /* copy members */
1785    object_properties_load(&intern->std, Z_ARRVAL(members));
1786    zval_ptr_dtor(&members);
1787
1788    /* done reading $serialized */
1789
1790    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1791    return;
1792
1793outexcept:
1794    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1795    zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %pd of %d bytes", (zend_long)((char*)p - buf), buf_len);
1796    return;
1797
1798} /* }}} */
1799
1800/* {{{ arginfo and function tbale */
1801ZEND_BEGIN_ARG_INFO(arginfo_array___construct, 0)
1802    ZEND_ARG_INFO(0, array)
1803ZEND_END_ARG_INFO()
1804
1805ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetGet, 0, 0, 1)
1806    ZEND_ARG_INFO(0, index)
1807ZEND_END_ARG_INFO()
1808
1809ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetSet, 0, 0, 2)
1810    ZEND_ARG_INFO(0, index)
1811    ZEND_ARG_INFO(0, newval)
1812ZEND_END_ARG_INFO()
1813
1814ZEND_BEGIN_ARG_INFO(arginfo_array_append, 0)
1815    ZEND_ARG_INFO(0, value)
1816ZEND_END_ARG_INFO()
1817
1818ZEND_BEGIN_ARG_INFO(arginfo_array_seek, 0)
1819    ZEND_ARG_INFO(0, position)
1820ZEND_END_ARG_INFO()
1821
1822ZEND_BEGIN_ARG_INFO(arginfo_array_exchangeArray, 0)
1823    ZEND_ARG_INFO(0, array)
1824ZEND_END_ARG_INFO()
1825
1826ZEND_BEGIN_ARG_INFO(arginfo_array_setFlags, 0)
1827    ZEND_ARG_INFO(0, flags)
1828ZEND_END_ARG_INFO()
1829
1830ZEND_BEGIN_ARG_INFO(arginfo_array_setIteratorClass, 0)
1831    ZEND_ARG_INFO(0, iteratorClass)
1832ZEND_END_ARG_INFO()
1833
1834ZEND_BEGIN_ARG_INFO(arginfo_array_uXsort, 0)
1835    ZEND_ARG_INFO(0, cmp_function)
1836ZEND_END_ARG_INFO();
1837
1838ZEND_BEGIN_ARG_INFO(arginfo_array_unserialize, 0)
1839    ZEND_ARG_INFO(0, serialized)
1840ZEND_END_ARG_INFO();
1841
1842ZEND_BEGIN_ARG_INFO(arginfo_array_void, 0)
1843ZEND_END_ARG_INFO()
1844
1845static const zend_function_entry spl_funcs_ArrayObject[] = {
1846    SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
1847    SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1848    SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1849    SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
1850    SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1851    SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
1852    SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
1853    SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1854    SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
1855    SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
1856    SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1857    SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1858    SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1859    SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1860    SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1861    SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1862    SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1863    SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
1864    /* ArrayObject specific */
1865    SPL_ME(Array, getIterator,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1866    SPL_ME(Array, exchangeArray,    arginfo_array_exchangeArray,    ZEND_ACC_PUBLIC)
1867    SPL_ME(Array, setIteratorClass, arginfo_array_setIteratorClass, ZEND_ACC_PUBLIC)
1868    SPL_ME(Array, getIteratorClass, arginfo_array_void,             ZEND_ACC_PUBLIC)
1869    PHP_FE_END
1870};
1871
1872static const zend_function_entry spl_funcs_ArrayIterator[] = {
1873    SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
1874    SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1875    SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1876    SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
1877    SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1878    SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
1879    SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
1880    SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1881    SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
1882    SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
1883    SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1884    SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1885    SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1886    SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1887    SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1888    SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1889    SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1890    SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
1891    /* ArrayIterator specific */
1892    SPL_ME(Array, rewind,           arginfo_array_void,             ZEND_ACC_PUBLIC)
1893    SPL_ME(Array, current,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1894    SPL_ME(Array, key,              arginfo_array_void,             ZEND_ACC_PUBLIC)
1895    SPL_ME(Array, next,             arginfo_array_void,             ZEND_ACC_PUBLIC)
1896    SPL_ME(Array, valid,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1897    SPL_ME(Array, seek,             arginfo_array_seek,             ZEND_ACC_PUBLIC)
1898    PHP_FE_END
1899};
1900
1901static const zend_function_entry spl_funcs_RecursiveArrayIterator[] = {
1902    SPL_ME(Array, hasChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
1903    SPL_ME(Array, getChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
1904    PHP_FE_END
1905};
1906/* }}} */
1907
1908/* {{{ PHP_MINIT_FUNCTION(spl_array) */
1909PHP_MINIT_FUNCTION(spl_array)
1910{
1911    REGISTER_SPL_STD_CLASS_EX(ArrayObject, spl_array_object_new, spl_funcs_ArrayObject);
1912    REGISTER_SPL_IMPLEMENTS(ArrayObject, Aggregate);
1913    REGISTER_SPL_IMPLEMENTS(ArrayObject, ArrayAccess);
1914    REGISTER_SPL_IMPLEMENTS(ArrayObject, Serializable);
1915    REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable);
1916    memcpy(&spl_handler_ArrayObject, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1917
1918    spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1919
1920    spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1921    spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1922    spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1923    spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1924    spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1925    spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1926
1927    spl_handler_ArrayObject.get_properties = spl_array_get_properties;
1928    spl_handler_ArrayObject.get_debug_info = spl_array_get_debug_info;
1929    spl_handler_ArrayObject.get_gc = spl_array_get_gc;
1930    spl_handler_ArrayObject.read_property = spl_array_read_property;
1931    spl_handler_ArrayObject.write_property = spl_array_write_property;
1932    spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
1933    spl_handler_ArrayObject.has_property = spl_array_has_property;
1934    spl_handler_ArrayObject.unset_property = spl_array_unset_property;
1935
1936    spl_handler_ArrayObject.compare_objects = spl_array_compare_objects;
1937    spl_handler_ArrayObject.dtor_obj = zend_objects_destroy_object;
1938    spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
1939
1940    REGISTER_SPL_STD_CLASS_EX(ArrayIterator, spl_array_object_new, spl_funcs_ArrayIterator);
1941    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Iterator);
1942    REGISTER_SPL_IMPLEMENTS(ArrayIterator, ArrayAccess);
1943    REGISTER_SPL_IMPLEMENTS(ArrayIterator, SeekableIterator);
1944    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Serializable);
1945    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable);
1946    memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));
1947    spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
1948
1949    REGISTER_SPL_SUB_CLASS_EX(RecursiveArrayIterator, ArrayIterator, spl_array_object_new, spl_funcs_RecursiveArrayIterator);
1950    REGISTER_SPL_IMPLEMENTS(RecursiveArrayIterator, RecursiveIterator);
1951    spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
1952
1953    REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
1954    REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
1955
1956    REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
1957    REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
1958
1959    REGISTER_SPL_CLASS_CONST_LONG(RecursiveArrayIterator, "CHILD_ARRAYS_ONLY", SPL_ARRAY_CHILD_ARRAYS_ONLY);
1960
1961    return SUCCESS;
1962}
1963/* }}} */
1964
1965/*
1966 * Local variables:
1967 * tab-width: 4
1968 * c-basic-offset: 4
1969 * End:
1970 * vim600: fdm=marker
1971 * vim: noet sw=4 ts=4
1972 */
1973