-
Notifications
You must be signed in to change notification settings - Fork 153
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
Changes from all commits
30be73e
e39f5ac
cecbd12
911bb30
27602a6
98c258e
7c221b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,78 @@ public async Task ExcludeTest() | |
await ValidateGeneratedBindings(inputContents, expectedOutputContents, excludedNames); | ||
} | ||
|
||
[Theory] | ||
[InlineData("unsigned char", "byte")] | ||
[InlineData("double", "double")] | ||
[InlineData("short", "short")] | ||
[InlineData("int", "int")] | ||
[InlineData("long long", "long")] | ||
[InlineData("char", "sbyte")] | ||
[InlineData("float", "float")] | ||
[InlineData("unsigned short", "ushort")] | ||
[InlineData("unsigned int", "uint")] | ||
[InlineData("unsigned long long", "ulong")] | ||
public async Task FixedSizedBufferNonPrimitiveTest(string nativeType, string expectedManagedType) | ||
{ | ||
var inputContents = $@"struct MyStruct | ||
{{ | ||
{nativeType} value; | ||
}}; | ||
|
||
struct MyOtherStruct | ||
{{ | ||
MyStruct c[3]; | ||
}}; | ||
"; | ||
|
||
var expectedOutputContents = $@"namespace ClangSharp.Test | ||
{{ | ||
public partial struct MyStruct | ||
{{ | ||
public {expectedManagedType} value; | ||
}} | ||
|
||
public partial struct MyOtherStruct | ||
{{ | ||
public MyStruct c0; public MyStruct c1; public MyStruct c2; | ||
}} | ||
}} | ||
"; | ||
|
||
await ValidateGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
|
||
[Theory] | ||
[InlineData("unsigned char", "byte")] | ||
[InlineData("double", "double")] | ||
[InlineData("short", "short")] | ||
[InlineData("int", "int")] | ||
[InlineData("long long", "long")] | ||
[InlineData("char", "sbyte")] | ||
[InlineData("float", "float")] | ||
[InlineData("unsigned short", "ushort")] | ||
[InlineData("unsigned int", "uint")] | ||
[InlineData("unsigned long long", "ulong")] | ||
public async Task FixedSizedBufferPrimitiveTest(string nativeType, string expectedManagedType) | ||
{ | ||
var inputContents = $@"struct MyStruct | ||
{{ | ||
{nativeType} c[3]; | ||
}}; | ||
"; | ||
|
||
var expectedOutputContents = $@"namespace ClangSharp.Test | ||
{{ | ||
public partial struct MyStruct | ||
{{ | ||
public {expectedManagedType} c0; public {expectedManagedType} c1; public {expectedManagedType} c2; | ||
}} | ||
}} | ||
"; | ||
|
||
await ValidateGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
|
||
[Theory] | ||
[InlineData("unsigned char", "byte")] | ||
[InlineData("double", "double")] | ||
|
@@ -218,6 +290,89 @@ public partial struct MyStruct | |
await ValidateGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
|
||
[Theory] | ||
[InlineData("unsigned char", "byte")] | ||
[InlineData("double", "double")] | ||
[InlineData("short", "short")] | ||
[InlineData("int", "int")] | ||
[InlineData("long long", "long")] | ||
[InlineData("char", "sbyte")] | ||
[InlineData("float", "float")] | ||
[InlineData("unsigned short", "ushort")] | ||
[InlineData("unsigned int", "uint")] | ||
[InlineData("unsigned long long", "ulong")] | ||
public async Task UnsafeFixedSizedBufferNonPrimitiveTest(string nativeType, string expectedManagedType) | ||
{ | ||
var inputContents = $@"struct MyStruct | ||
{{ | ||
{nativeType} value; | ||
}}; | ||
|
||
struct MyOtherStruct | ||
{{ | ||
MyStruct c[3]; | ||
}}; | ||
"; | ||
|
||
var expectedOutputContents = $@"using System.Runtime.InteropServices; | ||
|
||
namespace ClangSharp.Test | ||
{{ | ||
public unsafe partial struct MyStruct | ||
{{ | ||
public {expectedManagedType} value; | ||
}} | ||
|
||
public unsafe partial struct MyOtherStruct | ||
{{ | ||
public _c_e__FixedBuffer c; | ||
|
||
public partial struct _c_e__FixedBuffer | ||
{{ | ||
private MyStruct e0; | ||
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 commentThe 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 |
||
}} | ||
}} | ||
}} | ||
"; | ||
|
||
await ValidateUnsafeGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
|
||
[Theory] | ||
[InlineData("unsigned char", "byte")] | ||
[InlineData("double", "double")] | ||
[InlineData("short", "short")] | ||
[InlineData("int", "int")] | ||
[InlineData("long long", "long")] | ||
[InlineData("char", "sbyte")] | ||
[InlineData("float", "float")] | ||
[InlineData("unsigned short", "ushort")] | ||
[InlineData("unsigned int", "uint")] | ||
[InlineData("unsigned long long", "ulong")] | ||
public async Task UnsafeFixedSizedBufferPrimitiveTest(string nativeType, string expectedManagedType) | ||
{ | ||
var inputContents = $@"struct MyStruct | ||
{{ | ||
{nativeType} c[3]; | ||
}}; | ||
"; | ||
|
||
var expectedOutputContents = $@"namespace ClangSharp.Test | ||
{{ | ||
public unsafe partial struct MyStruct | ||
{{ | ||
public fixed {expectedManagedType} c[3]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}} | ||
}} | ||
"; | ||
|
||
await ValidateUnsafeGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
|
||
[Theory] | ||
[InlineData("unsigned char", "byte")] | ||
[InlineData("double", "double")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace ClangSharp.Test | ||
{ | ||
public sealed class VarDeclarationTest : PInvokeGeneratorTest | ||
{ | ||
[Theory] | ||
[InlineData("unsigned char", "byte")] | ||
[InlineData("double", "double")] | ||
[InlineData("short", "short")] | ||
[InlineData("int", "int")] | ||
[InlineData("long long", "long")] | ||
[InlineData("char", "sbyte")] | ||
[InlineData("float", "float")] | ||
[InlineData("unsigned short", "ushort")] | ||
[InlineData("unsigned int", "uint")] | ||
[InlineData("unsigned long long", "ulong")] | ||
public async Task BasicTest(string nativeType, string expectedManagedType) | ||
{ | ||
var inputContents = $@"{nativeType} MyVariable;"; | ||
|
||
var expectedOutputContents = $@"namespace ClangSharp.Test | ||
{{ | ||
public static partial class Methods | ||
{{ | ||
private const string libraryPath = ""ClangSharpPInvokeGenerator""; | ||
|
||
// public static extern {expectedManagedType} MyVariable; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned, ideally this would be an 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. |
||
}} | ||
}} | ||
"; | ||
|
||
await ValidateGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace ClangSharp | ||
{ | ||
internal static class IReadOnlyListExtensions | ||
{ | ||
public static int IndexOf<T>(this IReadOnlyList<T> self, T value) | ||
{ | ||
var comparer = EqualityComparer<T>.Default; | ||
|
||
for (int i = 0; i < self.Count; i++) | ||
{ | ||
if (comparer.Equals(self[i], value)) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
} |
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.