Skip to content

Commit aaf9474

Browse files
committed
Allow passing in arrays for params method parameters
1 parent 593751b commit aaf9474

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

Assets/UdonSharp/Editor/UdonSharpExpressionCapture.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -623,25 +623,32 @@ private SymbolDefinition[] GetExpandedInvokeParams(MethodBase targetMethod, Symb
623623
{
624624
int paramCount = invokeParams.Length - i;
625625

626-
SymbolDefinition paramsArraySymbol = visitorContext.topTable.CreateConstSymbol(methodParams[i].ParameterType,
627-
System.Activator.CreateInstance(methodParams[i].ParameterType, new object[] { paramCount }));
628-
629-
for (int j = i; j < invokeParams.Length; ++j)
626+
if (paramCount == 1 && methodParams[i].ParameterType.IsImplicitlyAssignableFrom(invokeParams[i].userCsType))
630627
{
631-
int paramArrayIndex = j - i;
632-
633-
// This can potentially grow unbounded, but we'll hope that the user doesn't go insane with the param count
634-
SymbolDefinition arrayIndexSymbol = visitorContext.topTable.CreateConstSymbol(typeof(int), paramArrayIndex);
628+
newInvokeParams.Add(invokeParams[i]);
629+
}
630+
else
631+
{
632+
SymbolDefinition paramsArraySymbol = visitorContext.topTable.CreateConstSymbol(methodParams[i].ParameterType,
633+
System.Activator.CreateInstance(methodParams[i].ParameterType, new object[] { paramCount }));
635634

636-
using (ExpressionCaptureScope paramArraySetterScope = new ExpressionCaptureScope(visitorContext, null))
635+
for (int j = i; j < invokeParams.Length; ++j)
637636
{
638-
paramArraySetterScope.SetToLocalSymbol(paramsArraySymbol);
639-
paramArraySetterScope.HandleArrayIndexerAccess(arrayIndexSymbol);
640-
paramArraySetterScope.ExecuteSet(invokeParams[j]);
637+
int paramArrayIndex = j - i;
638+
639+
// This can potentially grow unbounded, but we'll hope that the user doesn't go insane with the param count
640+
SymbolDefinition arrayIndexSymbol = visitorContext.topTable.CreateConstSymbol(typeof(int), paramArrayIndex);
641+
642+
using (ExpressionCaptureScope paramArraySetterScope = new ExpressionCaptureScope(visitorContext, null))
643+
{
644+
paramArraySetterScope.SetToLocalSymbol(paramsArraySymbol);
645+
paramArraySetterScope.HandleArrayIndexerAccess(arrayIndexSymbol);
646+
paramArraySetterScope.ExecuteSet(invokeParams[j]);
647+
}
641648
}
642-
}
643649

644-
newInvokeParams.Add(paramsArraySymbol);
650+
newInvokeParams.Add(paramsArraySymbol);
651+
}
645652
break;
646653
}
647654

Assets/UdonSharp/Editor/UdonSharpResolverContext.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,17 @@ public MethodBase FindBestOverloadFunction(MethodBase[] methods, List<System.Typ
609609
}
610610
else if (currentParam.HasParamsParameter()) // Make sure all params args can be assigned to the param type
611611
{
612-
System.Type paramType = currentParam.ParameterType.GetElementType();
613-
614-
for (int j = i; j < methodArgs.Count; ++j)
612+
if (!(currentParam.ParameterType.IsImplicitlyAssignableFrom(methodArgs[i]) && i == methodArgs.Count - 1)) // Handle passing in the actual array type for the params parameter
615613
{
616-
if (!paramType.IsImplicitlyAssignableFrom(methodArgs[j]))
614+
System.Type paramType = currentParam.ParameterType.GetElementType();
615+
616+
for (int j = i; j < methodArgs.Count; ++j)
617617
{
618-
isMethodValid = false;
619-
break;
618+
if (!paramType.IsImplicitlyAssignableFrom(methodArgs[j]))
619+
{
620+
isMethodValid = false;
621+
break;
622+
}
620623
}
621624
}
622625

0 commit comments

Comments
 (0)