-
Notifications
You must be signed in to change notification settings - Fork 149
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
Adding some more improvements to the PInvokeGenerator #58
Conversation
… rather than a direct call
|
||
public partial struct MyOtherStruct | ||
{{ | ||
public MyStruct c0; public MyStruct c1; public MyStruct c2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This covers the existing generation logic for fixed-sized buffers.
{{ | ||
public unsafe partial struct MyStruct | ||
{{ | ||
public fixed {expectedManagedType} c[3]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the "ideal" generation and is what is used when the type is one of the types C# supports for fixed-sized buffers.
private MyStruct e1; | ||
private MyStruct e2; | ||
|
||
public ref MyStruct this[int index] => ref MemoryMarshal.CreateSpan(ref e0, 3)[index]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the fallback generation (still for unsafe code) for when C# doesn't support the type for "proper" fixed-sized buffers.
It takes advantage of Span
and ref returns
to avoid pinning and allow "normal" use of the API.
{{ | ||
private const string libraryPath = ""ClangSharpPInvokeGenerator""; | ||
|
||
// public static extern {expectedManagedType} MyVariable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned, ideally this would be an IntPtr
or a T*
(depending on if safe or unsafe code) and would use the native library loader APIs (only available in .NET Core 3 and higher) to initialize the handle/address correctly.
I'll likely fix this up in a future PR, but for now, it lets consumers more easily see when these variables exist and take action themselves.
This fixes the PInvokeGeneartor to handle single-file generation properly (namely, it was emitting using directives in the wrong place and overwriting the stream for each processed output builder).
This updates the PInvokeGenerator to emit a comment for variable declarations. Ideally this would be hooked up to the NativeLibraryLoader APIs, but that involves unsafe code and might need to be opt-in.
This also adds some tests for the fixed-sized buffer support and improves the codegen when the user has opted into unsafe code.