Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Commit

Permalink
Allow passing in arrays for params method parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinVR committed Mar 31, 2020
1 parent 593751b commit aaf9474
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
35 changes: 21 additions & 14 deletions Assets/UdonSharp/Editor/UdonSharpExpressionCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,25 +623,32 @@ private SymbolDefinition[] GetExpandedInvokeParams(MethodBase targetMethod, Symb
{
int paramCount = invokeParams.Length - i;

SymbolDefinition paramsArraySymbol = visitorContext.topTable.CreateConstSymbol(methodParams[i].ParameterType,
System.Activator.CreateInstance(methodParams[i].ParameterType, new object[] { paramCount }));

for (int j = i; j < invokeParams.Length; ++j)
if (paramCount == 1 && methodParams[i].ParameterType.IsImplicitlyAssignableFrom(invokeParams[i].userCsType))
{
int paramArrayIndex = j - i;

// This can potentially grow unbounded, but we'll hope that the user doesn't go insane with the param count
SymbolDefinition arrayIndexSymbol = visitorContext.topTable.CreateConstSymbol(typeof(int), paramArrayIndex);
newInvokeParams.Add(invokeParams[i]);
}
else
{
SymbolDefinition paramsArraySymbol = visitorContext.topTable.CreateConstSymbol(methodParams[i].ParameterType,
System.Activator.CreateInstance(methodParams[i].ParameterType, new object[] { paramCount }));

using (ExpressionCaptureScope paramArraySetterScope = new ExpressionCaptureScope(visitorContext, null))
for (int j = i; j < invokeParams.Length; ++j)
{
paramArraySetterScope.SetToLocalSymbol(paramsArraySymbol);
paramArraySetterScope.HandleArrayIndexerAccess(arrayIndexSymbol);
paramArraySetterScope.ExecuteSet(invokeParams[j]);
int paramArrayIndex = j - i;

// This can potentially grow unbounded, but we'll hope that the user doesn't go insane with the param count
SymbolDefinition arrayIndexSymbol = visitorContext.topTable.CreateConstSymbol(typeof(int), paramArrayIndex);

using (ExpressionCaptureScope paramArraySetterScope = new ExpressionCaptureScope(visitorContext, null))
{
paramArraySetterScope.SetToLocalSymbol(paramsArraySymbol);
paramArraySetterScope.HandleArrayIndexerAccess(arrayIndexSymbol);
paramArraySetterScope.ExecuteSet(invokeParams[j]);
}
}
}

newInvokeParams.Add(paramsArraySymbol);
newInvokeParams.Add(paramsArraySymbol);
}
break;
}

Expand Down
15 changes: 9 additions & 6 deletions Assets/UdonSharp/Editor/UdonSharpResolverContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,17 @@ public MethodBase FindBestOverloadFunction(MethodBase[] methods, List<System.Typ
}
else if (currentParam.HasParamsParameter()) // Make sure all params args can be assigned to the param type
{
System.Type paramType = currentParam.ParameterType.GetElementType();

for (int j = i; j < methodArgs.Count; ++j)
if (!(currentParam.ParameterType.IsImplicitlyAssignableFrom(methodArgs[i]) && i == methodArgs.Count - 1)) // Handle passing in the actual array type for the params parameter
{
if (!paramType.IsImplicitlyAssignableFrom(methodArgs[j]))
System.Type paramType = currentParam.ParameterType.GetElementType();

for (int j = i; j < methodArgs.Count; ++j)
{
isMethodValid = false;
break;
if (!paramType.IsImplicitlyAssignableFrom(methodArgs[j]))
{
isMethodValid = false;
break;
}
}
}

Expand Down

0 comments on commit aaf9474

Please sign in to comment.