-
Notifications
You must be signed in to change notification settings - Fork 3
/
func.c
412 lines (335 loc) · 11.5 KB
/
func.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h" /* for zend_alter_ini_entry */
#include "Zend/zend_interfaces.h" /* for zend_call_method_with_* */
#include "ext/standard/php_var.h"
#include "php_yii.h"
#include "func.h"
void yii_inherit_not_found(char *class_name, char *inherit_name) {
fprintf(stderr, "Error: Class to extend '%s' was not found when registering class '%s'\n", class_name, inherit_name);
}
void yii_concat(zval **left, zval *right TSRMLS_DC){
zval left_copy, right_copy;
uint length;
int use_copy_left = 0, use_copy_right = 0;
if (Z_TYPE_P(right) != IS_STRING) {
return;
}
if (Z_TYPE_PP(left) == IS_NULL) {
Z_STRVAL_PP(left) = emalloc(Z_STRLEN_P(right)+1);
memcpy(Z_STRVAL_PP(left), Z_STRVAL_P(right), Z_STRLEN_P(right));
Z_STRVAL_PP(left)[Z_STRLEN_P(right)] = 0;
Z_STRLEN_PP(left) = Z_STRLEN_P(right);
Z_TYPE_PP(left) = IS_STRING;
if (use_copy_right) {
zval_dtor(&right_copy);
}
return;
}
if (Z_TYPE_PP(left) != IS_STRING) {
return;
}
length = Z_STRLEN_PP(left) + Z_STRLEN_P(right);
Z_STRVAL_PP(left) = erealloc(Z_STRVAL_PP(left), length+1);
memcpy(Z_STRVAL_PP(left) + Z_STRLEN_PP(left), Z_STRVAL_P(right), Z_STRLEN_P(right));
Z_STRVAL_PP(left)[length] = 0;
Z_STRLEN_PP(left) = length;
Z_TYPE_PP(left) = IS_STRING;
if (use_copy_left) {
zval_dtor(&left_copy);
}
if (use_copy_right) {
zval_dtor(&right_copy);
}
}
int yii_call_class_static_method(zval *object, char *class_name, char *method_name, zval **retval_ptr_ptr, zend_uint param_count, zval **params[]) {
zval *method_name_zv, *class_name_zv, *fn;
int status = FAILURE;
zend_class_entry *ce, *active_scope = NULL;
/*if (Z_TYPE_P(object) != IS_OBJECT) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Call to method %s() on a non object", method_name);
return FAILURE;
}*/
if (object) {
active_scope = EG(scope);
ce = Z_OBJCE_P(object);
if (ce->parent) {
yii_find_scope(ce, method_name TSRMLS_CC);
}
}
ALLOC_INIT_ZVAL(fn);
array_init_size(fn, 2);
YII_NEW_STRING(class_name_zv, class_name);
add_next_index_zval(fn, class_name_zv);
YII_NEW_STRING(method_name_zv, method_name);
add_next_index_zval(fn, method_name_zv);
status = call_user_function_ex(CG(function_table), NULL, fn, retval_ptr_ptr, param_count, params, 1, NULL TSRMLS_CC);
if (object) {
EG(scope) = active_scope;
}
YII_PTR_DTOR(method_name_zv);
YII_PTR_DTOR(class_name_zv);
YII_PTR_DTOR(fn);
return status;
}
int yii_call_class_static_method_0(zval *object, char *class_name, char *method_name, zval **retval) {
return yii_call_class_static_method(object, class_name, method_name, retval, 0, NULL);
}
int yii_call_class_static_method_0_no(zval *object, char *class_name, char *method_name) {
zval *retval;
int status;
status = yii_call_class_static_method(object, class_name, method_name, &retval, 0, NULL);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_class_static_method_1(zval *object, char *class_name, char *method_name, zval **retval, zval *param) {
zval **params[1];
params[0] = ¶m;
return yii_call_class_static_method(object, class_name, method_name, retval, 1, params);
}
int yii_call_class_static_method_1_no(zval *object, char *class_name, char *method_name, zval *param) {
zval *retval;
int status;
zval **params[1];
params[0] = ¶m;
status = yii_call_class_static_method(object, class_name, method_name, &retval, 1, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_class_static_method_2(zval *object, char *class_name, char *method_name, zval **retval, zval *param1, zval *param2) {
zval **params[2];
params[0] = ¶m1;
params[1] = ¶m2;
return yii_call_class_static_method(object, class_name, method_name, retval, 2, params);
}
int yii_call_class_static_method_2_no(zval *object, char *class_name, char *method_name, zval *param1, zval *param2) {
zval *retval;
int status;
zval **params[2];
params[0] = ¶m1;
params[1] = ¶m2;
status = yii_call_class_static_method(object, class_name, method_name, &retval, 2, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_user_fun(const char *function_name, zval **retval_ptr_ptr, zend_uint param_count, zval **params[]) {
TSRMLS_FETCH();
zval *func_name;
YII_NEW_STRING(func_name, function_name);
if (call_user_function_ex(EG(function_table), NULL, func_name, retval_ptr_ptr, param_count, params, 1, NULL TSRMLS_CC) == FAILURE) {
return FAILURE;
}
YII_PTR_DTOR(func_name);
return SUCCESS;
}
int yii_call_user_fun_0(const char *func_name, zval **retval) {
if (yii_call_user_fun(func_name, retval, 0, NULL) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
int yii_call_user_fun_0_no(const char *func_name) {
zval *retval;
int status;
status = yii_call_user_fun(func_name, &retval, 0, NULL);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_user_fun_1(const char *func_name, zval **retval, zval *param) {
zval **params[1];
params[0] = ¶m;
if (yii_call_user_fun(func_name, retval, 1, params) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
int yii_call_user_fun_1_no(const char *func_name, zval *param) {
zval **params[1], *retval;
int status;
params[0] = ¶m;
status = yii_call_user_fun(func_name, &retval, 1, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_user_fun_2(const char *func_name, zval **retval, zval *param1, zval *param2) {
zval **params[2];
params[0] = ¶m1; params[1] = ¶m2;
if (yii_call_user_fun(func_name, retval, 2, params) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
int yii_call_user_fun_2_no(const char *func_name, zval *param1, zval *param2) {
zval **params[2], *retval;
int status;
params[0] = ¶m1; params[1] = ¶m2;
status = yii_call_user_fun(func_name, &retval, 2, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_user_fun_3(const char *func_name, zval **retval, zval *param1, zval *param2, zval *param3) {
zval **params[3];
params[0] = ¶m1; params[1] = ¶m2; params[2] = ¶m3;
if (yii_call_user_fun(func_name, retval, 3, params) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
int yii_call_user_fun_3_no(const char *func_name, zval *param1, zval *param2, zval *param3) {
zval **params[3], *retval;
int status;
params[0] = ¶m1; params[1] = ¶m2; params[2] = ¶m3;
status = yii_call_user_fun(func_name, &retval, 3, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_user_fun_4(const char *func_name, zval **retval, zval *param1, zval *param2, zval *param3, zval *param4) {
zval **params[4];
params[0] = ¶m1; params[1] = ¶m2; params[2] = ¶m3; params[3] = ¶m4;
if (yii_call_user_fun(func_name, retval, 4, params) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
int yii_call_user_fun_4_no(const char *func_name, zval *param1, zval *param2, zval *param3, zval *param4) {
zval **params[4], *retval;
int status;
params[0] = ¶m1; params[1] = ¶m2; params[2] = ¶m3; params[3] = ¶m4;
status = yii_call_user_fun(func_name, &retval, 4, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_user_fun_5(const char *func_name, zval **retval, zval *param1, zval *param2, zval *param3, zval *param4, zval *param5) {
zval **params[5];
params[0] = ¶m1; params[1] = ¶m2; params[2] = ¶m3; params[3] = ¶m4; params[4] = ¶m5;
if (yii_call_user_fun(func_name, retval, 5, params) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
int yii_call_user_fun_5_no(const char *func_name, zval *param1, zval *param2, zval *param3, zval *param4, zval *param5) {
zval **params[5], *retval;
int status;
params[0] = ¶m1; params[1] = ¶m2; params[2] = ¶m3; params[3] = ¶m4; params[4] = ¶m5;
status = yii_call_user_fun(func_name, &retval, 5, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_class_method(zval *object, char *method_name, zval **retval_ptr_ptr, zend_uint param_count, zval **params[]) {
TSRMLS_FETCH();
zend_class_entry *ce;
zval *func_name;
YII_NEW_STRING(func_name, method_name);
if (Z_TYPE_P(object) != IS_OBJECT) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Call to method %s() on a non object", method_name);
return FAILURE;
}
ce = Z_OBJCE_P(object);
if (ce->parent) {
yii_find_scope(ce, method_name TSRMLS_CC);
} else {
EG(scope) = ce;
}
if (call_user_function_ex(&ce->function_table, &object, func_name, retval_ptr_ptr, param_count, params, 0, NULL TSRMLS_CC) == FAILURE) {
return FAILURE;
}
YII_PTR_DTOR(func_name);
return SUCCESS;
}
int yii_call_class_method_no(zval *object, char *method_name, zend_uint param_count, zval **params[]) {
zval *retval;
int status;
status = yii_call_class_method(object, method_name, &retval, param_count, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_class_method_0(zval *object, char *method_name, zval **retval) {
return yii_call_class_method(object, method_name, retval, 0, NULL);
}
int yii_call_class_method_0_no(zval *object, char *method_name) {
zval *retval;
int status;
status = yii_call_class_method(object, method_name, &retval, 0, NULL);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_class_method_1(zval *object, char *method_name, zval **retval, zval *param) {
zval **params[1];
params[0] = ¶m;
return yii_call_class_method(object, method_name, retval, 1, params);
}
int yii_call_class_method_1_no(zval *object, char *method_name, zval *param) {
zval **params[1], *retval;
params[0] = ¶m;
int status;
status = yii_call_class_method(object, method_name, &retval, 1, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_call_class_method_2(zval *object, char *method_name, zval **retval, zval *param1, zval *param2) {
zval **params[2];
params[0] = ¶m1;
params[1] = ¶m2;
return yii_call_class_method(object, method_name, retval, 2, params);
}
int yii_call_class_method_2_no(zval *object, char *method_name, zval *param1, zval *param2) {
zval **params[2], *retval;
params[0] = ¶m1;
params[1] = ¶m2;
int status;
status = yii_call_class_method(object, method_name, &retval, 2, params);
YII_PTR_DTOR(retval);
return status;
}
int yii_find_scope(zend_class_entry *ce, char *method_name TSRMLS_DC){
char *lcname = zend_str_tolower_dup(method_name, strlen(method_name));
unsigned long hash = zend_inline_hash_func(lcname, strlen(method_name)+1);
while (ce) {
if (zend_hash_quick_exists(&ce->function_table, lcname, strlen(method_name)+1, hash)) {
EG(scope) = ce;
efree(lcname);
return SUCCESS;
}
ce = ce->parent;
}
if (lcname) {
efree(lcname);
}
return FAILURE;
}
int yii_class_has_constructor(zval *object TSRMLS_DC){
zend_class_entry *ce = Z_OBJCE_P(object);
while (ce) {
if (ce->constructor) {
return 1;
}
ce = ce->parent;
}
return 0;
}
int yii_new_class_instance(zval **return_value, char *class_name, zend_uint param_count, zval **params[] TSRMLS_DC){
zend_class_entry *ce;
ce = zend_fetch_class(class_name, strlen(class_name), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
if (!ce) {
return FAILURE;
}
ALLOC_INIT_ZVAL(*return_value);
object_init_ex(*return_value, ce);
if (yii_class_has_constructor(*return_value TSRMLS_CC)) {
if (yii_call_class_method_no(*return_value, "__construct" , param_count, params) == FAILURE) {
YII_PTR_DTOR(*return_value);
return FAILURE;
}
}
return SUCCESS;
}
int yii_new_class_instance_0(zval **return_value, char *class_name TSRMLS_DC) {
return yii_new_class_instance(return_value, class_name, 0, NULL);
}
int yii_new_class_instance_1(zval **return_value, char *class_name, zval *param TSRMLS_DC) {
zval **params[1];
params[0] = ¶m;
return yii_new_class_instance(return_value, class_name, 1, params);
}