fix +/* feature/bug prefix
[m6w6/ext-pq] / src / php_pq_callback.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
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 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18 #include <Zend/zend_closures.h>
19
20 #include "php_pq_callback.h"
21
22 void php_pq_callback_dtor(php_pq_callback_t *cb)
23 {
24 if (cb->recursion) {
25 php_pq_callback_dtor(cb->recursion);
26 efree(cb->recursion);
27 cb->recursion = NULL;
28 }
29 if (cb->fci.size > 0) {
30 zend_fcall_info_args_clear(&cb->fci, 1);
31 zval_ptr_dtor(&cb->fci.function_name);
32 if (cb->fci.object_ptr) {
33 zval_ptr_dtor(&cb->fci.object_ptr);
34 }
35 cb->fci.size = 0;
36 }
37 }
38
39 void php_pq_callback_addref(php_pq_callback_t *cb)
40 {
41 Z_ADDREF_P(cb->fci.function_name);
42 if (cb->fci.object_ptr) {
43 Z_ADDREF_P(cb->fci.object_ptr);
44 }
45 }
46
47 zval *php_pq_callback_to_zval(php_pq_callback_t *cb)
48 {
49 zval *zcb;
50
51 php_pq_callback_addref(cb);
52
53 if (cb->fci.object_ptr) {
54 MAKE_STD_ZVAL(zcb);
55 array_init_size(zcb, 2);
56 add_next_index_zval(zcb, cb->fci.object_ptr);
57 add_next_index_zval(zcb, cb->fci.function_name);
58 } else {
59 zcb = cb->fci.function_name;
60 }
61
62 return zcb;
63 }
64
65 zend_bool php_pq_callback_is_locked(php_pq_callback_t *cb TSRMLS_DC)
66 {
67 if (cb->fci.size > 0 && Z_TYPE_P(cb->fci.function_name) == IS_OBJECT) {
68 const zend_function *closure = zend_get_closure_method_def(cb->fci.function_name TSRMLS_CC);
69
70 if (closure->type == ZEND_USER_FUNCTION) {
71 zend_execute_data *ex = EG(current_execute_data);
72
73 while (ex) {
74 if (ex->op_array == &closure->op_array) {
75 return 1;
76 }
77 ex = ex->prev_execute_data;
78 }
79 }
80 }
81 return 0;
82 }
83
84 void php_pq_callback_recurse(php_pq_callback_t *old, php_pq_callback_t *new TSRMLS_DC)
85 {
86 if (new && new->fci.size > 0 && php_pq_callback_is_locked(old TSRMLS_CC)) {
87 new->recursion = emalloc(sizeof(*old));
88 memcpy(new->recursion, old, sizeof(*old));
89 } else if (new && new->fci.size > 0) {
90 php_pq_callback_dtor(old);
91 php_pq_callback_addref(new);
92 memcpy(old, new, sizeof(*old));
93 new->fci.size = 0;
94 } else {
95 php_pq_callback_dtor(old);
96 }
97 }
98
99 /*
100 * Local variables:
101 * tab-width: 4
102 * c-basic-offset: 4
103 * End:
104 * vim600: noet sw=4 ts=4 fdm=marker
105 * vim<600: noet sw=4 ts=4
106 */