Skip to content

Commit

Permalink
优化:名字,空TypeId查找返回空,指针通过InstanceTemplate构造对象
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 23, 2024
1 parent 5c61994 commit 0265fec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
14 changes: 7 additions & 7 deletions unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void FCppObjectMapper::Initialize(v8::Isolate* InIsolate, v8::Local<v8::Context>
{
auto LocalTemplate = v8::FunctionTemplate::New(InIsolate, PointerNew);
LocalTemplate->InstanceTemplate()->SetInternalFieldCount(4); // 0 Ptr, 1, CDataName
PointerConstructor = v8::UniquePersistent<v8::Function>(InIsolate, LocalTemplate->GetFunction(InContext).ToLocalChecked());
PointerTemplate = v8::UniquePersistent<v8::FunctionTemplate>(InIsolate, LocalTemplate);
#ifndef WITH_QUICKJS
PrivateKey.Reset(InIsolate, v8::Symbol::New(InIsolate));
#endif
Expand Down Expand Up @@ -107,7 +107,7 @@ v8::Local<v8::Value> FCppObjectMapper::FindOrAddCppObject(
}
else
{
auto Result = PointerConstructor.Get(Isolate)->NewInstance(Context, 0, nullptr).ToLocalChecked();
auto Result = PointerTemplate.Get(Isolate)->InstanceTemplate()->NewInstance(Context).ToLocalChecked();
DataTransfer::SetPointer(Isolate, Result, Ptr, 0);
DataTransfer::SetPointer(Isolate, Result, TypeId, 1);
return Result;
Expand Down Expand Up @@ -164,8 +164,8 @@ static void CDataNew(const v8::FunctionCallbackInfo<v8::Value>& Info)

v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate* Isolate, const JSClassDefinition* ClassDefinition)
{
auto Iter = CDataNameToTemplateMap.find(ClassDefinition->TypeId);
if (Iter == CDataNameToTemplateMap.end())
auto Iter = TypeIdToTemplateMap.find(ClassDefinition->TypeId);
if (Iter == TypeIdToTemplateMap.end())
{
auto Template = v8::FunctionTemplate::New(
Isolate, CDataNew, v8::External::New(Isolate, const_cast<void*>(reinterpret_cast<const void*>(ClassDefinition))));
Expand Down Expand Up @@ -284,7 +284,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
}
}

CDataNameToTemplateMap[ClassDefinition->TypeId] = v8::UniquePersistent<v8::FunctionTemplate>(Isolate, Template);
TypeIdToTemplateMap[ClassDefinition->TypeId] = v8::UniquePersistent<v8::FunctionTemplate>(Isolate, Template);

return Template;
}
Expand Down Expand Up @@ -425,11 +425,11 @@ void FCppObjectMapper::UnInitialize(v8::Isolate* InIsolate)
}
}
CDataCache.clear();
CDataNameToTemplateMap.clear();
TypeIdToTemplateMap.clear();
#ifndef WITH_QUICKJS
PrivateKey.Reset();
#endif
PointerConstructor.Reset();
PointerTemplate.Reset();
}

} // namespace PUERTS_NAMESPACE
4 changes: 2 additions & 2 deletions unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class FCppObjectMapper final : public ICppObjectMapper
private:
std::unordered_map<void*, FObjectCacheNode, PointerHash, PointerEqual> CDataCache;

std::unordered_map<const void*, v8::UniquePersistent<v8::FunctionTemplate>, PointerHash, PointerEqual> CDataNameToTemplateMap;
std::unordered_map<const void*, v8::UniquePersistent<v8::FunctionTemplate>, PointerHash, PointerEqual> TypeIdToTemplateMap;

v8::UniquePersistent<v8::Function> PointerConstructor;
v8::UniquePersistent<v8::FunctionTemplate> PointerTemplate;

#ifndef WITH_QUICKJS
v8::Global<v8::Symbol> PrivateKey;
Expand Down
8 changes: 8 additions & 0 deletions unreal/Puerts/Source/JsEnv/Private/JSClassRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class JSClassRegister

const JSClassDefinition* LoadClassByID(const void* TypeId)
{
if (!TypeId)
{
return nullptr;
}
auto clsDef = FindClassByID(TypeId);
if (!clsDef && ClassNotFoundCallback)
{
Expand Down Expand Up @@ -213,6 +217,10 @@ void JSClassRegister::SetClassTypeInfo(const void* TypeId, const NamedFunctionIn

const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId)
{
if (!TypeId)
{
return nullptr;
}
auto Iter = CDataIdToClassDefinition.find(TypeId);
if (Iter == CDataIdToClassDefinition.end())
{
Expand Down

0 comments on commit 0265fec

Please sign in to comment.