Add support for passing back value via reference to C# scripting event

This commit is contained in:
Wojciech Figat
2021-12-06 16:46:16 +01:00
parent f4b62ce384
commit 23303802ec
8 changed files with 44 additions and 15 deletions

View File

@@ -185,6 +185,7 @@ namespace Flax.Build.Bindings
sb.Append('}');
return sb.ToString();
}
// Special case for value constructors
if (value.Contains('(') && value.Contains(')'))
return "new " + value;
@@ -680,7 +681,7 @@ namespace Flax.Build.Bindings
{
var paramType = eventInfo.Type.GenericArgs[i];
var result = GenerateCSharpNativeToManaged(buildData, paramType, classInfo);
if ((paramType.IsRef && !paramType.IsConst && paramType.IsPod(buildData, classInfo)) || result[result.Length - 1] == '*')
if ((paramType.IsRef && !paramType.IsConst) || result[result.Length - 1] == '*')
useCustomDelegateSignature = true;
CppParamsWrappersCache[i] = result;
}
@@ -695,7 +696,7 @@ namespace Flax.Build.Bindings
var paramType = eventInfo.Type.GenericArgs[i];
if (i != 0)
contents.Append(", ");
if (paramType.IsRef && !paramType.IsConst && paramType.IsPod(buildData, classInfo))
if (paramType.IsRef && !paramType.IsConst)
contents.Append("ref ");
contents.Append(CppParamsWrappersCache[i]).Append(" arg").Append(i);
}
@@ -774,9 +775,9 @@ namespace Flax.Build.Bindings
var paramType = eventInfo.Type.GenericArgs[i];
if (i != 0)
contents.Append(", ");
if (paramType.IsRef && !paramType.IsConst)
contents.Append("ref ");
contents.Append(CppParamsWrappersCache[i]);
if (paramType.IsRef && !paramType.IsConst && paramType.IsPod(buildData, classInfo))
contents.Append("*");
contents.Append(" arg").Append(i);
}
contents.Append(')').AppendLine();
@@ -788,8 +789,8 @@ namespace Flax.Build.Bindings
var paramType = eventInfo.Type.GenericArgs[i];
if (i != 0)
contents.Append(", ");
if (paramType.IsRef && !paramType.IsConst && paramType.IsPod(buildData, classInfo))
contents.Append("ref *");
if (paramType.IsRef && !paramType.IsConst)
contents.Append("ref ");
contents.Append("arg").Append(i);
}
contents.Append(");").AppendLine();

View File

@@ -695,6 +695,14 @@ namespace Flax.Build.Bindings
return "MConverter<" + apiType.Name + ">::Unbox({0})";
}
// Scripting Object
if (functionInfo == null && apiType.IsScriptingObject)
{
// Inside bindings function the managed runtime passes raw unamanged pointer
type = "MonoObject*";
return "(" + typeInfo.Type + "*)ScriptingObject::ToNative({0})";
}
// Nested type (namespace prefix is required)
if (!(apiType.Parent is FileInfo))
{
@@ -1494,6 +1502,28 @@ namespace Flax.Build.Bindings
contents.Append(" mono_runtime_invoke(mmethod->GetNative(), instance, params, &exception);").AppendLine();
contents.Append(" if (exception)").AppendLine();
contents.Append(" DebugLog::LogException(exception);").AppendLine();
for (var i = 0; i < paramsCount; i++)
{
var paramType = eventInfo.Type.GenericArgs[i];
if (paramType.IsRef && !paramType.IsConst)
{
// Convert value back from managed to native (could be modified there)
paramType.IsRef = false;
var managedToNative = GenerateCppWrapperManagedToNative(buildData, paramType, classInfo, out var managedType, null, out var _);
var passAsParamPtr = managedType.EndsWith("*");
var paramValue = $"({managedType}{(passAsParamPtr ? "" : "*")})params[{i}]";
if (!string.IsNullOrEmpty(managedToNative))
{
if (!passAsParamPtr)
paramValue = '*' + paramValue;
paramValue = string.Format(managedToNative, paramValue);
}
else if (!passAsParamPtr)
paramValue = '*' + paramValue;
contents.Append($" arg{i} = {paramValue};").AppendLine();
paramType.IsRef = true;
}
}
contents.Append(" }").AppendLine().AppendLine();
// C# event wrapper binding method (binds/unbinds C# wrapper to C++ delegate)