Skip to content

Commit d5d8081

Browse files
committed
[unity]合入 https://github.com/puerts/papi-quickjs 的避免依赖libc++方面的一些调整
1 parent 246c06e commit d5d8081

File tree

4 files changed

+76
-46
lines changed

4 files changed

+76
-46
lines changed

unity/native_src/Inc/JSClassRegister.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
#pragma once
1010

11-
#include "functional"
12-
1311
#if USING_IN_UNREAL_ENGINE
1412
#include "CoreMinimal.h"
1513

@@ -19,13 +17,10 @@ PRAGMA_DISABLE_UNDEFINED_IDENTIFIER_WARNINGS
1917
#pragma warning(pop)
2018
PRAGMA_ENABLE_UNDEFINED_IDENTIFIER_WARNINGS
2119
#else
22-
#define JSENV_API
2320
#define FORCEINLINE V8_INLINE
2421
#define UPTRINT uintptr_t
2522
#endif
2623

27-
#include <string>
28-
2924
#include "PuertsNamespaceDef.h"
3025

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

4338
MSVC_PRAGMA(warning(push))
4439
MSVC_PRAGMA(warning(disable : 4191))
45-
struct JSENV_API JSFunctionInfo
40+
struct REGISTER_API JSFunctionInfo
4641
{
4742
JSFunctionInfo() : Name(nullptr), Callback(nullptr)
4843
{
@@ -67,7 +62,7 @@ struct JSENV_API JSFunctionInfo
6762
const CFunctionInfo* ReflectionInfo = nullptr;
6863
};
6964

70-
struct JSENV_API JSPropertyInfo
65+
struct REGISTER_API JSPropertyInfo
7166
{
7267
JSPropertyInfo() : Name(nullptr), Getter(nullptr), Setter(nullptr)
7368
{
@@ -100,7 +95,7 @@ struct JSENV_API JSPropertyInfo
10095
struct NamedFunctionInfo;
10196
struct NamedPropertyInfo;
10297

103-
struct JSENV_API JSClassDefinition
98+
struct REGISTER_API JSClassDefinition
10499
{
105100
const void* TypeId;
106101
const void* SuperTypeId;
@@ -134,22 +129,24 @@ MSVC_PRAGMA(warning(pop))
134129
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
135130
}
136131

137-
void JSENV_API RegisterJSClass(const JSClassDefinition& ClassDefinition);
132+
void REGISTER_API RegisterJSClass(const JSClassDefinition& ClassDefinition);
138133

139-
void JSENV_API SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* ConstructorInfos, const NamedFunctionInfo* MethodInfos,
134+
void REGISTER_API SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* ConstructorInfos, const NamedFunctionInfo* MethodInfos,
140135
const NamedFunctionInfo* FunctionInfos, const NamedPropertyInfo* PropertyInfos, const NamedPropertyInfo* VariableInfos);
141136

142-
void JSENV_API ForeachRegisterClass(std::function<void(const JSClassDefinition* ClassDefinition)>);
137+
typedef void (*ClassDefinitionForeachCallback)(const JSClassDefinition* ClassDefinition);
138+
139+
void REGISTER_API ForeachRegisterClass(ClassDefinitionForeachCallback Callback);
143140

144-
JSENV_API const JSClassDefinition* FindClassByID(const void* TypeId);
141+
REGISTER_API const JSClassDefinition* FindClassByID(const void* TypeId);
145142

146-
JSENV_API void OnClassNotFound(pesapi_class_not_found_callback Callback);
143+
REGISTER_API void OnClassNotFound(pesapi_class_not_found_callback Callback);
147144

148-
JSENV_API const JSClassDefinition* LoadClassByID(const void* TypeId);
145+
REGISTER_API const JSClassDefinition* LoadClassByID(const void* TypeId);
149146

150-
JSENV_API const JSClassDefinition* FindCppTypeClassByName(const PString& Name);
147+
REGISTER_API const JSClassDefinition* FindCppTypeClassByName(const PString& Name);
151148

152-
JSENV_API bool TraceObjectLifecycle(const void* TypeId, pesapi_on_native_object_enter OnEnter, pesapi_on_native_object_exit OnExit);
149+
REGISTER_API bool TraceObjectLifecycle(const void* TypeId, pesapi_on_native_object_enter OnEnter, pesapi_on_native_object_exit OnExit);
153150

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

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

161-
JSENV_API const JSClassDefinition* FindClassByType(UStruct* Type);
158+
REGISTER_API const JSClassDefinition* FindClassByType(UStruct* Type);
162159

163-
JSENV_API bool IsEditorOnlyUFunction(const UFunction* Func);
160+
REGISTER_API bool IsEditorOnlyUFunction(const UFunction* Func);
164161

165162
#endif
166163

unity/native_src/Inc/PString.h

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
#pragma once
1010

11-
#include <cstring>
12-
#include <functional>
11+
#include <string.h>
1312
#include "NamespaceDef.h"
1413

1514
namespace PUERTS_NAMESPACE
@@ -18,18 +17,18 @@ class PString
1817
{
1918
public:
2019
// Constructors
21-
PString() : data_(nullptr), size_(0)
20+
PString() noexcept: data_(nullptr), size_(0)
2221
{
23-
data_ = new char[1];
22+
data_ = (char*)malloc(1);
2423
data_[0] = '\0';
2524
}
2625

2726
PString(const char* str)
2827
{
2928
if (str)
3029
{
31-
size_ = std::strlen(str);
32-
data_ = new char[size_ + 1];
30+
size_ = strlen(str);
31+
data_ = (char*)malloc(size_ + 1);
3332
#ifdef _MSC_VER
3433
strncpy_s(data_, size_ + 1, str, size_);
3534
#else
@@ -40,7 +39,7 @@ class PString
4039
else
4140
{
4241
size_ = 0;
43-
data_ = new char[1];
42+
data_ = (char*)malloc(1);
4443
data_[0] = '\0';
4544
}
4645
}
@@ -50,7 +49,7 @@ class PString
5049
if (str)
5150
{
5251
size_ = length;
53-
data_ = new char[size_ + 1];
52+
data_ = (char*)malloc(size_ + 1);
5453
#ifdef _MSC_VER
5554
strncpy_s(data_, size_ + 1, str, length);
5655
#else
@@ -61,15 +60,15 @@ class PString
6160
else
6261
{
6362
size_ = 0;
64-
data_ = new char[1];
63+
data_ = (char*)malloc(1);
6564
data_[0] = '\0';
6665
}
6766
}
6867

69-
PString(const PString& other)
68+
PString(const PString& other) noexcept
7069
{
7170
size_ = other.size_;
72-
data_ = new char[size_ + 1];
71+
data_ = (char*)malloc(size_ + 1);
7372
#ifdef _MSC_VER
7473
strncpy_s(data_, size_ + 1, other.data_, size_);
7574
#else
@@ -78,13 +77,13 @@ class PString
7877
data_[size_] = '\0';
7978
}
8079

81-
PString& operator=(const PString& other)
80+
PString& operator=(const PString& other) noexcept
8281
{
8382
if (this != &other)
8483
{
85-
delete[] data_;
84+
free(data_);
8685
size_ = other.size_;
87-
data_ = new char[size_ + 1];
86+
data_ = (char*)malloc(size_ + 1);
8887
#ifdef _MSC_VER
8988
strncpy_s(data_, size_ + 1, other.data_, size_);
9089
#else
@@ -95,17 +94,17 @@ class PString
9594
return *this;
9695
}
9796

98-
~PString()
97+
~PString() noexcept
9998
{
100-
delete[] data_;
99+
free(data_);
101100
}
102101

103102
PString operator+(const PString& other) const
104103
{
105104
PString result;
106105
result.size_ = size_ + other.size_;
107-
delete[] result.data_;
108-
result.data_ = new char[result.size_ + 1];
106+
free(result.data_);
107+
result.data_ = (char*)malloc(result.size_ + 1);
109108
#ifdef _MSC_VER
110109
strncpy_s(result.data_, result.size_ + 1, data_, size_);
111110
strncpy_s(result.data_ + size_, result.size_ - size_ + 1, other.data_, other.size_);
@@ -120,10 +119,10 @@ class PString
120119
friend PString operator+(const char* lhs, const PString& rhs)
121120
{
122121
PString result;
123-
size_t lhs_size = std::strlen(lhs);
122+
size_t lhs_size = strlen(lhs);
124123
result.size_ = lhs_size + rhs.size_;
125-
delete[] result.data_;
126-
result.data_ = new char[result.size_ + 1];
124+
free(result.data_);
125+
result.data_ = (char*)malloc(result.size_ + 1);
127126
#ifdef _MSC_VER
128127
strncpy_s(result.data_, result.size_ + 1, lhs, lhs_size);
129128
strncpy_s(result.data_ + lhs_size, result.size_ - lhs_size + 1, rhs.data_, rhs.size_);
@@ -138,7 +137,7 @@ class PString
138137
PString& operator+=(const PString& other)
139138
{
140139
size_t new_size = size_ + other.size_;
141-
char* new_data = new char[new_size + 1];
140+
char* new_data = (char*)malloc(new_size + 1);
142141
#ifdef _MSC_VER
143142
strncpy_s(new_data, new_size + 1, data_, size_);
144143
strncpy_s(new_data + size_, new_size - size_ + 1, other.data_, other.size_);
@@ -148,7 +147,7 @@ class PString
148147
#endif
149148
new_data[new_size] = '\0';
150149

151-
delete[] data_;
150+
free(data_);
152151
data_ = new_data;
153152
size_ = new_size;
154153

@@ -159,9 +158,9 @@ class PString
159158
{
160159
if (str)
161160
{
162-
size_t str_size = std::strlen(str);
161+
size_t str_size = strlen(str);
163162
size_t new_size = size_ + str_size;
164-
char* new_data = new char[new_size + 1];
163+
char* new_data = (char*)malloc(new_size + 1);
165164
#ifdef _MSC_VER
166165
strncpy_s(new_data, new_size + 1, data_, size_);
167166
strncpy_s(new_data + size_, new_size - size_ + 1, str, str_size);
@@ -171,7 +170,7 @@ class PString
171170
#endif
172171
new_data[new_size] = '\0';
173172

174-
delete[] data_;
173+
free(data_);
175174
data_ = new_data;
176175
size_ = new_size;
177176
}
@@ -195,12 +194,12 @@ class PString
195194

196195
bool operator<(const PString& other) const
197196
{
198-
return std::strcmp(data_, other.data_) < 0;
197+
return strcmp(data_, other.data_) < 0;
199198
}
200199

201200
bool operator==(const PString& other) const
202201
{
203-
return std::strcmp(data_, other.data_) == 0;
202+
return strcmp(data_, other.data_) == 0;
204203
}
205204

206205
private:

unity/native_src/Inc/PuertsNamespaceDef.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,36 @@
4444
#define MSVC_PRAGMA(...)
4545
#endif
4646
#endif
47+
48+
#if defined(__clang__)
49+
# define PUERTS_HAS_ATTRIBUTE_VISIBILITY (__has_attribute(visibility))
50+
#elif defined(__GNUC__)
51+
# define PUERTS_HAS_ATTRIBUTE_VISIBILITY 1
52+
#else
53+
# define PUERTS_HAS_ATTRIBUTE_VISIBILITY 0
54+
#endif
55+
56+
#ifndef REGISTER_API
57+
#ifdef _MSC_VER
58+
#ifdef BUILDING_REGISTER_API_SHARED
59+
# define REGISTER_API __declspec(dllexport)
60+
#elif USING_REGISTER_API_SHARED
61+
# define REGISTER_API __declspec(dllimport)
62+
#else
63+
# define REGISTER_API
64+
#endif // BUILDING_V8_SHARED
65+
66+
#else // _MSC_VER
67+
68+
#if PUERTS_HAS_ATTRIBUTE_VISIBILITY
69+
# ifdef BUILDING_REGISTER_API_SHARED
70+
# define REGISTER_API __attribute__ ((visibility("default")))
71+
# else
72+
# define REGISTER_API
73+
# endif
74+
#else
75+
# define REGISTER_API
76+
#endif
77+
78+
#endif // _MSC_VER
79+
#endif

unity/native_src/Inc/TypeInfo.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include <functional>
1112
#include <string>
1213
#ifdef WITH_V8_FAST_CALL
1314
#include "V8FastCall.hpp"

0 commit comments

Comments
 (0)