Skip to content

Commit

Permalink
[unity]简化,FunctionToDelegate_pesapi改为在Puerts_il2cpp.cpp中完成
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Sep 29, 2024
1 parent d977c41 commit 14c9644
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,12 @@ handle_underlying:
{
if (IsDelegate(klass))
{
return (Il2CppObject*)g_unityExports.FunctionToDelegate(env, jsval, klass, true);
JsClassInfoHeader* jsClassInfo = g_unityExports.GetJsClassInfo(klass);
if (!jsClassInfo)
{
Exception::Raise(Exception::GetInvalidOperationException("call not load type of delegate"));
}
return FunctionToDelegate(env, jsval, jsClassInfo);
}
return nullptr;
}
Expand Down Expand Up @@ -1694,8 +1699,6 @@ MethodPointer FindBridgeFunc(const char* signature);

puerts::UnityExports* GetUnityExports()
{
g_unityExports.ObjectAllocate = &ObjectAllocate;
g_unityExports.DelegateAllocate = &DelegateAllocate;
g_unityExports.ValueTypeDeallocate = &ValueTypeFree;
g_unityExports.MethodCallback = &MethodCallback;
g_unityExports.ConstructorCallback = &CtorCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ struct PObjectRefInfo
#if defined(USE_OUTSIZE_UNITY)

typedef void (*MethodPointer)();
typedef void* (*ObjectAllocateFunc)(const void* type);
typedef void (*ValueTypeDeallocateFunc)(void* ptr);
typedef void* (*DelegateAllocateFunc)(const void* type, MethodPointer methodPointer, struct PObjectRefInfo** outDelegateInfo);
typedef void MethodType;
typedef bool (*WrapFuncPtr)(MethodType* method, MethodPointer methodPointer, const v8::FunctionCallbackInfo<v8::Value>& info, bool checkArgument, struct WrapData* wrapData);
typedef v8::FunctionCallback FunctionCallbackFunc;
Expand All @@ -43,9 +41,7 @@ typedef v8::Value* (*GetModuleExecutorFunc)(v8::Context* env);

#define MethodPointer Il2CppMethodPointer

typedef void* (*ObjectAllocateFunc)(Il2CppClass *klass);
typedef void (*ValueTypeDeallocateFunc)(void* ptr);
typedef void* (*DelegateAllocateFunc)(Il2CppClass* klass, MethodPointer methodPointer, struct PObjectRefInfo** outDelegateInfo);
typedef MethodInfo MethodType;
typedef bool (*WrapFuncPtr)(MethodType* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkArgument, struct WrapData* wrapData);
typedef pesapi_callback FunctionCallbackFunc;
Expand All @@ -64,7 +60,7 @@ typedef pesapi_value (*GetModuleExecutorFunc)(pesapi_env env);

#endif

typedef void* (*FunctionToDelegateFunc)(pesapi_env env, pesapi_value pvalue, const void* TypeId, bool throwIfFail);
typedef struct JsClassInfoHeader* (*GetJsClassInfoFunc)(const void* TypeId);

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

Expand Down Expand Up @@ -105,8 +101,6 @@ struct JsClassInfoHeader
struct UnityExports
{
//.cpp api
ObjectAllocateFunc ObjectAllocate = nullptr;
DelegateAllocateFunc DelegateAllocate = nullptr;
ValueTypeDeallocateFunc ValueTypeDeallocate = nullptr;
FunctionCallbackFunc MethodCallback = nullptr;
InitializeFunc ConstructorCallback = nullptr;
Expand All @@ -124,7 +118,7 @@ struct UnityExports

SetNativePtrFunc SetNativePtr = nullptr;
UnrefJsObjectFunc UnrefJsObject = nullptr;
FunctionToDelegateFunc FunctionToDelegate = nullptr;
GetJsClassInfoFunc GetJsClassInfo = nullptr;

GetModuleExecutorFunc GetModuleExecutor = nullptr;

Expand Down
47 changes: 7 additions & 40 deletions unity/native_src_il2cpp/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,53 +172,20 @@ static void SetRuntimeObjectToPersistentObject(pesapi_env env, pesapi_value pval
}


static void* FunctionToDelegate_pesapi(pesapi_env env, pesapi_value jsval, const void* TypeId, bool throwIfFail)
static JsClassInfoHeader* GetJsClassInfo(const void* TypeId)
{
//TODO: pesapi 数据到v8的转换应该交给pesapi实现来提供
v8::Local<v8::Context> Context;
memcpy(static_cast<void*>(&Context), &env, sizeof(env));
v8::Local<v8::Object> Func;
memcpy(static_cast<void*>(&Func), &jsval, sizeof(jsval));

if (!Func->IsFunction())
auto ClassDefinition = FindClassByID(TypeId, true);
if (!ClassDefinition)
{
if (throwIfFail)
{
DataTransfer::ThrowException(Context->GetIsolate(), "not a function, can not use as Delegate");
}
return nullptr;
}
void* Ptr = _GetRuntimeObjectFromPersistentObject(Context, Func);
if (Ptr == nullptr)
{
auto ClassDefinition = FindClassByID(TypeId, true);
if (!ClassDefinition)
{
if (throwIfFail)
{
DataTransfer::ThrowException(Context->GetIsolate(), "call not load type of delegate");
}
return nullptr;
}

JsClassInfo* classInfo = static_cast<JsClassInfo*>(ClassDefinition->Data);

PObjectRefInfo* delegateInfo = nullptr;
Ptr = GUnityExports.DelegateAllocate(classInfo->Class, classInfo->DelegateBridge, &delegateInfo);
memset(delegateInfo, 0, sizeof(PObjectRefInfo));

delegateInfo->EnvRef = pesapi_create_env_ref(env);
delegateInfo->ValueRef = pesapi_create_value_ref(env, jsval);
//puerts::PLog(puerts::LogLevel::Log, "FunctionToDelegate (Plugin):%p, %p, %p, %p", Ptr, delegateInfo, delegateInfo->EnvRef, delegateInfo->ValueRef);

_SetRuntimeObjectToPersistentObject(Context, Func, Ptr);
}

return Ptr;
return static_cast<JsClassInfo*>(ClassDefinition->Data);
}

static v8::Value* GetModuleExecutor(v8::Context* env)
{
//TODO: pesapi 数据到v8的转换应该交给pesapi实现来提供
v8::Local<v8::Context> Context;
memcpy(static_cast<void*>(&Context), &env, sizeof(env));

Expand Down Expand Up @@ -717,7 +684,7 @@ V8_EXPORT void ExchangeAPI(puerts::UnityExports * exports)
exports->SetNativePtr = &puerts::SetNativePtr;
exports->SetExtraData = &puerts::SetExtraData;
exports->UnrefJsObject = &puerts::UnrefJsObject;
exports->FunctionToDelegate = &puerts::FunctionToDelegate_pesapi;
exports->GetJsClassInfo = &puerts::GetJsClassInfo;
exports->SetRuntimeObjectToPersistentObject = &puerts::SetRuntimeObjectToPersistentObject;
exports->GetRuntimeObjectFromPersistentObject = &puerts::GetRuntimeObjectFromPersistentObject;
exports->GetModuleExecutor = &puerts::GetModuleExecutor;
Expand Down Expand Up @@ -754,7 +721,7 @@ V8_EXPORT void SetObjectToGlobal(puerts::JSEnv* jsEnv, const char* key, void *ob
v8::Local<v8::Context> Context = jsEnv->MainContext.Get(Isolate);
v8::Context::Scope ContextScope(Context);

void* klass = *static_cast<void**>(obj);
void* klass = *static_cast<void**>(obj); //TODO: 这是Il2cpp内部实现
Context->Global()->Set(Context, v8::String::NewFromUtf8(Isolate, key).ToLocalChecked(), puerts::DataTransfer::FindOrAddCData(Isolate, Context, klass, obj, true)).Check();
}
}
Expand Down

0 comments on commit 14c9644

Please sign in to comment.