forked from crazyxman/simdjson_php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimdjson.c
276 lines (233 loc) · 7.92 KB
/
simdjson.c
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
/*
+----------------------------------------------------------------------+
| simdjson_php |
+----------------------------------------------------------------------+
| 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 |
+----------------------------------------------------------------------+
| Author: Jinxi Wang <1054636713@qq.com> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "main/SAPI.h"
#include "ext/standard/info.h"
#include "php_simdjson.h"
#include "src/bindings.h"
ZEND_DECLARE_MODULE_GLOBALS(simdjson);
ZEND_BEGIN_ARG_INFO(simdjson_isvalid_arginfo, 1)
ZEND_ARG_INFO(0, json)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(simdjson_decode_arginfo, 1)
ZEND_ARG_INFO(0, json)
ZEND_ARG_INFO(0, assoc)
ZEND_ARG_INFO(0, depth)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(simdjson_key_value_arginfo, 2)
ZEND_ARG_INFO(0, json)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, assoc)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(simdjson_key_exists_arginfo, 2)
ZEND_ARG_INFO(0, json)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(simdjson_resource_arginfo, 1)
ZEND_ARG_INFO(0, json)
ZEND_ARG_INFO(0, depth)
ZEND_END_ARG_INFO()
static int simdjson_pjh_resource_type;
static int simdjson_pj_resource_type;
extern unsigned char cplus_simdjson_isvalid(const char *json);
extern void cplus_simdjson_parse(const char *json, zval *return_value, unsigned char assoc, u_short depth);
extern void cplus_simdjson_key_value(const char *json, const char *key, zval *return_value, unsigned char assoc, u_short depth);
extern void cplus_simdjson_key_value_pjh(void *pjh, const char *key, zval *return_value, unsigned char assoc);
extern u_short cplus_simdjson_key_exists(const char *json, const char *key, u_short depth);
extern u_short cplus_simdjson_key_exists_pjh(void *pjh, const char *key);
extern void *cplus_simdjson_resource(const char *json, void *pj, u_short depth);
extern void cplus_simdjson_dtor(void *handle, u_short type);
PHP_FUNCTION (simdjson_isvalid) {
zend_string *json = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &json) == FAILURE) {
return;
}
short is_json = cplus_simdjson_isvalid(ZSTR_VAL(json));
ZVAL_BOOL(return_value, is_json);
}
PHP_FUNCTION (simdjson_decode) {
zend_bool assoc = 0;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
zend_string *json = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|bl", &json, &assoc, &depth) == FAILURE) {
return;
}
cplus_simdjson_parse(ZSTR_VAL(json), return_value, assoc, depth + 1);
}
PHP_FUNCTION (simdjson_key_value) {
zend_bool assoc = 0;
zval *json = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
zend_string *key = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS|bl", &json, &key, &assoc, &depth) == FAILURE) {
return;
}
if (IS_STRING == Z_TYPE_P(json)) {
cplus_simdjson_key_value(Z_STRVAL_P(json), ZSTR_VAL(key), return_value, assoc, depth + 1);
} else if (IS_RESOURCE == Z_TYPE_P(json)) {
void *pjh = zend_fetch_resource(Z_RES_P(json), "simdjson_pjh", simdjson_pjh_resource_type);
if (NULL == pjh) {
return;
}
cplus_simdjson_key_value_pjh(pjh, ZSTR_VAL(key), return_value, assoc);
} else {
php_error_docref(NULL, E_WARNING, "expects parameter 1 to be resource or string");
}
}
PHP_FUNCTION (simdjson_key_exists) {
zval *json = NULL;
zend_string *key = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS|l", &json, &key, &depth) == FAILURE) {
return;
}
u_short stats = SIMDJSON_PARSE_FAIL;
if (IS_STRING == Z_TYPE_P(json)) {
stats = cplus_simdjson_key_exists(Z_STRVAL_P(json), ZSTR_VAL(key), depth + 1);
} else if (IS_RESOURCE == Z_TYPE_P(json)) {
void *pjh = zend_fetch_resource(Z_RES_P(json), "simdjson_pjh", simdjson_pjh_resource_type);
if (NULL != pjh) {
stats = cplus_simdjson_key_exists_pjh(pjh, ZSTR_VAL(key));
}
} else {
php_error_docref(NULL, E_WARNING, "expects parameter 1 to be resource or string");
}
if (SIMDJSON_PARSE_FAIL == stats) {
RETURN_NULL();
} else if (SIMDJSON_PARSE_KEY_EXISTS == stats) {
RETURN_TRUE;
} else if (SIMDJSON_PARSE_KEY_NOEXISTS == stats) {
RETURN_FALSE;
}
}
PHP_FUNCTION (simdjson_resource) {
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
zend_string *json = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &json, &depth) == FAILURE) {
return;
}
void *pj = NULL;
void *pjh = cplus_simdjson_resource(ZSTR_VAL(json), pj, depth);
if (NULL == pjh) {
RETURN_NULL();
}
zend_resource *res = zend_register_resource(pjh, simdjson_pjh_resource_type);
zend_register_resource(pj, simdjson_pj_resource_type);
RETURN_RES(res);
}
void simdjson_dtor(zend_resource *res) {
if (simdjson_pjh_resource_type == res->type) {
cplus_simdjson_dtor(res->ptr, SIMDJSON_RESOUCE_PJH_TYPE);
} else if (simdjson_pj_resource_type == res->type) {
cplus_simdjson_dtor(res->ptr, SIMDJSON_RESOUCE_PJ_TYPE);
}
}
/* {{{ simdjson_functions[]
*/
zend_function_entry simdjson_functions[] = {
PHP_FE(simdjson_isvalid, simdjson_isvalid_arginfo)
PHP_FE(simdjson_decode, simdjson_decode_arginfo)
PHP_FE(simdjson_key_value, simdjson_key_value_arginfo)
PHP_FE(simdjson_key_exists, simdjson_key_exists_arginfo)
PHP_FE(simdjson_resource, simdjson_resource_arginfo)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ PHP_GINIT_FUNCTION
*/
PHP_GINIT_FUNCTION (simdjson) {
}
/* }}} */
/** {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION (simdjson) {
simdjson_pjh_resource_type = zend_register_list_destructors_ex(simdjson_dtor, NULL, "simdjson_pjh", module_number);
simdjson_pj_resource_type = zend_register_list_destructors_ex(simdjson_dtor, NULL, "simdjson_pj", module_number);
return SUCCESS;
}
/* }}} */
/** {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION (simdjson) {
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION (simdjson) {
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION (simdjson) {
return SUCCESS;
}
/* }}} */
/** {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION (simdjson) {
php_info_print_table_start();
if (PG(expose_php) && !sapi_module.phpinfo_as_text) {
php_info_print_table_header(2, "simdjson support", "enabled");
} else {
php_info_print_table_header(2, "simdjson support", "enabled");
}
php_info_print_table_row(2, "Version", PHP_SIMDJSON_VERSION);
php_info_print_table_row(2, "Support", SIMDJSON_SUPPORT_URL);
php_info_print_table_end();
}
/* }}} */
/** {{{ DL support
*/
#ifdef COMPILE_DL_SIMDJSON
ZEND_GET_MODULE(simdjson)
#endif
/* }}} */
/** {{{ module depends
*/
zend_module_dep simdjson_deps[] = {
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ simdjson_module_entry
*/
zend_module_entry simdjson_module_entry = {
STANDARD_MODULE_HEADER_EX, NULL,
simdjson_deps,
"simdjson",
simdjson_functions,
PHP_MINIT(simdjson),
PHP_MSHUTDOWN(simdjson),
PHP_RINIT(simdjson),
PHP_RSHUTDOWN(simdjson),
PHP_MINFO(simdjson),
PHP_SIMDJSON_VERSION,
PHP_MODULE_GLOBALS(simdjson),
PHP_GINIT(simdjson),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/