-
Notifications
You must be signed in to change notification settings - Fork 160
/
php7_wrapper.h
379 lines (330 loc) · 11.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* Copyright 2017 chuan-yun silkcutKs <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MOLTEN_PHP7_WRAPPER_H
#define MOLTEN_PHP7_WRAPPER_H
#include "php.h"
#include "ext/json/php_json.h"
#include "ext/standard/php_array.h"
#if PHP_VERSION_ID < 70000
/* smart string */
#include "ext/standard/php_smart_str.h"
#define smart_string smart_str
#define smart_string_free smart_str_free
#define smart_string_appends smart_str_appends
#define smart_string_appendl smart_str_appendl
#define smart_string_0 smart_str_0
/* php_json_encode */
#define mo_php_json_encode php_json_encode
#define mo_zend_hash_update zend_hash_update
#define mo_zend_hash_update_ptr zend_hash_update
#define mo_zend_hash_index_update zend_hash_index_update
#define mo_zend_hash_add zend_hash_add
#define mo_zend_hash_str_add_zval zend_hash_add
#define MO_EX_OBJ(ex) ex->object
#define MO_EX_OBJ_ZVAL(ex) ex->object
#define MO_EX_OBJCE(ex) Z_OBJCE_P(ex->object)
#define MO_EX_OPARR(ex) ex->op_array
#define MO_STR(v) v
#define P7_STR_LEN(v) strlen(v)
#define MO_ZVAL_STRING ZVAL_STRING
#define MO_ZVAL_STRINGL ZVAL_STRINGL
#define IS_TRUE 1
#define IS_FALSE -1
#define MO_MAKE_STD_ZVAL MAKE_STD_ZVAL
#define MO_ALLOC_INIT_ZVAL ALLOC_INIT_ZVAL
#define MO_INIT_ZVAL INIT_ZVAL
//#define MO_ARRAY_INIT(p) MO_ALLOC_INIT_ZVAL(p);array_init(p)
#define MO_COPY_ZVAL_TO_STRING(z, p) do { \
ALLOC_INIT_ZVAL(z); \
ZVAL_ZVAL(z,p,1,0); \
convert_to_string(z); \
}while(0)
#define MO_FREE_COPY_STRING(z) zval_dtor(z);
#define MO_FREE_ALLOC_ZVAL(p)
#define mo_add_assoc_string add_assoc_string
#define mo_add_next_index_string add_next_index_string
#define mo_zend_get_constant zend_get_constant
#define mo_zval_ptr_dtor zval_ptr_dtor
#define mo_zval_dtor zval_dtor
#define mo_call_user_function call_user_function
#define mo_zend_read_property zend_read_property
#define mo_zend_is_auto_global zend_is_auto_global
#define mo_zend_array_count zend_hash_num_elements
static inline void mo_array_merge(zval *dest, zval *src TSRMLS_DC)
{
php_array_merge(Z_ARRVAL_P(dest), Z_ARRVAL_P(src), 1);
}
static inline void array_init_persist(zval *arg ZEND_FILE_LINE_DC)
{
Z_ARRVAL_P(arg) = (HashTable *)pemalloc(sizeof(HashTable), 1);
#if PHP_VERSION_ID < 50600
_zend_hash_init(Z_ARRVAL_P(arg), 0, NULL, ZVAL_PTR_DTOR, 1 ZEND_FILE_LINE_RELAY_CC);
#else
_zend_hash_init(Z_ARRVAL_P(arg), 0, ZVAL_PTR_DTOR, 1 ZEND_FILE_LINE_RELAY_CC);
#endif
Z_TYPE_P(arg) = IS_ARRAY;
}
static inline void array_free_persist(zval *arg)
{
zend_hash_destroy(Z_ARRVAL_P(arg));
pefree(Z_ARRVAL_P(arg), 1);
}
static inline int MO_Z_TYPE_P(zval *z)
{
if (Z_TYPE_P(z) == IS_BOOL) {
if ((uint8_t)Z_LVAL_P(z) == 1) {
return IS_TRUE;
} else {
return IS_FALSE;
}
} else {
return Z_TYPE_P(z);
}
}
#define MO_Z_TYPE_PP(z) MO_Z_TYPE_P(*z)
static inline int mo_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 mo_zend_hash_zval_find mo_zend_hash_find
static inline int mo_zend_hash_index_find(HashTable *ht, ulong h, void **v)
{
zval **tmp = NULL;
if (zend_hash_index_find(ht, h, (void **)&tmp) == SUCCESS) {
*v = *tmp;
return SUCCESS;
} else {
*v = NULL;
return FAILURE;
}
}
#define mo_zend_hash_index_zval_find mo_zend_hash_index_find
static inline int mo_zend_hash_get_current_data(HashTable *ht, void **v)
{
zval **tmp = NULL;
if (zend_hash_get_current_data(ht, (void **)&tmp) == SUCCESS) {
*v = *tmp;
return SUCCESS;
} else {
*v = NULL;
return FAILURE;
}
}
#define mo_zend_hash_exists zend_hash_exists
#define mo_zend_hash_index_del zend_hash_index_del
#else
/* smart string */
#include "ext/standard/php_smart_string.h"
#include "Zend/zend_smart_str.h"
/* php_json_encode */
static void inline mo_php_json_encode(smart_string *s, zval *z, int options)
{
smart_str tmp = {0};
php_json_encode(&tmp, z, options);
smart_string_appendl(s, ZSTR_VAL(tmp.s), ZSTR_LEN(tmp.s));
smart_str_free(&tmp);
}
#if PHP_VERSION_ID < 70100
/* object fetching in PHP 7.0
* object = call ? Z_OBJ(call->This) : NULL; */
#define MO_EX_OBJ(ex) Z_OBJ(ex->This)
#else
/* object fetching in PHP 7.1
* object = (call && (Z_TYPE(call->This) == IS_OBJECT)) ? Z_OBJ(call->This) : NULL; */
#define MO_EX_OBJ(ex) (Z_TYPE(ex->This) == IS_OBJECT ? Z_OBJ(ex->This) : NULL)
#endif
#define MO_EX_OBJ_ZVAL(ex) &(ex->This)
#define MO_EX_OBJCE(ex) Z_OBJCE(ex->This)
#define MO_EX_OPARR(ex) (&(ex->func->op_array))
#define MO_STR(v) ZSTR_VAL(v)
#define MO_STR_LEN(v) ZSTR_LEN(v)
#define MO_ZVAL_STRING(z,s,dup) ZVAL_STRING(z,s)
#define MO_ZVAL_STRINGL(z,s,l,dup) ZVAL_STRINGL(z,s,l)
#define Z_RESVAL(z) Z_RES_HANDLE(z)
#define Z_RESVAL_P(z) Z_RES_HANDLE_P(z)
#define MO_MAKE_STD_ZVAL(p) zval _stack_zval_##p; p = &(_stack_zval_##p)
#define MO_ALLOC_INIT_ZVAL(p) do{p = emalloc(sizeof(zval)); bzero(p, sizeof(zval));}while(0)
#define MO_INIT_ZVAL(z) bzero(z, sizeof(zval))
#define mo_add_next_index_string(z,key,dup) add_next_index_string(z,key)
#define MO_COPY_ZVAL_TO_STRING(z, p) do { \
MO_ALLOC_INIT_ZVAL(z); \
ZVAL_DUP(z,p); \
convert_to_string(z); \
}while(0)
#define MO_FREE_COPY_STRING(z) zval_dtor(z);
//#define MO_ARRAY_INIT(p) array_init(p)
#define MO_FREE_ALLOC_ZVAL(p) efree(p)
#define mo_zval_ptr_dtor(p) zval_ptr_dtor(*p)
#define mo_zval_dtor(p) zval_ptr_dtor(p)
#define mo_add_assoc_string(array, key, value, dup) add_assoc_string(array, key, value)
#define MO_Z_TYPE_P Z_TYPE_P
#define MO_Z_TYPE_PP(z) Z_TYPE_P(*z)
#define MO_PHP_MAX_PARAMS_NUM 20
#define mo_zend_is_auto_global zend_is_auto_global_str
#define mo_zend_array_count zend_array_count
static inline int mo_call_user_function(HashTable *ht, zval **obj, zval *function_name, zval *retval_ptr, uint32_t param_count, zval **params)
{
zval pass_params[MO_PHP_MAX_PARAMS_NUM];
int i = 0;
for(;i < param_count; i++){
pass_params[i] = *params[i];
}
zval *pass_obj = obj ? *obj : NULL;
return call_user_function(ht, pass_obj, function_name, retval_ptr, param_count, pass_params);
}
static inline zval *mo_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 inline int mo_zend_get_constant(char *key, int len, zval *z)
{
zend_string *key_str = zend_string_init(key, len, 0);
zval *c = zend_get_constant(key_str);
zend_string_free(key_str);
if (c != NULL) {
ZVAL_COPY(z,c);
return 1;
} else {
return 0;
}
}
static inline void mo_array_merge(zval *dest, zval *src TSRMLS_DC)
{
php_array_merge_recursive(Z_ARRVAL_P(dest), Z_ARRVAL_P(src));
}
static inline void array_init_persist(zval *arg ZEND_FILE_LINE_DC)
{
ZVAL_NEW_PERSISTENT_ARR(arg);
_zend_hash_init(Z_ARRVAL_P(arg), 0, ZVAL_PTR_DTOR, 1 ZEND_FILE_LINE_RELAY_CC);
}
static inline void array_free_persist(zval *arg)
{
zend_hash_destroy(Z_ARRVAL_P(arg));
pefree(Z_ARRVAL_P(arg), 1);
}
/***********************hash********************/
static inline int mo_zend_hash_find(HashTable *ht, char *k, int len, void **v)
{
void *value = (void *)zend_hash_str_find_ptr(ht, k, len - 1);
if (value == NULL) {
return FAILURE;
} else {
*v = value;
return SUCCESS;
}
}
static inline int mo_zend_hash_zval_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 = value;
return SUCCESS;
}
}
static inline int mo_zend_hash_index_find(HashTable *ht, ulong h, void **v)
{
void **value = (void **)zend_hash_index_find_ptr(ht, h);
if (value == NULL) {
return FAILURE;
} else {
*v = *value;
return SUCCESS;
}
}
static inline int mo_zend_hash_index_zval_find(HashTable *ht, ulong h, void **v)
{
zval *value = zend_hash_index_find(ht, h);
if (value == NULL) {
return FAILURE;
} else {
*v = value;
return SUCCESS;
}
}
static inline int mo_zend_hash_get_current_data(HashTable *ht, void **v)
{
zval *value = zend_hash_get_current_data(ht);
if (value == NULL) {
return FAILURE;
} else {
*v = Z_PTR_P(value);
return SUCCESS;
}
}
static inline int mo_zend_hash_get_current_data_zval(HashTable *ht, void **v)
{
zval *value = zend_hash_get_current_data(ht);
if (value == NULL) {
return FAILURE;
} else {
*v = value;
return SUCCESS;
}
}
static inline int mo_zend_hash_update_ptr(HashTable *ht, char *k, int len, void *val, int nDataSize, void **pDest)
{
void **v = (void **)val;
return zend_hash_str_update_ptr(ht, k, len - 1, *v) ? SUCCESS : FAILURE;
}
static inline int mo_zend_hash_update(HashTable *ht, char *k, int len, void *val, int nDataSize, void **pDest)
{
//return zend_hash_str_update_ptr(ht, k, len, val) ? SUCCESS : FAILURE;
void **v = (void **)val;
return zend_hash_str_update_ptr(ht, k, len - 1, *v) ? SUCCESS : FAILURE;
}
static inline int mo_zend_hash_index_update(HashTable *ht, ulong h, void *pData, uint nDataSize, void **pDest)
{
void **v = (void **)pData;
return zend_hash_index_update_ptr(ht, h, *v) ? SUCCESS : FAILURE;
}
static inline void mo_zend_hash_str_add_zval(HashTable *ht, char *k, int len, zval *val, int datasize, void **pDest)
{
zend_hash_str_add(ht, k, len - 1, val);
}
static inline int mo_zend_hash_add(HashTable *ht, char *k, int len, void *val, int datasize, void **pDest)
{
void **v = (void **)val;
return zend_hash_str_add_ptr(ht, k, len - 1, v) ? SUCCESS : FAILURE;
}
static inline int mo_zend_hash_exists(HashTable *ht, char *k, int len)
{
zend_string *k_str = zend_string_init(k, len - 1, 0);
int result = zend_hash_exists(ht, k_str);
zend_string_free(k_str);
return result;
}
static inline int mo_zend_hash_index_del(HashTable *ht, ulong h)
{
zend_ulong uh = (zend_ulong)h;
return zend_hash_index_del(ht, uh);
}
#endif
/* smart string wrapper */
#define smart_string_len(s) s.len
#define smart_string_str(s) s.c
#define smart_strcmp(s,s1) strcmp(s.c, s1)
#define smart_strncmp(s,s1,size) strncmp(s.c, s1, size)
#endif