Skip to content

Commit d141fb6

Browse files
authored
Fix generator bug with nested delegate types (#376)
1 parent 772064b commit d141fb6

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

src/NodeApi.Generator/ModuleGenerator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ private void ExportModule(
395395
}
396396

397397
// Generate adapters for exported delegates for later use in method marshalling.
398+
// Delegate types are not exported as properties, only as marshalling adapters.
398399
foreach (ITypeSymbol exportDelegate in exportItems.OfType<ITypeSymbol>()
399400
.Where((t) => t.TypeKind == TypeKind.Delegate))
400401
{
@@ -659,8 +660,10 @@ private void ExportMembers(
659660
{
660661
s += $".AddProperty(\"{field.Name}\", {field.ConstantValue}, {propertyAttributes})";
661662
}
662-
else if (member is INamedTypeSymbol nestedType)
663+
else if (member is INamedTypeSymbol nestedType &&
664+
nestedType.TypeKind != TypeKind.Delegate)
663665
{
666+
// Delegate types are not exported as properties, only as marshalling adapters.
664667
string nestedTypeVariableName = "type_" + GetFullName(nestedType).Replace('.', '_');
665668
s += $".AddProperty(\"{GetExportName(nestedType)}\", {nestedTypeVariableName}, " +
666669
$"{propertyAttributes})";

test/TestCases/napi-dotnet/Delegates.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Microsoft.JavaScript.NodeApi.TestCases;
1515
[JSExport]
1616
public static class Delegates
1717
{
18+
public delegate string NestedDelegate(string value);
19+
1820
public static void CallAction(Action<int> actionDelegate, int value) => actionDelegate(value);
1921

2022
public static int CallFunc(Func<int, int> funcDelegate, int value) => funcDelegate(value);

0 commit comments

Comments
 (0)