From c0ba27d7561920b0dda9650cb39847fb2139ccac Mon Sep 17 00:00:00 2001 From: James Chen Date: Sun, 19 Feb 2023 17:14:43 +0800 Subject: [PATCH] [native] Fix that JSB conversion for std::function doesn't support passing 'null' from JS. (#14303) --- .../cocos/bindings/manual/jsb_conversions.h | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/native/cocos/bindings/manual/jsb_conversions.h b/native/cocos/bindings/manual/jsb_conversions.h index b171e28331..5a54fff33e 100644 --- a/native/cocos/bindings/manual/jsb_conversions.h +++ b/native/cocos/bindings/manual/jsb_conversions.h @@ -754,6 +754,11 @@ bool nativevalue_to_se_args_v(se::ValueArray &array, Args &&...args); // NOLINT( template inline bool sevalue_to_native(const se::Value &from, std::function *func, se::Object *self) { // NOLINT(readability-identifier-naming) + if (from.isNullOrUndefined()) { + *func = nullptr; + return true; + } + if (from.isObject() && from.toObject()->isFunction()) { CC_ASSERT(from.toObject()->isRooted()); *func = [from, self]() { @@ -778,6 +783,11 @@ inline bool sevalue_to_native(const se::Value &from, std::function *func, s template inline bool sevalue_to_native(const se::Value &from, std::function *func, se::Object *self) { // NOLINT(readability-identifier-naming) + if (from.isNullOrUndefined()) { + *func = nullptr; + return true; + } + if (from.isObject() && from.toObject()->isFunction()) { CC_ASSERT(from.toObject()->isRooted()); *func = [from, self](Args... inargs) { @@ -805,6 +815,11 @@ inline bool sevalue_to_native(const se::Value &from, std::function * } inline bool sevalue_to_native(const se::Value &from, std::function *func, se::Object *self) { // NOLINT(readability-identifier-naming) + if (from.isNullOrUndefined()) { + *func = nullptr; + return true; + } + if (from.isObject() && from.toObject()->isFunction()) { CC_ASSERT(from.toObject()->isRooted()); *func = [from, self]() { @@ -825,6 +840,11 @@ inline bool sevalue_to_native(const se::Value &from, std::function *func template inline bool sevalue_to_native(const se::Value &from, std::function *func, se::Object *self) { // NOLINT(readability-identifier-naming) + if (from.isNullOrUndefined()) { + *func = nullptr; + return true; + } + if (from.isObject() && from.toObject()->isFunction()) { CC_ASSERT(from.toObject()->isRooted()); *func = [from, self](Args... inargs) {