Skip to content

Commit

Permalink
[unity]更新到最新的模板绑定库
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 11, 2024
1 parent a783f02 commit b11d781
Show file tree
Hide file tree
Showing 14 changed files with 2,751 additions and 2,062 deletions.
1,510 changes: 4 additions & 1,506 deletions unity/native_src_il2cpp/Inc/Binding.hpp

Large diffs are not rendered by default.

78 changes: 65 additions & 13 deletions unity/native_src_il2cpp/Inc/DataTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
#if USING_IN_UNREAL_ENGINE
#include "CoreMinimal.h"
#include "UObject/Package.h"
#include "UObject/Class.h"
#else
#include "JSClassRegister.h"
#endif

#include "NamespaceDef.h"

PRAGMA_DISABLE_UNDEFINED_IDENTIFIER_WARNINGS
#pragma warning(push, 0)
#include "v8.h"
#pragma warning(pop)
PRAGMA_ENABLE_UNDEFINED_IDENTIFIER_WARNINGS

#if !defined(MAPPER_ISOLATE_DATA_POS)
#define MAPPER_ISOLATE_DATA_POS 0
Expand All @@ -31,7 +36,7 @@

#include <sstream>

namespace puerts
namespace PUERTS_NAMESPACE
{
template <typename T, typename FT, typename = void>
struct TOuterLinker
Expand Down Expand Up @@ -227,17 +232,27 @@ class JSENV_API DataTransfer
}

template <typename T>
FORCEINLINE static T* GetPointerFast(v8::Local<v8::Object> Object, int Index = 0)
FORCEINLINE static T* GetPointerFast(v8::Local<v8::Object> Object, int Index)
{
if (Object->InternalFieldCount() > (Index * 2 + 1))
int P1 = Index << 1;
int P2 = P1 + 1;
if (V8_LIKELY(Object->InternalFieldCount() > P2))
{
return static_cast<T*>(MakeAddressWithHighPartOfTwo(
Object->GetAlignedPointerFromInternalField(Index * 2), Object->GetAlignedPointerFromInternalField(Index * 2 + 1)));
Object->GetAlignedPointerFromInternalField(P1), Object->GetAlignedPointerFromInternalField(P2)));
}
else
return nullptr;
}

template <typename T>
FORCEINLINE static T* GetPointerFast(v8::Local<v8::Object> Object)
{
if (V8_LIKELY(Object->InternalFieldCount() > 1))
{
return nullptr;
return static_cast<T*>(MakeAddressWithHighPartOfTwo(
Object->GetAlignedPointerFromInternalField(0), Object->GetAlignedPointerFromInternalField(1)));
}
return nullptr;
}

template <typename T>
Expand Down Expand Up @@ -330,8 +345,51 @@ class JSENV_API DataTransfer

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

static void ThrowException(v8::Isolate* Isolate, const char* Message);
#endif

template <typename T1, typename T2>
FORCEINLINE static void LinkOuter(v8::Local<v8::Context> Context, v8::Local<v8::Value> Outer, v8::Local<v8::Value> Inner)
{
TOuterLinker<T1, T2>::Link(Context, Outer, Inner);
}

FORCEINLINE static v8::Local<v8::ArrayBuffer> NewArrayBuffer(v8::Local<v8::Context> Context, void* Data, size_t DataLength)
{
#if defined(HAS_ARRAYBUFFER_NEW_WITHOUT_STL)
return v8::ArrayBuffer_New_Without_Stl(Context->GetIsolate(), Data, DataLength);
#else
#if USING_IN_UNREAL_ENGINE
return v8::ArrayBuffer::New(Context->GetIsolate(), Data, DataLength);
#else
auto Backing = v8::ArrayBuffer::NewBackingStore(Data, DataLength, v8::BackingStore::EmptyDeleter, nullptr);
return v8::ArrayBuffer::New(Context->GetIsolate(), std::move(Backing));
#endif
#endif
}

FORCEINLINE static void* GetArrayBufferData(v8::Local<v8::ArrayBuffer> InArrayBuffer)
{
size_t DataLength;
return GetArrayBufferData(InArrayBuffer, DataLength);
}

FORCEINLINE static void* GetArrayBufferData(v8::Local<v8::ArrayBuffer> InArrayBuffer, size_t& DataLength)
{
#if defined(HAS_ARRAYBUFFER_NEW_WITHOUT_STL)
return v8::ArrayBuffer_Get_Data(InArrayBuffer, DataLength);
#else
#if USING_IN_UNREAL_ENGINE
DataLength = InArrayBuffer->GetContents().ByteLength();
return InArrayBuffer->GetContents().Data();
#else
auto BS = InArrayBuffer->GetBackingStore();
DataLength = BS->ByteLength();
return BS->Data();
#endif
#endif
}

FORCEINLINE static void ThrowException(v8::Isolate* Isolate, const char* Message)
{
auto ExceptionStr = v8::String::NewFromUtf8(Isolate, Message, v8::NewStringType::kNormal).ToLocalChecked();
Expand Down Expand Up @@ -372,11 +430,5 @@ class JSENV_API DataTransfer
return stm.str();
}
}

template <typename T1, typename T2>
FORCEINLINE static void LinkOuter(v8::Local<v8::Context> Context, v8::Local<v8::Value> Outer, v8::Local<v8::Value> Inner)
{
TOuterLinker<T1, T2>::Link(Context, Outer, Inner);
}
};
} // namespace puerts
} // namespace PUERTS_NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* Tencent is pleased to support the open source community by making Puerts available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may
* be subject to their corresponding license terms. This file is subject to the terms and conditions defined in file 'LICENSE',
* which is part of this source code package.
*/

#pragma once
#if defined(BUILDING_PES_EXTENSION)
#include "PesapiConverter.hpp"
#else
#include "V8Converter.hpp"
#endif
/*
* Tencent is pleased to support the open source community by making Puerts available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
* Puerts is licensed under the BSD 3-Clause License, except for the third-party components listed in the file 'LICENSE' which may
* be subject to their corresponding license terms. This file is subject to the terms and conditions defined in file 'LICENSE',
* which is part of this source code package.
*/

#pragma once

#include "PuertsNamespaceDef.h"

#if defined(WITH_QJS_NAMESPACE_SUFFIX)
namespace v8 = v8_qjs;
#endif
62 changes: 11 additions & 51 deletions unity/native_src_il2cpp/Inc/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

#pragma once

#if BUILDING_PES_EXTENSION
#if defined(BUILDING_PES_EXTENSION)
#include "PesapiObject.hpp"
#else
#endif
#if !defined(BUILDING_PES_EXTENSION) || defined(PES_EXTENSION_WITH_V8_API)
#include "V8Object.hpp"
#endif

namespace puerts
namespace PUERTS_NAMESPACE
{
namespace internal
{
constexpr std::size_t NumDigits(std::size_t n)
{
Expand Down Expand Up @@ -74,59 +77,16 @@ struct ParamsDecl<N>
}
};

} // namespace internal

template <typename R, typename... Args>
struct ScriptTypeName<std::function<R(Args...)>>
{
static constexpr auto value()
{
return Literal("(") + ParamsDecl<0, Args...>::Get() + Literal(") => ") + ScriptTypeNameWithNamespace<R>::value();
}
};

namespace converter
{
template <typename R, typename... Args>
struct Converter<std::function<R(Args...)>>
{
static ValueType toScript(ContextType context, std::function<R(Args...)> value)
{
return GetUndefined(context);
}

static std::function<R(Args...)> toCpp(ContextType context, const ValueType value)
{
if (IsNullOrUndefined(context, value))
return nullptr;
Function PF(context, value);
return [=](Args... cppArgs) -> R { return PF.Func<R>(cppArgs...); };
}

static bool accept(ContextType context, const ValueType value)
{
return IsNullOrUndefined(context, value) || Converter<Function>::accept(context, value);
return internal::Literal("(") + internal::ParamsDecl<0, Args...>::Get() + internal::Literal(") => ") +
ScriptTypeNameWithNamespace<R>::value();
}
};

template <typename... Args>
struct Converter<std::function<void(Args...)>>
{
static ValueType toScript(ContextType context, std::function<void(Args...)> value)
{
return GetUndefined(context);
}

static std::function<void(Args...)> toCpp(ContextType context, const ValueType value)
{
if (IsNullOrUndefined(context, value))
return nullptr;
Function PF(context, value);
return [=](Args... cppArgs) -> void { PF.Action(cppArgs...); };
}

static bool accept(ContextType context, const ValueType value)
{
return IsNullOrUndefined(context, value) || Converter<Function>::accept(context, value);
}
};
} // namespace converter
} // namespace puerts
} // namespace PUERTS_NAMESPACE
Loading

0 comments on commit b11d781

Please sign in to comment.