Skip to content

Commit

Permalink
[unreal]静态绑定IsInstanceOf得先判断是不是Object,fix #1929
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Dec 3, 2024
1 parent 874a731 commit 8815721
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 65 deletions.
24 changes: 21 additions & 3 deletions unity/native_src/Inc/DataTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ struct TScriptStructTraits<FPlane>
}
};

template <>
struct TScriptStructTraits<FMatrix>
{
static UScriptStruct* Get()
{
return GetScriptStructInCoreUObject(TEXT("Matrix"));
}
};

template <>
struct TScriptStructTraits<FIntVector4>
{
static UScriptStruct* Get()
{
return GetScriptStructInCoreUObject(TEXT("IntVector4"));
}
};

template <class...>
using ToVoid = void;

Expand Down Expand Up @@ -319,7 +337,7 @@ class JSENV_API DataTransfer
static v8::Local<v8::Value> FindOrAddCData(
v8::Isolate* Isolate, v8::Local<v8::Context> Context, const void* TypeId, const void* Ptr, bool PassByPointer);

static bool IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Object> JsObject);
static bool IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Value> JsObject);

static v8::Local<v8::Value> UnRef(v8::Isolate* Isolate, const v8::Local<v8::Value>& Value);

Expand Down Expand Up @@ -347,12 +365,12 @@ class JSENV_API DataTransfer
v8::Isolate* Isolate, v8::Local<v8::Context> Context, UScriptStruct* ScriptStruct, void* Ptr, bool PassByPointer);

template <typename T>
static bool IsInstanceOf(v8::Isolate* Isolate, v8::Local<v8::Object> JsObject)
static bool IsInstanceOf(v8::Isolate* Isolate, v8::Local<v8::Value> JsObject)
{
return IsInstanceOf(Isolate, TScriptStructTraits<T>::Get(), JsObject);
}

static bool IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Object> JsObject);
static bool IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Value> JsObject);

static FString ToFString(v8::Isolate* Isolate, v8::Local<v8::Value> Value);

Expand Down
8 changes: 4 additions & 4 deletions unity/native_src/Src/DataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ v8::Local<v8::Value> DataTransfer::FindOrAddCData(
Isolate, Context, TypeId, const_cast<void*>(Ptr), PassByPointer);
}

bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Object> JsObject)
bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Value> JsObject)
{
return IsolateData<ICppObjectMapper>(Isolate)->IsInstanceOfCppObject(Isolate, TypeId, JsObject);
return JsObject->IsObject() && IsolateData<ICppObjectMapper>(Isolate)->IsInstanceOfCppObject(Isolate, TypeId, JsObject.As<v8::Object>());
}

v8::Local<v8::Value> DataTransfer::UnRef(v8::Isolate* Isolate, const v8::Local<v8::Value>& Value)
Expand Down Expand Up @@ -64,9 +64,9 @@ v8::Local<v8::Value> DataTransfer::FindOrAddStruct(
return FV8Utils::IsolateData<IObjectMapper>(Isolate)->FindOrAddStruct(Isolate, Context, ScriptStruct, Ptr, PassByPointer);
}

bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Object> JsObject)
bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Value> JsObject)
{
return FV8Utils::IsolateData<IObjectMapper>(Isolate)->IsInstanceOf(Struct, JsObject);
return JsObject->IsObject() && FV8Utils::IsolateData<IObjectMapper>(Isolate)->IsInstanceOf(Struct, JsObject.As<v8::Object>());
}

void DataTransfer::ThrowException(v8::Isolate* Isolate, const char* Message)
Expand Down
9 changes: 5 additions & 4 deletions unreal/Puerts/Source/JsEnv/Private/DataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ v8::Local<v8::Value> DataTransfer::FindOrAddCData(
Isolate, Context, TypeId, const_cast<void*>(Ptr), PassByPointer);
}

bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Object> JsObject)
bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Value> JsObject)
{
return IsolateData<ICppObjectMapper>(Isolate)->IsInstanceOfCppObject(Isolate, TypeId, JsObject);
return JsObject->IsObject() &&
IsolateData<ICppObjectMapper>(Isolate)->IsInstanceOfCppObject(Isolate, TypeId, JsObject.As<v8::Object>());
}

v8::Local<v8::Value> DataTransfer::UnRef(v8::Isolate* Isolate, const v8::Local<v8::Value>& Value)
Expand Down Expand Up @@ -64,9 +65,9 @@ v8::Local<v8::Value> DataTransfer::FindOrAddStruct(
return FV8Utils::IsolateData<IObjectMapper>(Isolate)->FindOrAddStruct(Isolate, Context, ScriptStruct, Ptr, PassByPointer);
}

bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Object> JsObject)
bool DataTransfer::IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Value> JsObject)
{
return FV8Utils::IsolateData<IObjectMapper>(Isolate)->IsInstanceOf(Struct, JsObject);
return JsObject->IsObject() && FV8Utils::IsolateData<IObjectMapper>(Isolate)->IsInstanceOf(Struct, JsObject.As<v8::Object>());
}

void DataTransfer::ThrowException(v8::Isolate* Isolate, const char* Message)
Expand Down
6 changes: 3 additions & 3 deletions unreal/Puerts/Source/JsEnv/Public/DataTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class JSENV_API DataTransfer
static v8::Local<v8::Value> FindOrAddCData(
v8::Isolate* Isolate, v8::Local<v8::Context> Context, const void* TypeId, const void* Ptr, bool PassByPointer);

static bool IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Object> JsObject);
static bool IsInstanceOf(v8::Isolate* Isolate, const void* TypeId, v8::Local<v8::Value> JsObject);

static v8::Local<v8::Value> UnRef(v8::Isolate* Isolate, const v8::Local<v8::Value>& Value);

Expand Down Expand Up @@ -331,12 +331,12 @@ class JSENV_API DataTransfer
v8::Isolate* Isolate, v8::Local<v8::Context> Context, UScriptStruct* ScriptStruct, void* Ptr, bool PassByPointer);

template <typename T>
static bool IsInstanceOf(v8::Isolate* Isolate, v8::Local<v8::Object> JsObject)
static bool IsInstanceOf(v8::Isolate* Isolate, v8::Local<v8::Value> JsObject)
{
return IsInstanceOf(Isolate, TScriptStructTraits<T>::Get(), JsObject);
}

static bool IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Object> JsObject);
static bool IsInstanceOf(v8::Isolate* Isolate, UStruct* Struct, v8::Local<v8::Value> JsObject);

static FString ToFString(v8::Isolate* Isolate, v8::Local<v8::Value> Value);

Expand Down
100 changes: 49 additions & 51 deletions unreal/Puerts/Source/JsEnv/Public/V8Backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,54 @@
}; \
}

#define __DefCDataPointerConverter_v8_impl(CLS) \
namespace PUERTS_NAMESPACE \
{ \
namespace v8_impl \
{ \
template <> \
struct Converter<CLS*> \
{ \
static v8::Local<v8::Value> toScript(v8::Local<v8::Context> context, CLS* value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::FindOrAddCData( \
context->GetIsolate(), context, DynamicTypeId<CLS>::get(value), value, true); \
} \
static CLS* toCpp(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::GetPointerFast<CLS>(value.As<v8::Object>()); \
} \
static bool accept(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::IsInstanceOf( \
context->GetIsolate(), StaticTypeId<CLS>::get(), value.As<v8::Object>()); \
} \
}; \
} \
}

#define __DefCDataConverter_v8_impl(CLS) \
namespace PUERTS_NAMESPACE \
{ \
namespace v8_impl \
{ \
template <> \
struct Converter<CLS> \
{ \
static v8::Local<v8::Value> toScript(v8::Local<v8::Context> context, CLS value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::FindOrAddCData( \
context->GetIsolate(), context, DynamicTypeId<CLS>::get(&value), new CLS(value), false); \
} \
static CLS toCpp(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return *::PUERTS_NAMESPACE::DataTransfer::GetPointerFast<CLS>(value.As<v8::Object>()); \
} \
static bool accept(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::IsInstanceOf( \
context->GetIsolate(), StaticTypeId<CLS>::get(), value.As<v8::Object>()); \
} \
}; \
} \
#define __DefCDataPointerConverter_v8_impl(CLS) \
namespace PUERTS_NAMESPACE \
{ \
namespace v8_impl \
{ \
template <> \
struct Converter<CLS*> \
{ \
static v8::Local<v8::Value> toScript(v8::Local<v8::Context> context, CLS* value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::FindOrAddCData( \
context->GetIsolate(), context, DynamicTypeId<CLS>::get(value), value, true); \
} \
static CLS* toCpp(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::GetPointerFast<CLS>(value.As<v8::Object>()); \
} \
static bool accept(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::IsInstanceOf(context->GetIsolate(), StaticTypeId<CLS>::get(), value); \
} \
}; \
} \
}

#define __DefCDataConverter_v8_impl(CLS) \
namespace PUERTS_NAMESPACE \
{ \
namespace v8_impl \
{ \
template <> \
struct Converter<CLS> \
{ \
static v8::Local<v8::Value> toScript(v8::Local<v8::Context> context, CLS value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::FindOrAddCData( \
context->GetIsolate(), context, DynamicTypeId<CLS>::get(&value), new CLS(value), false); \
} \
static CLS toCpp(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return *::PUERTS_NAMESPACE::DataTransfer::GetPointerFast<CLS>(value.As<v8::Object>()); \
} \
static bool accept(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value) \
{ \
return ::PUERTS_NAMESPACE::DataTransfer::IsInstanceOf(context->GetIsolate(), StaticTypeId<CLS>::get(), value); \
} \
}; \
} \
}

namespace PUERTS_NAMESPACE
Expand Down Expand Up @@ -642,7 +640,7 @@ struct Converter<T, typename std::enable_if<std::is_copy_constructible<T>::value
}
static bool accept(v8::Local<v8::Context> context, const v8::Local<v8::Value>& value)
{
return DataTransfer::IsInstanceOf(context->GetIsolate(), StaticTypeId<T>::get(), value.As<v8::Object>());
return DataTransfer::IsInstanceOf(context->GetIsolate(), StaticTypeId<T>::get(), value);
}
};

Expand Down

0 comments on commit 8815721

Please sign in to comment.