bottlenoselabs / SDL-cs

Automatically updated C# bindings for SDL v3 https://github.com/libsdl-org/SDL with native dynamic link libraries.
MIT License
16 stars 3 forks source link

Function pointers don't specifcy calling convention #493

Open Susko3 opened 7 months ago

Susko3 commented 7 months ago

The SDL definition notably includes SDLCALL, which is defined as __cdecl.

typedef int (SDLCALL *SDL_EventFilter)(void *userdata, SDL_Event *event);

So this should map to the following C# type (notice the [Cdecl]): ClangSharpPInvokeGenerator correctly generates this definition.

delegate* unmanaged[Cdecl]<void*, SDL_Event*, int>

Current code: https://github.com/bottlenoselabs/SDL-cs/blob/158a06cccd26a36ac37dc001733d40e668d5c46a/src/cs/production/SDL/Generated/SDL.gen.cs#L3778-L3783


Tip: A cdecl callback function would be defined as:

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static int myFilter(void* userdata, SDL_Event* e)
{
    // do work
    return 1;
}
lithiumtoast commented 7 months ago

Yes, this is correct for .NET 5+. Before .NET 5+, it could not be done. I have the type information of the calling convention but didn't update the C# code generator to use said information when adding function pointer support.