forked from crazyxman/simdjson_php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_simdjson.cpp
292 lines (252 loc) · 9.36 KB
/
php_simdjson.cpp
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
/*
+----------------------------------------------------------------------+
| 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 <[email protected]> |
+----------------------------------------------------------------------+
*/
extern "C" {
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "main/SAPI.h"
#include "ext/standard/info.h"
#include "php_simdjson.h"
}
#include "src/bindings.h"
ZEND_DECLARE_MODULE_GLOBALS(simdjson);
#if PHP_VERSION_ID >= 70200
#define SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null)
#else
#define SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, NULL, allow_null)
#endif
SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_is_valid_arginfo, 0, 1, _IS_BOOL, 1)
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(simdjson_decode_arginfo, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, assoc, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(simdjson_key_value_arginfo, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, assoc, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
ZEND_END_ARG_INFO()
SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_exists_arginfo, 0, 2, _IS_BOOL, 1)
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
ZEND_END_ARG_INFO()
SIMDJSON_ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(simdjson_key_count_arginfo, 0, 2, IS_LONG, 1)
ZEND_ARG_TYPE_INFO(0, json, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, depth, IS_LONG, 0)
ZEND_END_ARG_INFO()
extern simdjson::dom::parser* cplus_simdjson_create_parser(void);
extern void cplus_simdjson_free_parser(simdjson::dom::parser* parser);
extern bool cplus_simdjson_is_valid(simdjson::dom::parser& parser, const char *json, size_t len, size_t depth);
extern void cplus_simdjson_parse(simdjson::dom::parser& parser, const char *json, size_t len, zval *return_value, unsigned char assoc, size_t depth);
extern void cplus_simdjson_key_value(simdjson::dom::parser& parser, const char *json, size_t len, const char *key, zval *return_value, unsigned char assoc, size_t depth);
extern u_short cplus_simdjson_key_exists(simdjson::dom::parser& parser, const char *json, size_t len, const char *key, size_t depth);
extern void cplus_simdjson_key_count(simdjson::dom::parser& parser, const char *json, size_t len, const char *key, zval *return_value, size_t depth);
#define SIMDJSON_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(simdjson, v)
static simdjson::dom::parser &simdjson_get_parser() {
simdjson::dom::parser *parser = (simdjson::dom::parser *)SIMDJSON_G(parser);
if (parser == NULL) {
parser = cplus_simdjson_create_parser();
SIMDJSON_G(parser) = parser;
ZEND_ASSERT(parser != NULL);
}
return *parser;
}
// The simdjson parser accepts strings with at most 32-bit lengths, for now.
#define SIMDJSON_MAX_DEPTH ((zend_long)((SIZE_MAX / 8) < (UINT32_MAX / 2) ? (SIZE_MAX / 8) : (UINT32_MAX / 2)))
static bool simdjson_validate_depth(zend_long depth) {
if (UNEXPECTED(depth <= 0)) {
php_error_docref(NULL, E_WARNING, "Depth must be greater than zero");
return false;
} else if (UNEXPECTED(depth > SIMDJSON_MAX_DEPTH)) {
php_error_docref(NULL, E_WARNING, "Depth exceeds maximum allowed value of " ZEND_LONG_FMT, SIMDJSON_MAX_DEPTH);
return false;
}
return true;
}
PHP_FUNCTION (simdjson_is_valid) {
zend_string *json = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &json, &depth) == FAILURE) {
return;
}
if (!simdjson_validate_depth(depth)) {
RETURN_NULL();
}
bool is_json = cplus_simdjson_is_valid(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), depth);
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;
}
if (!simdjson_validate_depth(depth)) {
RETURN_NULL();
}
cplus_simdjson_parse(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), return_value, assoc, depth);
}
PHP_FUNCTION (simdjson_key_value) {
zend_string *json = NULL;
zend_string *key = NULL;
zend_bool assoc = 0;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|bl", &json, &key, &assoc, &depth) == FAILURE) {
return;
}
if (!simdjson_validate_depth(depth)) {
RETURN_NULL();
}
cplus_simdjson_key_value(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), return_value, assoc, depth);
}
PHP_FUNCTION (simdjson_key_count) {
zend_string *json = NULL;
zend_string *key = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|l", &json, &key, &depth) == FAILURE) {
return;
}
if (!simdjson_validate_depth(depth)) {
RETURN_NULL();
}
cplus_simdjson_key_count(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), return_value, depth);
}
PHP_FUNCTION (simdjson_key_exists) {
zend_string *json = NULL;
zend_string *key = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|l", &json, &key, &depth) == FAILURE) {
return;
}
if (!simdjson_validate_depth(depth)) {
return;
}
u_short stats = cplus_simdjson_key_exists(simdjson_get_parser(), ZSTR_VAL(json), ZSTR_LEN(json), ZSTR_VAL(key), depth);
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;
}
}
/* {{{ simdjson_functions[]
*/
zend_function_entry simdjson_functions[] = {
PHP_FE(simdjson_is_valid, simdjson_is_valid_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_key_count, simdjson_key_count_arginfo)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ PHP_GINIT_FUNCTION
*/
PHP_GINIT_FUNCTION (simdjson) {
#if defined(COMPILE_DL_SIMDJSON) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
}
/* }}} */
/** {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION (simdjson) {
return SUCCESS;
}
/* }}} */
/** {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION (simdjson) {
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION (simdjson) {
SIMDJSON_G(parser) = NULL;
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION (simdjson) {
void *parser = SIMDJSON_G(parser);
if (parser != NULL) {
cplus_simdjson_free_parser((simdjson::dom::parser *) parser);
SIMDJSON_G(parser) = NULL;
}
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_row(2, "Implementation", simdjson::get_active_implementation()->description().c_str());
php_info_print_table_end();
}
/* }}} */
/** {{{ 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
};
/* }}} */
/** {{{ DL support
*/
#ifdef COMPILE_DL_SIMDJSON
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(simdjson)
#endif
/* }}} */