Skip to content

Commit

Permalink
Optimize code related to parameter parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 26, 2024
1 parent dd9c95e commit 28d20f1
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 139 deletions.
12 changes: 8 additions & 4 deletions ext-src/swoole_async_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,15 @@ PHP_FUNCTION(swoole_async_dns_lookup_coro) {
Coroutine::get_current_safe();

zval *domain;
long type = AF_INET;
zend_long type = AF_INET;
double timeout = swoole::network::Socket::default_dns_timeout;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|dl", &domain, &timeout, &type) == FAILURE) {
RETURN_FALSE;
}

ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_ZVAL(domain)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
Z_PARAM_LONG(type)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (Z_TYPE_P(domain) != IS_STRING) {
php_swoole_fatal_error(E_WARNING, "invalid domain name");
Expand Down
18 changes: 10 additions & 8 deletions ext-src/swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1169,10 +1169,11 @@ static PHP_METHOD(swoole_coroutine, exists) {
}

static PHP_METHOD(swoole_coroutine, resume) {
long cid;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &cid) == FAILURE) {
RETURN_FALSE;
}
zend_long cid;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(cid)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

auto coroutine_iterator = user_yield_coros.find(cid);
if (coroutine_iterator == user_yield_coros.end()) {
Expand Down Expand Up @@ -1283,10 +1284,11 @@ static PHP_METHOD(swoole_coroutine, join) {
}

static PHP_METHOD(swoole_coroutine, cancel) {
long cid;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &cid) == FAILURE) {
RETURN_FALSE;
}
zend_long cid;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(cid)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

Coroutine *co = swoole_coroutine_get(cid);
if (!co) {
Expand Down
30 changes: 15 additions & 15 deletions ext-src/swoole_coroutine_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ PHP_FUNCTION(swoole_coroutine_gethostbyname) {
zend_long family = AF_INET;
double timeout = -1;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ld", &domain_name, &l_domain_name, &family, &timeout) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STRING(domain_name, l_domain_name)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(family)
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (l_domain_name == 0) {
php_swoole_fatal_error(E_WARNING, "domain name is empty");
Expand Down Expand Up @@ -164,18 +167,15 @@ PHP_METHOD(swoole_coroutine_system, getaddrinfo) {
size_t l_service = 0;
double timeout = -1;

if (zend_parse_parameters(ZEND_NUM_ARGS(),
"s|lllsd",
&hostname,
&l_hostname,
&family,
&socktype,
&protocol,
&service,
&l_service,
&timeout) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(1, 6)
Z_PARAM_STRING(hostname, l_hostname)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(family)
Z_PARAM_LONG(socktype)
Z_PARAM_LONG(protocol)
Z_PARAM_STRING(service, l_service)
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (l_hostname == 0) {
php_swoole_fatal_error(E_WARNING, "hostname is empty");
Expand Down
8 changes: 5 additions & 3 deletions ext-src/swoole_http2_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1428,9 +1428,11 @@ static PHP_METHOD(swoole_http2_client_coro, stats) {

static PHP_METHOD(swoole_http2_client_coro, isStreamExist) {
zend_long stream_id = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &stream_id) == FAILURE) {
RETURN_FALSE;
}

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(stream_id)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (stream_id < 0) {
RETURN_FALSE;
}
Expand Down
25 changes: 16 additions & 9 deletions ext-src/swoole_http_response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ void php_swoole_http_response_minit(int module_number) {

static PHP_METHOD(swoole_http_response, write) {
zval *zdata;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zdata) == FAILURE) {
RETURN_FALSE;
}

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(zdata)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

HttpContext *ctx = php_swoole_http_response_get_and_check_context(ZEND_THIS);
if (UNEXPECTED(!ctx)) {
Expand Down Expand Up @@ -920,9 +921,12 @@ static PHP_METHOD(swoole_http_response, sendfile) {
zend_long offset = 0;
zend_long length = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &file, &l_file, &offset, &length) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STRING(file, l_file)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(offset)
Z_PARAM_LONG(length)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (l_file == 0) {
php_swoole_error(E_WARNING, "file name is empty");
Expand Down Expand Up @@ -1128,13 +1132,16 @@ static PHP_METHOD(swoole_http_response, goaway) {
php_swoole_fatal_error(E_WARNING, "fd[%ld] is not a HTTP2 conncetion", ctx->fd);
RETURN_FALSE;
}

zend_long error_code = SW_HTTP2_ERROR_NO_ERROR;
char *debug_data = nullptr;
size_t debug_data_len = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ls", &error_code, &debug_data, &debug_data_len) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(error_code)
Z_PARAM_STRING(debug_data, debug_data_len)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

SW_CHECK_RETURN(swoole_http2_server_goaway(ctx, error_code, debug_data, debug_data_len));
}
Expand Down
8 changes: 5 additions & 3 deletions ext-src/swoole_lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ static PHP_METHOD(swoole_lock, lock) {
static PHP_METHOD(swoole_lock, lockwait) {
double timeout = 1.0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "d", &timeout) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

Lock *lock = php_swoole_lock_get_and_check_ptr(ZEND_THIS);
if (lock->get_type() != Lock::MUTEX) {
zend_throw_exception(swoole_exception_ce, "only mutex supports lockwait", -2);
Expand Down
21 changes: 12 additions & 9 deletions ext-src/swoole_redis_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ static PHP_METHOD(swoole_redis_server, setHandler) {
size_t command_len;
zval *zcallback;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &command, &command_len, &zcallback) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(command, command_len)
Z_PARAM_ZVAL(zcallback)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

if (command_len == 0 || command_len >= SW_REDIS_MAX_COMMAND_SIZE) {
php_swoole_fatal_error(E_ERROR, "invalid command");
Expand Down Expand Up @@ -234,9 +235,9 @@ static PHP_METHOD(swoole_redis_server, getHandler) {
char *command;
size_t command_len;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &command, &command_len) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(command, command_len)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

char _command[SW_REDIS_MAX_COMMAND_SIZE];
size_t _command_len = sw_snprintf(_command, sizeof(_command), "_handler_%s", command);
Expand All @@ -251,9 +252,11 @@ static PHP_METHOD(swoole_redis_server, format) {
zend_long type;
zval *value = nullptr;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|z", &type, &value) == FAILURE) {
RETURN_FALSE;
}
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_LONG(type)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(value)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

char message[256];
int length;
Expand Down
Loading

0 comments on commit 28d20f1

Please sign in to comment.