This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
1420 lines (1192 loc) · 43.5 KB
/
array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+------------------------------------------------------------------------+
| Zephir Language |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Zephir Team (http://www.zephir-lang.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Vladimir Kolesnikov <[email protected]> |
+------------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include "php_ext.h"
#include <ext/standard/php_array.h>
#include <Zend/zend_hash.h>
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/debug.h"
#include "kernel/array.h"
#include "kernel/operators.h"
#include "kernel/hash.h"
#include "kernel/backtrace.h"
#define ZEPHIR_MAX_ARRAY_LEVELS 16
/**
* @brief Fetches @a index if it exists from the array @a arr
* @param[out] fetched <code>&$arr[$index]</code>; @a fetched is modified only when the function returns 1
* @param arr Array
* @param index Index
* @return isset($arr[$index])
* @retval 0 Not exists, @a arr is not an array or @a index is of not supported type
* @retval 1 Exists
* @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer
* @note $arr[$index] is returned as is: no copying occurs, reference copunt is not updated
* @throw E_WARNING if @a offset is not a scalar
*/
int zephir_array_isset_fetch(zval **fetched, const zval *arr, zval *index, int readonly TSRMLS_DC) {
HashTable *h;
zval **val;
int result;
if (Z_TYPE_P(arr) != IS_ARRAY) {
*fetched = ZEPHIR_GLOBAL(global_null);
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 0;
}
h = Z_ARRVAL_P(arr);
switch (Z_TYPE_P(index)) {
case IS_NULL:
result = zephir_hash_find(h, SS(""), (void**)&val);
break;
case IS_DOUBLE:
result = zend_hash_index_find(h, (ulong)Z_DVAL_P(index), (void**)&val);
break;
case IS_LONG:
case IS_BOOL:
case IS_RESOURCE:
result = zend_hash_index_find(h, Z_LVAL_P(index), (void**)&val);
break;
case IS_STRING:
result = zend_symtable_find(h, (Z_STRLEN_P(index) ? Z_STRVAL_P(index) : ""), Z_STRLEN_P(index)+1, (void**)&val);
break;
default:
zend_error(E_WARNING, "Illegal offset type");
*fetched = ZEPHIR_GLOBAL(global_null);
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 0;
}
if (result == SUCCESS) {
*fetched = *val;
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 1;
}
*fetched = ZEPHIR_GLOBAL(global_null);
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 0;
}
int zephir_array_isset_quick_string_fetch(zval **fetched, zval *arr, char *index, uint index_length, unsigned long key, int readonly TSRMLS_DC) {
zval **zv;
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
if (zephir_hash_quick_find(Z_ARRVAL_P(arr), index, index_length, key, (void**) &zv) == SUCCESS) {
*fetched = *zv;
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 1;
}
}
*fetched = ZEPHIR_GLOBAL(global_null);
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 0;
}
int zephir_array_isset_string_fetch(zval **fetched, zval *arr, char *index, uint index_length, int readonly TSRMLS_DC) {
return zephir_array_isset_quick_string_fetch(fetched, arr, index, index_length, zend_inline_hash_func(index, index_length), readonly TSRMLS_CC);
}
int zephir_array_isset_long_fetch(zval **fetched, zval *arr, unsigned long index, int readonly TSRMLS_DC) {
zval **zv;
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
if (zend_hash_index_find(Z_ARRVAL_P(arr), index, (void**)&zv) == SUCCESS) {
*fetched = *zv;
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 1;
}
}
*fetched = ZEPHIR_GLOBAL(global_null);
if (!readonly) {
Z_ADDREF_P(*fetched);
}
return 0;
}
/**
* @brief Checks whether @a index exists in array @a arr
* @param arr Array
* @param index Index
* @return isset($arr[$index])
* @retval 0 Not exists, @a arr is not an array or @a index is of not supported type
* @retval 1 Exists
* @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer
* @throw E_WARNING if @a offset is not a scalar
*/
int ZEPHIR_FASTCALL zephir_array_isset(const zval *arr, zval *index) {
HashTable *h;
if (Z_TYPE_P(arr) != IS_ARRAY) {
return 0;
}
h = Z_ARRVAL_P(arr);
switch (Z_TYPE_P(index)) {
case IS_NULL:
return zephir_hash_exists(h, SS(""));
case IS_DOUBLE:
return zend_hash_index_exists(h, (ulong)Z_DVAL_P(index));
case IS_BOOL:
case IS_LONG:
case IS_RESOURCE:
return zend_hash_index_exists(h, Z_LVAL_P(index));
case IS_STRING:
return zend_symtable_exists(h, Z_STRVAL_P(index), Z_STRLEN_P(index)+1);
default:
zend_error(E_WARNING, "Illegal offset type");
return 0;
}
}
/**
* @brief Checks whether string @a index exists in array @a arr
* @param arr Array
* @param index Index
* @param index_length strlen(index)+1
* @return isset($arr[$index])
* @retval 0 Not exists, @a arr is not an array
* @retval 1 Exists
* @note The function is a wrapper around zephir_array_isset_quick_string()
* @see zephir_array_isset_quick_string()
*/
int ZEPHIR_FASTCALL zephir_array_isset_string(const zval *arr, const char *index, uint index_length) {
return zephir_array_isset_quick_string(arr, index, index_length, zend_inline_hash_func(index, index_length));
}
/**
* @brief Checks whether string @a index exists in array @a arr using a precomputed key @a key
* @param arr Array
* @param index Index
* @param index_length strlen(index)+1
* @param key Precomputed key
* @return isset($arr[$index])
* @retval 0 Not exists or @a arr is not an array
* @retval 1 Exists
*/
int ZEPHIR_FASTCALL zephir_array_isset_quick_string(const zval *arr, const char *index, uint index_length, unsigned long key) {
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
return zend_hash_quick_exists(Z_ARRVAL_P(arr), index, index_length, key);
}
return 0;
}
/**
* @brief Checks whether numeric @a index exists in array @a arr using a precomputed key @a key
* @param arr Array
* @param index Index
* @return isset($arr[$index])
* @retval 0 Not exists or @a arr is not an array
* @retval 1 Exists
*/
int ZEPHIR_FASTCALL zephir_array_isset_long(const zval *arr, unsigned long index) {
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
return zend_hash_index_exists(Z_ARRVAL_P(arr), index);
}
return 0;
}
/**
* @brief Unsets @a index from array @a arr
* @param[in,out] arr Array
* @param index Index
* @param flags Flags (@c PH_SEPARATE: separate array if its reference count is greater than 1; @c arr will contain the separated array)
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array or @a index is of not supported type
* @retval @c SUCCESS Success
* @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer
* @throw @c E_WARNING if @a offset is not a scalar
*/
int ZEPHIR_FASTCALL zephir_array_unset(zval **arr, zval *index, int flags) {
HashTable *ht;
if (Z_TYPE_PP(arr) != IS_ARRAY) {
return FAILURE;
}
if ((flags & PH_SEPARATE) == PH_SEPARATE) {
SEPARATE_ZVAL_IF_NOT_REF(arr);
}
ht = Z_ARRVAL_PP(arr);
switch (Z_TYPE_P(index)) {
case IS_NULL:
return (zend_hash_del(ht, "", 1) == SUCCESS);
case IS_DOUBLE:
return (zend_hash_index_del(ht, (ulong)Z_DVAL_P(index)) == SUCCESS);
case IS_LONG:
case IS_BOOL:
case IS_RESOURCE:
return (zend_hash_index_del(ht, Z_LVAL_P(index)) == SUCCESS);
case IS_STRING:
return (zend_symtable_del(ht, Z_STRVAL_P(index), Z_STRLEN_P(index)+1) == SUCCESS);
default:
zend_error(E_WARNING, "Illegal offset type");
return 0;
}
}
/**
* @brief Unsets string @a index from array @a arr
* @param[in,out] arr Array
* @param index Index
* @param index_length strlen(index)+1
* @param flags Flags (@c PH_SEPARATE: separate array if its reference count is greater than 1; @c arr will contain the separated array)
* @return Whether the operation succeeded
* @retval @c FAILURE Failure or @a arr is not an array
* @retval @c SUCCESS Success
*/
int ZEPHIR_FASTCALL zephir_array_unset_string(zval **arr, const char *index, uint index_length, int flags) {
if (Z_TYPE_PP(arr) != IS_ARRAY) {
return 0;
}
if ((flags & PH_SEPARATE) == PH_SEPARATE) {
SEPARATE_ZVAL_IF_NOT_REF(arr);
}
return zend_hash_del(Z_ARRVAL_PP(arr), index, index_length);
}
/**
* @brief Unsets numeric @a index from array @a arr
* @param[in,out] arr Array
* @param index Index
* @param flags Flags (@c PH_SEPARATE: separate array if its reference count is greater than 1; @c arr will contain the separated array)
* @return Whether the operation succeeded
* @retval @c FAILURE Failure or @a arr is not an array
* @retval @c SUCCESS Success
*/
int ZEPHIR_FASTCALL zephir_array_unset_long(zval **arr, unsigned long index, int flags) {
if (Z_TYPE_PP(arr) != IS_ARRAY) {
return 0;
}
if ((flags & PH_SEPARATE) == PH_SEPARATE) {
SEPARATE_ZVAL_IF_NOT_REF(arr);
}
return zend_hash_index_del(Z_ARRVAL_PP(arr), index);
}
/**
* @brief Pushes @a value onto the end of @a arr
* @param[in,out] arr Array
* @param[in,out] value Value to add; reference counter of @c *value will be incrememnted
* @param flags Flags (@c PH_SEPARATE: separate array if its reference count is greater than 1; @c arr will contain the separated array)
* @return Whether the operation succeeded
* @retval @c FAILURE Failure or @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a is not an array
*/
int zephir_array_append(zval **arr, zval *value, int flags ZEPHIR_DEBUG_PARAMS) {
if (Z_TYPE_PP(arr) != IS_ARRAY) {
zend_error(E_WARNING, "Cannot use a scalar value as an array in %s on line %d", file, line);
return FAILURE;
}
if ((flags & PH_SEPARATE) == PH_SEPARATE) {
SEPARATE_ZVAL_IF_NOT_REF(arr);
}
Z_ADDREF_P(value);
return add_next_index_zval(*arr, value);
}
/**
* @brief Appends a long integer @a value to @a arr
* @param[in,out] arr Array
* @param value Value
* @param separate Flags (@c PH_SEPARATE: separate array if its reference count is greater than 1; @c arr will contain the separated array)
* @return Whether the operation succeeded
* @retval @c FAILURE Failure or @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a is not an array
* @see zephir_array_append()
*
* Equivalent to <tt>$arr[] = $value</tt> in PHP, where @c $value is an integer.
*/
int zephir_array_append_long(zval **arr, long value, int separate) {
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
Z_SET_REFCOUNT_P(zvalue, 0);
ZVAL_LONG(zvalue, value);
return zephir_array_append(arr, zvalue, separate ZEPHIR_DEBUG_PARAMS_DUMMY);
}
/**
* @brief Appends a string @a value to @a arr
* @param[in,out] arr Array
* @param value Value
* @param value_length Length of the value (usually <tt>strlen(value)</tt>)
* @param separate Flags (@c PH_SEPARATE: separate array if its reference count is greater than 1; @c arr will contain the separated array)
* @return Whether the operation succeeded
* @retval @c FAILURE Failure or @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a is not an array
* @see zephir_array_append()
*
* Equivalent to <tt>$arr[] = $value</tt> in PHP, where @c $value is a string.
*/
int zephir_array_append_string(zval **arr, char *value, uint value_length, int separate) {
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
Z_SET_REFCOUNT_P(zvalue, 0);
ZVAL_STRINGL(zvalue, value, value_length, 1);
return zephir_array_append(arr, zvalue, separate ZEPHIR_DEBUG_PARAMS_DUMMY);
}
/**
* @brief Updates value in @a arr at position @a index with @a value
* @param[in,out] arr Array
* @param index Index
* @param[in,out] value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array or @a index is of not supported type
* @retval @c SUCCESS Success
* @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer
* @throw @c E_WARNING if @a offset is not a scalar or @c arr is not an array
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP.
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy; @c *value will be updated with the newly constructed value
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on @c **value
*/
int zephir_array_update_zval(zval **arr, zval *index, zval **value, int flags) {
HashTable *ht;
if (Z_TYPE_PP(arr) != IS_ARRAY) {
zend_error(E_WARNING, "Cannot use a scalar value as an array (2)");
return FAILURE;
}
if ((flags & PH_CTOR) == PH_CTOR) {
zval *new_zv;
Z_DELREF_PP(value);
ALLOC_ZVAL(new_zv);
INIT_PZVAL_COPY(new_zv, *value);
*value = new_zv;
zval_copy_ctor(new_zv);
}
if ((flags & PH_SEPARATE) == PH_SEPARATE) {
SEPARATE_ZVAL_IF_NOT_REF(arr);
}
if ((flags & PH_COPY) == PH_COPY) {
Z_ADDREF_PP(value);
}
ht = Z_ARRVAL_PP(arr);
switch (Z_TYPE_P(index)) {
case IS_NULL:
return zend_symtable_update(ht, "", 1, value, sizeof(zval*), NULL);
case IS_DOUBLE:
return zend_hash_index_update(ht, (ulong)Z_DVAL_P(index), value, sizeof(zval*), NULL);
case IS_LONG:
case IS_BOOL:
case IS_RESOURCE:
return zend_hash_index_update(ht, Z_LVAL_P(index), value, sizeof(zval*), NULL);
case IS_STRING:
return zend_symtable_update(ht, Z_STRVAL_P(index), Z_STRLEN_P(index)+1, value, sizeof(zval*), NULL);
default:
zend_error(E_WARNING, "Illegal offset type");
return FAILURE;
}
}
/**
* @brief Updates value in @a arr at position @a index with boolean @a value
* @param[in,out] arr Array
* @param index Index
* @param value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array or @a index is of not supported type
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
* @see zephir_array_update_zval()
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP, where @c $value is a boolean.
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on the internally constructed value
*
* Only @c PH_SEPARATE is meaningful with this function
*/
int zephir_array_update_zval_bool(zval **arr, zval *index, int value, int flags) {
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
ZVAL_BOOL(zvalue, value);
return zephir_array_update_zval(arr, index, &zvalue, flags);
}
/**
* @brief Updates value in @a arr at position @a index with boolean @a value
* @param[in,out] arr Array
* @param index Index
* @param value Value
* @param value_length Length of value (usually <tt>strlen(value)</tt>)
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array or @a index is of not supported type
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
* @see zephir_array_update_zval()
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP, where @c $value is a string.
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on the internally constructed value
*
* Only @c PH_SEPARATE is meaningful with this function
*/
int zephir_array_update_zval_string(zval **arr, zval *index, char *value, uint value_length, int flags) {
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
ZVAL_STRINGL(zvalue, value, value_length, 1);
return zephir_array_update_zval(arr, index, &zvalue, flags);
}
/**
* @brief Updates value in @a arr at position @a index with long integer @a value
* @param[in,out] arr Array
* @param index Index
* @param value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array or @a index is of not supported type
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
* @see zephir_array_update_zval()
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP, where @c $value is an integer.
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on the internally constructed value
*
* Only @c PH_SEPARATE is meaningful with this function.
*/
int zephir_array_update_zval_long(zval **arr, zval *index, long value, int flags) {
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
ZVAL_LONG(zvalue, value);
return zephir_array_update_zval(arr, index, &zvalue, flags);
}
/**
* @brief Updates value in @a arr at position @a index with @a value using the precomputed hash @a key
* @param[in,out] arr Array
* @param index Index
* @param index_length Length of the index, should include the trailing zero
* @param key Precomputed hash of @c value
* @param value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP.
*
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy; @c *value will be updated with the newly constructed value
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on @c **value
*/
int zephir_array_update_quick_string(zval **arr, const char *index, uint index_length, unsigned long key, zval **value, int flags){
if (Z_TYPE_PP(arr) != IS_ARRAY) {
zend_error(E_WARNING, "Cannot use a scalar value as an array (3)");
return FAILURE;
}
if ((flags & PH_CTOR) == PH_CTOR) {
zval *new_zv;
Z_DELREF_PP(value);
ALLOC_ZVAL(new_zv);
INIT_PZVAL_COPY(new_zv, *value);
*value = new_zv;
zval_copy_ctor(new_zv);
}
if ((flags & PH_SEPARATE) == PH_SEPARATE) {
SEPARATE_ZVAL_IF_NOT_REF(arr);
}
if ((flags & PH_COPY) == PH_COPY) {
Z_ADDREF_PP(value);
}
return zend_hash_quick_update(Z_ARRVAL_PP(arr), index, index_length, key, value, sizeof(zval *), NULL);
}
/**
* @brief Updates value in @a arr at position @a index with @a value
* @param[in,out] arr Array
* @param index Index
* @param index_length Length of the index, should include the trailing zero
* @param value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
* @see zephir_array_update_quick_string()
*
* The function is a wrapper over @c zephir_array_update_quick_string()
*
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy; @c *value will be updated with the newly constructed value
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on @c **value
*/
int zephir_array_update_string(zval **arr, const char *index, uint index_length, zval **value, int flags) {
return zephir_array_update_quick_string(arr, index, index_length + 1, zend_inline_hash_func(index, index_length + 1), value, flags);
}
/**
* @brief Updates value in @a arr at position @a index with boolean @a value
* @param[in,out] arr Array
* @param index Index
* @param index_length Length of the index, should include the trailing zero
* @param value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
* @see zephir_array_update_string()
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP, where @c $index is a string key and $value is a boolean.
*
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on the internally constructed value
*
* Only @c PH_SEPARATE is meaningful with this function.
*/
int zephir_array_update_string_bool(zval **arr, const char *index, uint index_length, int value, int flags){
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
ZVAL_BOOL(zvalue, value);
return zephir_array_update_string(arr, index, index_length, &zvalue, flags);
}
/**
* @brief Updates value in @a arr at position @a index with integer @a value
* @param[in,out] arr Array
* @param index Index
* @param index_length Length of the index, should include the trailing zero
* @param value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
* @see zephir_array_update_string()
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP, where @c $index is a string key and $value is an integer.
*
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on the internally constructed value
*
* Only @c PH_SEPARATE is meaningful with this function.
*/
int zephir_array_update_string_long(zval **arr, const char *index, uint index_length, long value, int flags){
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
ZVAL_LONG(zvalue, value);
return zephir_array_update_string(arr, index, index_length, &zvalue, flags);
}
/**
* @brief Updates value in @a arr at position @a index with string @a value
* @param[in,out] arr Array
* @param index Index
* @param index_length Length of the index, should include the trailing zero
* @param value Value
* @param value_length Length of the @a value; usually @c strlen()
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @a arr is not an array
* @see zephir_array_update_string()
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP, where @c $index is a string key and $value is a boolean.
*
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on the internally constructed value
*
* Only @c PH_SEPARATE is meaningful with this function.
*/
int zephir_array_update_string_string(zval **arr, const char *index, uint index_length, char *value, uint value_length, int flags){
zval *zvalue;
ALLOC_INIT_ZVAL(zvalue);
ZVAL_STRINGL(zvalue, value, value_length, 1);
return zephir_array_update_string(arr, index, index_length, &zvalue, flags);
}
/**
* @brief Updates value in @a arr at position @a index with @a value
* @param[in,out] arr Array
* @param index Index
* @param[in,out] value Value
* @param flags Flags
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @c arr is not an array
*
* Equivalent to <tt>$arr[$index] = $value</tt> in PHP where @c $index is an integer.
* Flags may be a bitwise OR of the following values:
* @arg @c PH_CTOR: create a copy of @a value and work with that copy; @c *value will be updated with the newly constructed value
* @arg @c PH_SEPARATE: separate @a arr if its reference count is greater than 1; @c *arr will contain the separated version
* @arg @c PH_COPY: increment the reference count on @c **value
*/
int zephir_array_update_long(zval **arr, unsigned long index, zval **value, int flags ZEPHIR_DEBUG_PARAMS){
if (Z_TYPE_PP(arr) != IS_ARRAY) {
zend_error(E_WARNING, "Cannot use a scalar value as an array in %s on line %d", file, line);
return FAILURE;
}
if ((flags & PH_CTOR) == PH_CTOR) {
zval *new_zv;
Z_DELREF_PP(value);
ALLOC_ZVAL(new_zv);
INIT_PZVAL_COPY(new_zv, *value);
*value = new_zv;
zval_copy_ctor(new_zv);
}
if ((flags & PH_SEPARATE) == PH_SEPARATE) {
SEPARATE_ZVAL_IF_NOT_REF(arr);
}
if ((flags & PH_COPY) == PH_COPY) {
Z_ADDREF_PP(value);
}
return zend_hash_index_update(Z_ARRVAL_PP(arr), index, value, sizeof(zval *), NULL);
}
/**
* @brief Reads an item from @a arr at position @a index and stores it to @a return_value
* @param return_value[out] Return value
* @param arr Array
* @param index Index
* @param silent 0 to suppress all warnings, @c PH_NOISY to enable
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @c arr is not an array and @c silent = @c PH_NOISY
* @throw @c E_WARNING if @c index is not of the supported type and @c silent = @c PH_NOISY
* @throw @c E_NOTICE if @c index does not exist and @c silent = @c PH_NOISY
* @warning @c *return_value should be either @c NULL (preferred) or point to not initialized memory; if @c *return_value points to a valid variable, mmemory leak is possible
* @note @c index will be handled as follows: @c NULL is treated as an empty string, @c double values are cast to @c integer, @c bool or @c resource are treated as @c integer
*/
int zephir_array_fetch(zval **return_value, zval *arr, zval *index, int flags ZEPHIR_DEBUG_PARAMS TSRMLS_DC){
zval **zv;
HashTable *ht;
int result;
ulong uidx = 0;
char *sidx = NULL;
if (Z_TYPE_P(arr) == IS_ARRAY) {
ht = Z_ARRVAL_P(arr);
switch (Z_TYPE_P(index)) {
case IS_NULL:
result = zephir_hash_find(ht, SS(""), (void**) &zv);
sidx = "";
break;
case IS_DOUBLE:
uidx = (ulong)Z_DVAL_P(index);
result = zend_hash_index_find(ht, uidx, (void**) &zv);
break;
case IS_LONG:
case IS_BOOL:
case IS_RESOURCE:
uidx = Z_LVAL_P(index);
result = zend_hash_index_find(ht, uidx, (void**) &zv);
break;
case IS_STRING:
sidx = Z_STRLEN_P(index) ? Z_STRVAL_P(index) : "";
result = zend_symtable_find(ht, Z_STRVAL_P(index), Z_STRLEN_P(index)+1, (void**) &zv);
break;
default:
if ((flags & PH_NOISY) == PH_NOISY) {
zend_error(E_WARNING, "Illegal offset type in %s on line %d", file, line);
}
result = FAILURE;
break;
}
if (result != FAILURE) {
*return_value = *zv;
if ((flags & PH_READONLY) != PH_READONLY) {
Z_ADDREF_PP(return_value);
}
return SUCCESS;
}
if ((flags & PH_NOISY) == PH_NOISY) {
if (sidx == NULL) {
zend_error(E_NOTICE, "Undefined index: %ld in %s on line %d", uidx, file, line);
} else {
zend_error(E_NOTICE, "Undefined index: %s in %s on line %d", sidx, file, line);
}
}
}
*return_value = ZEPHIR_GLOBAL(global_null);
if ((flags & PH_READONLY) != PH_READONLY) {
Z_ADDREF_PP(return_value);
}
return FAILURE;
}
/**
* @brief Reads an item from @a arr at position @a index using the precomputed hash @c key and stores it to @a return_value
* @param return_value[out] Return value
* @param arr Array
* @param index Index
* @param index Index length; must contain the trailing zero, if any
* @param key Precomputed hash of @c index
* @param silent 0 to suppress all warnings, @c PH_NOISY to enable
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @c arr is not an array and @c silent = @c PH_NOISY
* @throw @c E_NOTICE if @c index does not exist and @c silent = @c PH_NOISY
* @warning @c *return_value should be either @c NULL (preferred) or point to not initialized memory; if @c *return_value points to a valid variable, mmemory leak is possible
*/
int zephir_array_fetch_quick_string(zval **return_value, zval *arr, const char *index, uint index_length, unsigned long key, int flags ZEPHIR_DEBUG_PARAMS TSRMLS_DC){
zval **zv;
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
if (zephir_hash_quick_find(Z_ARRVAL_P(arr), index, index_length, key, (void**) &zv) == SUCCESS) {
*return_value = *zv;
if ((flags & PH_READONLY) != PH_READONLY) {
Z_ADDREF_PP(return_value);
}
return SUCCESS;
}
if ((flags & PH_NOISY) == PH_NOISY) {
zend_error(E_NOTICE, "Undefined index: %s", index);
}
} else {
if ((flags & PH_NOISY) == PH_NOISY) {
zend_error(E_NOTICE, "Cannot use a scalar value as an array in %s on line %d", file, line);
}
}
*return_value = ZEPHIR_GLOBAL(global_null);
if ((flags & PH_READONLY) != PH_READONLY) {
Z_ADDREF_PP(return_value);
}
return FAILURE;
}
/**
* @brief Reads an item from @a arr at position @a index and stores it to @a return_value
* @param return_value[out] Return value
* @param arr Array
* @param index Index
* @param index Index length; must contain the trailing zero, if any
* @param silent 0 to suppress all warnings, @c PH_NOISY to enable
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @c arr is not an array and @c silent = @c PH_NOISY
* @throw @c E_NOTICE if @c index does not exist and @c silent = @c PH_NOISY
* @warning @c *return_value should be either @c NULL (preferred) or point to not initialized memory; if @c *return_value points to a valid variable, mmemory leak is possible
* @see zephir_array_fetch_quick_string()
*
* The function is a wrapper over @c zephir_array_fetch_quick_string()
*/
int zephir_array_fetch_string(zval **return_value, zval *arr, const char *index, uint index_length, int flags ZEPHIR_DEBUG_PARAMS TSRMLS_DC){
return zephir_array_fetch_quick_string(return_value, arr, index, index_length + 1, zend_inline_hash_func(index, index_length + 1), flags, file, line TSRMLS_CC);
}
/**
* @brief Reads an item from @a arr at position @a index and stores it to @a return_value
* @param return_value[out] Return value
* @param arr Array
* @param index Index
* @param silent 0 to suppress all warnings, @c PH_NOISY to enable
* @return Whether the operation succeeded
* @retval @c FAILURE Failure, @a arr is not an array
* @retval @c SUCCESS Success
* @throw @c E_WARNING if @c arr is not an array and @c silent = @c PH_NOISY
* @throw @c E_NOTICE if @c index does not exist and @c silent = @c PH_NOISY
* @warning @c *return_value should be either @c NULL (preferred) or point to not initialized memory; if @c *return_value points to a valid variable, mmemory leak is possible
*/
int zephir_array_fetch_long(zval **return_value, zval *arr, unsigned long index, int flags ZEPHIR_DEBUG_PARAMS TSRMLS_DC){
zval **zv;
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
if (zend_hash_index_find(Z_ARRVAL_P(arr), index, (void**)&zv) == SUCCESS) {
*return_value = *zv;
if ((flags & PH_READONLY) != PH_READONLY) {
Z_ADDREF_PP(return_value);
}
return SUCCESS;
}
if ((flags & PH_NOISY) == PH_NOISY) {
zend_error(E_NOTICE, "Undefined index: %lu in %s on line %d", index, file, line);
}
}
else {
if ((flags & PH_NOISY) == PH_NOISY) {
zend_error(E_NOTICE, "Cannot use a scalar value as an array in %s on line %d", file, line);
}
}
*return_value = ZEPHIR_GLOBAL(global_null);
if ((flags & PH_READONLY) != PH_READONLY) {
Z_ADDREF_PP(return_value);
}
return FAILURE;
}
/**
* Appends every element of an array at the end of the left array
*/
void zephir_merge_append(zval *left, zval *values){
zval **tmp;
HashTable *arr_values;
HashPosition pos;
if (Z_TYPE_P(left) != IS_ARRAY) {
zend_error(E_NOTICE, "First parameter of zephir_merge_append must be an array");
return;
}
if (Z_TYPE_P(values) == IS_ARRAY) {
arr_values = Z_ARRVAL_P(values);
zend_hash_internal_pointer_reset_ex(arr_values, &pos);
while (zend_hash_get_current_data_ex(arr_values, (void **) &tmp, &pos) == SUCCESS) {
Z_ADDREF_PP(tmp);
add_next_index_zval(left, *tmp);
zend_hash_move_forward_ex(arr_values, &pos);
}
} else {