Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generator bug with nested delegate types #376

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading