Skip to content

Commit

Permalink
Compatibility with zend_property API changes (#3578)
Browse files Browse the repository at this point in the history
  • Loading branch information
twose authored Aug 17, 2020
1 parent ac74094 commit c0dd12a
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 342 deletions.
46 changes: 24 additions & 22 deletions php_swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extern zend_module_entry swoole_module_entry;
#endif

#define SW_CHECK_RETURN(s) if(s<0){RETURN_FALSE;}else{RETURN_TRUE;}
#define SW_LOCK_CHECK_RETURN(s) if(s==0){RETURN_TRUE;}else{zend_update_property_long(NULL,ZEND_THIS,SW_STRL("errCode"),s);RETURN_FALSE;}
#define SW_LOCK_CHECK_RETURN(s) if(s==0){RETURN_TRUE;}else{zend_update_property_long(NULL,SW_Z8_OBJ_P(ZEND_THIS),SW_STRL("errCode"),s);RETURN_FALSE;}

#define php_swoole_fatal_error(level, fmt_str, ...) \
php_error_docref(NULL, level, (const char *) (fmt_str), ##__VA_ARGS__)
Expand Down Expand Up @@ -442,6 +442,18 @@ static sw_inline void _sw_zend_bailout(const char *filename, uint32_t lineno)
#endif
/*}}}*/

/* PHP 8 compatibility macro {{{*/
#if PHP_VERSION_ID < 80000
#define sw_zend7_object zval
#define SW_Z7_OBJ_P(object) Z_OBJ_P(object)
#define SW_Z8_OBJ_P(zobj) zobj
#else
#define sw_zend7_object zend_object
#define SW_Z7_OBJ_P(object) object
#define SW_Z8_OBJ_P(zobj) Z_OBJ_P(zobj)
#endif
/*}}}*/

/* PHP 7 wrapper functions / macros */

//----------------------------------Zval API------------------------------------
Expand Down Expand Up @@ -843,11 +855,11 @@ static sw_inline void sw_zend_class_unset_property_deny(zend_object *object, zen

static sw_inline zval* sw_zend_read_property(zend_class_entry *ce, zval *obj, const char *s, int len, int silent)
{
zval rv, *property = zend_read_property(ce, obj, s, len, silent, &rv);
zval rv, *property = zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
if (UNEXPECTED(property == &EG(uninitialized_zval)))
{
zend_update_property_null(ce, obj, s, len);
return zend_read_property(ce, obj, s, len, silent, &rv);
zend_update_property_null(ce, SW_Z8_OBJ_P(obj), s, len);
return zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
}
return property;
}
Expand All @@ -857,30 +869,30 @@ static sw_inline void sw_zend_update_property_null_ex(zend_class_entry *scope, z
zval tmp;

ZVAL_NULL(&tmp);
zend_update_property_ex(scope, object, s, &tmp);
zend_update_property_ex(scope, SW_Z8_OBJ_P(object), s, &tmp);
}

static sw_inline zval* sw_zend_read_property_ex(zend_class_entry *ce, zval *obj, zend_string *s, int silent)
{
zval rv, *property = zend_read_property_ex(ce, obj, s, silent, &rv);
zval rv, *property = zend_read_property_ex(ce, SW_Z8_OBJ_P(obj), s, silent, &rv);
if (UNEXPECTED(property == &EG(uninitialized_zval)))
{
sw_zend_update_property_null_ex(ce, obj, s);
return zend_read_property_ex(ce, obj, s, silent, &rv);
return zend_read_property_ex(ce, SW_Z8_OBJ_P(obj), s, silent, &rv);
}
return property;
}

static sw_inline zval* sw_zend_read_property_not_null(zend_class_entry *ce, zval *obj, const char *s, int len, int silent)
{
zval rv, *property = zend_read_property(ce, obj, s, len, silent, &rv);
zval rv, *property = zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
zend_uchar type = Z_TYPE_P(property);
return (type == IS_NULL || UNEXPECTED(type == IS_UNDEF)) ? NULL : property;
}

static sw_inline zval* sw_zend_read_property_not_null_ex(zend_class_entry *ce, zval *obj, zend_string *s, int silent)
{
zval rv, *property = zend_read_property_ex(ce, obj, s, silent, &rv);
zval rv, *property = zend_read_property_ex(ce, SW_Z8_OBJ_P(obj), s, silent, &rv);
zend_uchar type = Z_TYPE_P(property);
return (type == IS_NULL || UNEXPECTED(type == IS_UNDEF)) ? NULL : property;
}
Expand All @@ -889,14 +901,14 @@ static sw_inline zval *sw_zend_update_and_read_property_array(zend_class_entry *
{
zval ztmp;
array_init(&ztmp);
zend_update_property(ce, obj, s, len, &ztmp);
zend_update_property(ce, SW_Z8_OBJ_P(obj), s, len, &ztmp);
zval_ptr_dtor(&ztmp);
return zend_read_property(ce, obj, s, len, 1, &ztmp);
return zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, 1, &ztmp);
}

static sw_inline zval* sw_zend_read_and_convert_property_array(zend_class_entry *ce, zval *obj, const char *s, int len, int silent)
{
zval rv, *property = zend_read_property(ce, obj, s, len, silent, &rv);
zval rv, *property = zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
if (Z_TYPE_P(property) != IS_ARRAY)
{
// NOTICE: if user unset the property, zend_read_property will return uninitialized_zval instead of NULL pointer
Expand All @@ -920,16 +932,6 @@ static sw_inline zval* sw_zend_read_and_convert_property_array(zend_class_entry

//----------------------------------Function API------------------------------------

#if PHP_VERSION_ID < 80000
#define sw_zend7_object zval
#define SW_Z7_OBJ_P(object) Z_OBJ_P(object)
#define SW_Z8_OBJ_P(zobj) zobj
#else
#define sw_zend7_object zend_object
#define SW_Z7_OBJ_P(object) object
#define SW_Z8_OBJ_P(zobj) Z_OBJ_P(zobj)
#endif

/**
* Notice (sw_zend_call_method_with_%u_params): If you don't want to check the return value, please set retval to NULL
*/
Expand Down
14 changes: 7 additions & 7 deletions swoole_channel_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,20 @@ static PHP_METHOD(swoole_channel_coro, __construct)

channel_coro *chan_t = php_swoole_channel_coro_fetch_object(Z_OBJ_P(ZEND_THIS));
chan_t->chan = new Channel(capacity);
zend_update_property_long(swoole_channel_coro_ce, ZEND_THIS, ZEND_STRL("capacity"), capacity);
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("capacity"), capacity);
}

static PHP_METHOD(swoole_channel_coro, push)
{
Channel *chan = php_swoole_get_channel(ZEND_THIS);
if (chan->is_closed())
{
zend_update_property_long(swoole_channel_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), SW_CHANNEL_CLOSED);
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_CLOSED);
RETURN_FALSE;
}
else
{
zend_update_property_long(swoole_channel_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), SW_CHANNEL_OK);
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_OK);
}

zval *zdata;
Expand All @@ -204,7 +204,7 @@ static PHP_METHOD(swoole_channel_coro, push)
}
else
{
zend_update_property_long(swoole_channel_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), chan->is_closed() ? SW_CHANNEL_CLOSED : SW_CHANNEL_TIMEOUT);
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), chan->is_closed() ? SW_CHANNEL_CLOSED : SW_CHANNEL_TIMEOUT);
Z_TRY_DELREF_P(zdata);
efree(zdata);
RETURN_FALSE;
Expand All @@ -216,12 +216,12 @@ static PHP_METHOD(swoole_channel_coro, pop)
Channel *chan = php_swoole_get_channel(ZEND_THIS);
if (chan->is_closed())
{
zend_update_property_long(swoole_channel_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), SW_CHANNEL_CLOSED);
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_CLOSED);
RETURN_FALSE;
}
else
{
zend_update_property_long(swoole_channel_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), SW_CHANNEL_OK);
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), SW_CHANNEL_OK);
}

double timeout = -1;
Expand All @@ -239,7 +239,7 @@ static PHP_METHOD(swoole_channel_coro, pop)
}
else
{
zend_update_property_long(swoole_channel_coro_ce, ZEND_THIS, ZEND_STRL("errCode"), chan->is_closed() ? SW_CHANNEL_CLOSED : SW_CHANNEL_TIMEOUT);
zend_update_property_long(swoole_channel_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), chan->is_closed() ? SW_CHANNEL_CLOSED : SW_CHANNEL_TIMEOUT);
RETURN_FALSE;
}
}
Expand Down
30 changes: 15 additions & 15 deletions swoole_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static sw_inline Client *client_get_ptr(zval *zobject) {
return cli;
} else {
swoole_set_last_error(SW_ERROR_CLIENT_NO_CONNECTION);
zend_update_property_long(swoole_client_ce, zobject, ZEND_STRL("errCode"), swoole_get_last_error());
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(zobject), ZEND_STRL("errCode"), swoole_get_last_error());
php_swoole_error(E_WARNING, "client is not connected to server");
return nullptr;
}
Expand Down Expand Up @@ -680,14 +680,14 @@ Client *php_swoole_client_new(zval *zobject, char *host, int host_len, int port)
goto _create_socket;
}
cli->reuse_count++;
zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("reuseCount"), cli->reuse_count);
zend_update_property_long(Z_OBJCE_P(zobject), SW_Z8_OBJ_P(zobject), ZEND_STRL("reuseCount"), cli->reuse_count);
}
} else {
_create_socket:
cli = new Client(php_swoole_socktype(type), false);
if (cli->socket == nullptr) {
php_swoole_sys_error(E_WARNING, "Client_create() failed");
zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("errCode"), errno);
zend_update_property_long(Z_OBJCE_P(zobject), SW_Z8_OBJ_P(zobject), ZEND_STRL("errCode"), errno);
delete cli;
return nullptr;
}
Expand All @@ -697,7 +697,7 @@ Client *php_swoole_client_new(zval *zobject, char *host, int host_len, int port)
cli->server_strlen = conn_key.length();
}

zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("sock"), cli->socket->fd);
zend_update_property_long(Z_OBJCE_P(zobject), SW_Z8_OBJ_P(zobject), ZEND_STRL("sock"), cli->socket->fd);

if (type & SW_FLAG_KEEP) {
cli->keep = 1;
Expand Down Expand Up @@ -739,9 +739,9 @@ static PHP_METHOD(swoole_client, __construct) {
RETURN_FALSE;
}

zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("type"), type);
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("type"), type);
if (id) {
zend_update_property_stringl(swoole_client_ce, ZEND_THIS, ZEND_STRL("id"), id, len);
zend_update_property_stringl(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("id"), id, len);
}
// init
php_swoole_client_set_cli(ZEND_THIS, nullptr);
Expand Down Expand Up @@ -816,7 +816,7 @@ static PHP_METHOD(swoole_client, connect) {
php_swoole_client_set_cli(ZEND_THIS, cli);

if (cli->keep && cli->active) {
zend_update_property_bool(swoole_client_ce, ZEND_THIS, ZEND_STRL("reuse"), 1);
zend_update_property_bool(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("reuse"), 1);
RETURN_TRUE;
} else if (cli->active == 1) {
php_swoole_fatal_error(E_WARNING, "connection to the server has already been established");
Expand All @@ -839,10 +839,10 @@ static PHP_METHOD(swoole_client, connect) {
swoole_strerror(swoole_get_last_error()),
swoole_get_last_error());
}
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), swoole_get_last_error());
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), swoole_get_last_error());
} else {
php_swoole_sys_error(E_WARNING, "connect to server[%s:%d] failed", host, (int) port);
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), errno);
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), errno);
}
php_swoole_client_free(ZEND_THIS, cli);
RETURN_FALSE;
Expand Down Expand Up @@ -876,7 +876,7 @@ static PHP_METHOD(swoole_client, send) {
ssize_t ret = cli->send(cli, data, data_len, flags);
if (ret < 0) {
php_swoole_sys_error(E_WARNING, "failed to send(%d) %zu bytes", cli->socket->fd, data_len);
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), swoole_get_last_error());
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), swoole_get_last_error());
RETVAL_FALSE;
} else {
RETURN_LONG(ret);
Expand Down Expand Up @@ -924,13 +924,13 @@ static PHP_METHOD(swoole_client, sendto) {
(int) port,
swoole_strerror(swoole_get_last_error()),
swoole_get_last_error());
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), swoole_get_last_error());
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), swoole_get_last_error());
RETURN_FALSE;
}

if (!inet_ntop(cli->_sock_domain, addr, ip, sizeof(ip))) {
php_swoole_error(E_WARNING, "ip[%s] is invalid", ip);
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), errno);
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), errno);
RETURN_FALSE;
}
}
Expand Down Expand Up @@ -980,7 +980,7 @@ static PHP_METHOD(swoole_client, sendfile) {
"sendfile() failed. Error: %s [%d]",
swoole_strerror(swoole_get_last_error()),
swoole_get_last_error());
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), swoole_get_last_error());
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), swoole_get_last_error());
RETVAL_FALSE;
} else {
RETVAL_TRUE;
Expand Down Expand Up @@ -1036,7 +1036,7 @@ static PHP_METHOD(swoole_client, recv) {
if (ret < 0) {
swoole_set_last_error(errno);
php_swoole_sys_error(E_WARNING, "recv() failed");
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), swoole_get_last_error());
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), swoole_get_last_error());
buffer->length = 0;
RETURN_FALSE;
} else if (ret == 0) {
Expand Down Expand Up @@ -1160,7 +1160,7 @@ static PHP_METHOD(swoole_client, recv) {
if (ret < 0) {
swoole_set_last_error(errno);
php_swoole_sys_error(E_WARNING, "recv() failed");
zend_update_property_long(swoole_client_ce, ZEND_THIS, ZEND_STRL("errCode"), swoole_get_last_error());
zend_update_property_long(swoole_client_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("errCode"), swoole_get_last_error());
if (strbuf) {
zend_string_free(strbuf);
}
Expand Down
Loading

0 comments on commit c0dd12a

Please sign in to comment.