From 0a1ab3d5b1cfde8b905192bec0535bc61b92871a Mon Sep 17 00:00:00 2001 From: twosee Date: Mon, 21 Sep 2020 19:05:45 +0800 Subject: [PATCH] Fix PHP8 build (ext/sockets compatibility) (#3684) --- php_swoole.h | 14 +++++++++++++- swoole_client.cc | 2 +- swoole_event.cc | 14 +++++++++++--- swoole_server.cc | 2 +- swoole_server_port.cc | 2 +- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/php_swoole.h b/php_swoole.h index 9137f21b8e..35be68aae7 100644 --- a/php_swoole.h +++ b/php_swoole.h @@ -487,7 +487,19 @@ static sw_inline void _sw_zend_bailout(const char *filename, uint32_t lineno) (ptr) = &(val); \ } while (0) -#define SW_ZEND_REGISTER_RESOURCE(return_value, result, le_result) ZVAL_RES(return_value,zend_register_resource(result, le_result)) +#if PHP_VERSION_ID < 80000 +#define SW_ZVAL_SOCKET(return_value, result) \ + ZVAL_RES(return_value,zend_register_resource((void *) (result), php_sockets_le_socket())) +#else +#define SW_ZVAL_SOCKET(return_value, result) \ + ZVAL_OBJ(return_value, &result->std) +#endif + +#if PHP_VERSION_ID < 80000 +#define SW_Z_SOCKET_P(zsocket) (php_socket *) zend_fetch_resource_ex(zsocket, nullptr, php_sockets_le_socket()) +#else +#define SW_Z_SOCKET_P(zsocket) Z_SOCKET_P(zsocket) +#endif #ifndef ZVAL_IS_BOOL static sw_inline zend_bool ZVAL_IS_BOOL(zval *v) diff --git a/swoole_client.cc b/swoole_client.cc index f2a23b8198..bc5de62d8d 100644 --- a/swoole_client.cc +++ b/swoole_client.cc @@ -1244,7 +1244,7 @@ static PHP_METHOD(swoole_client, getSocket) { if (!socket_object) { RETURN_FALSE; } - SW_ZEND_REGISTER_RESOURCE(return_value, (void *) socket_object, php_sockets_le_socket()); + SW_ZVAL_SOCKET(return_value, socket_object); zsocket = sw_zval_dup(return_value); Z_TRY_ADDREF_P(zsocket); php_swoole_client_set_zsocket(ZEND_THIS, zsocket); diff --git a/swoole_event.cc b/swoole_event.cc index 8dfcca37f2..8cc7fc2996 100644 --- a/swoole_event.cc +++ b/swoole_event.cc @@ -318,7 +318,7 @@ int php_swoole_convert_to_fd(zval *zsocket) { #ifdef SWOOLE_SOCKETS_SUPPORT else { php_socket *php_sock; - if ((php_sock = (php_socket *) zend_fetch_resource_ex(zsocket, nullptr, php_sockets_le_socket()))) { + if ((php_sock = SW_Z_SOCKET_P(zsocket))) { fd = php_sock->bsd_socket; return fd; } @@ -372,7 +372,7 @@ int php_swoole_convert_to_fd_ex(zval *zsocket, int *async) { #ifdef SWOOLE_SOCKETS_SUPPORT else { php_socket *php_sock; - if ((php_sock = (php_socket *) zend_fetch_resource_ex(zsocket, nullptr, php_sockets_le_socket()))) { + if ((php_sock = SW_Z_SOCKET_P(zsocket))) { fd = php_sock->bsd_socket; *async = 1; return fd; @@ -386,7 +386,9 @@ int php_swoole_convert_to_fd_ex(zval *zsocket, int *async) { #ifdef SWOOLE_SOCKETS_SUPPORT php_socket *php_swoole_convert_to_socket(int sock) { - php_socket *socket_object = (php_socket *) emalloc(sizeof *socket_object); + php_socket *socket_object; +#if PHP_VERSION_ID < 80000 + socket_object = (php_socket *) emalloc(sizeof *socket_object); sw_memset_zero(socket_object, sizeof(php_socket)); socket_object->bsd_socket = sock; socket_object->blocking = 1; @@ -410,6 +412,12 @@ php_socket *php_swoole_convert_to_socket(int sock) { } else { socket_object->blocking = !(t & O_NONBLOCK); } +#else + zval zsocket; + object_init_ex(&zsocket, socket_ce); + socket_object = Z_SOCKET_P(&zsocket); + socket_import_file_descriptor(sock, socket_object); +#endif return socket_object; } #endif diff --git a/swoole_server.cc b/swoole_server.cc index 5605a5de45..6e56733692 100644 --- a/swoole_server.cc +++ b/swoole_server.cc @@ -3529,7 +3529,7 @@ static PHP_METHOD(swoole_server, getSocket) { if (!socket_object) { RETURN_FALSE; } - SW_ZEND_REGISTER_RESOURCE(return_value, (void *) socket_object, php_sockets_le_socket()); + SW_ZVAL_SOCKET(return_value, socket_object); zval *zsocket = sw_zval_dup(return_value); Z_TRY_ADDREF_P(zsocket); } diff --git a/swoole_server_port.cc b/swoole_server_port.cc index 55093ea513..9be0b4e691 100644 --- a/swoole_server_port.cc +++ b/swoole_server_port.cc @@ -688,7 +688,7 @@ static PHP_METHOD(swoole_server_port, getSocket) { if (!socket_object) { RETURN_FALSE; } - SW_ZEND_REGISTER_RESOURCE(return_value, (void *) socket_object, php_sockets_le_socket()); + SW_ZVAL_SOCKET(return_value, socket_object); zval *zsocket = sw_zval_dup(return_value); Z_TRY_ADDREF_P(zsocket); }