Skip to content

Commit

Permalink
Fix module generator bug with interface type arg (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Mar 27, 2024
1 parent 5cce1cc commit 5a0f3d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/NodeApi.Generator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,28 +337,32 @@ static PropertyInfo GetAttributeProperty(Type type, string name)

private static void BuildBaseTypeAndInterfaces(INamedTypeSymbol typeSymbol)
{
if (typeSymbol.BaseType != null)
static void BuildType(INamedTypeSymbol typeSymbol)
{
BuildBaseTypeAndInterfaces(typeSymbol.BaseType);
BuildBaseTypeAndInterfaces(typeSymbol);

string baseTypeFullName = GetTypeSymbolFullName(typeSymbol.BaseType!);
if (SymbolicTypes.TryGetValue(baseTypeFullName, out Type? baseType) &&
baseType is TypeBuilder baseTypeBuilder)
string typeFullName = GetTypeSymbolFullName(typeSymbol);
if (SymbolicTypes.TryGetValue(typeFullName, out Type? type) &&
type is TypeBuilder baseTypeBuilder)
{
baseTypeBuilder.CreateType();
}
}

if (typeSymbol.BaseType != null)
{
BuildType(typeSymbol.BaseType);
}

foreach (INamedTypeSymbol interfaceTypeSymbol in typeSymbol.Interfaces)
{
BuildBaseTypeAndInterfaces(interfaceTypeSymbol);
BuildType(interfaceTypeSymbol);
}

string interfaceTypeFullName = GetTypeSymbolFullName(interfaceTypeSymbol);
if (SymbolicTypes.TryGetValue(interfaceTypeFullName, out Type? interfaceType) &&
interfaceType is TypeBuilder interfaceTypeBuilder)
{
interfaceTypeBuilder.CreateType();
}
foreach (INamedTypeSymbol typeArgSymbol in
typeSymbol.TypeArguments.OfType<INamedTypeSymbol>())
{
BuildType(typeArgSymbol);
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/TestCases/napi-dotnet/ComplexTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,13 @@ public static ClassWithPrivateConstructor CreateInstance(string value)

public string Value { get; }
}

// Ensure module generation handles implementing an interface with a custom type argument.
[JSExport]
public class CollectionOfClassObjects : IEnumerable<ClassObject>
{
public IEnumerator<ClassObject> GetEnumerator() => throw new NotImplementedException();

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
=> throw new NotImplementedException();
}

0 comments on commit 5a0f3d0

Please sign in to comment.