refactor for PHP-7.2
[m6w6/ext-propro] / src / php_propro_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: propro |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013 Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17
18 #include <php.h>
19 #include <ext/standard/info.h>
20
21 #include "php_propro_api.h"
22
23 #define DEBUG_PROPRO 0
24
25 static inline php_property_proxy_object_t *get_propro(zval *object);
26 static inline zval *get_proxied_value(zval *object, zval *return_value);
27 static inline void set_proxied_value(zval *object, zval *value);
28
29 static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value);
30 static ZEND_RESULT_CODE cast_obj(zval *object, zval *return_value, int type);
31 static void write_dimension(zval *object, zval *offset, zval *input_value);
32
33 #if DEBUG_PROPRO
34 /* we do not really care about TS when debugging */
35 static int level = 1;
36 static const char *inoutstr[] = {"< return","= "," > enter "};
37 static const char *types[] = {
38 "UNDEF",
39 "NULL",
40 "FALSE",
41 "TRUE",
42 "int",
43 "float",
44 "string",
45 "Array",
46 "Object",
47 "resource",
48 "reference",
49 "constant",
50 "const AST",
51 "_BOOL",
52 "callable",
53 "indirect",
54 "---",
55 "pointer"
56 };
57
58 static const char *_type(zval *zv)
59 {
60 switch (Z_TYPE_P(zv)) {
61 case IS_OBJECT:
62 if (zv->value.obj->ce == php_property_proxy_get_class_entry()) {
63 return "PROPRO";
64 }
65 /* no break */
66 default:
67 return types[Z_TYPE_P(zv)];
68 }
69 }
70
71 static int _walk(php_property_proxy_object_t *obj)
72 {
73 int p = 0;
74
75 if (obj) {
76 if (!Z_ISUNDEF(obj->parent)) {
77 p += _walk(get_propro(&obj->parent));
78 }
79 if (obj->proxy) {
80 p += fprintf(stderr, ".%s", obj->proxy->member->val);
81 }
82 }
83
84 return p;
85 }
86
87 static inline zval *get_container(zval *object, zval *tmp);
88 static inline zval *get_container_value(zval *container, zend_string *member, zval *return_value);
89
90 static void debug_propro(int inout, const char *f,
91 php_property_proxy_object_t *obj,
92 php_property_proxy_t *proxy,
93 zval *offset, zval *value)
94 {
95 int width;
96 zval tmp, *container = &tmp;
97
98 if (!proxy && obj) {
99 proxy = obj->proxy;
100 }
101
102 fprintf(stderr, "#PP %14p %*c %s %s\t", proxy, level, ' ', inoutstr[inout + 1], f);
103
104 level += inout;
105
106 if (proxy) {
107 ZVAL_UNDEF(&tmp);
108 if (obj) {
109 if (Z_ISUNDEF(obj->parent)) {
110 container = &obj->proxy->container;
111 } else {
112 zval parent_tmp, *parent_container;
113 php_property_proxy_object_t *parent_obj = get_propro(&obj->parent);
114
115 ZVAL_UNDEF(&parent_tmp);
116 parent_container = get_container(&obj->parent, &parent_tmp);
117 container = get_container_value(parent_container, parent_obj->proxy->member, &tmp);
118 }
119 }
120 fprintf(stderr, " container= %-14p < %-10s rc=%-11d ",
121 Z_REFCOUNTED_P(container) ? Z_COUNTED_P(container) : NULL,
122 _type(container),
123 Z_REFCOUNTED_P(container) ? Z_REFCOUNT_P(container) : 0);
124 if (Z_ISREF_P(container)) {
125 zval *ref = Z_REFVAL_P(container);
126 fprintf(stderr, " %-12s %p rc=% 2d",
127 _type(ref),
128 ref->value.counted,
129 Z_REFCOUNTED_P(ref) ? Z_REFCOUNT_P(ref) : -1);
130 }
131 fprintf(stderr, "> ");
132 }
133
134 width = _walk(obj);
135
136 if (*f++=='d'
137 && *f++=='i'
138 && *f++=='m'
139 ) {
140 char *offset_str = "[]";
141 zval *o = offset;
142
143 if (o) {
144 convert_to_string_ex(o);
145 offset_str = Z_STRVAL_P(o);
146 }
147
148 width += fprintf(stderr, ".%s", offset_str);
149
150 if (o && o != offset) {
151 zval_ptr_dtor(o);
152 }
153 }
154 if (value) {
155 fprintf(stderr, "%.*s", 32-width, " ");
156 if (Z_ISUNDEF_P(value)) {
157 fprintf(stderr, " = UNDEF");
158 } else {
159 fprintf(stderr, " = %-14p < %-10s rc=%-11d %3s> ",
160 Z_REFCOUNTED_P(value) ? Z_COUNTED_P(value) : NULL,
161 _type(value),
162 Z_REFCOUNTED_P(value) ? Z_REFCOUNT_P(value) : 0,
163 Z_ISREF_P(value) ? "ref" : "");
164 if (!Z_ISUNDEF_P(value) && Z_TYPE_P(value) != IS_INDIRECT) {
165 zend_print_flat_zval_r(value TSRMLS_CC);
166 }
167 }
168 }
169
170 fprintf(stderr, "\n");
171 }
172 #else
173 #define debug_propro(l, f, obj, proxy, off, val)
174 #endif
175
176 php_property_proxy_t *php_property_proxy_init(zval *container, zend_string *member)
177 {
178 php_property_proxy_t *proxy = ecalloc(1, sizeof(*proxy));
179 #if DEBUG_PROPRO
180 zval offset;
181
182 ZVAL_STR_COPY(&offset, member);
183 #endif
184
185 debug_propro(1, "init", NULL, proxy, &offset, NULL);
186
187 if (container) {
188 ZVAL_COPY(&proxy->container, container);
189 }
190 proxy->member = zend_string_copy(member);
191
192 debug_propro(-1, "init", NULL, proxy, &offset, NULL);
193
194 #if DEBUG_PROPRO
195 zval_dtor(&offset);
196 #endif
197
198 return proxy;
199 }
200
201 void php_property_proxy_free(php_property_proxy_t **proxy)
202 {
203 #if DEBUG_PROPRO
204 zval offset;
205
206 ZVAL_STR_COPY(&offset, (*proxy)->member);
207 debug_propro(1, "dtor", NULL, *proxy, &offset, NULL);
208 #endif
209
210 if (*proxy) {
211 zval_ptr_dtor(&(*proxy)->container);
212
213 ZVAL_UNDEF(&(*proxy)->container);
214 zend_string_release((*proxy)->member);
215 (*proxy)->member = NULL;
216 efree(*proxy);
217 *proxy = NULL;
218 }
219
220 #if DEBUG_PROPRO
221 debug_propro(-1, "dtor", NULL, NULL, &offset, NULL);
222
223 zval_dtor(&offset);
224 #endif
225 }
226
227 static zend_class_entry *php_property_proxy_class_entry;
228 static zend_object_handlers php_property_proxy_object_handlers;
229
230 zend_class_entry *php_property_proxy_get_class_entry(void)
231 {
232 return php_property_proxy_class_entry;
233 }
234
235 php_property_proxy_object_t *php_property_proxy_object_new_ex(
236 zend_class_entry *ce, php_property_proxy_t *proxy)
237 {
238 php_property_proxy_object_t *o;
239
240 if (!ce) {
241 ce = php_property_proxy_class_entry;
242 }
243
244 o = ecalloc(1, sizeof(*o) + sizeof(zval) * (ce->default_properties_count - 1));
245 zend_object_std_init(&o->zo, ce);
246 object_properties_init(&o->zo, ce);
247
248 o->proxy = proxy;
249 o->zo.handlers = &php_property_proxy_object_handlers;
250
251 return o;
252 }
253
254 zend_object *php_property_proxy_object_new(zend_class_entry *ce)
255 {
256 return &php_property_proxy_object_new_ex(ce, NULL)->zo;
257 }
258
259 static void destroy_obj(zend_object *object)
260 {
261 php_property_proxy_object_t *o = PHP_PROPRO_PTR(object);
262
263 if (o->proxy) {
264 php_property_proxy_free(&o->proxy);
265 }
266 if (!Z_ISUNDEF(o->parent)) {
267 zval_ptr_dtor(&o->parent);
268 ZVAL_UNDEF(&o->parent);
269 }
270 zend_object_std_dtor(object);
271 }
272
273 static ZEND_RESULT_CODE cast_obj(zval *object, zval *return_value, int type)
274 {
275 zval tmp;
276
277 ZVAL_UNDEF(&tmp);
278 RETVAL_ZVAL(get_proxied_value(object, &tmp), 1, 0);
279
280 debug_propro(0, "cast", get_propro(object), NULL, NULL, return_value);
281
282 if (!Z_ISUNDEF_P(return_value)) {
283 ZVAL_DEREF(return_value);
284 convert_to_explicit_type_ex(return_value, type);
285 return SUCCESS;
286 }
287
288 return FAILURE;
289 }
290
291 static zval *get_obj(zval *object, zval *return_value)
292 {
293 zval tmp;
294
295 ZVAL_UNDEF(&tmp);
296 RETVAL_ZVAL(get_proxied_value(object, &tmp), 1, 0);
297 return return_value;
298 }
299
300 static void set_obj(zval *object, zval *value) {
301 set_proxied_value(object, value);
302 }
303
304 static inline php_property_proxy_object_t *get_propro(zval *object)
305 {
306 ZEND_ASSERT(Z_TYPE_P(object) == IS_OBJECT);
307 return PHP_PROPRO_PTR(Z_OBJ_P(object));
308 }
309
310 static inline zval *get_container(zval *object, zval *tmp)
311 {
312 php_property_proxy_object_t *obj = get_propro(object);
313 zval *container;
314
315 if (Z_ISUNDEF(obj->parent)) {
316 container = &obj->proxy->container;
317 } else {
318 container = get_proxied_value(&obj->parent, tmp);
319 }
320
321 return container;
322 }
323
324 static inline void set_container(zval *object, zval *container)
325 {
326 php_property_proxy_object_t *obj = get_propro(object);
327
328 if (Z_ISUNDEF(obj->parent)) {
329 zval tmp;
330
331 ZVAL_COPY_VALUE(&tmp, &obj->proxy->container);
332 ZVAL_COPY(&obj->proxy->container, container);
333 zval_ptr_dtor(&tmp);
334 } else {
335 set_proxied_value(&obj->parent, container);
336 }
337 }
338
339 static inline zval *get_container_value(zval *container, zend_string *member, zval *return_value)
340 {
341 zval *found_value = NULL, prop_tmp;
342
343 ZVAL_DEREF(container);
344 switch (Z_TYPE_P(container)) {
345 case IS_OBJECT:
346 found_value = zend_read_property(Z_OBJCE_P(container), container,
347 member->val, member->len, 0, &prop_tmp);
348
349 break;
350
351 case IS_ARRAY:
352 found_value = zend_symtable_find(Z_ARRVAL_P(container), member);
353 break;
354 }
355
356 if (found_value) {
357 RETVAL_ZVAL(found_value, 0, 0);
358 }
359
360 return return_value;
361 }
362
363 static inline void cleanup_container(zval *container, zend_bool separated)
364 {
365 #if DEBUG_PROPRO > 1
366 fprintf(stderr, ">> cleanup(%s) %p rc=%d\n",
367 separated || (Z_REFCOUNTED_P(container) && !Z_REFCOUNT_P(container)) ? "yes" : "no",
368 Z_REFCOUNTED_P(container) ? Z_COUNTED_P(container) : container,
369 Z_REFCOUNTED_P(container) ? Z_REFCOUNT_P(container) : -1);
370 #endif
371 if (separated) {
372 zval_ptr_dtor(container);
373 }
374 }
375
376 static inline zend_bool separate_container(zval *container)
377 {
378 #if DEBUG_PROPRO > 1
379 fprintf(stderr, ">> separate is_ref=%d rc=%d\n", Z_ISREF_P(container),
380 Z_REFCOUNTED_P(container) ? Z_REFCOUNT_P(container) : -1);
381 #endif
382
383 switch (Z_TYPE_P(container)) {
384 case IS_OBJECT:
385 break;
386
387 case IS_ARRAY:
388 if (Z_REFCOUNT_P(container) > 1) {
389 SEPARATE_ZVAL(container);
390 #if DEBUG_PROPRO > 1
391 fprintf(stderr, ">> array_dup %p\n", Z_COUNTED_P(container));
392 #endif
393 return 1;
394 }
395 break;
396
397 case IS_UNDEF:
398 array_init(container);
399 #if DEBUG_PROPRO > 1
400 fprintf(stderr, ">> created %p\n", Z_COUNTED_P(container));
401 #endif
402 return 1;
403
404 default:
405 SEPARATE_ZVAL(container);
406 Z_TRY_ADDREF_P(container);
407 convert_to_array(container);
408 #if DEBUG_PROPRO > 1
409 fprintf(stderr, ">> converted %p\n", Z_COUNTED_P(container));
410 #endif
411 return 1;
412 }
413
414 #if DEBUG_PROPRO > 1
415 fprintf(stderr, ">> nothing %p\n", Z_COUNTED_P(container));
416 #endif
417 return 0;
418 }
419
420 static inline zval *set_container_value(zval *container, zend_string *member, zval *value)
421 {
422 ZVAL_DEREF(container);
423 switch (Z_TYPE_P(container)) {
424 case IS_OBJECT:
425 zend_update_property(Z_OBJCE_P(container), container,
426 member->val, member->len, value);
427 break;
428
429 case IS_ARRAY:
430 Z_TRY_ADDREF_P(value);
431 if (member) {
432 value = zend_symtable_update(Z_ARRVAL_P(container), member, value);
433 } else {
434 value = zend_hash_next_index_insert(Z_ARRVAL_P(container), value);
435 }
436 break;
437
438 default:
439 ZEND_ASSERT(0);
440 break;
441 }
442
443 return value;
444 }
445
446
447 static zval *get_proxied_value(zval *object, zval *return_value)
448 {
449 php_property_proxy_object_t *obj = get_propro(object);
450
451 debug_propro(1, "get", obj, NULL, NULL, NULL);
452
453 if (obj->proxy) {
454 zval tmp, *container;
455
456 ZVAL_UNDEF(&tmp);
457 container = get_container(object, &tmp);
458
459 return_value = get_container_value(container, obj->proxy->member, return_value);
460 }
461
462 debug_propro(-1, "get", obj, NULL, NULL, return_value);
463
464 return return_value;
465 }
466
467 static void set_proxied_value(zval *object, zval *value)
468 {
469 php_property_proxy_object_t *obj = get_propro(object);
470
471 debug_propro(1, "set", obj, NULL, NULL, value);
472
473 if (obj->proxy) {
474 zval tmp, *container;
475 zend_bool separated;
476
477 Z_TRY_ADDREF_P(value);
478
479 ZVAL_UNDEF(&tmp);
480 container = get_container(object, &tmp);
481 separated = separate_container(container);
482 set_container_value(container, obj->proxy->member, value);
483 set_container(object, container);
484 cleanup_container(container, separated);
485
486 Z_TRY_DELREF_P(value);
487
488 debug_propro(0, "set", obj, NULL, NULL, value);
489 }
490
491 debug_propro(-1, "set", obj, NULL, NULL, value);
492 }
493
494 static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value)
495 {
496 zval *value, tmp;
497 zend_string *member = offset ? zval_get_string(offset) : NULL;
498
499 debug_propro(1, type == BP_VAR_R ? "dim_r" : "dim_R",
500 get_propro(object), NULL, offset, NULL);
501
502 ZVAL_UNDEF(&tmp);
503 value = get_proxied_value(object, &tmp);
504
505 if (type == BP_VAR_R || type == BP_VAR_IS) {
506 ZEND_ASSERT(member);
507
508 if (!Z_ISUNDEF_P(value)) {
509 zval tmp;
510
511 ZVAL_UNDEF(&tmp);
512 RETVAL_ZVAL(get_container_value(value, member, &tmp), 1, 0);
513 }
514 } else {
515 php_property_proxy_t *proxy;
516 php_property_proxy_object_t *proxy_obj;
517
518 if (Z_ISUNDEF_P(value)) {
519 ZVAL_NULL(value);
520 }
521
522 if (!member) {
523 if (Z_TYPE_P(value) == IS_ARRAY) {
524 member = zend_long_to_str(zend_hash_next_free_element(
525 Z_ARRVAL_P(value)));
526 } else if (Z_TYPE_P(value) != IS_OBJECT){
527 member = zend_long_to_str(0);
528 }
529 }
530
531 proxy = php_property_proxy_init(NULL, member);
532 proxy_obj = php_property_proxy_object_new_ex(NULL, proxy);
533 ZVAL_COPY(&proxy_obj->parent, object);
534 RETVAL_OBJ(&proxy_obj->zo);
535
536 debug_propro(0, "dim_R pp", get_propro(object), NULL, offset, return_value);
537 }
538
539 if (member) {
540 zend_string_release(member);
541 }
542
543 debug_propro(-1, type == BP_VAR_R ? "dim_r" : "dim_R",
544 get_propro(object), NULL, offset, return_value);
545
546 return return_value;
547 }
548
549 static int has_dimension(zval *object, zval *offset, int check_empty)
550 {
551 zval *value, tmp;
552 int exists = 0;
553
554 debug_propro(1, "dim_e", get_propro(object), NULL, offset, NULL);
555
556 ZVAL_UNDEF(&tmp);
557 value = get_proxied_value(object, &tmp);
558
559 if (!Z_ISUNDEF_P(value)) {
560 zend_string *zs = zval_get_string(offset);
561
562 ZVAL_DEREF(value);
563 if (Z_TYPE_P(value) == IS_ARRAY) {
564 zval *zentry = zend_symtable_find(Z_ARRVAL_P(value), zs);
565
566 if (zentry) {
567 if (check_empty) {
568 exists = !Z_ISNULL_P(zentry);
569 } else {
570 exists = 1;
571 }
572 }
573 }
574
575 zend_string_release(zs);
576 }
577
578 debug_propro(-1, "dim_e", get_propro(object), NULL, offset, NULL);
579
580 return exists;
581 }
582
583 static void write_dimension(zval *object, zval *offset, zval *input_value)
584 {
585 zval *array, tmp;
586 zend_string *zs = NULL;
587 zend_bool separated;
588
589 debug_propro(1, "dim_w", get_propro(object), NULL, offset, input_value);
590
591 if (offset) {
592 zs = zval_get_string(offset);
593 }
594
595 ZVAL_UNDEF(&tmp);
596 array = get_proxied_value(object, &tmp);
597 separated = separate_container(array);
598 set_container_value(array, zs, input_value);
599 set_proxied_value(object, array);
600 cleanup_container(array, separated);
601
602 if (zs) {
603 zend_string_release(zs);
604 }
605
606 debug_propro(-1, "dim_w", get_propro(object), NULL, offset, input_value);
607 }
608
609 static void unset_dimension(zval *object, zval *offset)
610 {
611 zval *array, *value, tmp;
612
613 debug_propro(1, "dim_u", get_propro(object), NULL, offset, NULL);
614
615 ZVAL_UNDEF(&tmp);
616 value = get_proxied_value(object, &tmp);
617 array = value;
618 ZVAL_DEREF(array);
619
620 if (Z_TYPE_P(array) == IS_ARRAY) {
621 zend_string *o = zval_get_string(offset);
622
623 SEPARATE_ARRAY(array);
624 zend_symtable_del(Z_ARRVAL_P(array), o);
625
626 set_proxied_value(object, value);
627
628 zend_string_release(o);
629 }
630
631 debug_propro(-1, "dim_u", get_propro(object), NULL, offset, NULL);
632 }
633
634 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct, 0, 0, 2)
635 ZEND_ARG_INFO(0, object)
636 ZEND_ARG_INFO(0, member)
637 ZEND_ARG_OBJ_INFO(0, parent, php\\PropertyProxy, 1)
638 ZEND_END_ARG_INFO();
639 static PHP_METHOD(propro, __construct) {
640 zend_error_handling zeh;
641 zval *reference, *parent = NULL;
642 zend_string *member;
643
644 zend_replace_error_handling(EH_THROW, NULL, &zeh);
645 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "o!S|O!",
646 &reference, &member, &parent,
647 php_property_proxy_class_entry)) {
648 php_property_proxy_object_t *obj;
649
650 obj = get_propro(getThis());
651
652 if (parent) {
653 ZVAL_COPY(&obj->parent, parent);
654 obj->proxy = php_property_proxy_init(NULL, member);
655 } else if (reference) {
656 zval *container = reference;
657
658 obj->proxy = php_property_proxy_init(container, member);
659 } else {
660 php_error(E_WARNING, "Either object or parent must be set");
661 }
662 }
663 zend_restore_error_handling(&zeh);
664 }
665
666 static const zend_function_entry php_property_proxy_method_entry[] = {
667 PHP_ME(propro, __construct, ai_propro_construct, ZEND_ACC_PUBLIC)
668 {0}
669 };
670
671 static PHP_MINIT_FUNCTION(propro)
672 {
673 zend_class_entry ce = {0};
674
675 INIT_NS_CLASS_ENTRY(ce, "php", "PropertyProxy",
676 php_property_proxy_method_entry);
677 php_property_proxy_class_entry = zend_register_internal_class(&ce);
678 php_property_proxy_class_entry->create_object = php_property_proxy_object_new;
679 php_property_proxy_class_entry->ce_flags |= ZEND_ACC_FINAL;
680
681 memcpy(&php_property_proxy_object_handlers, zend_get_std_object_handlers(),
682 sizeof(zend_object_handlers));
683 php_property_proxy_object_handlers.offset = XtOffsetOf(php_property_proxy_object_t, zo);
684 php_property_proxy_object_handlers.free_obj = destroy_obj;
685 php_property_proxy_object_handlers.set = set_obj;
686 php_property_proxy_object_handlers.get = get_obj;
687 php_property_proxy_object_handlers.cast_object = cast_obj;
688 php_property_proxy_object_handlers.read_dimension = read_dimension;
689 php_property_proxy_object_handlers.write_dimension = write_dimension;
690 php_property_proxy_object_handlers.has_dimension = has_dimension;
691 php_property_proxy_object_handlers.unset_dimension = unset_dimension;
692
693 return SUCCESS;
694 }
695
696 PHP_MINFO_FUNCTION(propro)
697 {
698 php_info_print_table_start();
699 php_info_print_table_header(2, "Property proxy support", "enabled");
700 php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION);
701 php_info_print_table_end();
702 }
703
704 static const zend_function_entry propro_functions[] = {
705 {0}
706 };
707
708 zend_module_entry propro_module_entry = {
709 STANDARD_MODULE_HEADER,
710 "propro",
711 propro_functions,
712 PHP_MINIT(propro),
713 NULL,
714 NULL,
715 NULL,
716 PHP_MINFO(propro),
717 PHP_PROPRO_VERSION,
718 STANDARD_MODULE_PROPERTIES
719 };
720
721 #ifdef COMPILE_DL_PROPRO
722 ZEND_GET_MODULE(propro)
723 #endif
724
725
726 /*
727 * Local variables:
728 * tab-width: 4
729 * c-basic-offset: 4
730 * End:
731 * vim600: noet sw=4 ts=4 fdm=marker
732 * vim<600: noet sw=4 ts=4
733 */