From e2e66e0eec59e8692f95977f2a867ed8f9d67785 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 30 Dec 2022 00:08:32 +0100 Subject: [PATCH] Fix crash when unboxing integer value type from managed callback via generated bindings --- .../Bindings/BindingsGenerator.Cpp.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs index 624f54d39..0fd5ead49 100644 --- a/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs +++ b/Source/Tools/Flax.Build/Bindings/BindingsGenerator.Cpp.cs @@ -1277,7 +1277,8 @@ namespace Flax.Build.Bindings thunkCall += GenerateCppWrapperNativeToBox(buildData, parameterInfo.Type, classInfo, parameterInfo.Name); } - if (functionInfo.ReturnType.IsVoid) + var returnType = functionInfo.ReturnType; + if (returnType.IsVoid) { contents.AppendLine($" typedef void (*Thunk)(void* instance{thunkParams}, MonoObject** exception);"); contents.AppendLine(" const auto thunk = (Thunk)method->GetThunk();"); @@ -1295,18 +1296,30 @@ namespace Flax.Build.Bindings contents.AppendLine(" if (exception)"); contents.AppendLine(" DebugLog::LogException(exception);"); - if (!functionInfo.ReturnType.IsVoid) + if (!returnType.IsVoid) { - if (functionInfo.ReturnType.IsRef) + if (returnType.IsRef) throw new NotSupportedException($"Passing return value by reference is not supported for virtual API methods. Used on method '{functionInfo}'."); - switch (functionInfo.ReturnType.Type) + switch (returnType.Type) { case "bool": - contents.AppendLine($" return __result != 0;"); + contents.AppendLine(" return __result != 0;"); + break; + case "int8": + case "int16": + case "int32": + case "int64": + contents.AppendLine($" return ({returnType.Type})(intptr)__result;"); + break; + case "uint8": + case "uint16": + case "uint32": + case "uint64": + contents.AppendLine($" return ({returnType.Type})(uintptr)__result;"); break; default: - contents.AppendLine($" return MUtils::Unbox<{functionInfo.ReturnType}>(__result);"); + contents.AppendLine($" return MUtils::Unbox<{returnType}>(__result);"); break; } }