Skip to content

Commit 76f27c7

Browse files
committed
Resolved CustomFunc delegate problems in params registration.
1 parent dfa4229 commit 76f27c7

File tree

6 files changed

+80
-5
lines changed

6 files changed

+80
-5
lines changed

Source/ExcelDna.Integration/Registration/ParamsRegistration.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ static LambdaExpression WrapMethodParams(LambdaExpression functionLambda)
144144
*
145145
*/
146146

147-
int maxArguments = 125; // Constrained by 255 char registration string, take off 3 type chars, use up to 2 chars per param (before we start doing object...) (& also return)
148-
// CONSIDER: Might improve this if we generate the delegate based on the max length...
147+
int maxArguments = NativeAOT.IsActive ? 16 : 125; // Constrained by 255 char registration string, take off 3 type chars, use up to 2 chars per param (before we start doing object...) (& also return)
148+
// CONSIDER: Might improve this if we generate the delegate based on the max length...
149149

150150
var normalParams = functionLambda.Parameters.Take(functionLambda.Parameters.Count() - 1).ToList();
151151
var normalParamCount = normalParams.Count;
@@ -210,13 +210,19 @@ static LambdaExpression WrapMethodParams(LambdaExpression functionLambda)
210210
if (maxArguments == 125)
211211
{
212212
delegateType = typeof(CustomFunc125<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>)
213-
.MakeGenericType(allParamTypes.ToArray());
213+
.MakeGenericType(allParamTypes.ToArray());
214214
}
215-
else // if (maxArguments == 29)
215+
else if (maxArguments == 29)
216216
{
217217
delegateType = typeof(CustomFunc29<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>)
218218
.MakeGenericType(allParamTypes.ToArray());
219219
}
220+
else // if (maxArguments == 16)
221+
{
222+
delegateType = typeof(Func<,,,,,,,,,,,,,,,,>)
223+
.MakeGenericType(allParamTypes.ToArray());
224+
}
225+
220226
return Expression.Lambda(delegateType, blockExpr, allParamExprs);
221227
}
222228
}

Source/ExcelDna.SourceGenerator.NativeAOT/Generator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ public static short Initialize(void* xlAddInExportInfoAddress, void* hModuleXll,
8181
{
8282
functions += $"typeRefs.Add(typeof(Func<object, {Util.GetFullTypeName(p.Type)}>));\r\n";
8383
}
84-
functions += "\r\n";
8584

8685
if (i.Parameters.Length > 0 && i.Parameters.Last().IsParams && i.Parameters.Last().Type is IArrayTypeSymbol arrayType)
8786
{
8887
methods += $"methodRefs.Add(typeof(List<{Util.GetFullTypeName(arrayType.ElementType)}>).GetMethod(\"ToArray\")!);\r\n";
88+
functions += $"typeRefs.Add(typeof(Func<{Util.CreateFunc16Args(i)}>));\r\n";
8989
}
90+
91+
functions += "\r\n";
9092
}
9193
source = source.Replace("[FUNCTIONS]", functions + methods);
9294
}

Source/ExcelDna.SourceGenerator.NativeAOT/Util.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ public static string MethodType(IMethodSymbol method)
3939
$"Func<{(string.IsNullOrWhiteSpace(parameters) ? null : $"{parameters}, ")}{GetFullTypeName(method.ReturnType)}>";
4040
}
4141

42+
public static string CreateFunc16Args(IMethodSymbol method)
43+
{
44+
List<ITypeSymbol?> allParamTypes = method.Parameters.Take(method.Parameters.Length - 1).Select(p => p.Type).Cast<ITypeSymbol?>().ToList();
45+
var toAdd = 16 - allParamTypes.Count;
46+
for (int i = 0; i < toAdd; i++)
47+
{
48+
allParamTypes.Add(null);
49+
}
50+
allParamTypes.Add(method.ReturnType);
51+
52+
return string.Join(",", allParamTypes.Select(i => i == null ? "object" : GetFullTypeName(i)));
53+
}
54+
4255
private static SymbolDisplayFormat FullNameFormat = new SymbolDisplayFormat(typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces);
4356
}
4457
}

Source/Tests/ExcelDna.AddIn.RuntimeTestsAOT/Functions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,38 @@ public static string NativeStringArray2D(string[,] s)
141141

142142
return $"Native StringArray2D VALS: {result}";
143143
}
144+
145+
[ExcelFunction]
146+
public static string NativeParamsFunc1(
147+
[ExcelArgument(Name = "first.Input", Description = "is a useful start")]
148+
object input,
149+
[ExcelArgument(Description = "is another param start")]
150+
string QtherInpEt,
151+
[ExcelArgument(Name = "Value", Description = "gives the Rest")]
152+
params object[] args)
153+
{
154+
return input + "," + QtherInpEt + ", : " + args.Length;
155+
}
156+
157+
[ExcelFunction]
158+
public static string NativeParamsFunc2(
159+
[ExcelArgument(Name = "first.Input", Description = "is a useful start")]
160+
object input,
161+
[ExcelArgument(Name = "second.Input", Description = "is some more stuff")]
162+
string input2,
163+
[ExcelArgument(Description = "is another param ")]
164+
string QtherInpEt,
165+
[ExcelArgument(Name = "Value", Description = "gives the Rest")]
166+
params object[] args)
167+
{
168+
var content = string.Join(",", args.Select(ValueType => ValueType.ToString()));
169+
return input + "," + input2 + "," + QtherInpEt + ", " + $"[{args.Length}: {content}]";
170+
}
171+
172+
[ExcelFunction]
173+
public static string NativeParamsJoinString(string separator, params string[] values)
174+
{
175+
return String.Join(separator, values);
176+
}
144177
}
145178
}

Source/Tests/ExcelDna.RuntimeTests/NativeAOT.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,25 @@ public void StringArray2D()
209209

210210
Assert.Equal("Native StringArray2D VALS: 15 2.36.7 HelloWorld ", functionRange.Value.ToString());
211211
}
212+
213+
[ExcelFact(Workbook = "", AddIn = AddInPath.RuntimeTestsAOT)]
214+
public void Params()
215+
{
216+
{
217+
Range functionRange = ((Worksheet)ExcelDna.Testing.Util.Workbook.Sheets[1]).Range["B1"];
218+
functionRange.Formula = "=NativeParamsFunc1(1,\"2\",4,5)";
219+
Assert.Equal("1,2, : 2", functionRange.Value.ToString());
220+
}
221+
{
222+
Range functionRange = ((Worksheet)ExcelDna.Testing.Util.Workbook.Sheets[1]).Range["B2"];
223+
functionRange.Formula = "=NativeParamsFunc2(\"a\",,\"c\",\"d\",,\"f\")";
224+
Assert.Equal("a,,c, [3: d,ExcelDna.Integration.ExcelMissing,f]", functionRange.Value.ToString());
225+
}
226+
{
227+
Range functionRange = ((Worksheet)ExcelDna.Testing.Util.Workbook.Sheets[1]).Range["B3"];
228+
functionRange.Formula = "=NativeParamsJoinString(\"//\",\"5\",\"4\",\"3\")";
229+
Assert.Equal("5//4//3", functionRange.Value.ToString());
230+
}
231+
}
212232
}
213233
}

Source/Tests/ExcelDna.SourceGenerator.NativeAOT.Tests/Generator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static string NativeParamsJoinString(string separator, params string[] va
3434
typeRefs.Add(typeof(Func<string, string[], string>));
3535
typeRefs.Add(typeof(Func<object, string>));
3636
typeRefs.Add(typeof(Func<object, string[]>));
37+
typeRefs.Add(typeof(Func<string,object,object,object,object,object,object,object,object,object,object,object,object,object,object,object,string>));
3738
3839
List<MethodInfo> methodRefs = new List<MethodInfo>();
3940
methodRefs.Add(typeof(List<string>).GetMethod("ToArray")!);

0 commit comments

Comments
 (0)