Skip to content

Commit

Permalink
[unity]Delegate构造移动到Puerts_il2cpp.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 24, 2024
1 parent d0fd822 commit 561f194
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,18 @@ void* GetDefaultValuePtr(const MethodInfo* method, uint32_t index)

static void* CtorCallback(pesapi_callback_info info);

static void* DelegateCtorCallback(pesapi_callback_info Info);

static puerts::UnityExports g_unityExports;

void IL2cppLog(const char* value)
{
if (g_unityExports.LogCallback)
{
g_unityExports.LogCallback(value);
}
}

static void* CtorCallback(pesapi_callback_info info)
{
JsClassInfoHeader* classInfo = reinterpret_cast<JsClassInfoHeader*>(pesapi_get_constructor_userdata(info));
Expand Down Expand Up @@ -407,6 +417,21 @@ static void* CtorCallback(pesapi_callback_info info)
return nullptr;
}

static void* DelegateCtorCallback(pesapi_callback_info info)
{
IL2cppLog("DelegateCtorCallback in Puerts_il2cpp.cpp");
pesapi_env env = pesapi_get_env(info);
pesapi_value jsval = pesapi_get_arg(info, 0);
if (!pesapi_is_function(env, jsval))
{
pesapi_throw_by_string(info, "expect a function");
return nullptr;
}
JsClassInfoHeader* classInfo = reinterpret_cast<JsClassInfoHeader*>(pesapi_get_constructor_userdata(info));

return g_unityExports.FunctionToDelegate(env, jsval, classInfo->Class, true);
}

void ReleaseScriptObject(RuntimeObject* obj)
{
int32_t _offset = 1;
Expand Down Expand Up @@ -1615,21 +1640,14 @@ Il2CppObject* GetJSObjectValue(Il2CppObject* jsObject, Il2CppString* key, Il2Cpp
WrapFuncPtr FindWrapFunc(const char* signature);
struct FieldWrapFuncInfo * FindFieldWrapFuncInfo(const char* signature);

void IL2cppLog(const char* value)
{
if (g_unityExports.LogCallback)
{
g_unityExports.LogCallback(value);
}
}

puerts::UnityExports* GetUnityExports()
{
g_unityExports.ObjectAllocate = &ObjectAllocate;
g_unityExports.DelegateAllocate = &DelegateAllocate;
g_unityExports.ValueTypeDeallocate = &ValueTypeFree;
g_unityExports.MethodCallback = &MethodCallback;
g_unityExports.ConstructorCallback = &CtorCallback;
g_unityExports.DelegateConstructorCallback = &DelegateCtorCallback;
g_unityExports.FieldGet = &GetFieldValue;
g_unityExports.FieldSet = &SetFieldValue;
g_unityExports.GetValueTypeFieldPtr = &GetValueTypeFieldPtr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ typedef struct FieldWrapFuncInfo * (*FindFieldWrapFuncInfoFunc)(const char* sign

typedef void(*LogCallbackFunc)(const char* value);

typedef void (*AddPendingReleasePersistentObjectFunc)(pesapi_env env, pesapi_value pvalue);

struct WrapData
{
WrapFuncPtr Wrap;
Expand Down Expand Up @@ -184,6 +186,7 @@ struct UnityExports
ValueTypeDeallocateFunc ValueTypeDeallocate = nullptr;
FunctionCallbackFunc MethodCallback = nullptr;
InitializeFunc ConstructorCallback = nullptr;
InitializeFunc DelegateConstructorCallback = nullptr;
FieldOperationFunc FieldGet = nullptr;
FieldOperationFunc FieldSet = nullptr;
GetValueTypeFieldPtrFunc GetValueTypeFieldPtr = nullptr;
Expand Down Expand Up @@ -212,6 +215,10 @@ struct UnityExports
WrapFuncPtr ReflectionWrapper = nullptr;
FieldWrapFuncPtr ReflectionGetFieldWrapper = nullptr;
FieldWrapFuncPtr ReflectionSetFieldWrapper = nullptr;

FindWrapFuncFunc FindWrapFunc = nullptr;
FindFieldWrapFuncInfoFunc FindFieldWrapFuncInfo = nullptr;

int SizeOfRuntimeObject = 0;
//plugin api

Expand All @@ -227,10 +234,10 @@ struct UnityExports

GetRuntimeObjectFromPersistentObjectFunc GetRuntimeObjectFromPersistentObject = nullptr;
SetRuntimeObjectToPersistentObjectFunc SetRuntimeObjectToPersistentObject = nullptr;
LogCallbackFunc LogCallback = nullptr;

FindWrapFuncFunc FindWrapFunc = nullptr;
FindFieldWrapFuncInfoFunc FindFieldWrapFuncInfo = nullptr;
AddPendingReleasePersistentObjectFunc AddPendingReleasePersistentObject = nullptr;

LogCallbackFunc LogCallback = nullptr;
};

}
2 changes: 2 additions & 0 deletions unity/native_src_il2cpp/Inc/CppObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class FCppObjectMapper final : public ICppObjectMapper
void* ObjectPoolInstance = nullptr;

FPersistentObjectEnvInfo PersistentObjectEnvInfo;

void AddPendingReleasePersistentObject(v8::Local<v8::Context> Context, v8::Local<v8::Object> Obj);

void ClearPendingPersistentObject(v8::Isolate* Isolate, v8::Local<v8::Context> Context);

Expand Down
6 changes: 6 additions & 0 deletions unity/native_src_il2cpp/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ static void CDataNew(const v8::FunctionCallbackInfo<v8::Value>& Info)
}
}

void FCppObjectMapper::AddPendingReleasePersistentObject(v8::Local<v8::Context> Context, v8::Local<v8::Object> Obj)
{
std::lock_guard<std::mutex> guard(PersistentObjectEnvInfo.Mutex);
PersistentObjectEnvInfo.PendingReleaseObjects.push_back(v8::Global<v8::Object>(Context->GetIsolate(), Obj));
}

void FCppObjectMapper::ClearPendingPersistentObject(v8::Isolate* Isolate, v8::Local<v8::Context> Context)
{
std::lock_guard<std::mutex> guard(PersistentObjectEnvInfo.Mutex);
Expand Down
25 changes: 24 additions & 1 deletion unity/native_src_il2cpp/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,28 @@ static void UnrefJsObject(PersistentObjectInfo* objectInfo)
objectInfo->EnvInfo = nullptr;
}

static void _AddPendingReleasePersistentObject(v8::Local<v8::Context> Context, v8::Local<v8::Object> Obj)
{
auto Isolate = Context->GetIsolate();
auto POEnv = DataTransfer::GetPersistentObjectEnvInfo(Isolate);

puerts::FCppObjectMapper* mapper = static_cast<puerts::FCppObjectMapper*>(Isolate->GetData(MAPPER_ISOLATE_DATA_POS));

mapper->AddPendingReleasePersistentObject(Context, Obj);

}

static void AddPendingReleasePersistentObject(pesapi_env env, pesapi_value pvalue)
{
// TODO: quickjs兼容 ///
v8::Local<v8::Context> Context;
memcpy(static_cast<void*>(&Context), &env, sizeof(env));
v8::Local<v8::Object> Obj;
memcpy(static_cast<void*>(&Obj), &pvalue, sizeof(pvalue));

_AddPendingReleasePersistentObject(Context, Obj);
}

template <typename T>
struct RestArguments
{
Expand Down Expand Up @@ -1027,7 +1049,7 @@ V8_EXPORT bool RegisterCSharpType(puerts::JsClassInfo* classInfo)
ClassDef.TypeId = classInfo->TypeId;
ClassDef.SuperTypeId = classInfo->SuperTypeId;

ClassDef.Initialize = classInfo->DelegateBridge ? puerts::DelegateCtorCallback : puerts::GUnityExports.ConstructorCallback;
ClassDef.Initialize = classInfo->DelegateBridge ? puerts::GUnityExports.DelegateConstructorCallback : puerts::GUnityExports.ConstructorCallback;
ClassDef.Finalize = classInfo->IsValueType ? puerts::GUnityExports.ValueTypeDeallocate : (puerts::FinalizeFunc)nullptr;
ClassDef.Data = classInfo;

Expand Down Expand Up @@ -1152,6 +1174,7 @@ V8_EXPORT void ExchangeAPI(puerts::UnityExports * exports)
exports->GetJSObjectValue = &puerts::GetJSObjectValue;
exports->GetModuleExecutor = &puerts::GetModuleExecutor;
exports->LogCallback = puerts::GLogCallback;
exports->AddPendingReleasePersistentObject = &puerts::AddPendingReleasePersistentObject;
puerts::GUnityExports = *exports;
}

Expand Down

0 comments on commit 561f194

Please sign in to comment.