Skip to content

Commit

Permalink
fix 4, --filter=[unit]
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Sep 26, 2024
1 parent 3928997 commit 7c11d46
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
36 changes: 35 additions & 1 deletion ext-src/php_swoole_cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,15 @@ class CharPtr {
}
};

struct Callable {
class Callable {
private:
zval zfn;
zend_fcall_info_cache fcc;
char *fn_name = nullptr;

Callable() {}

public:
Callable(zval *_zfn) {
ZVAL_UNDEF(&zfn);
if (!zval_is_true(_zfn)) {
Expand All @@ -597,6 +601,16 @@ struct Callable {
return !ZVAL_IS_UNDEF(&zfn);
}

Callable *dup() {
auto copy = new Callable();
copy->fcc = fcc;
copy->zfn = zfn;
if (fn_name) {
copy->fn_name = estrdup(fn_name);
}
return copy;
}

bool call(uint32_t argc, zval *argv, zval *retval) {
return sw_zend_call_function_ex(&zfn, &fcc, argc, argv, retval) == SUCCESS;
}
Expand Down Expand Up @@ -726,3 +740,23 @@ static inline zend::Callable *sw_callable_create(zval *zfn) {
return nullptr;
}
}

static inline zend::Callable *sw_callable_create_ex(zval *zfn, const char *fname, bool allow_null = true) {
if (zfn == nullptr || ZVAL_IS_NULL(zfn)) {
if (!allow_null) {
zend_throw_exception_ex(
swoole_exception_ce, SW_ERROR_INVALID_PARAMS, "%s must be of type callable, null given", fname);
}
return nullptr;
}
auto cb = sw_callable_create(zfn);
if (!cb) {
zend_throw_exception_ex(swoole_exception_ce,
SW_ERROR_INVALID_PARAMS,
"%s must be of type callable, %s given",
fname,
zend_zval_type_name(zfn));
return nullptr;
}
return cb;
}
21 changes: 10 additions & 11 deletions ext-src/swoole_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,16 @@ static int event_error_callback(Reactor *reactor, Event *event) {
}

static void event_defer_callback(void *data) {
zend::Callable *fn = (zend::Callable *) data;
if (UNEXPECTED(!zend::function::call(&fn->fcc, 0, nullptr, nullptr, php_swoole_is_enable_coroutine()))) {
zend::Callable *cb = (zend::Callable *) data;
if (UNEXPECTED(!zend::function::call(cb, 0, nullptr, nullptr, php_swoole_is_enable_coroutine()))) {
php_swoole_error(E_WARNING, "%s::defer callback handler error", ZSTR_VAL(swoole_event_ce->name));
}
delete fn;
delete cb;
}

static void event_end_callback(void *data) {
zend::Callable *cb = (zend::Callable *) data;
if (UNEXPECTED(!zend::function::call(&cb->fcc, 0, nullptr, nullptr, php_swoole_is_enable_coroutine()))) {
if (UNEXPECTED(!zend::function::call(cb, 0, nullptr, nullptr, php_swoole_is_enable_coroutine()))) {
php_swoole_error(E_WARNING, "%s::end callback handler error", ZSTR_VAL(swoole_event_ce->name));
}
}
Expand Down Expand Up @@ -434,14 +434,14 @@ static PHP_FUNCTION(swoole_event_add) {
RETURN_FALSE;
}

auto readable_callback = zreadable_callback ? sw_callable_create(zreadable_callback) : nullptr;
auto readable_callback = sw_callable_create_ex(zreadable_callback, "readable_callback", true);
if ((events & SW_EVENT_READ) && readable_callback == nullptr) {
php_swoole_fatal_error(
E_WARNING, "%s: unable to find readable callback of fd [%d]", ZSTR_VAL(swoole_event_ce->name), socket_fd);
RETURN_FALSE;
}

auto writable_callback = zwritable_callback ? sw_callable_create(zwritable_callback) : nullptr;
auto writable_callback = sw_callable_create_ex(zwritable_callback, "writable_callback", true);
if ((events & SW_EVENT_WRITE) && writable_callback == nullptr) {
php_swoole_fatal_error(
E_WARNING, "%s: unable to find writable callback of fd [%d]", ZSTR_VAL(swoole_event_ce->name), socket_fd);
Expand Down Expand Up @@ -539,8 +539,8 @@ static PHP_FUNCTION(swoole_event_set) {
}

EventObject *peo = (EventObject *) socket->object;
auto readable_callback = sw_callable_create(zreadable_callback);
auto writable_callback = sw_callable_create(zwritable_callback);
auto readable_callback = sw_callable_create_ex(zreadable_callback, "readable_callback");
auto writable_callback = sw_callable_create_ex(zwritable_callback, "writable_callback");
if (readable_callback) {
if (peo->readable_callback) {
swoole_event_defer(sw_callable_free, peo->readable_callback);
Expand Down Expand Up @@ -627,9 +627,7 @@ static PHP_FUNCTION(swoole_event_cycle) {

event_check_reactor();

auto callback = sw_callable_create(zcallback);

if (callback == nullptr) {
if (ZVAL_IS_NULL(zcallback)) {
if (sw_reactor()->idle_task.callback == nullptr) {
RETURN_FALSE;
} else {
Expand All @@ -640,6 +638,7 @@ static PHP_FUNCTION(swoole_event_cycle) {
}
}

auto callback = sw_callable_create(zcallback);
if (!before) {
if (sw_reactor()->idle_task.data != nullptr) {
swoole_event_defer(sw_callable_free, sw_reactor()->idle_task.data);
Expand Down
8 changes: 4 additions & 4 deletions ext-src/swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3339,13 +3339,13 @@ static PHP_METHOD(swoole_server, task) {

if (!serv->is_worker()) {
buf.info.ext_flags |= SW_TASK_NOREPLY;
} else if (zfn) {
} else if (zfn && zval_is_true(zfn)) {
buf.info.ext_flags |= SW_TASK_CALLBACK;
auto fci_cache = sw_callable_create(zfn);
if (!fci_cache) {
auto cb = sw_callable_create(zfn);
if (!cb) {
RETURN_FALSE;
}
server_object->property->task_callbacks[task_id] = fci_cache;
server_object->property->task_callbacks[task_id] = cb;
}

buf.info.ext_flags |= SW_TASK_NONBLOCK;
Expand Down
5 changes: 5 additions & 0 deletions ext-src/swoole_socket_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,11 @@ static PHP_METHOD(swoole_socket_coro, accept) {
client_sock->socket = conn;
ZVAL_OBJ(return_value, &client_sock->std);
socket_coro_init(return_value, client_sock);
// It must be copied once to avoid destroying the function when the connection closes.
if (sock->socket->protocol.private_data_1) {
zend::Callable *cb = (zend::Callable *) sock->socket->protocol.private_data_1;
conn->protocol.private_data_1 = cb->dup();
}
} else {
socket_coro_sync_properties(ZEND_THIS, sock);
RETURN_FALSE;
Expand Down

0 comments on commit 7c11d46

Please sign in to comment.