fix failure on travis with PHP-7.0 and 7.1
[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 *object, zval *container, zend_bool separated)
364 {
365 if (separated) {
366 zval_ptr_dtor(container);
367 }
368 }
369
370 static inline zend_bool separate_container(zval *object, zval *container)
371 {
372 switch (Z_TYPE_P(container)) {
373 case IS_OBJECT:
374 return 0;
375
376 case IS_ARRAY:
377 /* always duplicate for PHP-7.0 and 7.1 on travis */
378 ZVAL_ARR(container, zend_array_dup(Z_ARRVAL_P(container)));
379 break;
380
381 case IS_UNDEF:
382 array_init(container);
383 break;
384
385 default:
386 SEPARATE_ZVAL(container);
387 Z_TRY_ADDREF_P(container);
388 convert_to_array(container);
389 break;
390 }
391
392 return 1;
393 }
394
395 static inline zval *set_container_value(zval *container, zend_string *member, zval *value)
396 {
397 ZVAL_DEREF(container);
398 switch (Z_TYPE_P(container)) {
399 case IS_OBJECT:
400 zend_update_property(Z_OBJCE_P(container), container,
401 member->val, member->len, value);
402 break;
403
404 case IS_ARRAY:
405 Z_TRY_ADDREF_P(value);
406 if (member) {
407 value = zend_symtable_update(Z_ARRVAL_P(container), member, value);
408 } else {
409 value = zend_hash_next_index_insert(Z_ARRVAL_P(container), value);
410 }
411 break;
412
413 default:
414 ZEND_ASSERT(0);
415 break;
416 }
417
418 return value;
419 }
420
421
422 static zval *get_proxied_value(zval *object, zval *return_value)
423 {
424 php_property_proxy_object_t *obj = get_propro(object);
425
426 debug_propro(1, "get", obj, NULL, NULL, NULL);
427
428 if (obj->proxy) {
429 zval tmp, *container;
430
431 ZVAL_UNDEF(&tmp);
432 container = get_container(object, &tmp);
433
434 return_value = get_container_value(container, obj->proxy->member, return_value);
435 }
436
437 debug_propro(-1, "get", obj, NULL, NULL, return_value);
438
439 return return_value;
440 }
441
442 static void set_proxied_value(zval *object, zval *value)
443 {
444 php_property_proxy_object_t *obj = get_propro(object);
445
446 debug_propro(1, "set", obj, NULL, NULL, value);
447
448 if (obj->proxy) {
449 zval tmp, *container;
450 zend_bool separated;
451
452 Z_TRY_ADDREF_P(value);
453
454 ZVAL_UNDEF(&tmp);
455 container = get_container(object, &tmp);
456 separated = separate_container(object, container);
457 set_container_value(container, obj->proxy->member, value);
458 set_container(object, container);
459 cleanup_container(object, container, separated);
460
461 Z_TRY_DELREF_P(value);
462
463 debug_propro(0, "set", obj, NULL, NULL, value);
464 }
465
466 debug_propro(-1, "set", obj, NULL, NULL, value);
467 }
468
469 static zval *read_dimension(zval *object, zval *offset, int type, zval *return_value)
470 {
471 zval *value, tmp;
472 zend_string *member = offset ? zval_get_string(offset) : NULL;
473
474 debug_propro(1, type == BP_VAR_R ? "dim_r" : "dim_R",
475 get_propro(object), NULL, offset, NULL);
476
477 ZVAL_UNDEF(&tmp);
478 value = get_proxied_value(object, &tmp);
479
480 if (type == BP_VAR_R || type == BP_VAR_IS) {
481 ZEND_ASSERT(member);
482
483 if (!Z_ISUNDEF_P(value)) {
484 zval tmp;
485
486 ZVAL_UNDEF(&tmp);
487 RETVAL_ZVAL(get_container_value(value, member, &tmp), 1, 0);
488 }
489 } else {
490 php_property_proxy_t *proxy;
491 php_property_proxy_object_t *proxy_obj;
492
493 if (Z_ISUNDEF_P(value)) {
494 ZVAL_NULL(value);
495 }
496
497 if (!member) {
498 if (Z_TYPE_P(value) == IS_ARRAY) {
499 member = zend_long_to_str(zend_hash_next_free_element(
500 Z_ARRVAL_P(value)));
501 } else if (Z_TYPE_P(value) != IS_OBJECT){
502 member = zend_long_to_str(0);
503 }
504 }
505
506 proxy = php_property_proxy_init(NULL, member);
507 proxy_obj = php_property_proxy_object_new_ex(NULL, proxy);
508 ZVAL_COPY(&proxy_obj->parent, object);
509 RETVAL_OBJ(&proxy_obj->zo);
510
511 debug_propro(0, "dim_R pp", get_propro(object), NULL, offset, return_value);
512 }
513
514 if (member) {
515 zend_string_release(member);
516 }
517
518 debug_propro(-1, type == BP_VAR_R ? "dim_r" : "dim_R",
519 get_propro(object), NULL, offset, return_value);
520
521 return return_value;
522 }
523
524 static int has_dimension(zval *object, zval *offset, int check_empty)
525 {
526 zval *value, tmp;
527 int exists = 0;
528
529 debug_propro(1, "dim_e", get_propro(object), NULL, offset, NULL);
530
531 ZVAL_UNDEF(&tmp);
532 value = get_proxied_value(object, &tmp);
533
534 if (!Z_ISUNDEF_P(value)) {
535 zend_string *zs = zval_get_string(offset);
536
537 ZVAL_DEREF(value);
538 if (Z_TYPE_P(value) == IS_ARRAY) {
539 zval *zentry = zend_symtable_find(Z_ARRVAL_P(value), zs);
540
541 if (zentry) {
542 if (check_empty) {
543 exists = !Z_ISNULL_P(zentry);
544 } else {
545 exists = 1;
546 }
547 }
548 }
549
550 zend_string_release(zs);
551 }
552
553 debug_propro(-1, "dim_e", get_propro(object), NULL, offset, NULL);
554
555 return exists;
556 }
557
558 static void write_dimension(zval *object, zval *offset, zval *input_value)
559 {
560 zval *array, tmp;
561 zend_string *zs = NULL;
562 zend_bool separated;
563
564 debug_propro(1, "dim_w", get_propro(object), NULL, offset, input_value);
565
566 if (offset) {
567 zs = zval_get_string(offset);
568 }
569
570 ZVAL_UNDEF(&tmp);
571 array = get_proxied_value(object, &tmp);
572 separated = separate_container(object, array);
573 set_container_value(array, zs, input_value);
574 set_proxied_value(object, array);
575 cleanup_container(object, array, separated);
576
577 if (zs) {
578 zend_string_release(zs);
579 }
580
581 debug_propro(-1, "dim_w", get_propro(object), NULL, offset, input_value);
582 }
583
584 static void unset_dimension(zval *object, zval *offset)
585 {
586 zval *array, *value, tmp;
587
588 debug_propro(1, "dim_u", get_propro(object), NULL, offset, NULL);
589
590 ZVAL_UNDEF(&tmp);
591 value = get_proxied_value(object, &tmp);
592 array = value;
593 ZVAL_DEREF(array);
594
595 if (Z_TYPE_P(array) == IS_ARRAY) {
596 zend_string *o = zval_get_string(offset);
597
598 SEPARATE_ARRAY(array);
599 zend_symtable_del(Z_ARRVAL_P(array), o);
600
601 set_proxied_value(object, value);
602
603 zend_string_release(o);
604 }
605
606 debug_propro(-1, "dim_u", get_propro(object), NULL, offset, NULL);
607 }
608
609 ZEND_BEGIN_ARG_INFO_EX(ai_propro_construct, 0, 0, 2)
610 ZEND_ARG_INFO(0, object)
611 ZEND_ARG_INFO(0, member)
612 ZEND_ARG_OBJ_INFO(0, parent, php\\PropertyProxy, 1)
613 ZEND_END_ARG_INFO();
614 static PHP_METHOD(propro, __construct) {
615 zend_error_handling zeh;
616 zval *reference, *parent = NULL;
617 zend_string *member;
618
619 zend_replace_error_handling(EH_THROW, NULL, &zeh);
620 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "o!S|O!",
621 &reference, &member, &parent,
622 php_property_proxy_class_entry)) {
623 php_property_proxy_object_t *obj;
624
625 obj = get_propro(getThis());
626
627 if (parent) {
628 ZVAL_COPY(&obj->parent, parent);
629 obj->proxy = php_property_proxy_init(NULL, member);
630 } else if (reference) {
631 zval *container = reference;
632
633 obj->proxy = php_property_proxy_init(container, member);
634 } else {
635 php_error(E_WARNING, "Either object or parent must be set");
636 }
637 }
638 zend_restore_error_handling(&zeh);
639 }
640
641 static const zend_function_entry php_property_proxy_method_entry[] = {
642 PHP_ME(propro, __construct, ai_propro_construct, ZEND_ACC_PUBLIC)
643 {0}
644 };
645
646 static PHP_MINIT_FUNCTION(propro)
647 {
648 zend_class_entry ce = {0};
649
650 INIT_NS_CLASS_ENTRY(ce, "php", "PropertyProxy",
651 php_property_proxy_method_entry);
652 php_property_proxy_class_entry = zend_register_internal_class(&ce);
653 php_property_proxy_class_entry->create_object = php_property_proxy_object_new;
654 php_property_proxy_class_entry->ce_flags |= ZEND_ACC_FINAL;
655
656 memcpy(&php_property_proxy_object_handlers, zend_get_std_object_handlers(),
657 sizeof(zend_object_handlers));
658 php_property_proxy_object_handlers.offset = XtOffsetOf(php_property_proxy_object_t, zo);
659 php_property_proxy_object_handlers.free_obj = destroy_obj;
660 php_property_proxy_object_handlers.set = set_obj;
661 php_property_proxy_object_handlers.get = get_obj;
662 php_property_proxy_object_handlers.cast_object = cast_obj;
663 php_property_proxy_object_handlers.read_dimension = read_dimension;
664 php_property_proxy_object_handlers.write_dimension = write_dimension;
665 php_property_proxy_object_handlers.has_dimension = has_dimension;
666 php_property_proxy_object_handlers.unset_dimension = unset_dimension;
667
668 return SUCCESS;
669 }
670
671 PHP_MINFO_FUNCTION(propro)
672 {
673 php_info_print_table_start();
674 php_info_print_table_header(2, "Property proxy support", "enabled");
675 php_info_print_table_row(2, "Extension version", PHP_PROPRO_VERSION);
676 php_info_print_table_end();
677 }
678
679 static const zend_function_entry propro_functions[] = {
680 {0}
681 };
682
683 zend_module_entry propro_module_entry = {
684 STANDARD_MODULE_HEADER,
685 "propro",
686 propro_functions,
687 PHP_MINIT(propro),
688 NULL,
689 NULL,
690 NULL,
691 PHP_MINFO(propro),
692 PHP_PROPRO_VERSION,
693 STANDARD_MODULE_PROPERTIES
694 };
695
696 #ifdef COMPILE_DL_PROPRO
697 ZEND_GET_MODULE(propro)
698 #endif
699
700
701 /*
702 * Local variables:
703 * tab-width: 4
704 * c-basic-offset: 4
705 * End:
706 * vim600: noet sw=4 ts=4 fdm=marker
707 * vim<600: noet sw=4 ts=4
708 */