Skip to content

Commit

Permalink
Compatible with PHP 8.4 - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFreeman committed Aug 29, 2024
1 parent d14d6a2 commit fae814f
Show file tree
Hide file tree
Showing 16 changed files with 1,222 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [ '8.0', '8.1', '8.2', '8.3' ]
php-version: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
framework: [ 'Laravel Octane', 'Hyperf', 'Simps', 'imi' ]
name: ${{ matrix.framework }} - PHP ${{ matrix.php-version }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.0', '8.1', '8.2', '8.3', '8.4-dev']
steps:
- uses: actions/checkout@v4
- name: Setup PHP
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/xdebug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.0', '8.1', '8.2', '8.3', '8.4-dev']

name: PHP ${{ matrix.php }} - Swoole

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PROJECT(libswoole)

ENABLE_LANGUAGE(ASM)
set(SWOOLE_VERSION 5.1.4)
set(SWOOLE_VERSION 5.1.5-dev)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")
Expand Down
4 changes: 3 additions & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,8 @@ EOF
thirdparty/php/sockets/conversions.cc \
thirdparty/php/sockets/sockaddr_conv.cc \
thirdparty/php/standard/var_decoder.cc \
thirdparty/php/standard/proc_open.cc"
thirdparty/php/standard/proc_open.cc \
thirdparty/php84/hash/hash_sha.c "

swoole_source_file="$swoole_source_file \
thirdparty/swoole_http_parser.c \
Expand Down Expand Up @@ -1232,6 +1233,7 @@ EOF
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/php/sockets)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/php/standard)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/php/curl)
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/php84/hash)
if test "$PHP_NGHTTP2_DIR" = "no"; then
PHP_ADD_BUILD_DIR($ext_builddir/thirdparty/nghttp2)
fi
Expand Down
1 change: 1 addition & 0 deletions ext-src/php_swoole_coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#define SW_DEFAULT_MAX_CORO_NUM 100000
#define SW_DEFAULT_PHP_STACK_PAGE_SIZE 8192
enum sw_exit_flags { SW_EXIT_IN_COROUTINE = 1 << 1, SW_EXIT_IN_SERVER = 1 << 2 };

#define SWOG ((zend_output_globals *) &OG(handlers))

Expand Down
4 changes: 4 additions & 0 deletions ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ extern zend_object_handlers swoole_exception_handlers;
extern zend_class_entry *swoole_error_ce;
extern zend_class_entry *swoole_resolve_context_ce;
extern zend_object_handlers swoole_resolve_context_handlers;
#if PHP_VERSION_ID >= 80400
extern zend_class_entry *swoole_exit_exception_ce;
extern zend_object_handlers swoole_exit_exception_handlers;
#endif

PHP_FUNCTION(swoole_clear_dns_cache);
PHP_FUNCTION(swoole_last_error);
Expand Down
22 changes: 17 additions & 5 deletions ext-src/swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include "php_swoole_cxx.h"
#include "php_swoole_coroutine_system.h"

#if PHP_VERSION_ID < 80400
#include "swoole_server.h"
#endif
#include "swoole_signal.h"

#include "zend_builtin_functions.h"
Expand Down Expand Up @@ -55,8 +57,6 @@ static zend_always_inline zend_vm_stack zend_vm_stack_new_page(size_t size, zend
}
#endif

enum sw_exit_flags { SW_EXIT_IN_COROUTINE = 1 << 1, SW_EXIT_IN_SERVER = 1 << 2 };

bool PHPCoroutine::activated = false;
zend_array *PHPCoroutine::options = nullptr;

Expand All @@ -73,19 +73,27 @@ bool PHPCoroutine::interrupt_thread_running = false;

extern void php_swoole_load_library();

static zend_atomic_bool *zend_vm_interrupt = nullptr;
#if PHP_VERSION_ID < 80400
static user_opcode_handler_t ori_exit_handler = nullptr;
#endif

static zend_atomic_bool *zend_vm_interrupt = nullptr;
static user_opcode_handler_t ori_begin_silence_handler = nullptr;
static user_opcode_handler_t ori_end_silence_handler = nullptr;
static unordered_map<long, Coroutine *> user_yield_coros;

static void (*orig_interrupt_function)(zend_execute_data *execute_data) = nullptr;

static zend_class_entry *swoole_coroutine_util_ce;
static zend_class_entry *swoole_exit_exception_ce;
static zend_object_handlers swoole_exit_exception_handlers;
static zend_class_entry *swoole_coroutine_iterator_ce;
static zend_class_entry *swoole_coroutine_context_ce;
#if PHP_VERSION_ID >= 80400
zend_class_entry *swoole_exit_exception_ce;
zend_object_handlers swoole_exit_exception_handlers;
#else
static zend_class_entry *swoole_exit_exception_ce;
static zend_object_handlers swoole_exit_exception_handlers;
#endif

SW_EXTERN_C_BEGIN
static PHP_METHOD(swoole_coroutine, exists);
Expand Down Expand Up @@ -180,6 +188,7 @@ static const zend_function_entry swoole_exit_exception_methods[] = {
};
// clang-format on

#if PHP_VERSION_ID < 80400
static int coro_exit_handler(zend_execute_data *execute_data) {
zval ex;
zend_object *obj;
Expand Down Expand Up @@ -224,6 +233,7 @@ static int coro_exit_handler(zend_execute_data *execute_data) {

return ZEND_USER_OPCODE_DISPATCH;
}
#endif

static int coro_begin_silence_handler(zend_execute_data *execute_data) {
PHPContext *task = PHPCoroutine::get_context();
Expand Down Expand Up @@ -989,8 +999,10 @@ void php_swoole_coroutine_minit(int module_number) {

void php_swoole_coroutine_rinit() {
if (SWOOLE_G(cli)) {
#if PHP_VERSION_ID < 80400
ori_exit_handler = zend_get_user_opcode_handler(ZEND_EXIT);
zend_set_user_opcode_handler(ZEND_EXIT, coro_exit_handler);
#endif

ori_begin_silence_handler = zend_get_user_opcode_handler(ZEND_BEGIN_SILENCE);
zend_set_user_opcode_handler(ZEND_BEGIN_SILENCE, coro_begin_silence_handler);
Expand Down
6 changes: 6 additions & 0 deletions ext-src/swoole_mysql_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@
#undef L64

SW_EXTERN_C_BEGIN

#if PHP_VERSION_ID >= 80400
#include "thirdparty/php84/hash/php_hash.h"
#include "thirdparty/php84/hash/php_hash_sha.h"
#else
#include "ext/hash/php_hash.h"
#include "ext/hash/php_hash_sha.h"
#endif
#include "ext/standard/php_math.h"
#ifdef SW_USE_MYSQLND
#include "ext/mysqlnd/mysqlnd.h"
Expand Down
55 changes: 55 additions & 0 deletions ext-src/swoole_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include "swoole_socket.h"
#include "swoole_util.h"

#if PHP_VERSION_ID >= 80400
#include "swoole_server.h"
#endif

#include "thirdparty/php/standard/proc_open.h"
#ifdef SW_USE_CURL
#include "thirdparty/php/curl/curl_interface.h"
Expand Down Expand Up @@ -76,6 +80,9 @@ static PHP_FUNCTION(swoole_time_sleep_until);
static PHP_FUNCTION(swoole_stream_select);
static PHP_FUNCTION(swoole_stream_socket_pair);
static PHP_FUNCTION(swoole_user_func_handler);
#if PHP_VERSION_ID >= 80400
static PHP_FUNCTION(swoole_exit);
#endif
SW_EXTERN_C_END

static void inherit_class(const char *child_name, size_t child_length, const char *parent_name, size_t parent_length);
Expand Down Expand Up @@ -233,6 +240,9 @@ struct real_func {
void php_swoole_runtime_rinit() {
tmp_function_table = (zend_array *) emalloc(sizeof(zend_array));
zend_hash_init(tmp_function_table, 8, nullptr, nullptr, 0);
#if PHP_VERSION_ID >= 80400
SW_HOOK_FUNC(exit);
#endif
}

void php_swoole_runtime_rshutdown() {
Expand Down Expand Up @@ -2120,3 +2130,48 @@ static void clear_class_entries() {
}
child_class_entries.clear();
}

#if PHP_VERSION_ID >= 80400
static PHP_FUNCTION(swoole_exit) {
zend_string *message = NULL;
zend_long status = 0;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_LONG(message, status)
ZEND_PARSE_PARAMETERS_END();

zend_long flags = 0;
if (Coroutine::get_current()) {
flags |= SW_EXIT_IN_COROUTINE;
}

if (sw_server() && sw_server()->is_started()) {
flags |= SW_EXIT_IN_SERVER;
}

if (flags) {
zval ex = {};
zend_object *obj = zend_throw_exception(swoole_exit_exception_ce, (message ? ZSTR_VAL(message) : "swoole exit"), 0);
ZVAL_OBJ(&ex, obj);
zend_update_property_long(swoole_exit_exception_ce, SW_Z8_OBJ_P(&ex), ZEND_STRL("flags"), flags);
zend_update_property_long(swoole_exit_exception_ce, SW_Z8_OBJ_P(&ex), ZEND_STRL("status"), status);
} else {
if (message) {
size_t len = ZSTR_LEN(message);
if (len != 0) {
/* An exception might be emitted by an output handler */
zend_write(ZSTR_VAL(message), len);
if (EG(exception)) {
RETURN_THROWS();
}
}
} else {
EG(exit_status) = status;
}

ZEND_ASSERT(!EG(exception));
zend_throw_unwind_exit();
}
}
#endif
6 changes: 3 additions & 3 deletions include/swoole_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

#define SWOOLE_MAJOR_VERSION 5
#define SWOOLE_MINOR_VERSION 1
#define SWOOLE_RELEASE_VERSION 4
#define SWOOLE_RELEASE_VERSION 5
#define SWOOLE_EXTRA_VERSION ""
#define SWOOLE_VERSION "5.1.4"
#define SWOOLE_VERSION_ID 50104
#define SWOOLE_VERSION "5.1.5-dev"
#define SWOOLE_VERSION_ID 50105
#define SWOOLE_API_VERSION_ID 0x202208a

#define SWOOLE_BUG_REPORT \
Expand Down
59 changes: 59 additions & 0 deletions tests/swoole_http_server/exit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
swoole_http_server: exit function
--SKIPIF--
<?php
if (PHP_VERSION_ID < 80400) {
require __DIR__ . '/../include/skipif.inc';
skip('php version 8.4 or higher');
}
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

$pm = new ProcessManager;
$pm->parentFunc = function () use ($pm) {
Co\run(function () use ($pm) {
httpRequest("http://127.0.0.1:{$pm->getFreePort()}");
});

$pm->kill();
echo "DONE\n";
};

$pm->childFunc = function () use ($pm) {
$http = new Swoole\Http\Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
$http->on('workerStart', function () use ($pm) {
$pm->wakeup();
});

$http->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($pm) {
try {
exit('Swoole Server exit!!!');
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
}

try {
exit();
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
}

try {
exit(400);
} catch (Exception $e) {
echo $e->getStatus().PHP_EOL;
}
$response->end();
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECTF--
Swoole Server exit!!!
swoole exit
400
DONE
11 changes: 11 additions & 0 deletions thirdparty/php/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@
#include "ext/standard/php_filestat.h"

#include <fcntl.h>

#if PHP_VERSION_ID >= 80400
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#else
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#if HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#endif

#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
Expand Down
Loading

0 comments on commit fae814f

Please sign in to comment.