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
/
object.c
1887 lines (1516 loc) · 49.5 KB
/
object.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"
#ifdef PHP_WIN32
#include "php_string.h"
#endif
#include "php_ext.h"
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/object.h"
#include "kernel/exception.h"
#include "kernel/fcall.h"
#include "kernel/hash.h"
#include "kernel/array.h"
#include "kernel/operators.h"
/**
* Reads class constant from string name and returns its value
*/
int zephir_get_class_constant(zval *return_value, zend_class_entry *ce, char *constant_name, unsigned int constant_length TSRMLS_DC) {
zval **result_ptr;
if (zephir_hash_find(&ce->constants_table, constant_name, constant_length, (void **) &result_ptr) != SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Undefined class constant '%s::%s'", ce->name, constant_name);
return FAILURE;
}
ZVAL_ZVAL(return_value, *result_ptr, 1, 0);
return SUCCESS;
}
/**
* Check if class is instance of
*/
int zephir_instance_of(zval *result, const zval *object, const zend_class_entry *ce TSRMLS_DC) {
if (Z_TYPE_P(object) != IS_OBJECT) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "instanceof expects an object instance");
ZVAL_FALSE(result);
return FAILURE;
}
ZVAL_BOOL(result, instanceof_function(Z_OBJCE_P(object), ce TSRMLS_CC));
return SUCCESS;
}
int zephir_instance_of_ev(const zval *object, const zend_class_entry *ce TSRMLS_DC) {
if (Z_TYPE_P(object) != IS_OBJECT) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "instanceof expects an object instance");
return 0;
}
return instanceof_function(Z_OBJCE_P(object), ce TSRMLS_CC);
}
/**
* Check if an object is instance of a class
*/
int zephir_is_instance_of(zval *object, const char *class_name, unsigned int class_length TSRMLS_DC) {
zend_class_entry *ce, *temp_ce;
if (Z_TYPE_P(object) == IS_OBJECT) {
ce = Z_OBJCE_P(object);
if (ce->name_length == class_length) {
return !zend_binary_strcasecmp(ce->name, ce->name_length, class_name, class_length);
}
temp_ce = zend_fetch_class(class_name, class_length, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
if (temp_ce) {
return instanceof_function(ce, temp_ce TSRMLS_CC);
}
}
return 0;
}
/**
* Returns a class name into a zval result
*/
void zephir_get_class(zval *result, zval *object, int lower TSRMLS_DC) {
zend_class_entry *ce;
if (Z_TYPE_P(object) == IS_OBJECT) {
ce = Z_OBJCE_P(object);
Z_STRLEN_P(result) = ce->name_length;
Z_STRVAL_P(result) = (char *) emalloc(ce->name_length + 1);
memcpy(Z_STRVAL_P(result), ce->name, ce->name_length);
Z_STRVAL_P(result)[Z_STRLEN_P(result)] = 0;
Z_TYPE_P(result) = IS_STRING;
if (lower) {
zend_str_tolower(Z_STRVAL_P(result), Z_STRLEN_P(result));
}
} else {
ZVAL_NULL(result);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "zephir_get_class expects an object");
}
}
/**
* Returns a class name into a zval result
*/
void zephir_get_class_ns(zval *result, zval *object, int lower TSRMLS_DC) {
int found = 0;
zend_class_entry *ce;
unsigned int i, class_length;
const char *cursor, *class_name;
if (Z_TYPE_P(object) != IS_OBJECT) {
if (Z_TYPE_P(object) != IS_STRING) {
ZVAL_NULL(result);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "zephir_get_class_ns expects an object");
return;
}
}
if (Z_TYPE_P(object) == IS_OBJECT) {
ce = Z_OBJCE_P(object);
class_name = ce->name;
class_length = ce->name_length;
} else {
class_name = Z_STRVAL_P(object);
class_length = Z_STRLEN_P(object);
}
if (!class_length) {
ZVAL_NULL(result);
return;
}
i = class_length;
cursor = (char *) (class_name + class_length - 1);
while (i > 0) {
if ((*cursor) == '\\') {
found = 1;
break;
}
cursor--;
i--;
}
if (found) {
Z_STRLEN_P(result) = class_length - i;
Z_STRVAL_P(result) = (char *) emalloc(class_length - i + 1);
memcpy(Z_STRVAL_P(result), class_name + i, class_length - i);
Z_STRVAL_P(result)[Z_STRLEN_P(result)] = 0;
Z_TYPE_P(result) = IS_STRING;
} else {
ZVAL_STRINGL(result, class_name, class_length, 1);
}
if (lower) {
zend_str_tolower(Z_STRVAL_P(result), Z_STRLEN_P(result));
}
}
/**
* Returns a namespace from a class name
*/
void zephir_get_ns_class(zval *result, zval *object, int lower TSRMLS_DC) {
zend_class_entry *ce;
int found = 0;
unsigned int i, j, class_length;
const char *cursor, *class_name;
if (Z_TYPE_P(object) != IS_OBJECT) {
if (Z_TYPE_P(object) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "zephir_get_ns_class expects an object");
ZVAL_NULL(result);
return;
}
}
if (Z_TYPE_P(object) == IS_OBJECT) {
ce = Z_OBJCE_P(object);
class_name = ce->name;
class_length = ce->name_length;
} else {
class_name = Z_STRVAL_P(object);
class_length = Z_STRLEN_P(object);
}
if (!class_length) {
ZVAL_NULL(result);
return;
}
j = 0;
i = class_length;
cursor = (char *) (class_name + class_length - 1);
while (i > 0) {
if ((*cursor) == '\\') {
found = 1;
break;
}
cursor--;
i--;
j++;
}
if (j > 0) {
if (found) {
Z_STRLEN_P(result) = class_length - j - 1;
Z_STRVAL_P(result) = (char *) emalloc(class_length - j);
memcpy(Z_STRVAL_P(result), class_name, class_length - j - 1);
Z_STRVAL_P(result)[Z_STRLEN_P(result)] = 0;
Z_TYPE_P(result) = IS_STRING;
} else {
ZVAL_EMPTY_STRING(result);
}
if (lower) {
zend_str_tolower(Z_STRVAL_P(result), Z_STRLEN_P(result));
}
} else {
ZVAL_NULL(result);
}
}
/**
* Returns the called in class in the current scope
*/
void zephir_get_called_class(zval *return_value TSRMLS_DC) {
if (EG(called_scope)) {
RETURN_STRINGL(EG(called_scope)->name, EG(called_scope)->name_length, 1);
}
if (!EG(scope)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "zephir_get_called_class() called from outside a class");
}
}
/**
* Fetches a zend class entry from a zval value
*/
zend_class_entry *zephir_fetch_class(const zval *class_name TSRMLS_DC) {
if (Z_TYPE_P(class_name) == IS_STRING) {
return zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "class name must be a string");
return zend_fetch_class("stdclass", strlen("stdclass"), ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
}
zend_class_entry* zephir_fetch_self_class(TSRMLS_D) {
return zend_fetch_class(NULL, 0, ZEND_FETCH_CLASS_SELF TSRMLS_CC);
}
zend_class_entry* zephir_fetch_parent_class(TSRMLS_D) {
return zend_fetch_class(NULL, 0, ZEND_FETCH_CLASS_PARENT TSRMLS_CC);
}
zend_class_entry* zephir_fetch_static_class(TSRMLS_D) {
return zend_fetch_class(NULL, 0, ZEND_FETCH_CLASS_STATIC TSRMLS_CC);
}
/**
* Checks if a class exist
*/
int zephir_class_exists(const zval *class_name, int autoload TSRMLS_DC) {
zend_class_entry **ce;
if (Z_TYPE_P(class_name) == IS_STRING) {
if (zend_lookup_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), &ce TSRMLS_CC) == SUCCESS) {
#if PHP_VERSION_ID < 50400
return (((*ce)->ce_flags & ZEND_ACC_INTERFACE) == 0);
#else
return ((*ce)->ce_flags & (ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT - ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) == 0;
#endif
}
return 0;
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "class name must be a string");
return 0;
}
/**
* Checks if a interface exist
*/
int zephir_interface_exists(const zval *class_name, int autoload TSRMLS_DC) {
zend_class_entry **ce;
if (Z_TYPE_P(class_name) == IS_STRING) {
if (zend_lookup_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), &ce TSRMLS_CC) == SUCCESS) {
return (((*ce)->ce_flags & ZEND_ACC_INTERFACE) > 0);
}
return 0;
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "interface name must be a string");
return 0;
}
/**
* Clones an object from obj to destination
*/
int zephir_clone(zval *destination, zval *obj TSRMLS_DC) {
int status = SUCCESS;
zend_class_entry *ce;
zend_object_clone_obj_t clone_call;
if (Z_TYPE_P(obj) != IS_OBJECT) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "__clone method called on non-object");
status = FAILURE;
} else {
ce = Z_OBJCE_P(obj);
clone_call = Z_OBJ_HT_P(obj)->clone_obj;
if (!clone_call) {
if (ce) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Trying to clone an uncloneable object of class %s", ce->name);
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Trying to clone an uncloneable object");
}
status = FAILURE;
} else {
if (!EG(exception)) {
Z_OBJVAL_P(destination) = clone_call(obj TSRMLS_CC);
Z_TYPE_P(destination) = IS_OBJECT;
Z_SET_REFCOUNT_P(destination, 1);
Z_UNSET_ISREF_P(destination);
if (EG(exception)) {
zval_ptr_dtor(&destination);
}
}
}
}
return status;
}
/**
* Checks if property exists on object
*/
int zephir_isset_property_quick(zval *object, const char *property_name, unsigned int property_length, unsigned long hash TSRMLS_DC) {
if (Z_TYPE_P(object) == IS_OBJECT) {
if (likely(zephir_hash_quick_exists(&Z_OBJCE_P(object)->properties_info, property_name, property_length, hash))) {
return 1;
} else {
return zephir_hash_quick_exists(Z_OBJ_HT_P(object)->get_properties(object TSRMLS_CC), property_name, property_length, hash);
}
}
return 0;
}
/**
* Checks if property exists on object
*/
int zephir_isset_property(zval *object, const char *property_name, unsigned int property_length TSRMLS_DC) {
return zephir_isset_property_quick(object, property_name, property_length, zend_inline_hash_func(property_name, property_length) TSRMLS_CC);
}
/**
* Checks if string property exists on object
*/
int zephir_isset_property_zval(zval *object, const zval *property TSRMLS_DC) {
unsigned long hash;
if (Z_TYPE_P(object) == IS_OBJECT) {
if (Z_TYPE_P(property) == IS_STRING) {
hash = zend_inline_hash_func(Z_STRVAL_P(property), Z_STRLEN_P(property) + 1);
if (likely(zephir_hash_quick_exists(&Z_OBJCE_P(object)->properties_info, Z_STRVAL_P(property), Z_STRLEN_P(property) + 1, hash))) {
return 1;
} else {
return zephir_hash_quick_exists(Z_OBJ_HT_P(object)->get_properties(object TSRMLS_CC), Z_STRVAL_P(property), Z_STRLEN_P(property) + 1, hash);
}
}
}
return 0;
}
/*
* Lookup exact class where a property is defined (precomputed key)
*
*/
static inline zend_class_entry *zephir_lookup_class_ce_quick(zend_class_entry *ce, const char *property_name, zend_uint property_length, ulong hash TSRMLS_DC) {
zend_class_entry *original_ce = ce;
while (ce) {
if (zephir_hash_quick_exists(&ce->properties_info, property_name, property_length + 1, hash)) {
return ce;
}
ce = ce->parent;
}
return original_ce;
}
/**
* Lookup exact class where a property is defined
*
*/
static inline zend_class_entry *zephir_lookup_class_ce(zend_class_entry *ce, const char *property_name, unsigned int property_length TSRMLS_DC) {
return zephir_lookup_class_ce_quick(ce, property_name, property_length, zend_inline_hash_func(property_name, property_length + 1) TSRMLS_CC);
}
/**
* Reads a property from an object
*/
int zephir_read_property(zval **result, zval *object, const char *property_name, zend_uint property_length, int silent TSRMLS_DC) {
zval *property;
zend_class_entry *ce, *old_scope;
if (Z_TYPE_P(object) != IS_OBJECT) {
if (silent == PH_NOISY) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Trying to get property of non-object");
}
ALLOC_INIT_ZVAL(*result);
return FAILURE;
}
ce = Z_OBJCE_P(object);
if (ce->parent) {
ce = zephir_lookup_class_ce(ce, property_name, property_length TSRMLS_CC);
}
old_scope = EG(scope);
EG(scope) = ce;
if (!Z_OBJ_HT_P(object)->read_property) {
#if PHP_VERSION_ID < 50400
char *class_name;
#else
const char *class_name;
#endif
zend_uint class_name_len;
zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC);
zend_error(E_CORE_ERROR, "Property %s of class %s cannot be read", property_name, class_name);
}
MAKE_STD_ZVAL(property);
ZVAL_STRINGL(property, property_name, property_length, 0);
#if PHP_VERSION_ID < 50400
*result = Z_OBJ_HT_P(object)->read_property(object, property, silent ? BP_VAR_IS : BP_VAR_R TSRMLS_CC);
#else
*result = Z_OBJ_HT_P(object)->read_property(object, property, silent ? BP_VAR_IS : BP_VAR_R, 0 TSRMLS_CC);
#endif
Z_ADDREF_PP(result);
if (Z_REFCOUNT_P(property) > 1) {
ZVAL_STRINGL(property, property_name, property_length, 1);
} else {
ZVAL_NULL(property);
}
zval_ptr_dtor(&property);
EG(scope) = old_scope;
return SUCCESS;
}
zval* zephir_fetch_property_this_quick(zval *object, const char *property_name, zend_uint property_length, ulong key, int silent TSRMLS_DC) {
zval **zv = NULL;
zend_object *zobj;
zend_property_info *property_info;
zend_class_entry *ce, *old_scope;
if (likely(Z_TYPE_P(object) == IS_OBJECT)) {
ce = Z_OBJCE_P(object);
if (ce->parent) {
ce = zephir_lookup_class_ce_quick(ce, property_name, property_length, key TSRMLS_CC);
}
old_scope = EG(scope);
EG(scope) = ce;
zobj = zend_objects_get_address(object TSRMLS_CC);
if (zephir_hash_quick_find(&ce->properties_info, property_name, property_length + 1, key, (void **) &property_info) == SUCCESS) {
#if PHP_VERSION_ID < 50400
if (zephir_hash_quick_find(zobj->properties, property_info->name, property_info->name_length + 1, property_info->h, (void **) &zv) == SUCCESS) {
EG(scope) = old_scope;
return *zv;
}
#else
int flag;
if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) && property_info->offset >= 0) {
if (zobj->properties) {
zv = (zval**) zobj->properties_table[property_info->offset];
flag = (zv == NULL) ? 1 : 0;
} else {
zv = &zobj->properties_table[property_info->offset];
flag = (*zv == NULL) ? 1 : 0;
}
} else if (UNEXPECTED(!zobj->properties)) {
flag = 1;
} else if (UNEXPECTED(zephir_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &zv) == FAILURE)) {
flag = 2;
} else {
flag = 0;
}
if (unlikely(flag) && zobj->properties) {
if (
(flag == 2 || zephir_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &zv) == FAILURE)
&& zv && *zv
) {
flag = 0;
}
}
if (likely(!flag)) {
EG(scope) = old_scope;
return *zv;
}
#endif
}
EG(scope) = old_scope;
} else {
if (silent == PH_NOISY) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Trying to get property of non-object");
}
}
return NULL;
}
/**
* Returns an object's member
*/
int zephir_return_property_quick(zval *return_value, zval **return_value_ptr, zval *object, char *property_name, unsigned int property_length, unsigned long key TSRMLS_DC) {
zval **zv;
zend_object *zobj;
zend_property_info *property_info;
zend_class_entry *ce, *old_scope;
if (likely(Z_TYPE_P(object) == IS_OBJECT)) {
ce = Z_OBJCE_P(object);
if (ce->parent) {
ce = zephir_lookup_class_ce_quick(ce, property_name, property_length, key TSRMLS_CC);
}
old_scope = EG(scope);
EG(scope) = ce;
zobj = zend_objects_get_address(object TSRMLS_CC);
if (zephir_hash_quick_find(&ce->properties_info, property_name, property_length + 1, key, (void **) &property_info) == SUCCESS) {
#if PHP_VERSION_ID < 50400
if (zephir_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &zv) == SUCCESS) {
EG(scope) = old_scope;
if (return_value_ptr) {
zval_ptr_dtor(return_value_ptr);
Z_ADDREF_PP(zv);
*return_value_ptr = *zv;
}
else {
ZVAL_ZVAL(return_value, *zv, 1, 0);
}
return SUCCESS;
}
#else
int flag;
if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) && property_info->offset >= 0) {
if (zobj->properties) {
zv = (zval**) zobj->properties_table[property_info->offset];
flag = (zv == NULL) ? 1 : 0;
} else {
zv = &zobj->properties_table[property_info->offset];
flag = (*zv == NULL) ? 1 : 0;
}
} else if (UNEXPECTED(!zobj->properties)) {
flag = 1;
} else if (UNEXPECTED(zephir_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &zv) == FAILURE)) {
flag = 2;
} else {
flag = 0;
}
if (unlikely(flag) && zobj->properties) {
if (
(flag == 2 || zephir_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &zv) == FAILURE)
&& zv && *zv
) {
flag = 0;
}
}
if (likely(!flag)) {
EG(scope) = old_scope;
if (return_value_ptr) {
zval_ptr_dtor(return_value_ptr);
Z_ADDREF_PP(zv);
*return_value_ptr = *zv;
}
else {
ZVAL_ZVAL(return_value, *zv, 1, 0);
}
return SUCCESS;
}
#endif
}
EG(scope) = old_scope;
} else {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Trying to get property of non-object");
}
ZVAL_NULL(return_value);
return FAILURE;
}
/**
* Returns an object's member
*/
int zephir_return_property(zval *return_value, zval **return_value_ptr, zval *object, char *property_name, unsigned int property_length TSRMLS_DC) {
return zephir_return_property_quick(return_value, return_value_ptr, object, property_name, property_length, zend_inline_hash_func(property_name, property_length + 1) TSRMLS_CC);
}
/**
* Reads a property from an object
*/
int zephir_read_property_zval(zval **result, zval *object, zval *property, int flags TSRMLS_DC) {
if (unlikely(Z_TYPE_P(property) != IS_STRING)) {
if ((flags & PH_NOISY) == PH_NOISY) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot access empty property %d", Z_TYPE_P(property));
}
*result = ZEPHIR_GLOBAL(global_null);
Z_ADDREF_P(*result);
return FAILURE;
}
return zephir_read_property(result, object, Z_STRVAL_P(property), Z_STRLEN_P(property), flags TSRMLS_CC);
}
/**
* Checks whether obj is an object and updates property with long value
*/
int zephir_update_property_long(zval *object, char *property_name, unsigned int property_length, long value TSRMLS_DC) {
zval *v;
ALLOC_ZVAL(v);
Z_UNSET_ISREF_P(v);
Z_SET_REFCOUNT_P(v, 0);
ZVAL_LONG(v, value);
return zephir_update_property_zval(object, property_name, property_length, v TSRMLS_CC);
}
/**
* Checks whether obj is an object and updates property with string value
*/
int zephir_update_property_string(zval *object, char *property_name, unsigned int property_length, char *str, unsigned int str_length TSRMLS_DC) {
zval *value;
int res;
ALLOC_ZVAL(value);
Z_UNSET_ISREF_P(value);
Z_SET_REFCOUNT_P(value, 0);
ZVAL_STRINGL(value, str, str_length, 1);
res = zephir_update_property_zval(object, property_name, property_length, value TSRMLS_CC);
if (res == SUCCESS) {
return SUCCESS;
}
return FAILURE;
}
/**
* Checks whether obj is an object and updates property with bool value
*/
int zephir_update_property_bool(zval *object, char *property_name, unsigned int property_length, int value TSRMLS_DC) {
return zephir_update_property_zval(object, property_name, property_length, value ? ZEPHIR_GLOBAL(global_true) : ZEPHIR_GLOBAL(global_false) TSRMLS_CC);
}
/**
* Checks whether obj is an object and updates property with null value
*/
int zephir_update_property_null(zval *object, char *property_name, unsigned int property_length TSRMLS_DC) {
return zephir_update_property_zval(object, property_name, property_length, ZEPHIR_GLOBAL(global_null) TSRMLS_CC);
}
/**
* Checks whether obj is an object and updates property with another zval
*/
int zephir_update_property_zval(zval *object, const char *property_name, unsigned int property_length, zval *value TSRMLS_DC){
zend_class_entry *ce, *old_scope;
zval *property;
old_scope = EG(scope);
if (Z_TYPE_P(object) != IS_OBJECT) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to assign property of non-object");
return FAILURE;
}
ce = Z_OBJCE_P(object);
if (ce->parent) {
ce = zephir_lookup_class_ce(ce, property_name, property_length TSRMLS_CC);
}
EG(scope) = ce;
if (!Z_OBJ_HT_P(object)->write_property) {
#if PHP_VERSION_ID < 50400
char *class_name;
#else
const char *class_name;
#endif
zend_uint class_name_len;
zend_get_object_classname(object, &class_name, &class_name_len TSRMLS_CC);
zend_error(E_CORE_ERROR, "Property %s of class %s cannot be updated", property_name, class_name);
}
MAKE_STD_ZVAL(property);
ZVAL_STRINGL(property, property_name, property_length, 0);
#if PHP_VERSION_ID < 50400
Z_OBJ_HT_P(object)->write_property(object, property, value TSRMLS_CC);
#else
Z_OBJ_HT_P(object)->write_property(object, property, value, 0 TSRMLS_CC);
#endif
if (Z_REFCOUNT_P(property) > 1) {
ZVAL_STRINGL(property, property_name, property_length, 1);
} else {
ZVAL_NULL(property);
}
zval_ptr_dtor(&property);
EG(scope) = old_scope;
return SUCCESS;
}
/**
* Updates properties on this_ptr (quick)
* Variables must be defined in the class definition. This function ignores magic methods or dynamic properties
*/
int zephir_update_property_this_quick(zval *object, const char *property_name, zend_uint property_length, zval *value, ulong key TSRMLS_DC){
zend_class_entry *ce, *old_scope;
if (unlikely(Z_TYPE_P(object) != IS_OBJECT)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attempt to assign property of non-object");
return FAILURE;
}
ce = Z_OBJCE_P(object);
if (ce->parent) {
ce = zephir_lookup_class_ce_quick(ce, property_name, property_length, key TSRMLS_CC);
}
old_scope = EG(scope);
EG(scope) = ce;
#if PHP_VERSION_ID < 50400
{
zval *property;
if (!Z_OBJ_HT_P(object)->write_property) {
EG(scope) = old_scope;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Property %s of class %s cannot be updated", property_name, ce->name);
return FAILURE;
}
MAKE_STD_ZVAL(property);
ZVAL_STRINGL(property, property_name, property_length, 0);
Z_OBJ_HT_P(object)->write_property(object, property, value TSRMLS_CC);
if (Z_REFCOUNT_P(property) > 1) {
ZVAL_STRINGL(property, property_name, property_length, 1);
} else {
ZVAL_NULL(property);
}
zval_ptr_dtor(&property);
}
#else
{
zend_object *zobj;
zval **variable_ptr;
zend_property_info *property_info;
zobj = zend_objects_get_address(object TSRMLS_CC);
if (zephir_hash_quick_find(&ce->properties_info, property_name, property_length + 1, key, (void **) &property_info) == SUCCESS) {
assert(property_info != NULL);
/** This is as zend_std_write_property, but we're not interesed in validate properties visibility */
if (property_info->offset >= 0 ? (zobj->properties ? ((variable_ptr = (zval**) zobj->properties_table[property_info->offset]) != NULL) : (*(variable_ptr = &zobj->properties_table[property_info->offset]) != NULL)) : (EXPECTED(zobj->properties != NULL) && EXPECTED(zephir_hash_quick_find(zobj->properties, property_info->name, property_info->name_length + 1, property_info->h, (void **) &variable_ptr) == SUCCESS))) {
if (EXPECTED(*variable_ptr != value)) {
/* if we are assigning reference, we shouldn't move it, but instead assign variable to the same pointer */
if (PZVAL_IS_REF(*variable_ptr)) {
zval garbage = **variable_ptr; /* old value should be destroyed */
/* To check: can't *variable_ptr be some system variable like error_zval here? */
Z_TYPE_PP(variable_ptr) = Z_TYPE_P(value);
(*variable_ptr)->value = value->value;
if (Z_REFCOUNT_P(value) > 0) {
zval_copy_ctor(*variable_ptr);
} else {
efree(value);
}
zval_dtor(&garbage);
} else {
zval *garbage = *variable_ptr;
/* if we assign referenced variable, we should separate it */
Z_ADDREF_P(value);
if (PZVAL_IS_REF(value)) {
SEPARATE_ZVAL(&value);
}
*variable_ptr = value;
zval_ptr_dtor(&garbage);
}
}
}
}
}
#endif
EG(scope) = old_scope;
return SUCCESS;
}
/**
* Updates properties on this_ptr
* Variables must be defined in the class definition. This function ignores magic methods or dynamic properties
*/
int zephir_update_property_this(zval *object, char *property_name, unsigned int property_length, zval *value TSRMLS_DC){
return zephir_update_property_this_quick(object, property_name, property_length, value, zend_inline_hash_func(property_name, property_length + 1) TSRMLS_CC);
}
/**
* Checks whether obj is an object and updates zval property with another zval
*/
int zephir_update_property_zval_zval(zval *object, zval *property, zval *value TSRMLS_DC){
if (Z_TYPE_P(property) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Property should be string");
return FAILURE;
}
return zephir_update_property_zval(object, Z_STRVAL_P(property), Z_STRLEN_P(property), value TSRMLS_CC);
}
/**
* Updates an array property
*/
int zephir_update_property_array(zval *object, const char *property, zend_uint property_length, const zval *index, zval *value TSRMLS_DC) {
zval *tmp;
int separated = 0;
if (Z_TYPE_P(object) == IS_OBJECT) {
zephir_read_property(&tmp, object, property, property_length, PH_NOISY TSRMLS_CC);
Z_DELREF_P(tmp);
/** Separation only when refcount > 1 */
if (Z_REFCOUNT_P(tmp) > 1) {
zval *new_zv;
ALLOC_ZVAL(new_zv);
INIT_PZVAL_COPY(new_zv, tmp);
tmp = new_zv;
zval_copy_ctor(new_zv);
Z_SET_REFCOUNT_P(tmp, 0);
separated = 1;
}
/** Convert the value to array if not is an array */
if (Z_TYPE_P(tmp) != IS_ARRAY) {
if (separated) {
convert_to_array(tmp);
} else {
zval *new_zv;
ALLOC_ZVAL(new_zv);
INIT_PZVAL_COPY(new_zv, tmp);
tmp = new_zv;
zval_copy_ctor(new_zv);
Z_SET_REFCOUNT_P(tmp, 0);
array_init(tmp);
separated = 1;
}
}
Z_ADDREF_P(value);
if (Z_TYPE_P(index) == IS_STRING) {
zend_symtable_update(Z_ARRVAL_P(tmp), Z_STRVAL_P(index), Z_STRLEN_P(index) + 1, &value, sizeof(zval*), NULL);
} else if (Z_TYPE_P(index) == IS_LONG) {
zend_hash_index_update(Z_ARRVAL_P(tmp), Z_LVAL_P(index), &value, sizeof(zval *), NULL);
} else if (Z_TYPE_P(index) == IS_NULL) {
zend_hash_next_index_insert(Z_ARRVAL_P(tmp), (void**)&value, sizeof(zval*), NULL);
}
if (separated) {
zephir_update_property_zval(object, property, property_length, tmp TSRMLS_CC);
}
}
return SUCCESS;
}
/**
* Multiple array-offset update