From 47e43b0afa1aa22e566f7570f3ec3f0d4425d362 Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 31 Aug 2023 19:22:07 +0800 Subject: [PATCH] =?UTF-8?q?[change]=20=E5=B0=8F=E5=B9=85=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=94=9F=E6=88=90Native2Managed=E5=8F=8AAdjustorThunk=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=B6=88=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/MethodBridge/Generator.cs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Editor/MethodBridge/Generator.cs b/Editor/MethodBridge/Generator.cs index ba5bc66..f0ae4f9 100644 --- a/Editor/MethodBridge/Generator.cs +++ b/Editor/MethodBridge/Generator.cs @@ -555,7 +555,7 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_ return s.ToString(); } - public string GenerateCopyArgumentToInterpreterStack(List paramInfos, bool adjustorThunk) + public string GenerateCopyArgumentToInterpreterStack(List paramInfos) { StringBuilder s = new StringBuilder(); int index = 0; @@ -565,11 +565,11 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_ { if (param.Type.NeedExpandValue()) { - s.AppendLine($"\targs[__ARG_OFFSET_{index}__].u64 = {(index == 0 && adjustorThunk ? $"(uint64_t)((uintptr_t)__arg{index} + sizeof(Il2CppObject))" : $"__arg{index}")};"); + s.AppendLine($"\targs[__ARG_OFFSET_{index}__].u64 = __arg{index};"); } else { - s.AppendLine($"\t*({param.Type.GetTypeName()}*)(args + __ARG_OFFSET_{index}__) = {(index == 0 && adjustorThunk ? $"(uintptr_t)__arg{index} + sizeof(Il2CppObject)" : $"__arg{index}")};"); + s.AppendLine($"\t*({param.Type.GetTypeName()}*)(args + __ARG_OFFSET_{index}__) = __arg{index};"); } } else @@ -581,33 +581,29 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_ return s.ToString(); } - public void GenerateNative2ManagedMethod(MethodDesc method, List lines) + private void GenerateNative2ManagedMethod0(MethodDesc method, bool adjustorThunk, List lines) { string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()}({paramListStr}) +static {method.ReturnInfo.Type.GetTypeName()} __N2M_{(adjustorThunk ? "AdjustorThunk_" : "")}{method.CreateCallSigName()}({paramListStr}) {{ + {(adjustorThunk ? "__arg0 += sizeof(Il2CppObject);" : "")} {GenerateArgumentSizeAndOffset(method.ParamInfos)} StackObject args[__TOTAL_ARG_SIZE__]; -{GenerateCopyArgumentToInterpreterStack(method.ParamInfos, false)} +{GenerateCopyArgumentToInterpreterStack(method.ParamInfos)} {(method.ReturnInfo.IsVoid ? "Interpreter::Execute(method, args, nullptr);" : $"{method.ReturnInfo.Type.GetTypeName()} ret; Interpreter::Execute(method, args, &ret); return ret;")} }} "); } + public void GenerateNative2ManagedMethod(MethodDesc method, List lines) + { + GenerateNative2ManagedMethod0(method, false, lines); + } + public void GenerateAdjustThunkMethod(MethodDesc method, List lines) { - string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); - - lines.Add($@" -static {method.ReturnInfo.Type.GetTypeName()} __N2M_AdjustorThunk_{method.CreateCallSigName()}({paramListStr}) -{{ -{GenerateArgumentSizeAndOffset(method.ParamInfos)} - StackObject args[__TOTAL_ARG_SIZE__]; -{GenerateCopyArgumentToInterpreterStack(method.ParamInfos, true)} - {(method.ReturnInfo.IsVoid ? "Interpreter::Execute(method, args, nullptr);" : $"{method.ReturnInfo.Type.GetTypeName()} ret; Interpreter::Execute(method, args, &ret); return ret;")} -}} -"); + GenerateNative2ManagedMethod0(method, true, lines); } } }