From da29490479042ad66c4afe0f1e025b7d4f06e248 Mon Sep 17 00:00:00 2001 From: johnche Date: Mon, 30 Sep 2024 17:17:46 +0800 Subject: [PATCH] =?UTF-8?q?[unity]=E5=8E=BB=E6=8E=89plugin=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E7=9A=84SetNativePtr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../puerts/templates/il2cppwrapper.tpl.mjs | 2 +- .../puerts/templates/il2cppwrapperdef.tpl.mjs | 4 +- .../puerts/xil2cpp/Puerts_il2cpp.cpp.txt | 59 +++++++++++-------- .../Resources/puerts/xil2cpp/TDataTrans.h.txt | 3 +- .../puerts/xil2cpp/UnityExports4Puerts.h.txt | 6 -- unity/cli/UnityExports4Puerts.h | 6 -- unity/native_src_il2cpp/Src/Puerts.cpp | 7 --- 7 files changed, 38 insertions(+), 49 deletions(-) diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapper.tpl.mjs b/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapper.tpl.mjs index 69203012c9..71dd7c6f67 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapper.tpl.mjs +++ b/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapper.tpl.mjs @@ -25,7 +25,7 @@ export default function Gen(genInfos) { namespace puerts { -${wrapperInfos.map((wrapperInfo) => `bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkJSArgument, WrapData* wrapData);`).join('\n')} +${wrapperInfos.map((wrapperInfo) => `bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkJSArgument, WrapData* wrapData);`).join('\n')} static WrapFuncInfo g_wrapFuncInfos[] = { ${FOR(wrapperInfos, info => t` diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapperdef.tpl.mjs b/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapperdef.tpl.mjs index 7602b75b5d..1adb051f0e 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapperdef.tpl.mjs +++ b/unity/Assets/core/upm/Editor/Resources/puerts/templates/il2cppwrapperdef.tpl.mjs @@ -13,12 +13,11 @@ function genFuncWrapper(wrapperInfo) { return t` // ${wrapperInfo.CsName} -bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkJSArgument, WrapData* wrapData) { +bool w_${wrapperInfo.Signature}(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkJSArgument, WrapData* wrapData) { // PLog("Running w_${wrapperInfo.Signature}"); ${il2cpp_snippets.declareTypeInfo(wrapperInfo)} - pesapi_env env = pesapi_get_env(info); int js_args_len = pesapi_get_args_len(info); ${parameterSignatures.map((x, i) => ` pesapi_value _sv${i} = pesapi_get_arg(info, ${i});`).join('\n')} @@ -29,7 +28,6 @@ ${parameterSignatures.map((x, i) => ` pesapi_value _sv${i} = pesapi_get_arg(i ${il2cpp_snippets.checkJSArg(x, i)} `)} } - ${il2cpp_snippets.getThis(wrapperInfo.ThisSignature)} ${parameterSignatures.map((x, i) => il2cpp_snippets.JSValToCSVal(x, `_sv${i}`, `p${i}`)).join('\n')} diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt index 0508f16c6a..5f5bf9e717 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt +++ b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt @@ -269,10 +269,25 @@ static void MethodCallback(pesapi_callback_info info) try { WrapData** wrapDatas = (WrapData**)pesapi_get_userdata(info); + void* self = nullptr; + pesapi_env env = pesapi_get_env(info); + if (!((*wrapDatas)->IsStatic)) + { + pesapi_value jsThis = pesapi_get_holder(info); + self = pesapi_get_native_object_ptr(env, jsThis); + if ((*wrapDatas)->NeedBoxing) + { + auto ptrType = (Il2CppClass*) pesapi_get_native_object_typeid(env, jsThis); + if (Class::IsValuetype(ptrType)) + { + self = Object::Box(ptrType, self); + } + } + } bool checkArgument = *wrapDatas && *(wrapDatas + 1); while(*wrapDatas) { - if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, checkArgument, *wrapDatas)) + if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, env, self, checkArgument, *wrapDatas)) { return; } @@ -417,9 +432,7 @@ static void* CtorCallback(pesapi_callback_info info) return nullptr; } - void* Ptr = ObjectAllocate(classInfo->Class); - - g_unityExports.SetNativePtr(pesapi_get_this(info), Ptr, classInfo->TypeId); + void* self = ObjectAllocate(classInfo->Class); auto isValueType = Class::IsValuetype(classInfo->Class); @@ -427,18 +440,19 @@ static void* CtorCallback(pesapi_callback_info info) { WrapData** wrapDatas = classInfo->CtorWrapDatas; bool checkArgument = *wrapDatas && *(wrapDatas + 1); + pesapi_env env = pesapi_get_env(info); while(*wrapDatas) { - if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, checkArgument, *wrapDatas)) + if ((*wrapDatas)->Wrap((*wrapDatas)->Method, (*wrapDatas)->MethodPointer, info, env, self, checkArgument, *wrapDatas)) { - return Ptr; + return self; } ++wrapDatas; } if (isValueType && pesapi_get_args_len(info) == 0) - return Ptr; + return self; else pesapi_throw_by_string(info, "invalid arguments"); @@ -464,7 +478,7 @@ static void* CtorCallback(pesapi_callback_info info) if (isValueType) { - ValueTypeFree(Ptr); + ValueTypeFree(self); } return nullptr; @@ -1188,9 +1202,8 @@ static void JsObjectSetRef(pesapi_env env, pesapi_value outer, pesapi_value val) } } -static bool ReflectionWrapper(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkJSArgument, WrapData* wrapData) +static bool ReflectionWrapper(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkJSArgument, WrapData* wrapData) { - pesapi_env env = pesapi_get_env(info); int js_args_len = pesapi_get_args_len(info); bool hasParamArray = wrapData->HasParamArray; bool isExtensionMethod = wrapData->IsExtensionMethod; @@ -1373,26 +1386,17 @@ handle_underlying: } void** args = method->parameters_count > 0 ? (void**)alloca(sizeof(void*) * method->parameters_count) : nullptr; pesapi_value jsThis = pesapi_get_holder(info); - void* csThis = nullptr; - if (Method::IsInstance(method)) +#ifndef UNITY_2021_1_OR_NEWER + if (self) { - csThis = pesapi_get_native_object_ptr(env, jsThis); Il2CppClass* thisType = method->klass; -#ifndef UNITY_2021_1_OR_NEWER + if (Class::IsValuetype(thisType)) { - csThis = ((uint8_t*)csThis) - sizeof(Il2CppObject); - } -#endif - if (thisType == il2cpp_defaults.object_class) - { - auto ptrType = (Il2CppClass*) pesapi_get_native_object_typeid(env, jsThis); - if (Class::IsValuetype(ptrType)) - { - csThis = Object::Box(ptrType, csThis); - } + self = ((uint8_t*)self) - sizeof(Il2CppObject); } } +#endif if (isExtensionMethod) { args[0] = pesapi_get_native_object_ptr(env, jsThis); @@ -1520,7 +1524,7 @@ handle_underlying: } } - Il2CppObject* ret = Runtime::InvokeWithThrow(method, csThis, args); //返回ValueType有boxing + Il2CppObject* ret = Runtime::InvokeWithThrow(method, self, args); //返回ValueType有boxing for (int i = csArgStart; i < method->parameters_count; ++i) { @@ -1910,6 +1914,7 @@ static void SetParamArrayFlagAndOptionalNum(puerts::WrapData* data, const char* { data->HasParamArray = false; data->OptionalNum = 0; + data->NeedBoxing = false; const char* p = signature; while(*p) @@ -1918,6 +1923,10 @@ static void SetParamArrayFlagAndOptionalNum(puerts::WrapData* data, const char* { data->HasParamArray = true; } + if (*p == 'T') + { + data->NeedBoxing = true; + } if (*p == 'D') { ++data->OptionalNum; diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/TDataTrans.h.txt b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/TDataTrans.h.txt index 5059579f63..68aaebfa2c 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/TDataTrans.h.txt +++ b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/TDataTrans.h.txt @@ -11,7 +11,7 @@ namespace puerts { typedef void (*FieldWrapFuncPtr)(pesapi_callback_info info, FieldInfo* field, size_t offset, Il2CppClass* fieldType); -typedef bool (*WrapFuncPtr)(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, bool checkArgument, struct WrapData* wrapData); +typedef bool (*WrapFuncPtr)(MethodInfo* method, Il2CppMethodPointer methodPointer, pesapi_callback_info info, pesapi_env env, void* self, bool checkArgument, struct WrapData* wrapData); struct FieldWrapData { @@ -31,6 +31,7 @@ struct WrapData bool IsExtensionMethod; bool HasParamArray; int OptionalNum; + bool NeedBoxing; Il2CppClass* TypeInfos[0]; }; diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/UnityExports4Puerts.h.txt b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/UnityExports4Puerts.h.txt index f2d180bd7d..86897d0f72 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/UnityExports4Puerts.h.txt +++ b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/UnityExports4Puerts.h.txt @@ -21,8 +21,6 @@ struct PObjectRefInfo typedef void* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad); -typedef void (*SetNativePtrFunc)(v8::Object* obj, void* ptr, void* type_id); - typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* objectInfo); typedef const void* (*CSharpTypeToTypeIdFunc)(const void *type); @@ -33,8 +31,6 @@ typedef v8::Value* (*GetModuleExecutorFunc)(v8::Context* env); typedef struct JsClassInfoHeader* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad); -typedef void (*SetNativePtrFunc)(pesapi_value obj, void* ptr, const void* type_id); - typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* delegateInfo); typedef const void* (*CSharpTypeToTypeIdFunc)(Il2CppObject *type); @@ -58,8 +54,6 @@ struct UnityExports CSharpTypeToTypeIdFunc CSharpTypeToTypeId = nullptr; //plugin api - - SetNativePtrFunc SetNativePtr = nullptr; UnrefJsObjectFunc UnrefJsObject = nullptr; GetJsClassInfoFunc GetJsClassInfo = nullptr; diff --git a/unity/cli/UnityExports4Puerts.h b/unity/cli/UnityExports4Puerts.h index 453ff20965..415a8cd1af 100644 --- a/unity/cli/UnityExports4Puerts.h +++ b/unity/cli/UnityExports4Puerts.h @@ -21,8 +21,6 @@ struct PObjectRefInfo typedef void* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad); -typedef void (*SetNativePtrFunc)(v8::Object* obj, void* ptr, void* type_id); - typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* objectInfo); typedef const void* (*CSharpTypeToTypeIdFunc)(const void *type); @@ -33,8 +31,6 @@ typedef v8::Value* (*GetModuleExecutorFunc)(v8::Context* env); typedef struct JsClassInfoHeader* (*GetJsClassInfoFunc)(const void* TypeId, bool TryLazyLoad); -typedef void (*SetNativePtrFunc)(pesapi_value obj, void* ptr, const void* type_id); - typedef void (*UnrefJsObjectFunc)(struct PObjectRefInfo* delegateInfo); typedef const void* (*CSharpTypeToTypeIdFunc)(Il2CppObject *type); @@ -58,8 +54,6 @@ struct UnityExports CSharpTypeToTypeIdFunc CSharpTypeToTypeId = nullptr; //plugin api - - SetNativePtrFunc SetNativePtr = nullptr; UnrefJsObjectFunc UnrefJsObject = nullptr; GetJsClassInfoFunc GetJsClassInfo = nullptr; diff --git a/unity/native_src_il2cpp/Src/Puerts.cpp b/unity/native_src_il2cpp/Src/Puerts.cpp index 2ab07e11e0..00c9dc7d8d 100644 --- a/unity/native_src_il2cpp/Src/Puerts.cpp +++ b/unity/native_src_il2cpp/Src/Puerts.cpp @@ -72,12 +72,6 @@ void PLog(LogLevel Level, const std::string Fmt, ...) } } -static void SetNativePtr(v8::Object* obj, void* ptr, void* type_id) -{ - DataTransfer::SetPointer(obj, ptr, 0); - DataTransfer::SetPointer(obj, type_id, 1); -} - static void* _GetRuntimeObjectFromPersistentObject(v8::Local Context, v8::Local Obj) { auto Isolate = Context->GetIsolate(); @@ -344,7 +338,6 @@ V8_EXPORT pesapi_env_ref GetPesapiEnvHolder(puerts::JSEnv* jsEnv) V8_EXPORT void ExchangeAPI(puerts::UnityExports * exports) { - exports->SetNativePtr = &puerts::SetNativePtr; exports->SetExtraData = &puerts::SetExtraData; exports->UnrefJsObject = &puerts::UnrefJsObject; exports->GetJsClassInfo = &puerts::GetJsClassInfo;