Skip to content

Commit

Permalink
Fix generator bug with nested delegate types (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Sep 6, 2024
1 parent 772064b commit d141fb6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/NodeApi.Generator/ModuleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITypeSymbol>()
.Where((t) => t.TypeKind == TypeKind.Delegate))
{
Expand Down Expand Up @@ -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})";
Expand Down
2 changes: 2 additions & 0 deletions test/TestCases/napi-dotnet/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> actionDelegate, int value) => actionDelegate(value);

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

0 comments on commit d141fb6

Please sign in to comment.