Skip to content

Commit

Permalink
[unity]用pesapi_on_class_not_found实现lazyload
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 15, 2024
1 parent 76a3ec2 commit 1585c04
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,23 @@ static Il2CppClass *g_typeofPersistentObjectInfo;
static Il2CppClass *g_typeofArrayBuffer;
static Il2CppClass *g_typeofTypedValue;

static const MethodInfo *g_methodInfoRegisterNoThrow;
typedef void (*RegisterNoThrowFunc) (const void* typeId, bool includeNonPublic, const MethodInfo* methodInfo);
static RegisterNoThrowFunc g_registerNoThrow;

static bool ClassNotFoundCallback(const void* typeId)
{
g_registerNoThrow(typeId, false, g_methodInfoRegisterNoThrow);
return true;
}

static void SetRegisterNoThrow(Il2CppReflectionMethod* method)
{
g_registerNoThrow = (RegisterNoThrowFunc)GetMethodPointer(method);
g_methodInfoRegisterNoThrow = (const MethodInfo *)GetMethodInfoPointer(method);
pesapi_on_class_not_found(ClassNotFoundCallback);
}

Il2CppClass* GetReturnType(const MethodInfo* method) {
if (kInvalidIl2CppMethodSlot != method->slot) {
Class::Init(method->klass);
Expand Down Expand Up @@ -1953,6 +1970,7 @@ void InitialPuerts(pesapi_func_ptr* func_array)
InternalCalls::Add("PuertsIl2cpp.NativeAPI::InitialPapiEnvRef(System.IntPtr)", (Il2CppMethodPointer)puerts::InitialPapiEnvRef);
InternalCalls::Add("PuertsIl2cpp.NativeAPI::CleanupPapiEnvRef(System.IntPtr)", (Il2CppMethodPointer)puerts::CleanupPapiEnvRef);
InternalCalls::Add("Puerts.JSObject::GetJSObjectValue(System.String,System.Type)", (Il2CppMethodPointer)puerts::GetJSObjectValue);
InternalCalls::Add("PuertsIl2cpp.NativeAPI::SetRegisterNoThrow(System.Reflection.MethodBase)", (Il2CppMethodPointer)puerts::SetRegisterNoThrow);
pesapi_init(func_array);
}

Expand Down
2 changes: 1 addition & 1 deletion unity/Assets/core/upm/Runtime/Src/IL2Cpp/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public JsEnv(ILoader loader, int debugPort = -1)
PuertsIl2cpp.NativeAPI.SetLogCallback(PuertsIl2cpp.NativeAPI.Log);
PuertsIl2cpp.NativeAPI.InitialPuerts(PuertsIl2cpp.NativeAPI.GetPesapiImpl());
tryLoadTypeMethodInfo = typeof(TypeRegister).GetMethod("RegisterNoThrow");
PuertsIl2cpp.NativeAPI.SetTryLoadCallback(PuertsIl2cpp.NativeAPI.GetMethodInfoPointer(tryLoadTypeMethodInfo), PuertsIl2cpp.NativeAPI.GetMethodPointer(tryLoadTypeMethodInfo));
PuertsIl2cpp.NativeAPI.SetRegisterNoThrow(tryLoadTypeMethodInfo);

persistentObjectInfoType = typeof(Puerts.JSObject);
PuertsIl2cpp.NativeAPI.SetGlobalType_TypedValue(typeof(TypedValue));
Expand Down
6 changes: 4 additions & 2 deletions unity/Assets/core/upm/Runtime/Src/IL2Cpp/Native/NativeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public static void CleanupPapiEnvRef(IntPtr envRef)
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetObjectPool(IntPtr jsEnv, IntPtr objectPoolAddMethodInfo, IntPtr objectPoolAdd, IntPtr objectPoolRemoveMethodInfo, IntPtr objectPoolRemove, IntPtr objectPoolInstance);

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetTryLoadCallback(IntPtr tryLoadMethodInfo, IntPtr tryLoad);
[MethodImpl(MethodImplOptions.InternalCall)]
public static void SetRegisterNoThrow(MethodBase methodInfo)
{
}

[MethodImpl(MethodImplOptions.InternalCall)]
public static void SetObjectToGlobal(IntPtr envRef, string key, Object obj)
Expand Down
5 changes: 1 addition & 4 deletions unity/native_src_il2cpp/Inc/JSClassRegister.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ void JSENV_API SetClassTypeInfo(const void* TypeId, const NamedFunctionInfo* Con

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

JSENV_API const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad = false);

typedef void (*LoadTypeFunc) (const void* typeId);
JSENV_API void SetLazyLoadCallback(LoadTypeFunc Callback);
JSENV_API const JSClassDefinition* FindClassByID(const void* TypeId);

JSENV_API void OnClassNotFound(ClassNotFoundCallback Callback);

Expand Down
4 changes: 2 additions & 2 deletions unity/native_src_il2cpp/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ v8::Local<v8::Value> FCppObjectMapper::FindOrAddCppObject(
if (!Template.IsEmpty())
{
auto Result = Template->InstanceTemplate()->NewInstance(Context).ToLocalChecked();
BindCppObject(Isolate, const_cast<JSClassDefinition*>(FindClassByID(TypeId, true)), Ptr, Result, PassByPointer);
BindCppObject(Isolate, const_cast<JSClassDefinition*>(LoadClassByID(TypeId)), Ptr, Result, PassByPointer);
return Result;
}
else
Expand Down Expand Up @@ -181,7 +181,7 @@ v8::Local<v8::FunctionTemplate> FCppObjectMapper::GetTemplateOfClass(v8::Isolate
auto Iter = TypeIdToTemplateMap.find(TypeId);
if (Iter == TypeIdToTemplateMap.end())
{
auto ClassDefinition = FindClassByID(TypeId, true);
auto ClassDefinition = LoadClassByID(TypeId);
if (!ClassDefinition)
{
return v8::Local<v8::FunctionTemplate>();
Expand Down
25 changes: 5 additions & 20 deletions unity/native_src_il2cpp/Src/JSClassRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class JSClassRegister

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

const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad = false);
const JSClassDefinition* FindClassByID(const void* TypeId);

void OnClassNotFound(ClassNotFoundCallback InCallback)
{
Expand All @@ -101,11 +101,6 @@ class JSClassRegister
}
return clsDef;
}

void SetLazyLoadCallback(LoadTypeFunc InCallback)
{
LazyLoad = InCallback;
}

const JSClassDefinition* FindCppTypeClassByName(const std::string& Name);

Expand All @@ -120,7 +115,6 @@ class JSClassRegister
private:
std::map<const void*, JSClassDefinition*> CDataIdToClassDefinition;
std::map<std::string, JSClassDefinition*> CDataNameToClassDefinition;
LoadTypeFunc LazyLoad = nullptr;
std::recursive_mutex RegInfoMutex;
ClassNotFoundCallback ClassNotFoundCallback = nullptr;
#if USING_IN_UNREAL_ENGINE
Expand Down Expand Up @@ -226,15 +220,10 @@ void JSClassRegister::SetClassTypeInfo(const void* TypeId, const NamedFunctionIn
}
}

const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId, bool TryLazyLoad)
const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId)
{
std::lock_guard<std::recursive_mutex> guard(RegInfoMutex);
auto Iter = CDataIdToClassDefinition.find(TypeId);
if (Iter == CDataIdToClassDefinition.end() && TryLazyLoad && LazyLoad)
{
LazyLoad(TypeId);
Iter = CDataIdToClassDefinition.find(TypeId);
}
if (Iter == CDataIdToClassDefinition.end())
{
return nullptr;
Expand Down Expand Up @@ -334,25 +323,21 @@ void ForeachRegisterClass(std::function<void(const JSClassDefinition* ClassDefin
GetJSClassRegister()->ForeachRegisterClass(Callback);
}

const JSClassDefinition* FindClassByID(const void* TypeId, bool TryLazyLoad)
const JSClassDefinition* FindClassByID(const void* TypeId)
{
return GetJSClassRegister()->FindClassByID(TypeId, TryLazyLoad);
return GetJSClassRegister()->FindClassByID(TypeId);
}

void OnClassNotFound(ClassNotFoundCallback Callback)
{
GetJSClassRegister()->OnClassNotFound(Callback);
}

const JSClassDefinition* LoadClassByID(const void* TypeId)
{
return GetJSClassRegister()->LoadClassByID(TypeId);
}

void SetLazyLoadCallback(LoadTypeFunc Callback)
{
GetJSClassRegister()->SetLazyLoadCallback(Callback);
}

const JSClassDefinition* FindCppTypeClassByName(const std::string& Name)
{
return GetJSClassRegister()->FindCppTypeClassByName(Name);
Expand Down
2 changes: 1 addition & 1 deletion unity/native_src_il2cpp/Src/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ MSVC_PRAGMA(warning(pop))

void* pesapi_load_class_data(const void* type_id, bool callback_if_not_found)
{
auto clsDef = puerts::FindClassByID(type_id, callback_if_not_found);
auto clsDef = callback_if_not_found ? puerts::LoadClassByID(type_id) : puerts::FindClassByID(type_id);
return clsDef ? clsDef->Data : nullptr;
}

Expand Down
27 changes: 0 additions & 27 deletions unity/native_src_il2cpp/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ static LogCallback GLogCallback = nullptr;

typedef void (*LazyLoadTypeFunc) (const void* typeId, bool includeNonPublic, void* method);

void* GTryLoadTypeMethodInfo = nullptr;

LazyLoadTypeFunc GTryLazyLoadType = nullptr;

static void LazyLoad(const void* typeId)
{
GTryLazyLoadType(typeId, false, GTryLoadTypeMethodInfo);
}

void PLog(LogLevel Level, const std::string Fmt, ...)
{
static char SLogBuffer[1024];
Expand All @@ -65,17 +56,6 @@ void PLog(LogLevel Level, const std::string Fmt, ...)
}
}

static void* GetJsClassInfo(const void* TypeId, bool TryLazyLoad)
{
auto ClassDefinition = FindClassByID(TypeId, TryLazyLoad);
if (!ClassDefinition)
{
return nullptr;
}

return ClassDefinition->Data;
}

struct JSEnv
{
JSEnv()
Expand Down Expand Up @@ -204,13 +184,6 @@ V8_EXPORT void SetObjectPool(puerts::JSEnv* jsEnv, void* ObjectPoolAddMethodInfo
jsEnv->CppObjectMapper.ObjectPoolInstance = ObjectPoolInstance;
}

V8_EXPORT void SetTryLoadCallback(void* tryLoadMethodInfo, puerts::LazyLoadTypeFunc tryLoad)
{
puerts::GTryLoadTypeMethodInfo = tryLoadMethodInfo;
puerts::GTryLazyLoadType = tryLoad;
puerts::SetLazyLoadCallback(puerts::LazyLoad);
}

V8_EXPORT void CreateInspector(puerts::JSEnv* jsEnv, int32_t Port)
{
jsEnv->BackendEnv.CreateInspector(jsEnv->MainIsolate, &jsEnv->MainContext, Port);
Expand Down

0 comments on commit 1585c04

Please sign in to comment.