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

Support exporting generic types and methods to JS from a .NET module #289

Open
alexeybut opened this issue Apr 25, 2024 · 3 comments
Open
Labels
enhancement New feature or request
Milestone

Comments

@alexeybut
Copy link

Hello,
The next code fails nodejs module generation with an error.
The code:

    public class CollectionBase<T>
    {
        public CollectionBase()
        {
        }
        public void Add(T item) { }
    }

The error:

CSC : error NAPI1001: NotSupportedException : This method is not supported on signature types.   at System.Reflection.Sig natureType.GetConstructors(BindingFlags bindingAttr)   at Microsoft.JavaScript.NodeApi.Generator.SymbolExtensions.AsConst ructorInfo(IMethodSymbol methodSymbol)   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.ToArray()   at Microso ft.JavaScript.NodeApi.Generator.ModuleGenerator.ExportType(SourceBuilder& s, ITypeSymbol type, String exportName)   at Mi crosoft.JavaScript.NodeApi.Generator.ModuleGenerator.ExportModule(SourceBuilder& s, ITypeSymbol moduleType, IEnumerable`1  exportItems)   at Microsoft.JavaScript.NodeApi.Generator.ModuleGenerator.GenerateModuleInitializer(ISymbol moduleInitial izer, IEnumerable`1 exportItems)   at Microsoft.JavaScript.NodeApi.Generator.ModuleGenerator.Execute(GeneratorExecutionCo ntext context) [ConsoleApp1.csproj]                                                                                                                                   

Command:

dotnet clean
dotnet build-server shutdown
dotnet publish ConsoleApp1.csproj -r win-x64 -f net8.0 

Version: 0.7.8
AOT is enabled

@jasongin
Copy link
Member

jasongin commented Apr 26, 2024

Generics are not supported for APIs exported to JavaScript with [JSExport], because it is not possible to generate marshalling code at compile-time when the type of a generic parameter is not known ahead of time. Maybe if there was a way you could give the source-generator a hint about what are all the valid types for the type parameter, then generics could be supported within those constraints. But that will take a lot more work and I'm not even certain about the feasibility.

As long as generics are not supported, the source-generator should at least provide a better error message than the NotSupportedException above. I'll fix that.

Generics are currently supported for the dynamic invocation interop scenario, in which marshalling code is generated at runtime. However AOT does not support runtime code-generation, so it can never use that approach.

The documentation about generics currently isn't very clear about these limitations; I will try to improve that.

@alexeybut
Copy link
Author

@jasongin is it possible to skip unsupported features with a warning? There are hundreds similar issues in real projects. The generator fails and stops on each of them. It's takes too much time to find out and implement a workaround for such issues. JSExport(false) doesn't help to workaround such issues because of complex classes reference tree.

@jasongin
Copy link
Member

I created #298 about reporting a warning. We'll keep this issue to track actually implementing support for exporting generic types and methods to JS from a .NET module.

@jasongin jasongin added the enhancement New feature or request label May 17, 2024
@jasongin jasongin changed the title Add support of generics Support exporting generic types and methods to JS from a .NET module May 17, 2024
@jasongin jasongin added this to the Backlog milestone May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants