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

Adding some more improvements to the PInvokeGenerator #58

Merged
merged 7 commits into from
May 31, 2019

Conversation

tannergooding
Copy link
Member

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.


public partial struct MyOtherStruct
{{
public MyStruct c0; public MyStruct c1; public MyStruct c2;
Copy link
Member Author

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];
Copy link
Member Author

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];
Copy link
Member Author

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;
Copy link
Member Author

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.

@tannergooding tannergooding merged commit 92d65a1 into dotnet:master May 31, 2019
@tannergooding tannergooding deleted the improvements branch May 31, 2019 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants