Skip to content

Commit 7b74fc7

Browse files
committed
Add Fast ZPP string|int type check
1 parent 5a62840 commit 7b74fc7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

Zend/zend_API.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,23 @@ ZEND_API int ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest
506506
}
507507
/* }}} */
508508

509+
ZEND_API int ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long) /* {{{ */
510+
{
511+
if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
512+
return 0;
513+
}
514+
if (zend_parse_arg_long_weak(arg, dest_long)) {
515+
*dest_str = NULL;
516+
return 1;
517+
} else if (zend_parse_arg_str_weak(arg, dest_str)) {
518+
*dest_long = 0;
519+
return 1;
520+
} else {
521+
return 0;
522+
}
523+
}
524+
/* }}} */
525+
509526
static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, const char **spec, char **error) /* {{{ */
510527
{
511528
const char *spec_walk = *spec;

Zend/zend_API.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,8 @@ static zend_always_inline zval *zend_try_array_init(zval *zv)
11631163
_(Z_EXPECTED_NUMBER_OR_NULL, "of type int|float|null") \
11641164
_(Z_EXPECTED_STRING_OR_ARRAY, "of type string|array") \
11651165
_(Z_EXPECTED_STRING_OR_ARRAY_OR_NULL, "of type string|array|null") \
1166+
_(Z_EXPECTED_STRING_OR_LONG, "of type string|int") \
1167+
_(Z_EXPECTED_STRING_OR_LONG_OR_NULL, "of type string|int|null") \
11661168

11671169
#define Z_EXPECTED_TYPE
11681170

@@ -1598,6 +1600,19 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num
15981600
#define Z_PARAM_STR_OR_ARRAY_HT_OR_NULL(dest_str, dest_ht) \
15991601
Z_PARAM_STR_OR_ARRAY_HT_EX(dest_str, dest_ht, 1);
16001602

1603+
#define Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, is_null, allow_null) \
1604+
Z_PARAM_PROLOGUE(0, 0); \
1605+
if (UNEXPECTED(!zend_parse_arg_str_or_long(_arg, &dest_str, &dest_long, &is_null, allow_null))) { \
1606+
_expected_type = allow_null ? Z_EXPECTED_STRING_OR_LONG_OR_NULL : Z_EXPECTED_STRING_OR_LONG; \
1607+
_error_code = ZPP_ERROR_WRONG_ARG; \
1608+
break; \
1609+
}
1610+
1611+
#define Z_PARAM_STR_OR_LONG(dest_str, dest_long) \
1612+
Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, _dummy, 0);
1613+
1614+
#define Z_PARAM_STR_OR_LONG_OR_NULL(dest_str, dest_long, is_null) \
1615+
Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, is_null, 1);
16011616
/* End of new parameter parsing API */
16021617

16031618
/* Inlined implementations shared by new and old parameter parsing APIs */
@@ -1612,6 +1627,7 @@ ZEND_API int ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest);
16121627
ZEND_API int ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest);
16131628
ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest);
16141629
ZEND_API int ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest);
1630+
ZEND_API int ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long);
16151631

16161632
static zend_always_inline int zend_parse_arg_bool(zval *arg, zend_bool *dest, zend_bool *is_null, int check_null)
16171633
{
@@ -1835,6 +1851,26 @@ static zend_always_inline int zend_parse_arg_str_or_array_ht(
18351851
return 1;
18361852
}
18371853

1854+
static zend_always_inline int zend_parse_arg_str_or_long(zval *arg, zend_string **dest_str, zend_long *dest_long,
1855+
zend_bool *is_null, int allow_null)
1856+
{
1857+
if (allow_null) {
1858+
*is_null = 0;
1859+
}
1860+
if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
1861+
*dest_str = Z_STR_P(arg);
1862+
} else if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
1863+
*dest_str = NULL;
1864+
*dest_long = Z_LVAL_P(arg);
1865+
} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
1866+
*dest_str = NULL;
1867+
*is_null = 1;
1868+
} else {
1869+
return zend_parse_arg_str_or_long_slow(arg, dest_str, dest_long);
1870+
}
1871+
return 1;
1872+
}
1873+
18381874
END_EXTERN_C()
18391875

18401876
#endif /* ZEND_API_H */

0 commit comments

Comments
 (0)