-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
executable file
·600 lines (487 loc) · 13 KB
/
main.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. If you did not receive
* a copy of the license it is available through the world-wide-web at the
* following url: https://docs.zephir-lang.com/en/latest/license
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include <php_ext.h>
#include <php_main.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>
#include <ext/spl/spl_exceptions.h>
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/fcall.h"
#include "kernel/object.h"
#include "kernel/exception.h"
zend_string* i_parent = NULL;
zend_string* i_static = NULL;
zend_string* i_self = NULL;
int zephir_is_iterable_ex(zval *arr, int duplicate)
{
if (UNEXPECTED(Z_TYPE_P(arr) == IS_OBJECT && zephir_instance_of_ev(arr, (const zend_class_entry *)zend_ce_iterator))) {
return 1;
} else if (UNEXPECTED(Z_TYPE_P(arr) != IS_ARRAY)) {
return 0;
}
//TODO: duplicate
return 1;
}
/**
* Parses method parameters with minimum overhead
*/
int zephir_fetch_parameters(int num_args, int required_args, int optional_args, ...)
{
va_list va;
int arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
zval *arg, **p;
int i;
if (num_args < required_args || (num_args > (required_args + optional_args))) {
zephir_throw_exception_string(spl_ce_BadMethodCallException, SL("Wrong number of parameters"));
return FAILURE;
}
if (num_args > arg_count) {
zephir_throw_exception_string(spl_ce_BadMethodCallException, SL("Could not obtain parameters for parsing"));
return FAILURE;
}
if (!num_args) {
return SUCCESS;
}
va_start(va, optional_args);
i = 0;
while (num_args-- > 0) {
arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
p = va_arg(va, zval **);
*p = arg;
i++;
}
va_end(va);
return SUCCESS;
}
/**
* Gets the global zval into PG macro
*/
int zephir_get_global(zval *arr, const char *global, unsigned int global_length)
{
zend_array *symbol_table;
zend_string *str = zend_string_init(global, global_length, 0);
if (PG(auto_globals_jit)) {
zend_is_auto_global(str);
}
if (&EG(symbol_table)) {
zval *gv;
if ((gv = zend_hash_find_ind(&EG(symbol_table), str)) != NULL) {
ZVAL_DEREF(gv);
if (Z_TYPE_P(gv) == IS_ARRAY) {
if (Z_REFCOUNTED_P(gv) && Z_REFCOUNT_P(gv) <= 1) {
ZVAL_COPY_VALUE(arr, gv);
} else {
ZVAL_DUP(arr, gv);
zend_hash_update(&EG(symbol_table), str, arr);
}
zend_string_release(str);
return SUCCESS;
}
}
}
array_init(arr);
if (!(&EG(symbol_table))) {
symbol_table = zend_rebuild_symbol_table();
} else {
symbol_table = &EG(symbol_table);
}
zend_hash_update(symbol_table, str, arr);
zend_string_release(str);
return FAILURE;
}
/**
* Makes fast count on implicit array types
*/
void zephir_fast_count(zval *result, zval *value)
{
if (Z_TYPE_P(value) == IS_ARRAY) {
ZVAL_LONG(result, zend_hash_num_elements(Z_ARRVAL_P(value)));
return;
}
if (Z_TYPE_P(value) == IS_OBJECT) {
zval retval;
if (Z_OBJ_HT_P(value)->count_elements) {
ZVAL_LONG(result, 1);
#if PHP_VERSION_ID >= 80000
if (SUCCESS == Z_OBJ_HT(*value)->count_elements(Z_OBJ_P(value), &Z_LVAL_P(result))) {
#else
if (SUCCESS == Z_OBJ_HT(*value)->count_elements(value, &Z_LVAL_P(result))) {
#endif
return;
}
}
if (instanceof_function(Z_OBJCE_P(value), spl_ce_Countable)) {
#if PHP_VERSION_ID >= 80000
zend_call_method_with_0_params(Z_OBJ_P(value), NULL, NULL, "count", &retval);
#else
zend_call_method_with_0_params(value, NULL, NULL, "count", &retval);
#endif
if (Z_TYPE(retval) != IS_UNDEF) {
convert_to_long_ex(&retval);
ZVAL_LONG(result, Z_LVAL(retval));
zval_ptr_dtor(&retval);
}
return;
}
ZVAL_LONG(result, 0);
return;
}
if (Z_TYPE_P(value) == IS_NULL) {
ZVAL_LONG(result, 0);
return;
}
ZVAL_LONG(result, 1);
}
/**
* Makes fast count on implicit array types without creating a return zval value
*/
int zephir_fast_count_ev(zval *value)
{
zend_long count = 0;
if (Z_TYPE_P(value) == IS_ARRAY) {
return zend_hash_num_elements(Z_ARRVAL_P(value)) > 0;
}
if (Z_TYPE_P(value) == IS_OBJECT) {
zval retval;
if (Z_OBJ_HT_P(value)->count_elements) {
#if PHP_VERSION_ID >= 80000
Z_OBJ_HT(*value)->count_elements(Z_OBJ_P(value), &count);
#else
Z_OBJ_HT(*value)->count_elements(value, &count);
#endif
return (int) count > 0;
}
if (instanceof_function(Z_OBJCE_P(value), spl_ce_Countable)) {
#if PHP_VERSION_ID >= 80000
zend_call_method_with_0_params(Z_OBJ_P(value), NULL, NULL, "count", &retval);
#else
zend_call_method_with_0_params(value, NULL, NULL, "count", &retval);
#endif
if (Z_TYPE(retval) != IS_UNDEF) {
convert_to_long_ex(&retval);
count = Z_LVAL(retval);
zval_ptr_dtor(&retval);
return (int) count > 0;
}
return 0;
}
return 0;
}
if (Z_TYPE_P(value) == IS_NULL) {
return 0;
}
return 1;
}
/**
* Makes fast count on implicit array types without creating a return zval value
*/
int zephir_fast_count_int(zval *value)
{
zend_long count = 0;
if (Z_TYPE_P(value) == IS_ARRAY) {
return zend_hash_num_elements(Z_ARRVAL_P(value));
}
if (Z_TYPE_P(value) == IS_OBJECT) {
zval retval;
if (Z_OBJ_HT_P(value)->count_elements) {
#if PHP_VERSION_ID >= 80000
Z_OBJ_HT(*value)->count_elements(Z_OBJ_P(value), &count);
#else
Z_OBJ_HT(*value)->count_elements(value, &count);
#endif
return (int) count;
}
if (instanceof_function(Z_OBJCE_P(value), spl_ce_Countable)) {
#if PHP_VERSION_ID >= 80000
zend_call_method_with_0_params(Z_OBJ_P(value), NULL, NULL, "count", &retval);
#else
zend_call_method_with_0_params(value, NULL, NULL, "count", &retval);
#endif
if (Z_TYPE(retval) != IS_UNDEF) {
convert_to_long_ex(&retval);
count = Z_LVAL(retval);
zval_ptr_dtor(&retval);
return (int) count;
}
return 0;
}
return 0;
}
if (Z_TYPE_P(value) == IS_NULL) {
return 0;
}
return 1;
}
/**
* Check if a function exists
*
* @param function_name
* @return
*/
int zephir_function_exists(const zval *function_name)
{
if (zend_hash_str_exists(CG(function_table), Z_STRVAL_P(function_name), Z_STRLEN_P(function_name)) != NULL) {
return SUCCESS;
}
return FAILURE;
}
/**
* Check if a function exists using explicit function length
*
* TODO: Check if make sense to merge all logic of IS_STRING inside zephir_function_exists() function.
* @param function_name
* @param function_len strlen(function_name) + 1
*/
int zephir_function_exists_ex(const char *function_name, unsigned int function_len)
{
if (zend_hash_str_exists(CG(function_table), function_name, function_len) != NULL) {
return SUCCESS;
}
return FAILURE;
}
/**
* Checks if a zval is callable
*/
int zephir_is_callable(zval *var)
{
char *error = NULL;
zend_bool retval;
retval = zend_is_callable_ex(var, NULL, 0, NULL, NULL, &error);
if (error) {
efree(error);
}
return (int) retval;
}
/**
* Checks whether a variable has a scalar type
*/
int zephir_is_scalar(zval *var)
{
switch (Z_TYPE_P(var)) {
case IS_TRUE:
case IS_FALSE:
case IS_DOUBLE:
case IS_LONG:
case IS_STRING:
return 1;
break;
}
return 0;
}
/**
* Returns the type of a variable as a string
*/
void zephir_gettype(zval *return_value, zval *arg)
{
switch (Z_TYPE_P(arg)) {
case IS_NULL:
RETVAL_STRING("NULL");
break;
case IS_TRUE:
case IS_FALSE:
RETVAL_STRING("boolean");
break;
case IS_LONG:
RETVAL_STRING("integer");
break;
case IS_DOUBLE:
RETVAL_STRING("double");
break;
case IS_STRING:
RETVAL_STRING("string");
break;
case IS_ARRAY:
RETVAL_STRING("array");
break;
case IS_OBJECT:
RETVAL_STRING("object");
break;
case IS_RESOURCE:
{
const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
if (type_name) {
RETVAL_STRING("resource");
break;
}
}
/* no break */
default:
RETVAL_STRING("unknown type");
}
}
zend_class_entry* zephir_get_internal_ce(const char *class_name, unsigned int class_name_len)
{
zend_class_entry* temp_ce;
if ((temp_ce = zend_hash_str_find_ptr(CG(class_table), class_name, class_name_len)) == NULL) {
zend_error(E_ERROR, "Class '%s' not found", class_name);
return NULL;
}
return temp_ce;
}
/* Declare constants */
int zephir_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value)
{
#if PHP_VERSION_ID >= 80000
zend_string *key;
if (ce->type == ZEND_INTERNAL_CLASS) {
key = zend_string_init_interned(name, name_length, 1);
} else {
key = zend_string_init(name, name_length, 0);
}
zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
if (ce->type != ZEND_INTERNAL_CLASS) {
zend_string_release(key);
}
return SUCCESS;
#else
int ret;
zend_string *key;
if (ce->type == ZEND_INTERNAL_CLASS) {
key = zend_string_init_interned(name, name_length, 1);
} else {
key = zend_string_init(name, name_length, 0);
}
ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
if (ce->type != ZEND_INTERNAL_CLASS) {
zend_string_release(key);
}
return ret;
#endif
}
int zephir_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length)
{
zval constant;
ZVAL_NULL(&constant);
return zephir_declare_class_constant(ce, name, name_length, &constant);
}
int zephir_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value)
{
zval constant;
ZVAL_LONG(&constant, value);
return zephir_declare_class_constant(ce, name, name_length, &constant);
}
int zephir_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value)
{
zval constant;
ZVAL_BOOL(&constant, value);
return zephir_declare_class_constant(ce, name, name_length, &constant);
}
int zephir_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value)
{
zval constant;
ZVAL_DOUBLE(&constant, value);
return zephir_declare_class_constant(ce, name, name_length, &constant);
}
int zephir_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length)
{
zval constant;
ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
return zephir_declare_class_constant(ce, name, name_length, &constant);
}
int zephir_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value)
{
return zephir_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
}
/**
* Check is PHP Version equals to Runtime PHP Version
*/
int zephir_is_php_version(unsigned int id)
{
int php_major = PHP_MAJOR_VERSION * 10000;
int php_minor = PHP_MINOR_VERSION * 100;
int php_release = PHP_RELEASE_VERSION;
int zep_major = id / 10000;
int zep_minor = id / 100 - zep_major * 100;
int zep_release = id - (zep_major * 10000 + zep_minor * 100);
if (zep_minor == 0)
{
php_minor = 0;
}
if (zep_release == 0)
{
php_release = 0;
}
return ((php_major + php_minor + php_release) == id ? 1 : 0);
}
void zephir_get_args(zval *return_value)
{
zend_execute_data *ex = EG(current_execute_data);
uint32_t arg_count = ZEND_CALL_NUM_ARGS(ex);
array_init_size(return_value, arg_count);
if (arg_count) {
uint32_t first_extra_arg = ex->func->op_array.num_args;
zval *p = ZEND_CALL_ARG(ex, 1);
uint32_t i = 0;
if (arg_count > first_extra_arg) {
while (i < first_extra_arg) {
zval *q = p;
if (Z_TYPE_P(q) != IS_UNDEF) {
ZVAL_DEREF(q);
Z_TRY_ADDREF_P(q);
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), q);
}
++p;
++i;
}
p = ZEND_CALL_VAR_NUM(ex, i);
}
while (i < arg_count) {
zval *q = p;
if (Z_TYPE_P(q) != IS_UNDEF) {
ZVAL_DEREF(q);
Z_TRY_ADDREF_P(q);
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), q);
}
++p;
++i;
}
}
}
void zephir_get_arg(zval *return_value, zend_long idx)
{
zend_execute_data *ex = EG(current_execute_data);
uint32_t arg_count;
zval *arg;
if (UNEXPECTED(idx < 0)) {
zend_error(E_WARNING, "func_get_arg(): The argument number should be >= 0");
RETURN_FALSE;
}
arg_count = ZEND_CALL_NUM_ARGS(ex);
if (zend_forbid_dynamic_call("func_get_arg()") == FAILURE) {
RETURN_FALSE;
}
if (UNEXPECTED((zend_ulong)idx >= arg_count)) {
zend_error(E_WARNING, "func_get_arg(): Argument " ZEND_LONG_FMT " not passed to function", idx);
RETURN_FALSE;
}
arg = ZEND_CALL_VAR_NUM(ex, idx);
if (EXPECTED(!Z_ISUNDEF_P(arg))) {
ZVAL_DEREF(arg);
ZVAL_COPY(return_value, arg);
return;
}
RETURN_NULL();
}
void zephir_module_init()
{
/* Though these strings won't be interned in ZTS,
* we still benefit from using zend_string* instead of char*
* in hash tables
*/
i_parent = zend_new_interned_string(zend_string_init(ZEND_STRL("parent"), 1));
i_static = zend_new_interned_string(zend_string_init(ZEND_STRL("static"), 1));
i_self = zend_new_interned_string(zend_string_init(ZEND_STRL("self"), 1));
}