Skip to content

Commit

Permalink
让类型注册不依赖V8
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 24, 2024
1 parent dcf5d29 commit 8f20314
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 95 deletions.
18 changes: 9 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 Down Expand Up @@ -185,9 +185,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 +207,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 +224,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 +235,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 +254,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 +264,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
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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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.Initialize = (pesapi_constructor) _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
10 changes: 4 additions & 6 deletions unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,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 +1000,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 Down
32 changes: 18 additions & 14 deletions unreal/Puerts/Source/JsEnv/Private/StructWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ void FStructWrapper::InitTemplateProperties(
: v8::Local<v8::Value>();

Template->PrototypeTemplate()->SetAccessorProperty(FV8Utils::InternalString(Isolate, PropertyInfo->Name),
PropertyInfo->Getter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Getter, GetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Setter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Setter, SetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Getter
? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Getter, GetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Setter
? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Setter, SetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyAttribute);
}
++PropertyInfo;
Expand All @@ -128,10 +130,12 @@ void FStructWrapper::InitTemplateProperties(
: v8::Local<v8::Value>();

Template->SetAccessorProperty(FV8Utils::InternalString(Isolate, PropertyInfo->Name),
PropertyInfo->Getter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Getter, GetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Setter ? v8::FunctionTemplate::New(Isolate, PropertyInfo->Setter, SetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Getter
? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Getter, GetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyInfo->Setter
? v8::FunctionTemplate::New(Isolate, (v8::FunctionCallback) PropertyInfo->Setter, SetterData)
: v8::Local<v8::FunctionTemplate>(),
PropertyAttribute);
++PropertyInfo;
}
Expand Down Expand Up @@ -183,7 +187,7 @@ v8::Local<v8::FunctionTemplate> FStructWrapper::ToFunctionTemplate(v8::Isolate*

if (ClassDefinition)
{
ExternalInitialize = ClassDefinition->Initialize;
ExternalInitialize = (V8InitializeFuncType) ClassDefinition->Initialize;
ExternalFinalize = ClassDefinition->Finalize;
JSFunctionInfo* FunctionInfo = ClassDefinition->Methods;
while (FunctionInfo && FunctionInfo->Name && FunctionInfo->Callback)
Expand All @@ -196,7 +200,7 @@ v8::Local<v8::FunctionTemplate> FStructWrapper::ToFunctionTemplate(v8::Isolate*
if (FastCallInfo)
{
Result->PrototypeTemplate()->Set(FV8Utils::InternalString(Isolate, FunctionInfo->Name),
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 @@ -206,7 +210,7 @@ v8::Local<v8::FunctionTemplate> FStructWrapper::ToFunctionTemplate(v8::Isolate*
#endif
{
Result->PrototypeTemplate()->Set(FV8Utils::InternalString(Isolate, FunctionInfo->Name),
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>()));
}
Expand All @@ -224,7 +228,7 @@ v8::Local<v8::FunctionTemplate> FStructWrapper::ToFunctionTemplate(v8::Isolate*
if (FastCallInfo)
{
Result->Set(FV8Utils::InternalString(Isolate, FunctionInfo->Name),
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 @@ -234,7 +238,7 @@ v8::Local<v8::FunctionTemplate> FStructWrapper::ToFunctionTemplate(v8::Isolate*
#endif
{
Result->Set(FV8Utils::InternalString(Isolate, FunctionInfo->Name),
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>()));
}
Expand Down Expand Up @@ -628,7 +632,7 @@ void* FScriptStructWrapper::Alloc(UScriptStruct* InScriptStruct)
return ScriptStructMemory;
}

void FScriptStructWrapper::Free(TWeakObjectPtr<UStruct> InStruct, FinalizeFunc InExternalFinalize, void* Ptr)
void FScriptStructWrapper::Free(TWeakObjectPtr<UStruct> InStruct, pesapi_finalize InExternalFinalize, void* Ptr)
{
if (InExternalFinalize)
{
Expand Down
8 changes: 5 additions & 3 deletions unreal/Puerts/Source/JsEnv/Private/StructWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ class FStructWrapper

std::vector<UFunction*> ExtensionMethods;

InitializeFunc ExternalInitialize;
typedef void* (*V8InitializeFuncType)(const v8::FunctionCallbackInfo<v8::Value>& Info);

FinalizeFunc ExternalFinalize;
V8InitializeFuncType ExternalInitialize;

pesapi_finalize ExternalFinalize;

TWeakObjectPtr<UStruct> Struct;

Expand Down Expand Up @@ -111,7 +113,7 @@ class FScriptStructWrapper : public FStructWrapper

static void* Alloc(UScriptStruct* InScriptStruct);

static void Free(TWeakObjectPtr<UStruct> InStruct, FinalizeFunc InExternalFinalize, void* Ptr);
static void Free(TWeakObjectPtr<UStruct> InStruct, pesapi_finalize InExternalFinalize, void* Ptr);

void Free(void* Ptr)
{
Expand Down
Loading

0 comments on commit 8f20314

Please sign in to comment.