From 27221b9e92e09848d7f528f8da706a6bd2bd137a Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 7 Aug 2023 18:49:59 +0800 Subject: [PATCH] JSB supports to binding virtual inheritance classes. --- native/CMakeLists.txt | 3 ++ native/cocos/base/HasMemberFunction.h | 2 + native/cocos/base/VirtualInheritBase.h | 37 +++++++++++++++ .../bindings/jswrapper/v8/HelperMacros.h | 5 +++ .../cocos/bindings/manual/jsb_conversions.h | 9 +++- .../utils/jsb_garbagecollect_callback.h | 45 +++++++++++++++++++ 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 native/cocos/base/VirtualInheritBase.h create mode 100644 native/cocos/bindings/utils/jsb_garbagecollect_callback.h diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 2a5117eb24f..4a62953f55c 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -480,6 +480,7 @@ cocos_source_files( cocos/base/std/hash/extensions.hpp cocos/base/std/hash/hash_fwd.hpp cocos/base/std/hash/hash.h + cocos/base/VirtualInheritBase.h ) ############ application @@ -2133,6 +2134,7 @@ cocos_source_files(MODULE ccgeometry cocos_source_files( cocos/bindings/utils/BindingUtils.cpp cocos/bindings/utils/BindingUtils.h + cocos/bindings/utils/jsb_garbagecollect_callback.h ) ######## dop @@ -2506,6 +2508,7 @@ cocos_source_files( ) cocos_source_files( + cocos/bindings/manual/jsb_classtype.cpp cocos/bindings/manual/jsb_classtype.cpp cocos/bindings/manual/jsb_classtype.h cocos/bindings/manual/jsb_cocos_manual.cpp diff --git a/native/cocos/base/HasMemberFunction.h b/native/cocos/base/HasMemberFunction.h index 3e0900a842a..1856f3fd6a4 100644 --- a/native/cocos/base/HasMemberFunction.h +++ b/native/cocos/base/HasMemberFunction.h @@ -93,5 +93,7 @@ namespace cc { CC_DEFINE_HAS_MEMBER_FUNC(setScriptObject) CC_DEFINE_HAS_MEMBER_FUNC(getScriptObject) +CC_DEFINE_HAS_MEMBER_FUNC(onGarbageCollect) + } // namespace cc diff --git a/native/cocos/base/VirtualInheritBase.h b/native/cocos/base/VirtualInheritBase.h new file mode 100644 index 00000000000..61eaba255e4 --- /dev/null +++ b/native/cocos/base/VirtualInheritBase.h @@ -0,0 +1,37 @@ +/**************************************************************************** + Copyright (c) 2023 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ +#pragma once + +#include "base/RefCounted.h" + +namespace cc { + +class VirtualInheritBase : public RefCounted { +protected: + VirtualInheritBase() = default; +public: + virtual ~VirtualInheritBase() = default; +}; + +} // namespace cc diff --git a/native/cocos/bindings/jswrapper/v8/HelperMacros.h b/native/cocos/bindings/jswrapper/v8/HelperMacros.h index 7491fee2852..59e3a0df85a 100644 --- a/native/cocos/bindings/jswrapper/v8/HelperMacros.h +++ b/native/cocos/bindings/jswrapper/v8/HelperMacros.h @@ -70,6 +70,11 @@ constexpr inline T *SE_THIS_OBJECT(STATE &s) { // NOLINT(readability-identifier- return reinterpret_cast(s.nativeThisObject()); } +template +constexpr inline T *SE_THIS_OBJECT_VIRTUAL(STATE &s) { // NOLINT(readability-identifier-naming) + return dynamic_cast(reinterpret_cast(s.nativeThisObject())); +} + template constexpr typename std::enable_if::value, char *>::type SE_UNDERLYING_TYPE_NAME() { // NOLINT(readability-identifier-naming) return typeid(std::underlying_type_t).name(); diff --git a/native/cocos/bindings/manual/jsb_conversions.h b/native/cocos/bindings/manual/jsb_conversions.h index 9170e2ce8f4..51f64f47658 100644 --- a/native/cocos/bindings/manual/jsb_conversions.h +++ b/native/cocos/bindings/manual/jsb_conversions.h @@ -53,6 +53,7 @@ #include "renderer/gfx-base/states/GFXSampler.h" #include "base/HasMemberFunction.h" +#include "bindings/utils/jsb_garbagecollect_callback.h" #define SE_PRECONDITION2_VOID(condition, ...) \ do { \ @@ -377,8 +378,12 @@ native_ptr_to_seval(T &v_ref, se::Value *ret, bool *isReturnCachedValue = nullpt template bool native_ptr_to_seval(T *vp, se::Class *cls, se::Value *ret, bool *isReturnCachedValue = nullptr) { // NOLINT(readability-identifier-naming) - using DecayT = typename std::decay::type>::type; - auto *v = const_cast(vp); + using DecayT_ = typename std::decay::type>::type; + constexpr bool isVirtualInheritBase = std::is_base_of_v; + using DecayT = std::conditional_t; + + auto *v_ = const_cast(vp); + auto *v = static_cast(v_); CC_ASSERT_NOT_NULL(ret); if (v == nullptr) { ret->setNull(); diff --git a/native/cocos/bindings/utils/jsb_garbagecollect_callback.h b/native/cocos/bindings/utils/jsb_garbagecollect_callback.h new file mode 100644 index 00000000000..ff2ddb738b1 --- /dev/null +++ b/native/cocos/bindings/utils/jsb_garbagecollect_callback.h @@ -0,0 +1,45 @@ +/**************************************************************************** + Copyright (c) 2023 Xiamen Yaji Software Co., Ltd. + + https://www.cocos.com/ + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/VirtualInheritBase.h" +#include "base/HasMemberFunction.h" + +namespace cc { + +template +inline void invokeOnGarbageCollectMethod(STATE& s) { + if constexpr (cc::has_onGarbageCollect::value) { + if constexpr (std::is_base_of_v) { + auto cobj = SE_THIS_OBJECT_VIRTUAL(s); + if (cobj != nullptr) cobj->onGarbageCollect(); + } else { + auto* cobj = SE_THIS_OBJECT(s); + if (cobj != nullptr) cobj->onGarbageCollect(); + } + } +} + +} // namespace cc