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