From 82fa51772b34165cd0241a0b2667bae8f77790ab Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Fri, 6 Sep 2024 08:43:41 -1000 Subject: [PATCH] Fix generator bug with nested delegate types --- src/NodeApi.Generator/ModuleGenerator.cs | 5 ++++- test/TestCases/napi-dotnet/Delegates.cs | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/NodeApi.Generator/ModuleGenerator.cs b/src/NodeApi.Generator/ModuleGenerator.cs index 1aeb96fb..6671796c 100644 --- a/src/NodeApi.Generator/ModuleGenerator.cs +++ b/src/NodeApi.Generator/ModuleGenerator.cs @@ -395,6 +395,7 @@ private void ExportModule( } // Generate adapters for exported delegates for later use in method marshalling. + // Delegate types are not exported as properties, only as marshalling adapters. foreach (ITypeSymbol exportDelegate in exportItems.OfType() .Where((t) => t.TypeKind == TypeKind.Delegate)) { @@ -659,8 +660,10 @@ private void ExportMembers( { s += $".AddProperty(\"{field.Name}\", {field.ConstantValue}, {propertyAttributes})"; } - else if (member is INamedTypeSymbol nestedType) + else if (member is INamedTypeSymbol nestedType && + nestedType.TypeKind != TypeKind.Delegate) { + // Delegate types are not exported as properties, only as marshalling adapters. string nestedTypeVariableName = "type_" + GetFullName(nestedType).Replace('.', '_'); s += $".AddProperty(\"{GetExportName(nestedType)}\", {nestedTypeVariableName}, " + $"{propertyAttributes})"; diff --git a/test/TestCases/napi-dotnet/Delegates.cs b/test/TestCases/napi-dotnet/Delegates.cs index 87c29ee2..ab838192 100644 --- a/test/TestCases/napi-dotnet/Delegates.cs +++ b/test/TestCases/napi-dotnet/Delegates.cs @@ -15,6 +15,8 @@ namespace Microsoft.JavaScript.NodeApi.TestCases; [JSExport] public static class Delegates { + public delegate string NestedDelegate(string value); + public static void CallAction(Action actionDelegate, int value) => actionDelegate(value); public static int CallFunc(Func funcDelegate, int value) => funcDelegate(value);