Skip to content

Commit

Permalink
JSB supports to binding virtual inheritance classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dumganhar committed Aug 7, 2023
1 parent f024d3f commit 27221b9
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
3 changes: 3 additions & 0 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions native/cocos/base/HasMemberFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions native/cocos/base/VirtualInheritBase.h
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions native/cocos/bindings/jswrapper/v8/HelperMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ constexpr inline T *SE_THIS_OBJECT(STATE &s) { // NOLINT(readability-identifier-
return reinterpret_cast<T *>(s.nativeThisObject());
}

template <typename T, typename BASE, typename STATE>
constexpr inline T *SE_THIS_OBJECT_VIRTUAL(STATE &s) { // NOLINT(readability-identifier-naming)
return dynamic_cast<T *>(reinterpret_cast<BASE*>(s.nativeThisObject()));
}

template <typename T>
constexpr typename std::enable_if<std::is_enum<T>::value, char *>::type SE_UNDERLYING_TYPE_NAME() { // NOLINT(readability-identifier-naming)
return typeid(std::underlying_type_t<T>).name();
Expand Down
9 changes: 7 additions & 2 deletions native/cocos/bindings/manual/jsb_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 { \
Expand Down Expand Up @@ -377,8 +378,12 @@ native_ptr_to_seval(T &v_ref, se::Value *ret, bool *isReturnCachedValue = nullpt

template <typename T>
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<typename std::remove_const<T>::type>::type;
auto *v = const_cast<DecayT *>(vp);
using DecayT_ = typename std::decay<typename std::remove_const<T>::type>::type;
constexpr bool isVirtualInheritBase = std::is_base_of_v<cc::VirtualInheritBase, DecayT_>;
using DecayT = std::conditional_t<isVirtualInheritBase, cc::VirtualInheritBase, DecayT_>;

auto *v_ = const_cast<DecayT_ *>(vp);
auto *v = static_cast<DecayT *>(v_);
CC_ASSERT_NOT_NULL(ret);
if (v == nullptr) {
ret->setNull();
Expand Down
45 changes: 45 additions & 0 deletions native/cocos/bindings/utils/jsb_garbagecollect_callback.h
Original file line number Diff line number Diff line change
@@ -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<typename T, typename STATE>
inline void invokeOnGarbageCollectMethod(STATE& s) {
if constexpr (cc::has_onGarbageCollect<T, void()>::value) {
if constexpr (std::is_base_of_v<cc::VirtualInheritBase, T>) {
auto cobj = SE_THIS_OBJECT_VIRTUAL<T, cc::VirtualInheritBase>(s);
if (cobj != nullptr) cobj->onGarbageCollect();
} else {
auto* cobj = SE_THIS_OBJECT<T>(s);
if (cobj != nullptr) cobj->onGarbageCollect();
}
}
}

} // namespace cc

0 comments on commit 27221b9

Please sign in to comment.