forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php7_wrapper.h
356 lines (310 loc) · 13.6 KB
/
php7_wrapper.h
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
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifndef EXT_SWOOLE_PHP7_WRAPPER_H_
#define EXT_SWOOLE_PHP7_WRAPPER_H_
#if PHP_MAJOR_VERSION < 7
typedef zend_rsrc_list_entry zend_resource;
#define SW_RETURN_STRING RETURN_STRING
#define SW_Z_ARRVAL_P Z_ARRVAL_P
#define IS_TRUE 1
static inline int sw_zend_hash_find(HashTable *ht, char *k, int len, void **v)
{
zval **tmp = NULL;
if (zend_hash_find(ht, k, len, (void **) &tmp) == SUCCESS)
{
*v = *tmp;
return SUCCESS;
}
else
{
*v = NULL;
return FAILURE;
}
}
#define sw_zend_hash_del zend_hash_del
#define sw_zend_hash_update zend_hash_update
#define sw_zend_hash_index_find zend_hash_index_find
#define SW_ZVAL_STRINGL ZVAL_STRINGL
#define SW_ZEND_FETCH_RESOURCE_NO_RETURN ZEND_FETCH_RESOURCE_NO_RETURN
#define SW_ZEND_FETCH_RESOURCE ZEND_FETCH_RESOURCE
#define SW_ZEND_REGISTER_RESOURCE ZEND_REGISTER_RESOURCE
#define SW_MAKE_STD_ZVAL(p) MAKE_STD_ZVAL(p)
#define SW_ALLOC_INIT_ZVAL(p) ALLOC_INIT_ZVAL(p)
#define SW_ZVAL_STRING ZVAL_STRING
#define SW_RETVAL_STRINGL RETVAL_STRINGL
#define sw_smart_str smart_str
#define sw_php_var_unserialize php_var_unserialize
#define sw_zend_is_callable zend_is_callable
#define sw_zend_is_callable_ex zend_is_callable_ex
#define sw_zend_hash_add zend_hash_add
#define sw_zend_hash_index_update zend_hash_index_update
#define sw_call_user_function_ex call_user_function_ex
#define sw_copy_to_stack(a, b)
//----------------------------------Array API------------------------------------
#define sw_add_assoc_string add_assoc_string
#define sw_add_assoc_stringl_ex add_assoc_stringl_ex
#define sw_add_assoc_stringl add_assoc_stringl
#define sw_add_assoc_double_ex add_assoc_double_ex
#define sw_add_assoc_long_ex add_assoc_long_ex
#define sw_add_next_index_stringl add_next_index_stringl
#define sw_zval_ptr_dtor zval_ptr_dtor
#define sw_zend_hash_copy zend_hash_copy
#define sw_zval_add_ref zval_add_ref
#define sw_zval_dup(val) (val)
#define sw_zval_free(val) (sw_zval_ptr_dtor(&val))
#define sw_zend_hash_exists zend_hash_exists
#define sw_php_format_date php_format_date
#define sw_php_url_encode php_url_encode
#define sw_php_array_merge(dest,src) php_array_merge(dest,src,1 TSRMLS_CC)
#define SW_RETURN_STRINGL RETURN_STRINGL
#define sw_zend_register_internal_class_ex zend_register_internal_class_ex
#define sw_zend_call_method_with_0_params zend_call_method_with_0_params
#define sw_zend_call_method_with_1_params zend_call_method_with_1_params
#define sw_zend_call_method_with_2_params zend_call_method_with_2_params
typedef int zend_size_t;
#define SW_HASHTABLE_FOREACH_START(ht, entry)\
zval **tmp = NULL;\
for (zend_hash_internal_pointer_reset(ht);\
zend_hash_has_more_elements(ht) == SUCCESS; \
zend_hash_move_forward(ht)) {\
if (zend_hash_get_current_data(ht, (void**)&tmp) == FAILURE) {\
continue;\
}\
entry = *tmp;
#if defined(HASH_KEY_NON_EXISTANT) && !defined(HASH_KEY_NON_EXISTENT)
#define HASH_KEY_NON_EXISTENT HASH_KEY_NON_EXISTANT
#endif
#define SW_HASHTABLE_FOREACH_START2(ht, k, klen, ktype, entry)\
zval **tmp = NULL; ulong_t idx;\
for (zend_hash_internal_pointer_reset(ht); \
(ktype = zend_hash_get_current_key_ex(ht, &k, &klen, &idx, 0, NULL)) != HASH_KEY_NON_EXISTENT; \
zend_hash_move_forward(ht)\
) { \
if (zend_hash_get_current_data(ht, (void**)&tmp) == FAILURE) {\
continue;\
}\
entry = *tmp;\
klen --;
#define SW_HASHTABLE_FOREACH_END() }
#define sw_zend_read_property zend_read_property
#define sw_zend_hash_get_current_key(a,b,c,d) zend_hash_get_current_key_ex(a,b,c,d,0,NULL)
static inline int SW_Z_TYPE_P(zval *z)
{
if (Z_TYPE_P(z) == IS_BOOL)
{
if ((uint8_t) Z_BVAL_P(z) == 1)
{
return IS_TRUE;
}
else
{
return 0;
}
}
else
{
return Z_TYPE_P(z);
}
}
#define sw_php_var_serialize(a,b,c) php_var_serialize(a,&b,c)
#define IS_TRUE 1
inline int SW_Z_TYPE_P(zval *z);
#define SW_Z_TYPE_PP(z) SW_Z_TYPE_P(*z)
#else
#define sw_php_var_serialize php_var_serialize
typedef size_t zend_size_t;
#define ZEND_SET_SYMBOL(ht,str,arr) zval_add_ref(arr); zend_hash_str_update(ht, str, sizeof(str)-1, arr);
static sw_inline int Z_BVAL_P(zval *v)
{
if (Z_TYPE_P(v) == IS_TRUE)
{
return 1;
}
else
{
return 0;
}
}
//----------------------------------Array API------------------------------------
#define sw_add_assoc_stringl(__arg, __key, __str, __length, __duplicate) add_assoc_stringl_ex(__arg, __key, strlen(__key), __str, __length)
static sw_inline int sw_add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, char *str, size_t length, int __duplicate)
{
return add_assoc_stringl_ex(arg, key, key_len - 1, str, length);
}
#define sw_add_next_index_stringl(arr, str, len, dup) add_next_index_stringl(arr, str, len)
static sw_inline int sw_add_assoc_long_ex(zval *arg, const char *key, size_t key_len, long value)
{
return add_assoc_long_ex(arg, key, key_len - 1, value);
}
static sw_inline int sw_add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double value)
{
return add_assoc_double_ex(arg, key, key_len - 1, value);
}
#define SW_Z_ARRVAL_P(z) Z_ARRVAL_P(z)->ht
#define SW_HASHTABLE_FOREACH_START(ht, _val) ZEND_HASH_FOREACH_VAL(ht, _val); {
#define SW_HASHTABLE_FOREACH_START2(ht, k, klen, ktype, _val) zend_string *_foreach_key;\
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _foreach_key, _val);\
if (!_foreach_key) {k = NULL; klen = 0; ktype = 0;}\
else {k = _foreach_key->val, klen=_foreach_key->len; ktype = 1;} {
#define SW_HASHTABLE_FOREACH_END() } ZEND_HASH_FOREACH_END();
#define Z_ARRVAL_PP(s) Z_ARRVAL_P(*s)
#define SW_Z_TYPE_P Z_TYPE_P
#define SW_Z_TYPE_PP(s) SW_Z_TYPE_P(*s)
#define Z_STRVAL_PP(s) Z_STRVAL_P(*s)
#define Z_STRLEN_PP(s) Z_STRLEN_P(*s)
#define Z_LVAL_PP(v) Z_LVAL_P(*v)
static inline char* sw_php_format_date(char *format, size_t format_len, time_t ts, int localtime)
{
zend_string *time = php_format_date(format, format_len, ts, localtime);
char *return_str = (char*) emalloc(time->len + 1);
memcpy(return_str, time->val, time->len);
return_str[time->len] = 0;
zend_string_release(time);
return return_str;
}
static sw_inline char* sw_php_url_encode(char *value, size_t value_len, int* exten)
{
zend_string *str = php_url_encode(value, value_len);
*exten = str->len;
char *return_str = (char*) emalloc(str->len + 1);
memcpy(return_str, str->val, str->len);
zend_string_release(str);
return_str[str->len] = 0;
return return_str;
}
#define sw_zval_add_ref(p) Z_TRY_ADDREF_P(*p)
#define sw_zval_ptr_dtor(p) zval_ptr_dtor(*p)
#define SW_PHP_MAX_PARAMS_NUM 20
static sw_inline int sw_call_user_function_ex(HashTable *function_table, zval** object_pp, zval *function_name, zval **retval_ptr_ptr, uint32_t param_count, zval ***params, int no_separation, HashTable* ymbol_table)
{
zval real_params[SW_PHP_MAX_PARAMS_NUM];
int i = 0;
for (; i < param_count; i++)
{
real_params[i] = **params[i];
}
zval phpng_retval;
*retval_ptr_ptr = &phpng_retval;
zval *object_p = (object_pp == NULL) ? NULL : *object_pp;
return call_user_function_ex(function_table, object_p, function_name, &phpng_retval, param_count, real_params, no_separation, NULL);
}
#define sw_php_var_unserialize(rval, p, max, var_hash) php_var_unserialize(*rval, p, max, var_hash)
#define SW_MAKE_STD_ZVAL(p) zval _stack_zval_##p; p = &(_stack_zval_##p)
#define SW_ALLOC_INIT_ZVAL(p) p = emalloc(sizeof(zval)); bzero(p, sizeof(zval))
#define SW_RETURN_STRINGL(s, l, dup) RETURN_STRINGL(s, l)
#define SW_RETVAL_STRINGL(s, l, dup) RETVAL_STRINGL(s, l); if (dup == 0) efree(s)
#define SW_ZEND_FETCH_RESOURCE_NO_RETURN(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type) \
(rsrc = (rsrc_type) zend_fetch_resource(Z_RES_P(*passed_id), resource_type_name, resource_type))
#define SW_ZEND_REGISTER_RESOURCE(return_value, result, le_result) ZVAL_RES(return_value,zend_register_resource(result, le_result))
#define SW_RETURN_STRING(val, duplicate) RETURN_STRING(val)
#define sw_add_assoc_string(array, key, value, duplicate) add_assoc_string(array, key, value)
#define sw_zend_hash_copy(target,source,pCopyConstructor,tmp,size) zend_hash_copy(target,source,pCopyConstructor)
#define sw_php_array_merge php_array_merge
#define sw_zend_register_internal_class_ex(entry,parent_ptr,str) zend_register_internal_class_ex(entry,parent_ptr)
#define sw_zend_call_method_with_0_params(obj, ptr, what, method, retval) zend_call_method_with_0_params(*obj,ptr,what,method,*retval)
#define sw_zend_call_method_with_1_params(obj, ptr, what, method, retval, v1) zend_call_method_with_1_params(*obj,ptr,what,method,*retval,v1)
#define sw_zend_call_method_with_2_params(obj, ptr, what, method, retval, name, cb) zend_call_method_with_2_params(*obj,ptr,what,method,*retval,name,cb)
#define SW_ZVAL_STRINGL(z, s, l, dup) ZVAL_STRINGL(z, s, l)
#define SW_ZVAL_STRING(z,s,dup) ZVAL_STRING(z,s)
#define sw_smart_str smart_string
#define zend_get_class_entry Z_OBJCE_P
#define sw_copy_to_stack(a, b) {zval *__tmp = a;\
a = &b;\
memcpy(a, __tmp, sizeof(zval));}
static sw_inline zval* sw_zval_dup(zval *val)
{
zval *dup;
SW_ALLOC_INIT_ZVAL(dup);
memcpy(dup, val, sizeof(zval));
return dup;
}
static sw_inline void sw_zval_free(zval *val)
{
sw_zval_ptr_dtor(&val);
#if PHP_MAJOR_VERSION > 5
efree(val);
#endif
}
static sw_inline zval* sw_zend_read_property(zend_class_entry *class_ptr, zval *obj, char *s, int len, int silent)
{
zval rv;
return zend_read_property(class_ptr, obj, s, len, silent, &rv);
}
static sw_inline int sw_zend_is_callable(zval *cb, int a, char **name)
{
zend_string *key = NULL;
int ret = zend_is_callable(cb, a, &key);
char *tmp = (char *)emalloc(key->len);
memcpy(tmp, key->val, key->len);
zend_string_release(key);
*name = tmp;
return ret;
}
static inline int sw_zend_is_callable_ex(zval *callable, zval *object, uint check_flags, char **callable_name, int *callable_name_len, zend_fcall_info_cache *fcc, char **error TSRMLS_DC)
{
zend_string *key = NULL;
char *tmp = (char *)emalloc(key->len);
int ret = zend_is_callable_ex(callable, NULL, check_flags, &key, fcc, error);
memcpy(tmp, key->val, key->len);
zend_string_release(key);
*callable_name = tmp;
return ret;
}
static inline int sw_zend_hash_del(HashTable *ht, char *k, int len)
{
return zend_hash_str_del(ht, k, len - 1);
}
static inline int sw_zend_hash_add(HashTable *ht, char *k, int len, void *pData, int datasize, void **pDest)
{
zval **real_p = pData;
return zend_hash_str_add(ht, k, len - 1, *real_p) ? SUCCESS : FAILURE;
}
static inline int sw_zend_hash_index_update(HashTable *ht, int key, void *pData, int datasize, void **pDest)
{
zval **real_p = pData;
return zend_hash_index_update(ht, key, *real_p) ? SUCCESS : FAILURE;
}
static inline int sw_zend_hash_update(HashTable *ht, char *k, int len, void *val, int size, void *ptr)
{
return zend_hash_str_update(ht, k, len -1, val) ? SUCCESS : FAILURE;
}
static inline int sw_zend_hash_find(HashTable *ht, char *k, int len, void **v)
{
zval *value = zend_hash_str_find(ht, k, len - 1);
if (value == NULL)
{
return FAILURE;
}
else
{
*v = (void *) value;
return SUCCESS;
}
}
static inline int sw_zend_hash_exists(HashTable *ht, char *k, int len)
{
zval *value = zend_hash_str_find(ht, k, len - 1);
if (value == NULL)
{
return FAILURE;
}
else
{
return SUCCESS;
}
}
#endif
#endif /* EXT_SWOOLE_PHP7_WRAPPER_H_ */