-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmustache_ast.cpp
375 lines (311 loc) · 10.5 KB
/
mustache_ast.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
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_mustache.h"
#include "mustache_private.hpp"
#include "mustache_exceptions.hpp"
#include "mustache_ast.hpp"
/* {{{ ZE2 OO definitions */
zend_class_entry * MustacheAST_ce_ptr;
static zend_object_handlers MustacheAST_obj_handlers;
/* }}} */
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____construct_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_ARG_INFO(0, vars)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____sleep_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(MustacheAST__toArray_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
#if PHP_VERSION_ID >= 80200
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(MustacheAST____toString_args, 0, 0, IS_STRING, 0)
#else
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____toString_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
#endif
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(MustacheAST____wakeup_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ MustacheAST_methods */
static zend_function_entry MustacheAST_methods[] = {
PHP_ME(MustacheAST, __construct, MustacheAST____construct_args, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(MustacheAST, __sleep, MustacheAST____sleep_args, ZEND_ACC_PUBLIC)
PHP_ME(MustacheAST, toArray, MustacheAST__toArray_args, ZEND_ACC_PUBLIC)
PHP_ME(MustacheAST, __toString, MustacheAST____toString_args, ZEND_ACC_PUBLIC)
PHP_ME(MustacheAST, __wakeup, MustacheAST____wakeup_args, ZEND_ACC_PUBLIC)
{ NULL, NULL, NULL }
};
/* }}} */
/* {{{ mustache_node_from_binary_string */
void mustache_node_from_binary_string(mustache::Node ** node, char * str, int len)
{
std::vector<uint8_t> uint_str;
uint_str.resize(len);
int i = 0;
for( i = 0; i < len; i++ ) {
uint_str[i] = str[i];
}
size_t vpos = 0;
*node = mustache::Node::unserialize(uint_str, 0, &vpos);
}
/* }}} */
/* {{{ mustache_node_to_binary_string */
void mustache_node_to_binary_string(mustache::Node * node, char ** estr, int * elen)
{
std::vector<uint8_t> * serialPtr = node->serialize();
std::vector<uint8_t> & serial = *serialPtr;
int serialLen = serial.size();
char * str = (char *) emalloc(sizeof(char *) * (serialLen + 1));
for( int i = 0 ; i < serialLen; i++ ) {
str[i] = (char) serial[i];
}
str[serialLen] = '\0';
delete serialPtr;
*elen = serialLen;
*estr = str;
}
/* }}} */
/* {{{ mustache_node_to_zval */
void mustache_node_to_zval(mustache::Node * node, zval * current)
{
zval children = {0};
zval child = {0};
array_init(current);
// Basic data
add_assoc_long(current, "type", node->type);
add_assoc_long(current, "flags", node->flags);
if( NULL != node->data && node->data->length() > 0 ) {
add_assoc_stringl_ex(current, ZEND_STRL("data"), (char *) node->data->c_str(), node->data->length());
}
// Children
if( node->children.size() > 0 ) {
ZVAL_NULL(&children);
array_init(&children);
mustache::Node::Children::iterator it;
for ( it = node->children.begin() ; it != node->children.end(); it++ ) {
ZVAL_NULL(&child);
mustache_node_to_zval(*it, &child);
add_next_index_zval(&children, &child);
}
add_assoc_zval(current, "children", &children);
}
// Partials
if( node->partials.size() > 0 ) {
ZVAL_NULL(&children);
array_init(&children);
mustache::Node::Partials::iterator it;
for ( it = node->partials.begin() ; it != node->partials.end(); it++ ) {
ZVAL_NULL(&child);
mustache_node_to_zval(&(it->second), &child);
add_assoc_zval(&children, it->first.c_str(), &child);
}
add_assoc_zval(current, "partials", &children);
}
}
/* }}} */
/* {{{ php_mustache_ast_object_fetch_object */
static inline struct php_obj_MustacheAST * php_mustache_ast_fetch_object(zend_object * obj)
{
return (struct php_obj_MustacheAST *)((char*)(obj) - XtOffsetOf(struct php_obj_MustacheAST, std));
}
struct php_obj_MustacheAST * php_mustache_ast_object_fetch_object(zval * zv)
{
return php_mustache_ast_fetch_object(Z_OBJ_P(zv));
}
/* }}} */
/* {{{ MustacheAST_obj_free */
static void MustacheAST_obj_free(zend_object * object)
{
try {
struct php_obj_MustacheAST * payload = php_mustache_ast_fetch_object(object);
if( payload->node != NULL ) {
delete payload->node;
}
zend_object_std_dtor((zend_object *)object);
} catch(...) {
mustache_exception_handler();
}
}
/* }}} */
/* {{{ MustacheAST_obj_create */
static zend_object * MustacheAST_obj_create(zend_class_entry * ce)
{
struct php_obj_MustacheAST * intern;
try {
intern = (struct php_obj_MustacheAST *) ecalloc(1, sizeof(struct php_obj_MustacheAST) + zend_object_properties_size(ce));
zend_object_std_init(&intern->std, ce);
intern->std.handlers = &MustacheAST_obj_handlers;
intern->node = NULL;
return &intern->std;
} catch(...) {
mustache_exception_handler();
}
return NULL;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(mustache_ast)
{
try {
zend_class_entry ce;
memcpy(&MustacheAST_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
MustacheAST_obj_handlers.offset = XtOffsetOf(struct php_obj_MustacheAST, std);
MustacheAST_obj_handlers.free_obj = MustacheAST_obj_free;
MustacheAST_obj_handlers.clone_obj = NULL;
INIT_CLASS_ENTRY(ce, "MustacheAST", MustacheAST_methods);
ce.create_object = MustacheAST_obj_create;
MustacheAST_ce_ptr = zend_register_internal_class(&ce);
MustacheAST_ce_ptr->create_object = MustacheAST_obj_create;
zend_declare_property_null(MustacheAST_ce_ptr, ZEND_STRL("binaryString"), ZEND_ACC_PROTECTED);
return SUCCESS;
} catch(...) {
mustache_exception_handler();
return FAILURE;
}
}
/* }}} */
/* {{{ proto void MustacheAST::__construct(string binaryString) */
PHP_METHOD(MustacheAST, __construct)
{
try {
// Custom parameters
char * str = NULL;
long str_len = 0;
// Check parameters
zval * _this_zval = NULL;
if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O|s",
&_this_zval, MustacheAST_ce_ptr, &str, &str_len) == FAILURE) {
throw PhpInvalidParameterException();
}
// Class parameters
_this_zval = getThis();
struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
// Check payload
if( payload->node != NULL ) {
throw InvalidParameterException("MustacheAST is already initialized");
}
// Unserialize
mustache_node_from_binary_string(&payload->node, str, str_len);
} catch(...) {
mustache_exception_handler();
}
}
/* }}} MustacheAST::__construct */
/* {{{ proto void MustacheAST::__sleep() */
PHP_METHOD(MustacheAST, __sleep)
{
try {
// Check parameters
zval * _this_zval = NULL;
if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
&_this_zval, MustacheAST_ce_ptr) == FAILURE) {
throw PhpInvalidParameterException();
}
// Class parameters
_this_zval = getThis();
struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
array_init(return_value);
// Check payload
if( payload->node != NULL ) {
// Serialize and store
char * str = NULL;
int len = 0;
mustache_node_to_binary_string(payload->node, &str, &len);
if( str != NULL ) {
#if PHP_VERSION_ID < 80000
zend_update_property_stringl(MustacheAST_ce_ptr, _this_zval, ZEND_STRL("binaryString"), str, len);
#else
zend_update_property_stringl(MustacheAST_ce_ptr, Z_OBJ_P(_this_zval), ZEND_STRL("binaryString"), str, len);
#endif
add_next_index_string(return_value, "binaryString");
efree(str);
}
}
} catch(...) {
mustache_exception_handler();
}
}
/* }}} MustacheAST::__sleep */
/* {{{ proto array MustacheAST::toArray() */
PHP_METHOD(MustacheAST, toArray)
{
try {
// Check parameters
zval * _this_zval = NULL;
if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
&_this_zval, MustacheAST_ce_ptr) == FAILURE) {
throw PhpInvalidParameterException();
}
// Class parameters
_this_zval = getThis();
struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
// Check payload
if( payload->node == NULL ) {
throw InvalidParameterException("MustacheAST was not initialized properly");
}
// Convert to PHP array
mustache_node_to_zval(payload->node, return_value);
} catch(...) {
mustache_exception_handler();
}
}
/* }}} MustacheAST::toArray */
/* {{{ proto string MustacheAST::__toString() */
PHP_METHOD(MustacheAST, __toString)
{
try {
// Check parameters
zval * _this_zval = NULL;
if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
&_this_zval, MustacheAST_ce_ptr) == FAILURE) {
throw PhpInvalidParameterException();
}
// Class parameters
_this_zval = getThis();
struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
// Check payload
if( payload->node == NULL ) {
throw InvalidParameterException("MustacheAST was not initialized properly");
}
// Convert to PHP binary string
char * str = NULL;
int len = 0;
mustache_node_to_binary_string(payload->node, &str, &len);
if( str != NULL ) {
RETVAL_STRINGL(str, len);
efree(str);
}
} catch(...) {
mustache_exception_handler();
}
}
/* }}} MustacheAST::__toString */
/* {{{ proto void MustacheAST::__wakeup() */
static inline void php_mustache_ast_wakeup(zval * _this_zval, zval * return_value)
{
zval rv;
struct php_obj_MustacheAST * payload = php_mustache_ast_object_fetch_object(_this_zval);
#if PHP_VERSION_ID < 80000
zval * value = zend_read_property(Z_OBJCE_P(_this_zval), _this_zval, "binaryString", sizeof("binaryString")-1, 1, &rv);
#else
zval * value = zend_read_property(Z_OBJCE_P(_this_zval), Z_OBJ_P(_this_zval), "binaryString", sizeof("binaryString")-1, 1, &rv);
#endif
if( Z_TYPE_P(value) == IS_STRING && Z_STRLEN_P(value) > 0 ) {
mustache_node_from_binary_string(&payload->node, Z_STRVAL_P(value), Z_STRLEN_P(value));
}
}
PHP_METHOD(MustacheAST, __wakeup)
{
try {
// Check parameters
zval * _this_zval = NULL;
if( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), (char *) "O",
&_this_zval, MustacheAST_ce_ptr) == FAILURE) {
throw PhpInvalidParameterException();
}
php_mustache_ast_wakeup(getThis(), return_value);
} catch(...) {
mustache_exception_handler();
}
}
/* }}} MustacheAST::__wakeup */