Skip to content

Commit

Permalink
[p-api]类注册可以是通用的,应该不引用v8的类型 (#1879)
Browse files Browse the repository at this point in the history
* 让类型注册不依赖V8

* android的编译错误

* android下编译错误

* format check fail

* android编译错误

* android和mac的报错

* 忽略4191告警

* 4191 warning

* 4191 warning
  • Loading branch information
chexiongsheng authored Oct 24, 2024
1 parent dcf5d29 commit d918775
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 122 deletions.
21 changes: 12 additions & 9 deletions unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ static void CDataNew(const v8::FunctionCallbackInfo<v8::Value>& Info)
void* Ptr = nullptr;

if (ClassDefinition->Initialize)
Ptr = ClassDefinition->Initialize(Info);
Ptr = ClassDefinition->Initialize((pesapi_callback_info) &Info);
if (Ptr == nullptr)
return;

Expand All @@ -162,6 +162,8 @@ static void CDataNew(const v8::FunctionCallbackInfo<v8::Value>& Info)
}
}

MSVC_PRAGMA(warning(push))
MSVC_PRAGMA(warning(disable : 4191))
v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate* Isolate, const JSClassDefinition* ClassDefinition)
{
auto Iter = TypeIdToTemplateMap.find(ClassDefinition->TypeId);
Expand All @@ -185,9 +187,9 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
: v8::Local<v8::Value>();
Template->PrototypeTemplate()->SetAccessorProperty(
v8::String::NewFromUtf8(Isolate, PropertyInfo->Name, v8::NewStringType::kNormal).ToLocalChecked(),
PropertyInfo->Getter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Getter, GetterData)
PropertyInfo->Getter ? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Getter, GetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Setter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Setter, SetterData)
PropertyInfo->Setter ? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Setter, SetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyAttribute);
++PropertyInfo;
Expand All @@ -207,9 +209,9 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
: v8::Local<v8::Value>();
Template->SetAccessorProperty(
v8::String::NewFromUtf8(Isolate, PropertyInfo->Name, v8::NewStringType::kNormal).ToLocalChecked(),
PropertyInfo->Getter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Getter, GetterData)
PropertyInfo->Getter ? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Getter, GetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Setter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Setter, SetterData)
PropertyInfo->Setter ? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Setter, SetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyAttribute);
++PropertyInfo;
Expand All @@ -224,7 +226,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
{
Template->PrototypeTemplate()->Set(
v8::String::NewFromUtf8(Isolate, FunctionInfo->Name, v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(Isolate, FunctionInfo->Callback,
v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) FunctionInfo->Callback,
FunctionInfo->Data ? static_cast<v8::Local<v8::Value>>(v8::External::New(Isolate, FunctionInfo->Data))
: v8::Local<v8::Value>(),
v8::Local<v8::Signature>(), 0, v8::ConstructorBehavior::kThrow, v8::SideEffectType::kHasSideEffect,
Expand All @@ -235,7 +237,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
{
Template->PrototypeTemplate()->Set(
v8::String::NewFromUtf8(Isolate, FunctionInfo->Name, v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(Isolate, FunctionInfo->Callback,
v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) FunctionInfo->Callback,
FunctionInfo->Data ? static_cast<v8::Local<v8::Value>>(v8::External::New(Isolate, FunctionInfo->Data))
: v8::Local<v8::Value>()
#ifndef WITH_QUICKJS
Expand All @@ -254,7 +256,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
if (FastCallInfo)
{
Template->Set(v8::String::NewFromUtf8(Isolate, FunctionInfo->Name, v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(Isolate, FunctionInfo->Callback,
v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) FunctionInfo->Callback,
FunctionInfo->Data ? static_cast<v8::Local<v8::Value>>(v8::External::New(Isolate, FunctionInfo->Data))
: v8::Local<v8::Value>(),
v8::Local<v8::Signature>(), 0, v8::ConstructorBehavior::kThrow, v8::SideEffectType::kHasSideEffect,
Expand All @@ -264,7 +266,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
#endif
{
Template->Set(v8::String::NewFromUtf8(Isolate, FunctionInfo->Name, v8::NewStringType::kNormal).ToLocalChecked(),
v8::FunctionTemplate::New(Isolate, FunctionInfo->Callback,
v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) FunctionInfo->Callback,
FunctionInfo->Data ? static_cast<v8::Local<v8::Value>>(v8::External::New(Isolate, FunctionInfo->Data))
: v8::Local<v8::Value>()
#ifndef WITH_QUICKJS
Expand Down Expand Up @@ -293,6 +295,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
return v8::Local<v8::FunctionTemplate>::New(Isolate, Iter->second);
}
}
MSVC_PRAGMA(warning(pop))

static void CDataGarbageCollectedWithFree(const v8::WeakCallbackInfo<JSClassDefinition>& Data)
{
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FBox2D_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ struct AutoRegisterForFBox2D

Def.UETypeName = "Box2D";

Def.Initialize = _FBox2DNew_;
Def.SetInitialize(_FBox2DNew_);
Def.Finalize = _FBox2DDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FColor_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ struct AutoRegisterForFColor

Def.UETypeName = "Color";

Def.Initialize = _FColorNew_;
Def.SetInitialize(_FColorNew_);
Def.Finalize = _FColorDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FGuid_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ struct AutoRegisterForFGuid

Def.UETypeName = "Guid";

Def.Initialize = _FGuidNew_;
Def.SetInitialize(_FGuidNew_);
Def.Finalize = _FGuidDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FIntPoint_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ struct AutoRegisterForFIntPoint

Def.UETypeName = "IntPoint";

Def.Initialize = _FIntPointNew_;
Def.SetInitialize(_FIntPointNew_);
Def.Finalize = _FIntPointDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FIntVector_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ struct AutoRegisterForFIntVector

Def.UETypeName = "IntVector";

Def.Initialize = _FIntVectorNew_;
Def.SetInitialize(_FIntVectorNew_);
Def.Finalize = _FIntVectorDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ struct AutoRegisterForFLinearColor

Def.UETypeName = "LinearColor";

Def.Initialize = _FLinearColorNew_;
Def.SetInitialize(_FLinearColorNew_);
Def.Finalize = _FLinearColorDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FQuat_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,7 @@ struct AutoRegisterForFQuat

Def.UETypeName = "Quat";

Def.Initialize = _FQuatNew_;
Def.SetInitialize(_FQuatNew_);
Def.Finalize = _FQuatDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FRotator_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ struct AutoRegisterForFRotator

Def.UETypeName = "Rotator";

Def.Initialize = _FRotatorNew_;
Def.SetInitialize(_FRotatorNew_);
Def.Finalize = _FRotatorDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FTransform_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,7 @@ struct AutoRegisterForFTransform

Def.UETypeName = "Transform";

Def.Initialize = _FTransformNew_;
Def.SetInitialize(_FTransformNew_);
Def.Finalize = _FTransformDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FVector2D_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ struct AutoRegisterForFVector2D

Def.UETypeName = "Vector2D";

Def.Initialize = _FVector2DNew_;
Def.SetInitialize(_FVector2DNew_);
Def.Finalize = _FVector2DDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FVector4_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ struct AutoRegisterForFVector4

Def.UETypeName = "Vector4";

Def.Initialize = _FVector4New_;
Def.SetInitialize(_FVector4New_);
Def.Finalize = _FVector4Delete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
2 changes: 1 addition & 1 deletion unreal/Puerts/Source/JsEnv/Private/Gen/FVector_Wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3103,7 +3103,7 @@ struct AutoRegisterForFVector

Def.UETypeName = "Vector";

Def.Initialize = _FVectorNew_;
Def.SetInitialize(_FVectorNew_);
Def.Finalize = _FVectorDelete_;
Def.Properties = Properties;
Def.Methods = Methods;
Expand Down
8 changes: 4 additions & 4 deletions unreal/Puerts/Source/JsEnv/Private/JSClassRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class JSClassRegister

const JSClassDefinition* FindClassByID(const void* TypeId);

void OnClassNotFound(ClassNotFoundCallback InCallback)
void OnClassNotFound(pesapi_class_not_found_callback InCallback)
{
ClassNotFoundCallback = InCallback;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ class JSClassRegister
private:
std::map<const void*, JSClassDefinition*> CDataIdToClassDefinition;
std::map<std::string, JSClassDefinition*> CDataNameToClassDefinition;
ClassNotFoundCallback ClassNotFoundCallback = nullptr;
pesapi_class_not_found_callback ClassNotFoundCallback = nullptr;
#if USING_IN_UNREAL_ENGINE
std::map<std::string, AddonRegisterFunc> AddonRegisterInfos;
std::map<FString, JSClassDefinition*> StructNameToClassDefinition;
Expand Down Expand Up @@ -319,7 +319,7 @@ const JSClassDefinition* FindClassByID(const void* TypeId)
return GetJSClassRegister()->FindClassByID(TypeId);
}

void OnClassNotFound(ClassNotFoundCallback Callback)
void OnClassNotFound(pesapi_class_not_found_callback Callback)
{
GetJSClassRegister()->OnClassNotFound(Callback);
}
Expand All @@ -334,7 +334,7 @@ const JSClassDefinition* FindCppTypeClassByName(const std::string& Name)
return GetJSClassRegister()->FindCppTypeClassByName(Name);
}

bool TraceObjectLifecycle(const void* TypeId, OnObjectEnter OnEnter, OnObjectExit OnExit)
bool TraceObjectLifecycle(const void* TypeId, pesapi_on_native_object_enter OnEnter, pesapi_on_native_object_exit OnExit)
{
if (auto clsDef = const_cast<JSClassDefinition*>(GetJSClassRegister()->FindClassByID(TypeId)))
{
Expand Down
29 changes: 8 additions & 21 deletions unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
#include <vector>
#include <cstring>

#ifndef MSVC_PRAGMA
#if !defined(__clang__) && defined(_MSC_VER)
#define MSVC_PRAGMA(Pragma) __pragma(Pragma)
#else
#define MSVC_PRAGMA(...)
#endif
#endif

struct pesapi_env_ref__
{
explicit pesapi_env_ref__(v8::Local<v8::Context> context)
Expand Down Expand Up @@ -964,8 +956,6 @@ static void free_property_descriptor(pesapi_property_descriptor properties, size
// set module name here during loading, set nullptr after module loaded
const char* GPesapiModuleName = nullptr;

MSVC_PRAGMA(warning(push))
MSVC_PRAGMA(warning(disable : 4191))
void pesapi_define_class(const void* type_id, const void* super_type_id, const char* type_name, pesapi_constructor constructor,
pesapi_finalize finalize, size_t property_count, pesapi_property_descriptor properties, void* data)
{
Expand All @@ -985,7 +975,7 @@ void pesapi_define_class(const void* type_id, const void* super_type_id, const c
}
classDef.Data = data;

classDef.Initialize = reinterpret_cast<puerts::InitializeFunc>(constructor);
classDef.Initialize = constructor;
classDef.Finalize = finalize;

std::vector<puerts::JSFunctionInfo> p_methods;
Expand All @@ -1000,18 +990,16 @@ void pesapi_define_class(const void* type_id, const void* super_type_id, const c
{
if (p->is_static)
{
p_variables.push_back({p->name, reinterpret_cast<v8::FunctionCallback>(p->getter),
reinterpret_cast<v8::FunctionCallback>(p->setter), p->data0, p->data1});
p_variables.push_back({p->name, p->getter, p->setter, p->data0, p->data1});
}
else
{
p_properties.push_back({p->name, reinterpret_cast<v8::FunctionCallback>(p->getter),
reinterpret_cast<v8::FunctionCallback>(p->setter), p->data0, p->data1});
p_properties.push_back({p->name, p->getter, p->setter, p->data0, p->data1});
}
}
else if (p->method != nullptr)
{
puerts::JSFunctionInfo finfo{p->name, reinterpret_cast<v8::FunctionCallback>(p->method), p->data0};
puerts::JSFunctionInfo finfo{p->name, p->method, p->data0};
if (p->is_static)
{
p_functions.push_back(finfo);
Expand All @@ -1025,10 +1013,10 @@ void pesapi_define_class(const void* type_id, const void* super_type_id, const c

free_property_descriptor(properties, property_count);

p_methods.push_back({nullptr, nullptr, nullptr});
p_functions.push_back({nullptr, nullptr, nullptr});
p_properties.push_back({nullptr, nullptr, nullptr, nullptr});
p_variables.push_back({nullptr, nullptr, nullptr, nullptr});
p_methods.push_back(puerts::JSFunctionInfo());
p_functions.push_back(puerts::JSFunctionInfo());
p_properties.push_back(puerts::JSPropertyInfo());
p_variables.push_back(puerts::JSPropertyInfo());

classDef.Methods = p_methods.data();
classDef.Functions = p_functions.data();
Expand All @@ -1037,7 +1025,6 @@ void pesapi_define_class(const void* type_id, const void* super_type_id, const c

puerts::RegisterJSClass(classDef);
}
MSVC_PRAGMA(warning(pop))

void* pesapi_get_class_data(const void* type_id, bool force_load)
{
Expand Down
Loading

0 comments on commit d918775

Please sign in to comment.