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 = STR_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", offset_key->val);
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", offset_key->val);
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", offset_key->val);
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", offset_key->val);
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        RETURN_ZVAL(value, 1, 0);
714    }
715} /* }}} */
716
717/* {{{ proto void ArrayObject::offsetSet(mixed $index, mixed $newval)
718       proto void ArrayIterator::offsetSet(mixed $index, mixed $newval)
719   Sets the value at the specified $index to $newval. */
720SPL_METHOD(Array, offsetSet)
721{
722    zval *index, *value;
723    if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
724        return;
725    }
726    spl_array_write_dimension_ex(0, getThis(), index, value);
727} /* }}} */
728
729void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
730{
731    spl_array_object *intern = Z_SPLARRAY_P(object);
732    HashTable *aht = spl_array_get_hash_table(intern, 0);
733
734    if (!aht) {
735        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
736        return;
737    }
738
739    if (spl_array_is_object(intern)) {
740        php_error_docref(NULL, E_RECOVERABLE_ERROR, "Cannot append properties to objects, use %s::offsetSet() instead", Z_OBJCE_P(object)->name->val);
741        return;
742    }
743
744    spl_array_write_dimension(object, NULL, append_value);
745} /* }}} */
746
747/* {{{ proto void ArrayObject::append(mixed $newval)
748       proto void ArrayIterator::append(mixed $newval)
749   Appends the value (cannot be called for objects). */
750SPL_METHOD(Array, append)
751{
752    zval *value;
753
754    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
755        return;
756    }
757    spl_array_iterator_append(getThis(), value);
758} /* }}} */
759
760/* {{{ proto void ArrayObject::offsetUnset(mixed $index)
761       proto void ArrayIterator::offsetUnset(mixed $index)
762   Unsets the value at the specified $index. */
763SPL_METHOD(Array, offsetUnset)
764{
765    zval *index;
766    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
767        return;
768    }
769    spl_array_unset_dimension_ex(0, getThis(), index);
770} /* }}} */
771
772/* {{{ proto array ArrayObject::getArrayCopy()
773      proto array ArrayIterator::getArrayCopy()
774   Return a copy of the contained array */
775SPL_METHOD(Array, getArrayCopy)
776{
777    zval *object = getThis();
778    spl_array_object *intern = Z_SPLARRAY_P(object);
779
780    RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern, 0)));
781} /* }}} */
782
783static HashTable *spl_array_get_properties(zval *object) /* {{{ */
784{
785    spl_array_object *intern = Z_SPLARRAY_P(object);
786    HashTable *result;
787
788    if (intern->nApplyCount > 1) {
789        php_error_docref(NULL, E_ERROR, "Nesting level too deep - recursive dependency?");
790    }
791
792    intern->nApplyCount++;
793    result = spl_array_get_hash_table(intern, 1);
794    intern->nApplyCount--;
795    return result;
796} /* }}} */
797
798static HashTable* spl_array_get_debug_info(zval *obj, int *is_temp) /* {{{ */
799{
800    zval *storage;
801    zend_string *zname;
802    zend_class_entry *base;
803    spl_array_object *intern = Z_SPLARRAY_P(obj);
804
805    if (!intern->std.properties) {
806        rebuild_object_properties(&intern->std);
807    }
808
809    if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
810        *is_temp = 0;
811        return intern->std.properties;
812    } else {
813        HashTable *debug_info;
814        *is_temp = 1;
815
816        ALLOC_HASHTABLE(debug_info);
817        ZEND_INIT_SYMTABLE_EX(debug_info, zend_hash_num_elements(intern->std.properties) + 1, 0);
818        zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
819
820        storage = &intern->array;
821        Z_TRY_ADDREF_P(storage);
822
823        base = Z_OBJ_HT_P(obj) == &spl_handler_ArrayIterator
824            ? spl_ce_ArrayIterator : spl_ce_ArrayObject;
825        zname = spl_gen_private_prop_name(base, "storage", sizeof("storage")-1);
826        zend_symtable_update(debug_info, zname, storage);
827        zend_string_release(zname);
828
829        return debug_info;
830    }
831}
832/* }}} */
833
834static HashTable *spl_array_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
835{
836    spl_array_object *intern = Z_SPLARRAY_P(obj);
837    *gc_data = &intern->array;
838    *gc_data_count = 1;
839    return zend_std_get_properties(obj);
840}
841/* }}} */
842
843static zval *spl_array_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
844{
845    spl_array_object *intern = Z_SPLARRAY_P(object);
846
847    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
848        && !std_object_handlers.has_property(object, member, 2, cache_slot)) {
849        return spl_array_read_dimension(object, member, type, rv);
850    }
851    return std_object_handlers.read_property(object, member, type, cache_slot, rv);
852} /* }}} */
853
854static void spl_array_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
855{
856    spl_array_object *intern = Z_SPLARRAY_P(object);
857
858    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
859    && !std_object_handlers.has_property(object, member, 2, cache_slot)) {
860        spl_array_write_dimension(object, member, value);
861        return;
862    }
863    std_object_handlers.write_property(object, member, value, cache_slot);
864} /* }}} */
865
866static zval *spl_array_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
867{
868    spl_array_object *intern = Z_SPLARRAY_P(object);
869
870    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
871        && !std_object_handlers.has_property(object, member, 2, cache_slot)) {
872        return spl_array_get_dimension_ptr(1, object, member, type);
873    }
874    //!!! FIXME
875    //return std_object_handlers.get_property_ptr_ptr(object, member, type, key);
876    return NULL;
877} /* }}} */
878
879static int spl_array_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
880{
881    spl_array_object *intern = Z_SPLARRAY_P(object);
882
883    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
884        && !std_object_handlers.has_property(object, member, 2, cache_slot)) {
885        return spl_array_has_dimension(object, member, has_set_exists);
886    }
887    return std_object_handlers.has_property(object, member, has_set_exists, cache_slot);
888} /* }}} */
889
890static void spl_array_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
891{
892    spl_array_object *intern = Z_SPLARRAY_P(object);
893
894    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
895        && !std_object_handlers.has_property(object, member, 2, cache_slot)) {
896        spl_array_unset_dimension(object, member);
897        return;
898    }
899    std_object_handlers.unset_property(object, member, cache_slot);
900} /* }}} */
901
902static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
903{
904    HashTable           *ht1,
905                        *ht2;
906    spl_array_object    *intern1,
907                        *intern2;
908    int                 result  = 0;
909
910    intern1 = Z_SPLARRAY_P(o1);
911    intern2 = Z_SPLARRAY_P(o2);
912    ht1     = spl_array_get_hash_table(intern1, 0);
913    ht2     = spl_array_get_hash_table(intern2, 0);
914
915    result = zend_compare_symbol_tables(ht1, ht2);
916    /* if we just compared std.properties, don't do it again */
917    if (result == 0 &&
918            !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
919        result = std_object_handlers.compare_objects(o1, o2);
920    }
921    return result;
922} /* }}} */
923
924static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
925{
926    zend_string *string_key;
927    zend_ulong num_key;
928    zval *data;
929
930    if (spl_array_is_object(intern)) {
931        uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
932
933        do {
934            if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
935                data = zend_hash_get_current_data_ex(aht, pos_ptr);
936                if (data && Z_TYPE_P(data) == IS_INDIRECT &&
937                    Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
938                    /* skip */
939                } else if (!string_key->len || string_key->val[0]) {
940                    return SUCCESS;
941                }
942            } else {
943                return SUCCESS;
944            }
945            if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
946                return FAILURE;
947            }
948            zend_hash_move_forward_ex(aht, pos_ptr);
949        } while (1);
950    }
951    return FAILURE;
952} /* }}} */
953
954static int spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
955{
956    uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
957
958    zend_hash_move_forward_ex(aht, pos_ptr);
959    if (spl_array_is_object(intern)) {
960        return spl_array_skip_protected(intern, aht);
961    } else {
962        return zend_hash_has_more_elements_ex(aht, pos_ptr);
963    }
964} /* }}} */
965
966static int spl_array_next(spl_array_object *intern) /* {{{ */
967{
968    HashTable *aht = spl_array_get_hash_table(intern, 0);
969
970    return spl_array_next_ex(intern, aht);
971
972} /* }}} */
973
974static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
975{
976    zend_user_it_invalidate_current(iter);
977    zval_ptr_dtor(&iter->data);
978}
979/* }}} */
980
981static int spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
982{
983    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
984    HashTable *aht = spl_array_get_hash_table(object, 0);
985
986    if (object->ar_flags & SPL_ARRAY_OVERLOADED_VALID) {
987        return zend_user_it_valid(iter);
988    } else {
989        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::valid(): ") == FAILURE) {
990            return FAILURE;
991        }
992
993        return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
994    }
995}
996/* }}} */
997
998static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
999{
1000    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1001    HashTable *aht = spl_array_get_hash_table(object, 0);
1002
1003    if (object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT) {
1004        return zend_user_it_get_current_data(iter);
1005    } else {
1006        zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1007        if (Z_TYPE_P(data) == IS_INDIRECT) {
1008            data = Z_INDIRECT_P(data);
1009        }
1010        return data;
1011    }
1012}
1013/* }}} */
1014
1015static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1016{
1017    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1018    HashTable *aht = spl_array_get_hash_table(object, 0);
1019
1020    if (object->ar_flags & SPL_ARRAY_OVERLOADED_KEY) {
1021        zend_user_it_get_current_key(iter, key);
1022    } else {
1023        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::current(): ") == FAILURE) {
1024            ZVAL_NULL(key);
1025        } else {
1026            zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1027        }
1028    }
1029}
1030/* }}} */
1031
1032static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1033{
1034    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1035    HashTable *aht = spl_array_get_hash_table(object, 0);
1036
1037    if (object->ar_flags & SPL_ARRAY_OVERLOADED_NEXT) {
1038        zend_user_it_move_forward(iter);
1039    } else {
1040        zend_user_it_invalidate_current(iter);
1041        if (!aht) {
1042            php_error_docref(NULL, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array");
1043            return;
1044        }
1045
1046        spl_array_next_ex(object, aht);
1047    }
1048}
1049/* }}} */
1050
1051static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1052{
1053    HashTable *aht = spl_array_get_hash_table(intern, 0);
1054
1055    if (!aht) {
1056        php_error_docref(NULL, E_NOTICE, "ArrayIterator::rewind(): Array was modified outside object and is no longer an array");
1057        return;
1058    }
1059
1060    if (intern->ht_iter == (uint32_t)-1) {
1061        spl_array_get_pos_ptr(aht, intern);
1062    } else {
1063        zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1064        spl_array_skip_protected(intern, aht);
1065    }
1066}
1067/* }}} */
1068
1069static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1070{
1071    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1072
1073    if (object->ar_flags & SPL_ARRAY_OVERLOADED_REWIND) {
1074        zend_user_it_rewind(iter);
1075    } else {
1076        zend_user_it_invalidate_current(iter);
1077        spl_array_rewind(object);
1078    }
1079}
1080/* }}} */
1081
1082/* {{{ spl_array_set_array */
1083static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
1084    if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
1085        zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object, using empty array instead", 0);
1086        return;
1087    }
1088
1089    zval_ptr_dtor(&intern->array);
1090
1091    if (Z_TYPE_P(array) == IS_ARRAY) {
1092        //??? TODO: try to avoid array duplication
1093        ZVAL_DUP(&intern->array, array);
1094    } else {
1095        if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject || Z_OBJ_HT_P(array) == &spl_handler_ArrayIterator) {
1096            if (just_array) {
1097                spl_array_object *other = Z_SPLARRAY_P(array);
1098                ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
1099            }
1100            if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
1101                ar_flags |= SPL_ARRAY_IS_SELF;
1102                ZVAL_UNDEF(&intern->array);
1103            } else {
1104                ar_flags |= SPL_ARRAY_USE_OTHER;
1105                ZVAL_COPY(&intern->array, array);
1106            }
1107        } else {
1108            zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
1109            ZVAL_COPY(&intern->array, array);
1110            if (handler != std_object_handlers.get_properties
1111                || !spl_array_get_hash_table(intern, 0)) {
1112                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);
1113            }
1114        }
1115    }
1116
1117    intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
1118    intern->ar_flags |= ar_flags;
1119    intern->ht_iter = (uint32_t)-1;
1120}
1121/* }}} */
1122
1123/* iterator handler table */
1124zend_object_iterator_funcs spl_array_it_funcs = {
1125    spl_array_it_dtor,
1126    spl_array_it_valid,
1127    spl_array_it_get_current_data,
1128    spl_array_it_get_current_key,
1129    spl_array_it_move_forward,
1130    spl_array_it_rewind
1131};
1132
1133zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1134{
1135    zend_user_iterator *iterator;
1136    spl_array_object *array_object = Z_SPLARRAY_P(object);
1137
1138    if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
1139        zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
1140    }
1141
1142    iterator = emalloc(sizeof(zend_user_iterator));
1143
1144    zend_iterator_init(&iterator->it);
1145
1146    ZVAL_COPY(&iterator->it.data, object);
1147    iterator->it.funcs = &spl_array_it_funcs;
1148    iterator->ce = ce;
1149    ZVAL_UNDEF(&iterator->value);
1150
1151    return &iterator->it;
1152}
1153/* }}} */
1154
1155/* {{{ proto void ArrayObject::__construct(array|object ar = array() [, int flags = 0 [, string iterator_class = "ArrayIterator"]])
1156       proto void ArrayIterator::__construct(array|object ar = array() [, int flags = 0])
1157   Constructs a new array iterator from a path. */
1158SPL_METHOD(Array, __construct)
1159{
1160    zval *object = getThis();
1161    spl_array_object *intern;
1162    zval *array;
1163    zend_long ar_flags = 0;
1164    zend_class_entry *ce_get_iterator = spl_ce_Iterator;
1165
1166    if (ZEND_NUM_ARGS() == 0) {
1167        return; /* nothing to do */
1168    }
1169
1170    if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|lC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1171        return;
1172    }
1173
1174    intern = Z_SPLARRAY_P(object);
1175
1176    if (ZEND_NUM_ARGS() > 2) {
1177        intern->ce_get_iterator = ce_get_iterator;
1178    }
1179
1180    ar_flags &= ~SPL_ARRAY_INT_MASK;
1181
1182    spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1183}
1184 /* }}} */
1185
1186/* {{{ proto void ArrayObject::setIteratorClass(string iterator_class)
1187   Set the class used in getIterator. */
1188SPL_METHOD(Array, setIteratorClass)
1189{
1190    zval *object = getThis();
1191    spl_array_object *intern = Z_SPLARRAY_P(object);
1192    zend_class_entry * ce_get_iterator = spl_ce_Iterator;
1193
1194#ifndef FAST_ZPP
1195    if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &ce_get_iterator) == FAILURE) {
1196        return;
1197    }
1198#else
1199    ZEND_PARSE_PARAMETERS_START(1, 1)
1200        Z_PARAM_CLASS(ce_get_iterator)
1201    ZEND_PARSE_PARAMETERS_END();
1202#endif
1203
1204    intern->ce_get_iterator = ce_get_iterator;
1205}
1206/* }}} */
1207
1208/* {{{ proto string ArrayObject::getIteratorClass()
1209   Get the class used in getIterator. */
1210SPL_METHOD(Array, getIteratorClass)
1211{
1212    zval *object = getThis();
1213    spl_array_object *intern = Z_SPLARRAY_P(object);
1214
1215    if (zend_parse_parameters_none() == FAILURE) {
1216        return;
1217    }
1218
1219    zend_string_addref(intern->ce_get_iterator->name);
1220    RETURN_STR(intern->ce_get_iterator->name);
1221}
1222/* }}} */
1223
1224/* {{{ proto int ArrayObject::getFlags()
1225   Get flags */
1226SPL_METHOD(Array, getFlags)
1227{
1228    zval *object = getThis();
1229    spl_array_object *intern = Z_SPLARRAY_P(object);
1230
1231    if (zend_parse_parameters_none() == FAILURE) {
1232        return;
1233    }
1234
1235    RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1236}
1237/* }}} */
1238
1239/* {{{ proto void ArrayObject::setFlags(int flags)
1240   Set flags */
1241SPL_METHOD(Array, setFlags)
1242{
1243    zval *object = getThis();
1244    spl_array_object *intern = Z_SPLARRAY_P(object);
1245    zend_long ar_flags = 0;
1246
1247    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1248        return;
1249    }
1250
1251    intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1252}
1253/* }}} */
1254
1255/* {{{ proto Array|Object ArrayObject::exchangeArray(Array|Object ar = array())
1256   Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
1257SPL_METHOD(Array, exchangeArray)
1258{
1259    zval *object = getThis(), *array;
1260    spl_array_object *intern = Z_SPLARRAY_P(object);
1261
1262    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &array) == FAILURE) {
1263        return;
1264    }
1265
1266    RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern, 0)));
1267    spl_array_set_array(object, intern, array, 0L, 1);
1268}
1269/* }}} */
1270
1271/* {{{ proto ArrayIterator ArrayObject::getIterator()
1272   Create a new iterator from a ArrayObject instance */
1273SPL_METHOD(Array, getIterator)
1274{
1275    zval *object = getThis();
1276    spl_array_object *intern = Z_SPLARRAY_P(object);
1277    HashTable *aht = spl_array_get_hash_table(intern, 0);
1278
1279    if (zend_parse_parameters_none() == FAILURE) {
1280        return;
1281    }
1282
1283    if (!aht) {
1284        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1285        return;
1286    }
1287
1288    ZVAL_OBJ(return_value, spl_array_object_new_ex(intern->ce_get_iterator, object, 0));
1289}
1290/* }}} */
1291
1292/* {{{ proto void ArrayIterator::rewind()
1293   Rewind array back to the start */
1294SPL_METHOD(Array, rewind)
1295{
1296    zval *object = getThis();
1297    spl_array_object *intern = Z_SPLARRAY_P(object);
1298
1299    if (zend_parse_parameters_none() == FAILURE) {
1300        return;
1301    }
1302
1303    spl_array_rewind(intern);
1304}
1305/* }}} */
1306
1307/* {{{ proto void ArrayIterator::seek(int $position)
1308   Seek to position. */
1309SPL_METHOD(Array, seek)
1310{
1311    zend_long opos, position;
1312    zval *object = getThis();
1313    spl_array_object *intern = Z_SPLARRAY_P(object);
1314    HashTable *aht = spl_array_get_hash_table(intern, 0);
1315    int result;
1316
1317    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1318        return;
1319    }
1320
1321    if (!aht) {
1322        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1323        return;
1324    }
1325
1326    opos = position;
1327
1328    if (position >= 0) { /* negative values are not supported */
1329        spl_array_rewind(intern);
1330        result = SUCCESS;
1331
1332        while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1333
1334        if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1335            return; /* ok */
1336        }
1337    }
1338    zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position %pd is out of range", opos);
1339} /* }}} */
1340
1341int static spl_array_object_count_elements_helper(spl_array_object *intern, zend_long *count) /* {{{ */
1342{
1343    HashTable *aht = spl_array_get_hash_table(intern, 0);
1344    HashPosition pos, *pos_ptr;
1345
1346    if (!aht) {
1347        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1348        *count = 0;
1349        return FAILURE;
1350    }
1351
1352    if (spl_array_is_object(intern)) {
1353        /* We need to store the 'pos' since we'll modify it in the functions
1354         * we're going to call and which do not support 'pos' as parameter. */
1355        pos_ptr = spl_array_get_pos_ptr(aht, intern);
1356        pos = *pos_ptr;
1357        *count = 0;
1358        spl_array_rewind(intern);
1359        while (*pos_ptr != HT_INVALID_IDX && spl_array_next(intern) == SUCCESS) {
1360            (*count)++;
1361        }
1362        *pos_ptr = pos;
1363        return SUCCESS;
1364    } else {
1365        *count = zend_hash_num_elements(aht);
1366        return SUCCESS;
1367    }
1368} /* }}} */
1369
1370int spl_array_object_count_elements(zval *object, zend_long *count) /* {{{ */
1371{
1372    spl_array_object *intern = Z_SPLARRAY_P(object);
1373
1374    if (intern->fptr_count) {
1375        zval rv;
1376        zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1377        if (Z_TYPE(rv) != IS_UNDEF) {
1378            *count = zval_get_long(&rv);
1379            zval_ptr_dtor(&rv);
1380            return SUCCESS;
1381        }
1382        *count = 0;
1383        return FAILURE;
1384    }
1385    return spl_array_object_count_elements_helper(intern, count);
1386} /* }}} */
1387
1388/* {{{ proto int ArrayObject::count()
1389       proto int ArrayIterator::count()
1390   Return the number of elements in the Iterator. */
1391SPL_METHOD(Array, count)
1392{
1393    zend_long count;
1394    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1395
1396    if (zend_parse_parameters_none() == FAILURE) {
1397        return;
1398    }
1399
1400    spl_array_object_count_elements_helper(intern, &count);
1401
1402    RETURN_LONG(count);
1403} /* }}} */
1404
1405static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) /* {{{ */
1406{
1407    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1408    HashTable *aht = spl_array_get_hash_table(intern, 0);
1409    zval function_name, params[2], *arg = NULL;
1410    uint32_t old_refcount;
1411
1412    ZVAL_STRINGL(&function_name, fname, fname_len);
1413
1414    /* A tricky way to pass "aht" by reference, reset refcount */
1415    //??? It may be not safe, if user comparison handler accesses "aht"
1416    old_refcount = GC_REFCOUNT(aht);
1417    GC_REFCOUNT(aht) = 1;
1418    ZVAL_NEW_EMPTY_REF(&params[0]);
1419    ZVAL_ARR(Z_REFVAL(params[0]), aht);
1420
1421    if (!use_arg) {
1422        aht->u.v.nApplyCount++;
1423        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, 1, params, 1, NULL);
1424        aht->u.v.nApplyCount--;
1425    } else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
1426        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
1427            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
1428            goto exit;
1429        }
1430        if (arg) {
1431            ZVAL_COPY_VALUE(&params[1], arg);
1432        }
1433        aht->u.v.nApplyCount++;
1434        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, arg ? 2 : 1, params, 1, NULL);
1435        aht->u.v.nApplyCount--;
1436    } else {
1437        if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1438            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
1439            goto exit;
1440        }
1441        ZVAL_COPY_VALUE(&params[1], arg);
1442        aht->u.v.nApplyCount++;
1443        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, 2, params, 1, NULL);
1444        aht->u.v.nApplyCount--;
1445    }
1446
1447exit:
1448    /* A tricky way to pass "aht" by reference, copy back and cleanup */
1449    GC_REFCOUNT(aht) = old_refcount;
1450    efree(Z_REF(params[0]));
1451    zend_string_free(Z_STR(function_name));
1452} /* }}} */
1453
1454#define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1455SPL_METHOD(cname, fname) \
1456{ \
1457    spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1458}
1459
1460/* {{{ proto int ArrayObject::asort([int $sort_flags = SORT_REGULAR ])
1461       proto int ArrayIterator::asort([int $sort_flags = SORT_REGULAR ])
1462   Sort the entries by values. */
1463SPL_ARRAY_METHOD(Array, asort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1464
1465/* {{{ proto int ArrayObject::ksort([int $sort_flags = SORT_REGULAR ])
1466       proto int ArrayIterator::ksort([int $sort_flags = SORT_REGULAR ])
1467   Sort the entries by key. */
1468SPL_ARRAY_METHOD(Array, ksort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1469
1470/* {{{ proto int ArrayObject::uasort(callback cmp_function)
1471       proto int ArrayIterator::uasort(callback cmp_function)
1472   Sort the entries by values user defined function. */
1473SPL_ARRAY_METHOD(Array, uasort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1474
1475/* {{{ proto int ArrayObject::uksort(callback cmp_function)
1476       proto int ArrayIterator::uksort(callback cmp_function)
1477   Sort the entries by key using user defined function. */
1478SPL_ARRAY_METHOD(Array, uksort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1479
1480/* {{{ proto int ArrayObject::natsort()
1481       proto int ArrayIterator::natsort()
1482   Sort the entries by values using "natural order" algorithm. */
1483SPL_ARRAY_METHOD(Array, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1484
1485/* {{{ proto int ArrayObject::natcasesort()
1486       proto int ArrayIterator::natcasesort()
1487   Sort the entries by key using case insensitive "natural order" algorithm. */
1488SPL_ARRAY_METHOD(Array, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1489
1490/* {{{ proto mixed|NULL ArrayIterator::current()
1491   Return current array entry */
1492SPL_METHOD(Array, current)
1493{
1494    zval *object = getThis();
1495    spl_array_object *intern = Z_SPLARRAY_P(object);
1496    zval *entry;
1497    HashTable *aht = spl_array_get_hash_table(intern, 0);
1498
1499    if (zend_parse_parameters_none() == FAILURE) {
1500        return;
1501    }
1502
1503    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1504        return;
1505    }
1506
1507    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1508        return;
1509    }
1510    if (Z_TYPE_P(entry) == IS_INDIRECT) {
1511        entry = Z_INDIRECT_P(entry);
1512        if (Z_TYPE_P(entry) == IS_UNDEF) {
1513            return;
1514        }
1515    }
1516    RETVAL_ZVAL(entry, 1, 0);
1517}
1518/* }}} */
1519
1520/* {{{ proto mixed|NULL ArrayIterator::key()
1521   Return current array key */
1522SPL_METHOD(Array, key)
1523{
1524    if (zend_parse_parameters_none() == FAILURE) {
1525        return;
1526    }
1527
1528    spl_array_iterator_key(getThis(), return_value);
1529} /* }}} */
1530
1531void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1532{
1533    spl_array_object *intern = Z_SPLARRAY_P(object);
1534    HashTable *aht = spl_array_get_hash_table(intern, 0);
1535
1536    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1537        return;
1538    }
1539
1540    zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1541}
1542/* }}} */
1543
1544/* {{{ proto void ArrayIterator::next()
1545   Move to next entry */
1546SPL_METHOD(Array, next)
1547{
1548    zval *object = getThis();
1549    spl_array_object *intern = Z_SPLARRAY_P(object);
1550    HashTable *aht = spl_array_get_hash_table(intern, 0);
1551
1552    if (zend_parse_parameters_none() == FAILURE) {
1553        return;
1554    }
1555
1556    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1557        return;
1558    }
1559
1560    spl_array_next_ex(intern, aht);
1561}
1562/* }}} */
1563
1564/* {{{ proto bool ArrayIterator::valid()
1565   Check whether array contains more entries */
1566SPL_METHOD(Array, valid)
1567{
1568    zval *object = getThis();
1569    spl_array_object *intern = Z_SPLARRAY_P(object);
1570    HashTable *aht = spl_array_get_hash_table(intern, 0);
1571
1572    if (zend_parse_parameters_none() == FAILURE) {
1573        return;
1574    }
1575
1576    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1577        RETURN_FALSE;
1578    } else {
1579        RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1580    }
1581}
1582/* }}} */
1583
1584/* {{{ proto bool RecursiveArrayIterator::hasChildren()
1585   Check whether current element has children (e.g. is an array) */
1586SPL_METHOD(Array, hasChildren)
1587{
1588    zval *object = getThis(), *entry;
1589    spl_array_object *intern = Z_SPLARRAY_P(object);
1590    HashTable *aht = spl_array_get_hash_table(intern, 0);
1591
1592    if (zend_parse_parameters_none() == FAILURE) {
1593        return;
1594    }
1595
1596    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1597        RETURN_FALSE;
1598    }
1599
1600    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1601        RETURN_FALSE;
1602    }
1603
1604    RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1605}
1606/* }}} */
1607
1608/* {{{ proto object RecursiveArrayIterator::getChildren()
1609   Create a sub iterator for the current element (same class as $this) */
1610SPL_METHOD(Array, getChildren)
1611{
1612    zval *object = getThis(), *entry, flags;
1613    spl_array_object *intern = Z_SPLARRAY_P(object);
1614    HashTable *aht = spl_array_get_hash_table(intern, 0);
1615
1616    if (zend_parse_parameters_none() == FAILURE) {
1617        return;
1618    }
1619
1620    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1621        return;
1622    }
1623
1624    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1625        return;
1626    }
1627
1628    if (Z_TYPE_P(entry) == IS_OBJECT) {
1629        if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1630            return;
1631        }
1632        if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(getThis()))) {
1633            RETURN_ZVAL(entry, 1, 0);
1634        }
1635    }
1636
1637    ZVAL_LONG(&flags, intern->ar_flags);
1638    spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), return_value, entry, &flags);
1639}
1640/* }}} */
1641
1642/* {{{ proto string ArrayObject::serialize()
1643   Serialize the object */
1644SPL_METHOD(Array, serialize)
1645{
1646    zval *object = getThis();
1647    spl_array_object *intern = Z_SPLARRAY_P(object);
1648    HashTable *aht = spl_array_get_hash_table(intern, 0);
1649    zval members, flags;
1650    php_serialize_data_t var_hash;
1651    smart_str buf = {0};
1652
1653    if (zend_parse_parameters_none() == FAILURE) {
1654        return;
1655    }
1656
1657    if (!aht) {
1658        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1659        return;
1660    }
1661
1662    PHP_VAR_SERIALIZE_INIT(var_hash);
1663
1664    ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1665
1666    /* storage */
1667    smart_str_appendl(&buf, "x:", 2);
1668    //!!! php_var_serialize need to be modified
1669    php_var_serialize(&buf, &flags, &var_hash);
1670
1671    if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1672        php_var_serialize(&buf, &intern->array, &var_hash);
1673        smart_str_appendc(&buf, ';');
1674    }
1675
1676    /* members */
1677    smart_str_appendl(&buf, "m:", 2);
1678    if (!intern->std.properties) {
1679        rebuild_object_properties(&intern->std);
1680    }
1681
1682    ZVAL_ARR(&members, zend_array_dup(intern->std.properties));
1683
1684    php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1685
1686    zval_ptr_dtor(&members);
1687
1688    /* done */
1689    PHP_VAR_SERIALIZE_DESTROY(var_hash);
1690
1691    if (buf.s) {
1692        RETURN_NEW_STR(buf.s);
1693    }
1694
1695    RETURN_NULL();
1696} /* }}} */
1697
1698/* {{{ proto void ArrayObject::unserialize(string serialized)
1699 * unserialize the object
1700 */
1701SPL_METHOD(Array, unserialize)
1702{
1703    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1704
1705    char *buf;
1706    size_t buf_len;
1707    const unsigned char *p, *s;
1708    php_unserialize_data_t var_hash;
1709    zval members, zflags;
1710    HashTable *aht;
1711    zend_long flags;
1712
1713    if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1714        return;
1715    }
1716
1717    if (buf_len == 0) {
1718        return;
1719    }
1720
1721    aht = spl_array_get_hash_table(intern, 0);
1722    if (aht->u.v.nApplyCount > 0) {
1723        zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
1724        return;
1725    }
1726
1727    /* storage */
1728    s = p = (const unsigned char*)buf;
1729    PHP_VAR_UNSERIALIZE_INIT(var_hash);
1730
1731    if (*p!= 'x' || *++p != ':') {
1732        goto outexcept;
1733    }
1734    ++p;
1735
1736    if (!php_var_unserialize(&zflags, &p, s + buf_len, &var_hash) || Z_TYPE(zflags) != IS_LONG) {
1737        goto outexcept;
1738    }
1739
1740    --p; /* for ';' */
1741    flags = Z_LVAL(zflags);
1742    /* flags needs to be verified and we also need to verify whether the next
1743     * thing we get is ';'. After that we require an 'm' or somethign else
1744     * where 'm' stands for members and anything else should be an array. If
1745     * neither 'a' or 'm' follows we have an error. */
1746
1747    if (*p != ';') {
1748        goto outexcept;
1749    }
1750    ++p;
1751
1752    if (*p!='m') {
1753        if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1754            goto outexcept;
1755        }
1756        intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1757        intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1758        zval_ptr_dtor(&intern->array);
1759        if (!php_var_unserialize(&intern->array, &p, s + buf_len, &var_hash)) {
1760            goto outexcept;
1761        }
1762    }
1763    if (*p != ';') {
1764        goto outexcept;
1765    }
1766    ++p;
1767
1768    /* members */
1769    if (*p!= 'm' || *++p != ':') {
1770        goto outexcept;
1771    }
1772    ++p;
1773
1774    ZVAL_UNDEF(&members);
1775    if (!php_var_unserialize(&members, &p, s + buf_len, &var_hash) || Z_TYPE(members) != IS_ARRAY) {
1776        zval_ptr_dtor(&members);
1777        goto outexcept;
1778    }
1779
1780    /* copy members */
1781    object_properties_load(&intern->std, Z_ARRVAL(members));
1782    zval_ptr_dtor(&members);
1783
1784    /* done reading $serialized */
1785
1786    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1787    return;
1788
1789outexcept:
1790    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
1791    zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %pd of %d bytes", (zend_long)((char*)p - buf), buf_len);
1792    return;
1793
1794} /* }}} */
1795
1796/* {{{ arginfo and function tbale */
1797ZEND_BEGIN_ARG_INFO(arginfo_array___construct, 0)
1798    ZEND_ARG_INFO(0, array)
1799ZEND_END_ARG_INFO()
1800
1801ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetGet, 0, 0, 1)
1802    ZEND_ARG_INFO(0, index)
1803ZEND_END_ARG_INFO()
1804
1805ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetSet, 0, 0, 2)
1806    ZEND_ARG_INFO(0, index)
1807    ZEND_ARG_INFO(0, newval)
1808ZEND_END_ARG_INFO()
1809
1810ZEND_BEGIN_ARG_INFO(arginfo_array_append, 0)
1811    ZEND_ARG_INFO(0, value)
1812ZEND_END_ARG_INFO()
1813
1814ZEND_BEGIN_ARG_INFO(arginfo_array_seek, 0)
1815    ZEND_ARG_INFO(0, position)
1816ZEND_END_ARG_INFO()
1817
1818ZEND_BEGIN_ARG_INFO(arginfo_array_exchangeArray, 0)
1819    ZEND_ARG_INFO(0, array)
1820ZEND_END_ARG_INFO()
1821
1822ZEND_BEGIN_ARG_INFO(arginfo_array_setFlags, 0)
1823    ZEND_ARG_INFO(0, flags)
1824ZEND_END_ARG_INFO()
1825
1826ZEND_BEGIN_ARG_INFO(arginfo_array_setIteratorClass, 0)
1827    ZEND_ARG_INFO(0, iteratorClass)
1828ZEND_END_ARG_INFO()
1829
1830ZEND_BEGIN_ARG_INFO(arginfo_array_uXsort, 0)
1831    ZEND_ARG_INFO(0, cmp_function)
1832ZEND_END_ARG_INFO();
1833
1834ZEND_BEGIN_ARG_INFO(arginfo_array_unserialize, 0)
1835    ZEND_ARG_INFO(0, serialized)
1836ZEND_END_ARG_INFO();
1837
1838ZEND_BEGIN_ARG_INFO(arginfo_array_void, 0)
1839ZEND_END_ARG_INFO()
1840
1841static const zend_function_entry spl_funcs_ArrayObject[] = {
1842    SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
1843    SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1844    SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1845    SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
1846    SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1847    SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
1848    SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
1849    SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1850    SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
1851    SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
1852    SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1853    SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1854    SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1855    SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1856    SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1857    SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1858    SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1859    SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
1860    /* ArrayObject specific */
1861    SPL_ME(Array, getIterator,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1862    SPL_ME(Array, exchangeArray,    arginfo_array_exchangeArray,    ZEND_ACC_PUBLIC)
1863    SPL_ME(Array, setIteratorClass, arginfo_array_setIteratorClass, ZEND_ACC_PUBLIC)
1864    SPL_ME(Array, getIteratorClass, arginfo_array_void,             ZEND_ACC_PUBLIC)
1865    PHP_FE_END
1866};
1867
1868static const zend_function_entry spl_funcs_ArrayIterator[] = {
1869    SPL_ME(Array, __construct,      arginfo_array___construct,      ZEND_ACC_PUBLIC)
1870    SPL_ME(Array, offsetExists,     arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1871    SPL_ME(Array, offsetGet,        arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1872    SPL_ME(Array, offsetSet,        arginfo_array_offsetSet,        ZEND_ACC_PUBLIC)
1873    SPL_ME(Array, offsetUnset,      arginfo_array_offsetGet,        ZEND_ACC_PUBLIC)
1874    SPL_ME(Array, append,           arginfo_array_append,           ZEND_ACC_PUBLIC)
1875    SPL_ME(Array, getArrayCopy,     arginfo_array_void,             ZEND_ACC_PUBLIC)
1876    SPL_ME(Array, count,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1877    SPL_ME(Array, getFlags,         arginfo_array_void,             ZEND_ACC_PUBLIC)
1878    SPL_ME(Array, setFlags,         arginfo_array_setFlags,         ZEND_ACC_PUBLIC)
1879    SPL_ME(Array, asort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1880    SPL_ME(Array, ksort,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1881    SPL_ME(Array, uasort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1882    SPL_ME(Array, uksort,           arginfo_array_uXsort,           ZEND_ACC_PUBLIC)
1883    SPL_ME(Array, natsort,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1884    SPL_ME(Array, natcasesort,      arginfo_array_void,             ZEND_ACC_PUBLIC)
1885    SPL_ME(Array, unserialize,      arginfo_array_unserialize,      ZEND_ACC_PUBLIC)
1886    SPL_ME(Array, serialize,        arginfo_array_void,             ZEND_ACC_PUBLIC)
1887    /* ArrayIterator specific */
1888    SPL_ME(Array, rewind,           arginfo_array_void,             ZEND_ACC_PUBLIC)
1889    SPL_ME(Array, current,          arginfo_array_void,             ZEND_ACC_PUBLIC)
1890    SPL_ME(Array, key,              arginfo_array_void,             ZEND_ACC_PUBLIC)
1891    SPL_ME(Array, next,             arginfo_array_void,             ZEND_ACC_PUBLIC)
1892    SPL_ME(Array, valid,            arginfo_array_void,             ZEND_ACC_PUBLIC)
1893    SPL_ME(Array, seek,             arginfo_array_seek,             ZEND_ACC_PUBLIC)
1894    PHP_FE_END
1895};
1896
1897static const zend_function_entry spl_funcs_RecursiveArrayIterator[] = {
1898    SPL_ME(Array, hasChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
1899    SPL_ME(Array, getChildren,   arginfo_array_void, ZEND_ACC_PUBLIC)
1900    PHP_FE_END
1901};
1902/* }}} */
1903
1904/* {{{ PHP_MINIT_FUNCTION(spl_array) */
1905PHP_MINIT_FUNCTION(spl_array)
1906{
1907    REGISTER_SPL_STD_CLASS_EX(ArrayObject, spl_array_object_new, spl_funcs_ArrayObject);
1908    REGISTER_SPL_IMPLEMENTS(ArrayObject, Aggregate);
1909    REGISTER_SPL_IMPLEMENTS(ArrayObject, ArrayAccess);
1910    REGISTER_SPL_IMPLEMENTS(ArrayObject, Serializable);
1911    REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable);
1912    memcpy(&spl_handler_ArrayObject, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1913
1914    spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
1915
1916    spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
1917    spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
1918    spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
1919    spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
1920    spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
1921    spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
1922
1923    spl_handler_ArrayObject.get_properties = spl_array_get_properties;
1924    spl_handler_ArrayObject.get_debug_info = spl_array_get_debug_info;
1925    spl_handler_ArrayObject.get_gc = spl_array_get_gc;
1926    spl_handler_ArrayObject.read_property = spl_array_read_property;
1927    spl_handler_ArrayObject.write_property = spl_array_write_property;
1928    spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
1929    spl_handler_ArrayObject.has_property = spl_array_has_property;
1930    spl_handler_ArrayObject.unset_property = spl_array_unset_property;
1931
1932    spl_handler_ArrayObject.compare_objects = spl_array_compare_objects;
1933    spl_handler_ArrayObject.dtor_obj = zend_objects_destroy_object;
1934    spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
1935
1936    REGISTER_SPL_STD_CLASS_EX(ArrayIterator, spl_array_object_new, spl_funcs_ArrayIterator);
1937    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Iterator);
1938    REGISTER_SPL_IMPLEMENTS(ArrayIterator, ArrayAccess);
1939    REGISTER_SPL_IMPLEMENTS(ArrayIterator, SeekableIterator);
1940    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Serializable);
1941    REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable);
1942    memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));
1943    spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
1944
1945    REGISTER_SPL_SUB_CLASS_EX(RecursiveArrayIterator, ArrayIterator, spl_array_object_new, spl_funcs_RecursiveArrayIterator);
1946    REGISTER_SPL_IMPLEMENTS(RecursiveArrayIterator, RecursiveIterator);
1947    spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
1948
1949    REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
1950    REGISTER_SPL_CLASS_CONST_LONG(ArrayObject,   "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
1951
1952    REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "STD_PROP_LIST",    SPL_ARRAY_STD_PROP_LIST);
1953    REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "ARRAY_AS_PROPS",   SPL_ARRAY_ARRAY_AS_PROPS);
1954
1955    REGISTER_SPL_CLASS_CONST_LONG(RecursiveArrayIterator, "CHILD_ARRAYS_ONLY", SPL_ARRAY_CHILD_ARRAYS_ONLY);
1956
1957    return SUCCESS;
1958}
1959/* }}} */
1960
1961/*
1962 * Local variables:
1963 * tab-width: 4
1964 * c-basic-offset: 4
1965 * End:
1966 * vim600: fdm=marker
1967 * vim: noet sw=4 ts=4
1968 */
1969