Skip to content

Commit

Permalink
[unity]合入 https://github.com/puerts/papi-quickjs 的避免依赖libc++方面的一些调整
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Feb 26, 2025
1 parent 246c06e commit d5d8081
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 46 deletions.
33 changes: 15 additions & 18 deletions unity/native_src/Inc/JSClassRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

#pragma once

#include "functional"

#if USING_IN_UNREAL_ENGINE
#include "CoreMinimal.h"

Expand All @@ -19,13 +17,10 @@ PRAGMA_DISABLE_UNDEFINED_IDENTIFIER_WARNINGS
#pragma warning(pop)
PRAGMA_ENABLE_UNDEFINED_IDENTIFIER_WARNINGS
#else
#define JSENV_API
#define FORCEINLINE V8_INLINE
#define UPTRINT uintptr_t
#endif

#include <string>

#include "PuertsNamespaceDef.h"

#include "pesapi.h"
Expand All @@ -42,7 +37,7 @@ class CFunctionInfo;

MSVC_PRAGMA(warning(push))
MSVC_PRAGMA(warning(disable : 4191))
struct JSENV_API JSFunctionInfo
struct REGISTER_API JSFunctionInfo
{
JSFunctionInfo() : Name(nullptr), Callback(nullptr)
{
Expand All @@ -67,7 +62,7 @@ struct JSENV_API JSFunctionInfo
const CFunctionInfo* ReflectionInfo = nullptr;
};

struct JSENV_API JSPropertyInfo
struct REGISTER_API JSPropertyInfo
{
JSPropertyInfo() : Name(nullptr), Getter(nullptr), Setter(nullptr)
{
Expand Down Expand Up @@ -100,7 +95,7 @@ struct JSENV_API JSPropertyInfo
struct NamedFunctionInfo;
struct NamedPropertyInfo;

struct JSENV_API JSClassDefinition
struct REGISTER_API JSClassDefinition
{
const void* TypeId;
const void* SuperTypeId;
Expand Down Expand Up @@ -134,22 +129,24 @@ MSVC_PRAGMA(warning(pop))
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
}

void JSENV_API RegisterJSClass(const JSClassDefinition& ClassDefinition);
void REGISTER_API RegisterJSClass(const JSClassDefinition& ClassDefinition);

void JSENV_API SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* ConstructorInfos, const NamedFunctionInfo* MethodInfos,
void REGISTER_API SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* ConstructorInfos, const NamedFunctionInfo* MethodInfos,
const NamedFunctionInfo* FunctionInfos, const NamedPropertyInfo* PropertyInfos, const NamedPropertyInfo* VariableInfos);

void JSENV_API ForeachRegisterClass(std::function<void(const JSClassDefinition* ClassDefinition)>);
typedef void (*ClassDefinitionForeachCallback)(const JSClassDefinition* ClassDefinition);

void REGISTER_API ForeachRegisterClass(ClassDefinitionForeachCallback Callback);

JSENV_API const JSClassDefinition* FindClassByID(const void* TypeId);
REGISTER_API const JSClassDefinition* FindClassByID(const void* TypeId);

JSENV_API void OnClassNotFound(pesapi_class_not_found_callback Callback);
REGISTER_API void OnClassNotFound(pesapi_class_not_found_callback Callback);

JSENV_API const JSClassDefinition* LoadClassByID(const void* TypeId);
REGISTER_API const JSClassDefinition* LoadClassByID(const void* TypeId);

JSENV_API const JSClassDefinition* FindCppTypeClassByName(const PString& Name);
REGISTER_API const JSClassDefinition* FindCppTypeClassByName(const PString& Name);

JSENV_API bool TraceObjectLifecycle(const void* TypeId, pesapi_on_native_object_enter OnEnter, pesapi_on_native_object_exit OnExit);
REGISTER_API bool TraceObjectLifecycle(const void* TypeId, pesapi_on_native_object_enter OnEnter, pesapi_on_native_object_exit OnExit);

#if USING_IN_UNREAL_ENGINE
typedef void (*AddonRegisterFunc)(v8::Local<v8::Context> Context, v8::Local<v8::Object> Exports);
Expand All @@ -158,9 +155,9 @@ AddonRegisterFunc FindAddonRegisterFunc(const PString& Name);

void RegisterAddon(const char* Name, AddonRegisterFunc RegisterFunc);

JSENV_API const JSClassDefinition* FindClassByType(UStruct* Type);
REGISTER_API const JSClassDefinition* FindClassByType(UStruct* Type);

JSENV_API bool IsEditorOnlyUFunction(const UFunction* Func);
REGISTER_API bool IsEditorOnlyUFunction(const UFunction* Func);

#endif

Expand Down
55 changes: 27 additions & 28 deletions unity/native_src/Inc/PString.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#pragma once

#include <cstring>
#include <functional>
#include <string.h>
#include "NamespaceDef.h"

namespace PUERTS_NAMESPACE
Expand All @@ -18,18 +17,18 @@ class PString
{
public:
// Constructors
PString() : data_(nullptr), size_(0)
PString() noexcept: data_(nullptr), size_(0)
{
data_ = new char[1];
data_ = (char*)malloc(1);
data_[0] = '\0';
}

PString(const char* str)
{
if (str)
{
size_ = std::strlen(str);
data_ = new char[size_ + 1];
size_ = strlen(str);
data_ = (char*)malloc(size_ + 1);
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, str, size_);
#else
Expand All @@ -40,7 +39,7 @@ class PString
else
{
size_ = 0;
data_ = new char[1];
data_ = (char*)malloc(1);
data_[0] = '\0';
}
}
Expand All @@ -50,7 +49,7 @@ class PString
if (str)
{
size_ = length;
data_ = new char[size_ + 1];
data_ = (char*)malloc(size_ + 1);
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, str, length);
#else
Expand All @@ -61,15 +60,15 @@ class PString
else
{
size_ = 0;
data_ = new char[1];
data_ = (char*)malloc(1);
data_[0] = '\0';
}
}

PString(const PString& other)
PString(const PString& other) noexcept
{
size_ = other.size_;
data_ = new char[size_ + 1];
data_ = (char*)malloc(size_ + 1);
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, other.data_, size_);
#else
Expand All @@ -78,13 +77,13 @@ class PString
data_[size_] = '\0';
}

PString& operator=(const PString& other)
PString& operator=(const PString& other) noexcept
{
if (this != &other)
{
delete[] data_;
free(data_);
size_ = other.size_;
data_ = new char[size_ + 1];
data_ = (char*)malloc(size_ + 1);
#ifdef _MSC_VER
strncpy_s(data_, size_ + 1, other.data_, size_);
#else
Expand All @@ -95,17 +94,17 @@ class PString
return *this;
}

~PString()
~PString() noexcept
{
delete[] data_;
free(data_);
}

PString operator+(const PString& other) const
{
PString result;
result.size_ = size_ + other.size_;
delete[] result.data_;
result.data_ = new char[result.size_ + 1];
free(result.data_);
result.data_ = (char*)malloc(result.size_ + 1);
#ifdef _MSC_VER
strncpy_s(result.data_, result.size_ + 1, data_, size_);
strncpy_s(result.data_ + size_, result.size_ - size_ + 1, other.data_, other.size_);
Expand All @@ -120,10 +119,10 @@ class PString
friend PString operator+(const char* lhs, const PString& rhs)
{
PString result;
size_t lhs_size = std::strlen(lhs);
size_t lhs_size = strlen(lhs);
result.size_ = lhs_size + rhs.size_;
delete[] result.data_;
result.data_ = new char[result.size_ + 1];
free(result.data_);
result.data_ = (char*)malloc(result.size_ + 1);
#ifdef _MSC_VER
strncpy_s(result.data_, result.size_ + 1, lhs, lhs_size);
strncpy_s(result.data_ + lhs_size, result.size_ - lhs_size + 1, rhs.data_, rhs.size_);
Expand All @@ -138,7 +137,7 @@ class PString
PString& operator+=(const PString& other)
{
size_t new_size = size_ + other.size_;
char* new_data = new char[new_size + 1];
char* new_data = (char*)malloc(new_size + 1);
#ifdef _MSC_VER
strncpy_s(new_data, new_size + 1, data_, size_);
strncpy_s(new_data + size_, new_size - size_ + 1, other.data_, other.size_);
Expand All @@ -148,7 +147,7 @@ class PString
#endif
new_data[new_size] = '\0';

delete[] data_;
free(data_);
data_ = new_data;
size_ = new_size;

Expand All @@ -159,9 +158,9 @@ class PString
{
if (str)
{
size_t str_size = std::strlen(str);
size_t str_size = strlen(str);
size_t new_size = size_ + str_size;
char* new_data = new char[new_size + 1];
char* new_data = (char*)malloc(new_size + 1);
#ifdef _MSC_VER
strncpy_s(new_data, new_size + 1, data_, size_);
strncpy_s(new_data + size_, new_size - size_ + 1, str, str_size);
Expand All @@ -171,7 +170,7 @@ class PString
#endif
new_data[new_size] = '\0';

delete[] data_;
free(data_);
data_ = new_data;
size_ = new_size;
}
Expand All @@ -195,12 +194,12 @@ class PString

bool operator<(const PString& other) const
{
return std::strcmp(data_, other.data_) < 0;
return strcmp(data_, other.data_) < 0;
}

bool operator==(const PString& other) const
{
return std::strcmp(data_, other.data_) == 0;
return strcmp(data_, other.data_) == 0;
}

private:
Expand Down
33 changes: 33 additions & 0 deletions unity/native_src/Inc/PuertsNamespaceDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,36 @@
#define MSVC_PRAGMA(...)
#endif
#endif

#if defined(__clang__)
# define PUERTS_HAS_ATTRIBUTE_VISIBILITY (__has_attribute(visibility))
#elif defined(__GNUC__)
# define PUERTS_HAS_ATTRIBUTE_VISIBILITY 1
#else
# define PUERTS_HAS_ATTRIBUTE_VISIBILITY 0
#endif

#ifndef REGISTER_API
#ifdef _MSC_VER
#ifdef BUILDING_REGISTER_API_SHARED
# define REGISTER_API __declspec(dllexport)
#elif USING_REGISTER_API_SHARED
# define REGISTER_API __declspec(dllimport)
#else
# define REGISTER_API
#endif // BUILDING_V8_SHARED

#else // _MSC_VER

#if PUERTS_HAS_ATTRIBUTE_VISIBILITY
# ifdef BUILDING_REGISTER_API_SHARED
# define REGISTER_API __attribute__ ((visibility("default")))
# else
# define REGISTER_API
# endif
#else
# define REGISTER_API
#endif

#endif // _MSC_VER
#endif
1 change: 1 addition & 0 deletions unity/native_src/Inc/TypeInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include <functional>
#include <string>
#ifdef WITH_V8_FAST_CALL
#include "V8FastCall.hpp"
Expand Down

0 comments on commit d5d8081

Please sign in to comment.