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                        ht->u.v.flags |= HASH_FLAG_HAS_EMPTY_IND;
533                        zend_hash_move_forward_ex(ht, spl_array_get_pos_ptr(ht, intern));
534                        if (spl_array_is_object(intern)) {
535                            spl_array_skip_protected(intern, ht);
536                        }
537                    }
538                } else if (zend_symtable_del(ht, Z_STR_P(offset)) == FAILURE) {
539                    zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
540                }
541            } else {
542                zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
543            }
544        }
545        break;
546    case IS_DOUBLE:
547        index = (zend_long)Z_DVAL_P(offset);
548        goto num_index;
549    case IS_RESOURCE:
550        index = Z_RES_HANDLE_P(offset);
551        goto num_index;
552    case IS_FALSE:
553        index = 0;
554        goto num_index;
555    case IS_TRUE:
556        index = 1;
557        goto num_index;
558    case IS_LONG:
559        index = Z_LVAL_P(offset);
560num_index:
561        ht = spl_array_get_hash_table(intern, 0);
562        if (ht->u.v.nApplyCount > 0) {
563            zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
564            return;
565        }
566        if (zend_hash_index_del(ht, index) == FAILURE) {
567            zend_error(E_NOTICE,"Undefined offset: %pd", index);
568        }
569        break;
570    default:
571        zend_error(E_WARNING, "Illegal offset type");
572        return;
573    }
574} /* }}} */
575
576static void spl_array_unset_dimension(zval *object, zval *offset) /* {{{ */
577{
578    spl_array_unset_dimension_ex(1, object, offset);
579} /* }}} */
580
581static int spl_array_has_dimension_ex(int check_inherited, zval *object, zval *offset, int check_empty) /* {{{ */
582{
583    spl_array_object *intern = Z_SPLARRAY_P(object);
584    zend_long index;
585    zval rv, *value = NULL, *tmp;
586
587    if (check_inherited && intern->fptr_offset_has) {
588//???       zval offset_tmp;
589//???       ZVAL_COPY_VALUE(&offset_tmp, offset);
590//???       SEPARATE_ARG_IF_REF(&offset_tmp);
591//???       zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_has, "offsetExists", &rv, &offset_tmp);
592//???       zval_ptr_dtor(&offset_tmp);
593        SEPARATE_ARG_IF_REF(offset);
594        zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_has, "offsetExists", &rv, offset);
595        zval_ptr_dtor(offset);
596
597        if (!Z_ISUNDEF(rv) && zend_is_true(&rv)) {
598            zval_ptr_dtor(&rv);
599            if (check_empty != 1) {
600                return 1;
601            } else if (intern->fptr_offset_get) {
602                value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
603            }
604        } else {
605            zval_ptr_dtor(&rv);
606            return 0;
607        }
608    }
609
610    if (!value) {
611        HashTable *ht = spl_array_get_hash_table(intern, 0);
612
613        switch(Z_TYPE_P(offset)) {
614            case IS_STRING:
615                if ((tmp = zend_symtable_find(ht, Z_STR_P(offset))) != NULL) {
616                    if (check_empty == 2) {
617                        return 1;
618                    }
619                } else {
620                    return 0;
621                }
622                break;
623
624            case IS_DOUBLE:
625                index = (zend_long)Z_DVAL_P(offset);
626                goto num_index;
627            case IS_RESOURCE:
628                index = Z_RES_HANDLE_P(offset);
629                goto num_index;
630            case IS_FALSE:
631                index = 0;
632                goto num_index;
633            case IS_TRUE:
634                index = 1;
635                goto num_index;
636            case IS_LONG:
637                index = Z_LVAL_P(offset);
638num_index:
639                if ((tmp = zend_hash_index_find(ht, index)) != NULL) {
640                    if (check_empty == 2) {
641                        return 1;
642                    }
643                } else {
644                    return 0;
645                }
646                break;
647
648            default:
649                zend_error(E_WARNING, "Illegal offset type");
650                return 0;
651        }
652
653        if (check_empty && check_inherited && intern->fptr_offset_get) {
654            value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
655        } else {
656            value = tmp;
657        }
658    }
659
660    {
661        zend_bool result = check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
662        if (value == &rv) {
663            zval_ptr_dtor(&rv);
664        }
665        return result;
666    }
667} /* }}} */
668
669static int spl_array_has_dimension(zval *object, zval *offset, int check_empty) /* {{{ */
670{
671    return spl_array_has_dimension_ex(1, object, offset, check_empty);
672} /* }}} */
673
674/* {{{ spl_array_object_verify_pos_ex */
675static inline int spl_array_object_verify_pos_ex(spl_array_object *object, HashTable *ht, const char *msg_prefix)
676{
677    if (!ht) {
678        php_error_docref(NULL, E_NOTICE, "%sArray was modified outside object and is no longer an array", msg_prefix);
679        return FAILURE;
680    }
681
682    return SUCCESS;
683} /* }}} */
684
685/* {{{ spl_array_object_verify_pos */
686static inline int spl_array_object_verify_pos(spl_array_object *object, HashTable *ht)
687{
688    return spl_array_object_verify_pos_ex(object, ht, "");
689} /* }}} */
690
691/* {{{ proto bool ArrayObject::offsetExists(mixed $index)
692       proto bool ArrayIterator::offsetExists(mixed $index)
693   Returns whether the requested $index exists. */
694SPL_METHOD(Array, offsetExists)
695{
696    zval *index;
697    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
698        return;
699    }
700    RETURN_BOOL(spl_array_has_dimension_ex(0, getThis(), index, 2));
701} /* }}} */
702
703/* {{{ proto mixed ArrayObject::offsetGet(mixed $index)
704       proto mixed ArrayIterator::offsetGet(mixed $index)
705   Returns the value at the specified $index. */
706SPL_METHOD(Array, offsetGet)
707{
708    zval *value, *index;
709    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
710        return;
711    }
712    value = spl_array_read_dimension_ex(0, getThis(), index, BP_VAR_R, return_value);
713    if (value != return_value) {
714        ZVAL_DEREF(value);
715        ZVAL_COPY(return_value, value);
716    }
717} /* }}} */
718
719/* {{{ proto void ArrayObject::offsetSet(mixed $index, mixed $newval)
720       proto void ArrayIterator::offsetSet(mixed $index, mixed $newval)
721   Sets the value at the specified $index to $newval. */
722SPL_METHOD(Array, offsetSet)
723{
724    zval *index, *value;
725    if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
726        return;
727    }
728    spl_array_write_dimension_ex(0, getThis(), index, value);
729} /* }}} */
730
731void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
732{
733    spl_array_object *intern = Z_SPLARRAY_P(object);
734    HashTable *aht = spl_array_get_hash_table(intern, 0);
735
736    if (!aht) {
737        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
738        return;
739    }
740
741    if (spl_array_is_object(intern)) {
742        php_error_docref(NULL, E_RECOVERABLE_ERROR, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
743        return;
744    }
745
746    spl_array_write_dimension(object, NULL, append_value);
747} /* }}} */
748
749/* {{{ proto void ArrayObject::append(mixed $newval)
750       proto void ArrayIterator::append(mixed $newval)
751   Appends the value (cannot be called for objects). */
752SPL_METHOD(Array, append)
753{
754    zval *value;
755
756    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
757        return;
758    }
759    spl_array_iterator_append(getThis(), value);
760} /* }}} */
761
762/* {{{ proto void ArrayObject::offsetUnset(mixed $index)
763       proto void ArrayIterator::offsetUnset(mixed $index)
764   Unsets the value at the specified $index. */
765SPL_METHOD(Array, offsetUnset)
766{
767    zval *index;
768    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
769        return;
770    }
771    spl_array_unset_dimension_ex(0, getThis(), index);
772} /* }}} */
773
774/* {{{ proto array ArrayObject::getArrayCopy()
775      proto array ArrayIterator::getArrayCopy()
776   Return a copy of the contained array */
777SPL_METHOD(Array, getArrayCopy)
778{
779    zval *object = getThis();
780    spl_array_object *intern = Z_SPLARRAY_P(object);
781
782    RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern, 0)));
783} /* }}} */
784
785static HashTable *spl_array_get_properties(zval *object) /* {{{ */
786{
787    spl_array_object *intern = Z_SPLARRAY_P(object);
788    HashTable *result;
789
790    if (intern->nApplyCount > 1) {
791        php_error_docref(NULL, E_ERROR, "Nesting level too deep - recursive dependency?");
792    }
793
794    intern->nApplyCount++;
795    result = spl_array_get_hash_table(intern, 1);
796    intern->nApplyCount--;
797    return result;
798} /* }}} */
799
800static HashTable* spl_array_get_debug_info(zval *obj, int *is_temp) /* {{{ */
801{
802    zval *storage;
803    zend_string *zname;
804    zend_class_entry *base;
805    spl_array_object *intern = Z_SPLARRAY_P(obj);
806
807    if (!intern->std.properties) {
808        rebuild_object_properties(&intern->std);
809    }
810
811    if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
812        *is_temp = 0;
813        return intern->std.properties;
814    } else {
815        HashTable *debug_info;
816        *is_temp = 1;
817
818        ALLOC_HASHTABLE(debug_info);
819        ZEND_INIT_SYMTABLE_EX(debug_info, zend_hash_num_elements(intern->std.properties) + 1, 0);
820        zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
821
822        storage = &intern->array;
823        Z_TRY_ADDREF_P(storage);
824
825        base = Z_OBJ_HT_P(obj) == &spl_handler_ArrayIterator
826            ? spl_ce_ArrayIterator : spl_ce_ArrayObject;
827        zname = spl_gen_private_prop_name(base, "storage", sizeof("storage")-1);
828        zend_symtable_update(debug_info, zname, storage);
829        zend_string_release(zname);
830
831        return debug_info;
832    }
833}
834/* }}} */
835
836static HashTable *spl_array_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
837{
838    spl_array_object *intern = Z_SPLARRAY_P(obj);
839    *gc_data = &intern->array;
840    *gc_data_count = 1;
841    return zend_std_get_properties(obj);
842}
843/* }}} */
844
845static zval *spl_array_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
846{
847    spl_array_object *intern = Z_SPLARRAY_P(object);
848
849    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
850        && !std_object_handlers.has_property(object, member, 2, NULL)) {
851        return spl_array_read_dimension(object, member, type, rv);
852    }
853    return std_object_handlers.read_property(object, member, type, cache_slot, rv);
854} /* }}} */
855
856static void spl_array_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
857{
858    spl_array_object *intern = Z_SPLARRAY_P(object);
859
860    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
861    && !std_object_handlers.has_property(object, member, 2, NULL)) {
862        spl_array_write_dimension(object, member, value);
863        return;
864    }
865    std_object_handlers.write_property(object, member, value, cache_slot);
866} /* }}} */
867
868static zval *spl_array_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
869{
870    spl_array_object *intern = Z_SPLARRAY_P(object);
871
872    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
873        && !std_object_handlers.has_property(object, member, 2, NULL)) {
874        return spl_array_get_dimension_ptr(1, object, member, type);
875    }
876    //!!! FIXME
877    //return std_object_handlers.get_property_ptr_ptr(object, member, type, key);
878    return NULL;
879} /* }}} */
880
881static int spl_array_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
882{
883    spl_array_object *intern = Z_SPLARRAY_P(object);
884
885    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
886        && !std_object_handlers.has_property(object, member, 2, NULL)) {
887        return spl_array_has_dimension(object, member, has_set_exists);
888    }
889    return std_object_handlers.has_property(object, member, has_set_exists, cache_slot);
890} /* }}} */
891
892static void spl_array_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
893{
894    spl_array_object *intern = Z_SPLARRAY_P(object);
895
896    if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
897        && !std_object_handlers.has_property(object, member, 2, NULL)) {
898        spl_array_unset_dimension(object, member);
899        return;
900    }
901    std_object_handlers.unset_property(object, member, cache_slot);
902} /* }}} */
903
904static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
905{
906    HashTable           *ht1,
907                        *ht2;
908    spl_array_object    *intern1,
909                        *intern2;
910    int                 result  = 0;
911
912    intern1 = Z_SPLARRAY_P(o1);
913    intern2 = Z_SPLARRAY_P(o2);
914    ht1     = spl_array_get_hash_table(intern1, 0);
915    ht2     = spl_array_get_hash_table(intern2, 0);
916
917    result = zend_compare_symbol_tables(ht1, ht2);
918    /* if we just compared std.properties, don't do it again */
919    if (result == 0 &&
920            !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
921        result = std_object_handlers.compare_objects(o1, o2);
922    }
923    return result;
924} /* }}} */
925
926static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
927{
928    zend_string *string_key;
929    zend_ulong num_key;
930    zval *data;
931
932    if (spl_array_is_object(intern)) {
933        uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
934
935        do {
936            if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
937                data = zend_hash_get_current_data_ex(aht, pos_ptr);
938                if (data && Z_TYPE_P(data) == IS_INDIRECT &&
939                    Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
940                    /* skip */
941                } else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
942                    return SUCCESS;
943                }
944            } else {
945                return SUCCESS;
946            }
947            if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
948                return FAILURE;
949            }
950            zend_hash_move_forward_ex(aht, pos_ptr);
951        } while (1);
952    }
953    return FAILURE;
954} /* }}} */
955
956static int spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
957{
958    uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
959
960    zend_hash_move_forward_ex(aht, pos_ptr);
961    if (spl_array_is_object(intern)) {
962        return spl_array_skip_protected(intern, aht);
963    } else {
964        return zend_hash_has_more_elements_ex(aht, pos_ptr);
965    }
966} /* }}} */
967
968static int spl_array_next(spl_array_object *intern) /* {{{ */
969{
970    HashTable *aht = spl_array_get_hash_table(intern, 0);
971
972    return spl_array_next_ex(intern, aht);
973
974} /* }}} */
975
976static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
977{
978    zend_user_it_invalidate_current(iter);
979    zval_ptr_dtor(&iter->data);
980}
981/* }}} */
982
983static int spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
984{
985    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
986    HashTable *aht = spl_array_get_hash_table(object, 0);
987
988    if (object->ar_flags & SPL_ARRAY_OVERLOADED_VALID) {
989        return zend_user_it_valid(iter);
990    } else {
991        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::valid(): ") == FAILURE) {
992            return FAILURE;
993        }
994
995        return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
996    }
997}
998/* }}} */
999
1000static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
1001{
1002    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1003    HashTable *aht = spl_array_get_hash_table(object, 0);
1004
1005    if (object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT) {
1006        return zend_user_it_get_current_data(iter);
1007    } else {
1008        zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
1009        if (Z_TYPE_P(data) == IS_INDIRECT) {
1010            data = Z_INDIRECT_P(data);
1011        }
1012        return data;
1013    }
1014}
1015/* }}} */
1016
1017static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
1018{
1019    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1020    HashTable *aht = spl_array_get_hash_table(object, 0);
1021
1022    if (object->ar_flags & SPL_ARRAY_OVERLOADED_KEY) {
1023        zend_user_it_get_current_key(iter, key);
1024    } else {
1025        if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::current(): ") == FAILURE) {
1026            ZVAL_NULL(key);
1027        } else {
1028            zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
1029        }
1030    }
1031}
1032/* }}} */
1033
1034static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
1035{
1036    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1037    HashTable *aht = spl_array_get_hash_table(object, 0);
1038
1039    if (object->ar_flags & SPL_ARRAY_OVERLOADED_NEXT) {
1040        zend_user_it_move_forward(iter);
1041    } else {
1042        zend_user_it_invalidate_current(iter);
1043        if (!aht) {
1044            php_error_docref(NULL, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array");
1045            return;
1046        }
1047
1048        spl_array_next_ex(object, aht);
1049    }
1050}
1051/* }}} */
1052
1053static void spl_array_rewind(spl_array_object *intern) /* {{{ */
1054{
1055    HashTable *aht = spl_array_get_hash_table(intern, 0);
1056
1057    if (!aht) {
1058        php_error_docref(NULL, E_NOTICE, "ArrayIterator::rewind(): Array was modified outside object and is no longer an array");
1059        return;
1060    }
1061
1062    if (intern->ht_iter == (uint32_t)-1) {
1063        spl_array_get_pos_ptr(aht, intern);
1064    } else {
1065        zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
1066        spl_array_skip_protected(intern, aht);
1067    }
1068}
1069/* }}} */
1070
1071static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
1072{
1073    spl_array_object *object = Z_SPLARRAY_P(&iter->data);
1074
1075    if (object->ar_flags & SPL_ARRAY_OVERLOADED_REWIND) {
1076        zend_user_it_rewind(iter);
1077    } else {
1078        zend_user_it_invalidate_current(iter);
1079        spl_array_rewind(object);
1080    }
1081}
1082/* }}} */
1083
1084/* {{{ spl_array_set_array */
1085static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
1086    if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
1087        zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object, using empty array instead", 0);
1088        return;
1089    }
1090
1091    zval_ptr_dtor(&intern->array);
1092
1093    if (Z_TYPE_P(array) == IS_ARRAY) {
1094        //??? TODO: try to avoid array duplication
1095        ZVAL_DUP(&intern->array, array);
1096    } else {
1097        if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject || Z_OBJ_HT_P(array) == &spl_handler_ArrayIterator) {
1098            if (just_array) {
1099                spl_array_object *other = Z_SPLARRAY_P(array);
1100                ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
1101            }
1102            if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
1103                ar_flags |= SPL_ARRAY_IS_SELF;
1104                ZVAL_UNDEF(&intern->array);
1105            } else {
1106                ar_flags |= SPL_ARRAY_USE_OTHER;
1107                ZVAL_COPY(&intern->array, array);
1108            }
1109        } else {
1110            zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
1111            ZVAL_COPY(&intern->array, array);
1112            if (handler != std_object_handlers.get_properties
1113                || !spl_array_get_hash_table(intern, 0)) {
1114                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);
1115            }
1116        }
1117    }
1118
1119    intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
1120    intern->ar_flags |= ar_flags;
1121    intern->ht_iter = (uint32_t)-1;
1122}
1123/* }}} */
1124
1125/* iterator handler table */
1126zend_object_iterator_funcs spl_array_it_funcs = {
1127    spl_array_it_dtor,
1128    spl_array_it_valid,
1129    spl_array_it_get_current_data,
1130    spl_array_it_get_current_key,
1131    spl_array_it_move_forward,
1132    spl_array_it_rewind
1133};
1134
1135zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
1136{
1137    zend_user_iterator *iterator;
1138    spl_array_object *array_object = Z_SPLARRAY_P(object);
1139
1140    if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
1141        zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
1142    }
1143
1144    iterator = emalloc(sizeof(zend_user_iterator));
1145
1146    zend_iterator_init(&iterator->it);
1147
1148    ZVAL_COPY(&iterator->it.data, object);
1149    iterator->it.funcs = &spl_array_it_funcs;
1150    iterator->ce = ce;
1151    ZVAL_UNDEF(&iterator->value);
1152
1153    return &iterator->it;
1154}
1155/* }}} */
1156
1157/* {{{ proto void ArrayObject::__construct(array|object ar = array() [, int flags = 0 [, string iterator_class = "ArrayIterator"]])
1158       proto void ArrayIterator::__construct(array|object ar = array() [, int flags = 0])
1159   Constructs a new array iterator from a path. */
1160SPL_METHOD(Array, __construct)
1161{
1162    zval *object = getThis();
1163    spl_array_object *intern;
1164    zval *array;
1165    zend_long ar_flags = 0;
1166    zend_class_entry *ce_get_iterator = spl_ce_Iterator;
1167
1168    if (ZEND_NUM_ARGS() == 0) {
1169        return; /* nothing to do */
1170    }
1171
1172    if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|lC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
1173        return;
1174    }
1175
1176    intern = Z_SPLARRAY_P(object);
1177
1178    if (ZEND_NUM_ARGS() > 2) {
1179        intern->ce_get_iterator = ce_get_iterator;
1180    }
1181
1182    ar_flags &= ~SPL_ARRAY_INT_MASK;
1183
1184    spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
1185}
1186 /* }}} */
1187
1188/* {{{ proto void ArrayObject::setIteratorClass(string iterator_class)
1189   Set the class used in getIterator. */
1190SPL_METHOD(Array, setIteratorClass)
1191{
1192    zval *object = getThis();
1193    spl_array_object *intern = Z_SPLARRAY_P(object);
1194    zend_class_entry * ce_get_iterator = spl_ce_Iterator;
1195
1196#ifndef FAST_ZPP
1197    if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &ce_get_iterator) == FAILURE) {
1198        return;
1199    }
1200#else
1201    ZEND_PARSE_PARAMETERS_START(1, 1)
1202        Z_PARAM_CLASS(ce_get_iterator)
1203    ZEND_PARSE_PARAMETERS_END();
1204#endif
1205
1206    intern->ce_get_iterator = ce_get_iterator;
1207}
1208/* }}} */
1209
1210/* {{{ proto string ArrayObject::getIteratorClass()
1211   Get the class used in getIterator. */
1212SPL_METHOD(Array, getIteratorClass)
1213{
1214    zval *object = getThis();
1215    spl_array_object *intern = Z_SPLARRAY_P(object);
1216
1217    if (zend_parse_parameters_none() == FAILURE) {
1218        return;
1219    }
1220
1221    zend_string_addref(intern->ce_get_iterator->name);
1222    RETURN_STR(intern->ce_get_iterator->name);
1223}
1224/* }}} */
1225
1226/* {{{ proto int ArrayObject::getFlags()
1227   Get flags */
1228SPL_METHOD(Array, getFlags)
1229{
1230    zval *object = getThis();
1231    spl_array_object *intern = Z_SPLARRAY_P(object);
1232
1233    if (zend_parse_parameters_none() == FAILURE) {
1234        return;
1235    }
1236
1237    RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
1238}
1239/* }}} */
1240
1241/* {{{ proto void ArrayObject::setFlags(int flags)
1242   Set flags */
1243SPL_METHOD(Array, setFlags)
1244{
1245    zval *object = getThis();
1246    spl_array_object *intern = Z_SPLARRAY_P(object);
1247    zend_long ar_flags = 0;
1248
1249    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
1250        return;
1251    }
1252
1253    intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
1254}
1255/* }}} */
1256
1257/* {{{ proto Array|Object ArrayObject::exchangeArray(Array|Object ar = array())
1258   Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
1259SPL_METHOD(Array, exchangeArray)
1260{
1261    zval *object = getThis(), *array;
1262    spl_array_object *intern = Z_SPLARRAY_P(object);
1263
1264    if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &array) == FAILURE) {
1265        return;
1266    }
1267
1268    RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern, 0)));
1269    spl_array_set_array(object, intern, array, 0L, 1);
1270}
1271/* }}} */
1272
1273/* {{{ proto ArrayIterator ArrayObject::getIterator()
1274   Create a new iterator from a ArrayObject instance */
1275SPL_METHOD(Array, getIterator)
1276{
1277    zval *object = getThis();
1278    spl_array_object *intern = Z_SPLARRAY_P(object);
1279    HashTable *aht = spl_array_get_hash_table(intern, 0);
1280
1281    if (zend_parse_parameters_none() == FAILURE) {
1282        return;
1283    }
1284
1285    if (!aht) {
1286        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1287        return;
1288    }
1289
1290    ZVAL_OBJ(return_value, spl_array_object_new_ex(intern->ce_get_iterator, object, 0));
1291}
1292/* }}} */
1293
1294/* {{{ proto void ArrayIterator::rewind()
1295   Rewind array back to the start */
1296SPL_METHOD(Array, rewind)
1297{
1298    zval *object = getThis();
1299    spl_array_object *intern = Z_SPLARRAY_P(object);
1300
1301    if (zend_parse_parameters_none() == FAILURE) {
1302        return;
1303    }
1304
1305    spl_array_rewind(intern);
1306}
1307/* }}} */
1308
1309/* {{{ proto void ArrayIterator::seek(int $position)
1310   Seek to position. */
1311SPL_METHOD(Array, seek)
1312{
1313    zend_long opos, position;
1314    zval *object = getThis();
1315    spl_array_object *intern = Z_SPLARRAY_P(object);
1316    HashTable *aht = spl_array_get_hash_table(intern, 0);
1317    int result;
1318
1319    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
1320        return;
1321    }
1322
1323    if (!aht) {
1324        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1325        return;
1326    }
1327
1328    opos = position;
1329
1330    if (position >= 0) { /* negative values are not supported */
1331        spl_array_rewind(intern);
1332        result = SUCCESS;
1333
1334        while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
1335
1336        if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
1337            return; /* ok */
1338        }
1339    }
1340    zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position %pd is out of range", opos);
1341} /* }}} */
1342
1343int static spl_array_object_count_elements_helper(spl_array_object *intern, zend_long *count) /* {{{ */
1344{
1345    HashTable *aht = spl_array_get_hash_table(intern, 0);
1346    HashPosition pos, *pos_ptr;
1347
1348    if (!aht) {
1349        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1350        *count = 0;
1351        return FAILURE;
1352    }
1353
1354    if (spl_array_is_object(intern)) {
1355        /* We need to store the 'pos' since we'll modify it in the functions
1356         * we're going to call and which do not support 'pos' as parameter. */
1357        pos_ptr = spl_array_get_pos_ptr(aht, intern);
1358        pos = *pos_ptr;
1359        *count = 0;
1360        spl_array_rewind(intern);
1361        while (*pos_ptr != HT_INVALID_IDX && spl_array_next(intern) == SUCCESS) {
1362            (*count)++;
1363        }
1364        *pos_ptr = pos;
1365        return SUCCESS;
1366    } else {
1367        *count = zend_hash_num_elements(aht);
1368        return SUCCESS;
1369    }
1370} /* }}} */
1371
1372int spl_array_object_count_elements(zval *object, zend_long *count) /* {{{ */
1373{
1374    spl_array_object *intern = Z_SPLARRAY_P(object);
1375
1376    if (intern->fptr_count) {
1377        zval rv;
1378        zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
1379        if (Z_TYPE(rv) != IS_UNDEF) {
1380            *count = zval_get_long(&rv);
1381            zval_ptr_dtor(&rv);
1382            return SUCCESS;
1383        }
1384        *count = 0;
1385        return FAILURE;
1386    }
1387    return spl_array_object_count_elements_helper(intern, count);
1388} /* }}} */
1389
1390/* {{{ proto int ArrayObject::count()
1391       proto int ArrayIterator::count()
1392   Return the number of elements in the Iterator. */
1393SPL_METHOD(Array, count)
1394{
1395    zend_long count;
1396    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1397
1398    if (zend_parse_parameters_none() == FAILURE) {
1399        return;
1400    }
1401
1402    spl_array_object_count_elements_helper(intern, &count);
1403
1404    RETURN_LONG(count);
1405} /* }}} */
1406
1407static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) /* {{{ */
1408{
1409    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1410    HashTable *aht = spl_array_get_hash_table(intern, 0);
1411    zval function_name, params[2], *arg = NULL;
1412    uint32_t old_refcount;
1413
1414    ZVAL_STRINGL(&function_name, fname, fname_len);
1415
1416    /* A tricky way to pass "aht" by reference, reset refcount */
1417    //??? It may be not safe, if user comparison handler accesses "aht"
1418    old_refcount = GC_REFCOUNT(aht);
1419    GC_REFCOUNT(aht) = 1;
1420    ZVAL_NEW_EMPTY_REF(&params[0]);
1421    ZVAL_ARR(Z_REFVAL(params[0]), aht);
1422
1423    if (!use_arg) {
1424        aht->u.v.nApplyCount++;
1425        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, 1, params, 1, NULL);
1426        aht->u.v.nApplyCount--;
1427    } else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
1428        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
1429            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
1430            goto exit;
1431        }
1432        if (arg) {
1433            ZVAL_COPY_VALUE(&params[1], arg);
1434        }
1435        aht->u.v.nApplyCount++;
1436        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, arg ? 2 : 1, params, 1, NULL);
1437        aht->u.v.nApplyCount--;
1438    } else {
1439        if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
1440            zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
1441            goto exit;
1442        }
1443        ZVAL_COPY_VALUE(&params[1], arg);
1444        aht->u.v.nApplyCount++;
1445        call_user_function_ex(EG(function_table), NULL, &function_name, return_value, 2, params, 1, NULL);
1446        aht->u.v.nApplyCount--;
1447    }
1448
1449exit:
1450    /* A tricky way to pass "aht" by reference, copy back and cleanup */
1451    GC_REFCOUNT(aht) = old_refcount;
1452    efree(Z_REF(params[0]));
1453    zend_string_free(Z_STR(function_name));
1454} /* }}} */
1455
1456#define SPL_ARRAY_METHOD(cname, fname, use_arg) \
1457SPL_METHOD(cname, fname) \
1458{ \
1459    spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
1460}
1461
1462/* {{{ proto int ArrayObject::asort([int $sort_flags = SORT_REGULAR ])
1463       proto int ArrayIterator::asort([int $sort_flags = SORT_REGULAR ])
1464   Sort the entries by values. */
1465SPL_ARRAY_METHOD(Array, asort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1466
1467/* {{{ proto int ArrayObject::ksort([int $sort_flags = SORT_REGULAR ])
1468       proto int ArrayIterator::ksort([int $sort_flags = SORT_REGULAR ])
1469   Sort the entries by key. */
1470SPL_ARRAY_METHOD(Array, ksort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
1471
1472/* {{{ proto int ArrayObject::uasort(callback cmp_function)
1473       proto int ArrayIterator::uasort(callback cmp_function)
1474   Sort the entries by values user defined function. */
1475SPL_ARRAY_METHOD(Array, uasort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1476
1477/* {{{ proto int ArrayObject::uksort(callback cmp_function)
1478       proto int ArrayIterator::uksort(callback cmp_function)
1479   Sort the entries by key using user defined function. */
1480SPL_ARRAY_METHOD(Array, uksort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
1481
1482/* {{{ proto int ArrayObject::natsort()
1483       proto int ArrayIterator::natsort()
1484   Sort the entries by values using "natural order" algorithm. */
1485SPL_ARRAY_METHOD(Array, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1486
1487/* {{{ proto int ArrayObject::natcasesort()
1488       proto int ArrayIterator::natcasesort()
1489   Sort the entries by key using case insensitive "natural order" algorithm. */
1490SPL_ARRAY_METHOD(Array, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
1491
1492/* {{{ proto mixed|NULL ArrayIterator::current()
1493   Return current array entry */
1494SPL_METHOD(Array, current)
1495{
1496    zval *object = getThis();
1497    spl_array_object *intern = Z_SPLARRAY_P(object);
1498    zval *entry;
1499    HashTable *aht = spl_array_get_hash_table(intern, 0);
1500
1501    if (zend_parse_parameters_none() == FAILURE) {
1502        return;
1503    }
1504
1505    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1506        return;
1507    }
1508
1509    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1510        return;
1511    }
1512    if (Z_TYPE_P(entry) == IS_INDIRECT) {
1513        entry = Z_INDIRECT_P(entry);
1514        if (Z_TYPE_P(entry) == IS_UNDEF) {
1515            return;
1516        }
1517    }
1518    ZVAL_DEREF(entry);
1519    ZVAL_COPY(return_value, entry);
1520}
1521/* }}} */
1522
1523/* {{{ proto mixed|NULL ArrayIterator::key()
1524   Return current array key */
1525SPL_METHOD(Array, key)
1526{
1527    if (zend_parse_parameters_none() == FAILURE) {
1528        return;
1529    }
1530
1531    spl_array_iterator_key(getThis(), return_value);
1532} /* }}} */
1533
1534void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
1535{
1536    spl_array_object *intern = Z_SPLARRAY_P(object);
1537    HashTable *aht = spl_array_get_hash_table(intern, 0);
1538
1539    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1540        return;
1541    }
1542
1543    zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
1544}
1545/* }}} */
1546
1547/* {{{ proto void ArrayIterator::next()
1548   Move to next entry */
1549SPL_METHOD(Array, next)
1550{
1551    zval *object = getThis();
1552    spl_array_object *intern = Z_SPLARRAY_P(object);
1553    HashTable *aht = spl_array_get_hash_table(intern, 0);
1554
1555    if (zend_parse_parameters_none() == FAILURE) {
1556        return;
1557    }
1558
1559    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1560        return;
1561    }
1562
1563    spl_array_next_ex(intern, aht);
1564}
1565/* }}} */
1566
1567/* {{{ proto bool ArrayIterator::valid()
1568   Check whether array contains more entries */
1569SPL_METHOD(Array, valid)
1570{
1571    zval *object = getThis();
1572    spl_array_object *intern = Z_SPLARRAY_P(object);
1573    HashTable *aht = spl_array_get_hash_table(intern, 0);
1574
1575    if (zend_parse_parameters_none() == FAILURE) {
1576        return;
1577    }
1578
1579    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1580        RETURN_FALSE;
1581    } else {
1582        RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
1583    }
1584}
1585/* }}} */
1586
1587/* {{{ proto bool RecursiveArrayIterator::hasChildren()
1588   Check whether current element has children (e.g. is an array) */
1589SPL_METHOD(Array, hasChildren)
1590{
1591    zval *object = getThis(), *entry;
1592    spl_array_object *intern = Z_SPLARRAY_P(object);
1593    HashTable *aht = spl_array_get_hash_table(intern, 0);
1594
1595    if (zend_parse_parameters_none() == FAILURE) {
1596        return;
1597    }
1598
1599    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1600        RETURN_FALSE;
1601    }
1602
1603    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1604        RETURN_FALSE;
1605    }
1606
1607    RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
1608}
1609/* }}} */
1610
1611/* {{{ proto object RecursiveArrayIterator::getChildren()
1612   Create a sub iterator for the current element (same class as $this) */
1613SPL_METHOD(Array, getChildren)
1614{
1615    zval *object = getThis(), *entry, flags;
1616    spl_array_object *intern = Z_SPLARRAY_P(object);
1617    HashTable *aht = spl_array_get_hash_table(intern, 0);
1618
1619    if (zend_parse_parameters_none() == FAILURE) {
1620        return;
1621    }
1622
1623    if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
1624        return;
1625    }
1626
1627    if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
1628        return;
1629    }
1630
1631    if (Z_TYPE_P(entry) == IS_OBJECT) {
1632        if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
1633            return;
1634        }
1635        if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(getThis()))) {
1636            ZVAL_OBJ(return_value, Z_OBJ_P(entry));
1637            Z_ADDREF_P(return_value);
1638            return;
1639        }
1640    }
1641
1642    ZVAL_LONG(&flags, intern->ar_flags);
1643    spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), return_value, entry, &flags);
1644}
1645/* }}} */
1646
1647/* {{{ proto string ArrayObject::serialize()
1648   Serialize the object */
1649SPL_METHOD(Array, serialize)
1650{
1651    zval *object = getThis();
1652    spl_array_object *intern = Z_SPLARRAY_P(object);
1653    HashTable *aht = spl_array_get_hash_table(intern, 0);
1654    zval members, flags;
1655    php_serialize_data_t var_hash;
1656    smart_str buf = {0};
1657
1658    if (zend_parse_parameters_none() == FAILURE) {
1659        return;
1660    }
1661
1662    if (!aht) {
1663        php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
1664        return;
1665    }
1666
1667    PHP_VAR_SERIALIZE_INIT(var_hash);
1668
1669    ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
1670
1671    /* storage */
1672    smart_str_appendl(&buf, "x:", 2);
1673    //!!! php_var_serialize need to be modified
1674    php_var_serialize(&buf, &flags, &var_hash);
1675
1676    if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
1677        php_var_serialize(&buf, &intern->array, &var_hash);
1678        smart_str_appendc(&buf, ';');
1679    }
1680
1681    /* members */
1682    smart_str_appendl(&buf, "m:", 2);
1683    if (!intern->std.properties) {
1684        rebuild_object_properties(&intern->std);
1685    }
1686
1687    ZVAL_ARR(&members, zend_array_dup(intern->std.properties));
1688
1689    php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
1690
1691    zval_ptr_dtor(&members);
1692
1693    /* done */
1694    PHP_VAR_SERIALIZE_DESTROY(var_hash);
1695
1696    if (buf.s) {
1697        RETURN_NEW_STR(buf.s);
1698    }
1699
1700    RETURN_NULL();
1701} /* }}} */
1702
1703/* {{{ proto void ArrayObject::unserialize(string serialized)
1704 * unserialize the object
1705 */
1706SPL_METHOD(Array, unserialize)
1707{
1708    spl_array_object *intern = Z_SPLARRAY_P(getThis());
1709
1710    char *buf;
1711    size_t buf_len;
1712    const unsigned char *p, *s;
1713    php_unserialize_data_t var_hash;
1714    zval *members, *zflags;
1715    HashTable *aht;
1716    zend_long flags;
1717
1718    if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
1719        return;
1720    }
1721
1722    if (buf_len == 0) {
1723        return;
1724    }
1725
1726    aht = spl_array_get_hash_table(intern, 0);
1727    if (aht->u.v.nApplyCount > 0) {
1728        zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
1729        return;
1730    }
1731
1732    /* storage */
1733    s = p = (const unsigned char*)buf;
1734    PHP_VAR_UNSERIALIZE_INIT(var_hash);
1735
1736    if (*p!= 'x' || *++p != ':') {
1737        goto outexcept;
1738    }
1739    ++p;
1740
1741    zflags = var_tmp_var(&var_hash);
1742    if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
1743        goto outexcept;
1744    }
1745
1746    --p; /* for ';' */
1747    flags = Z_LVAL_P(zflags);
1748    /* flags needs to be verified and we also need to verify whether the next
1749     * thing we get is ';'. After that we require an 'm' or something else
1750     * where 'm' stands for members and anything else should be an array. If
1751     * neither 'a' or 'm' follows we have an error. */
1752
1753    if (*p != ';') {
1754        goto outexcept;
1755    }
1756    ++p;
1757
1758    if (*p!='m') {
1759        if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
1760            goto outexcept;
1761        }
1762        intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
1763        intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
1764        zval_ptr_dtor(&intern->array);
1765        if (!php_var_unserialize(&intern->array, &p, s + buf_len, &var_hash)) {
1766            goto outexcept;
1767        }
1768        var_push_dtor(&var_hash, &intern->array);
1769    }
1770    if (*p != ';') {
1771        goto outexcept;
1772    }
1773    ++p;
1774
1775    /* members */
1776    if (*p!= 'm' || *++p != ':') {
1777        goto outexcept;
1778    }
1779    ++p;
1780
1781    members = var_tmp_var(&var_hash);
1782    if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
1783        goto outexcept;
1784    }
1785
1786    /* copy members */
1787    object_properties_load(&intern->std, Z_ARRVAL_P(members));
1788
1789    /* done reading $serialized */
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 table */
1801ZEND_BEGIN_ARG_INFO_EX(arginfo_array___construct, 0, 0, 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