From 4ddbe0f0adf79bcbf00d8f2d2bf3cb08d301dec8 Mon Sep 17 00:00:00 2001 From: Unknown <17joshuanewcomb@gmail.com> Date: Sun, 30 Jun 2019 20:41:20 -0700 Subject: [PATCH 1/7] Add CallingConvention to NativeSymbolsAttribute --- .../Attributes/NativeSymbolsAttribute.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/AdvancedDLSupport/SymbolTransformation/Attributes/NativeSymbolsAttribute.cs b/AdvancedDLSupport/SymbolTransformation/Attributes/NativeSymbolsAttribute.cs index b6097ed1..962cc630 100644 --- a/AdvancedDLSupport/SymbolTransformation/Attributes/NativeSymbolsAttribute.cs +++ b/AdvancedDLSupport/SymbolTransformation/Attributes/NativeSymbolsAttribute.cs @@ -18,6 +18,7 @@ // using System; +using System.Runtime.InteropServices; using JetBrains.Annotations; namespace AdvancedDLSupport @@ -40,6 +41,12 @@ public class NativeSymbolsAttribute : Attribute [PublicAPI] public SymbolTransformationMethod SymbolTransformationMethod { get; set; } + /// + /// Gets or sets the default calling convention for symbols of the interface. + /// + [PublicAPI] + public CallingConvention DefaultCallingConvention { get; set; } + /// /// Initializes a new instance of the class. /// From 42e2ec0f4d8c592f3236af11c71bfc0875d107b1 Mon Sep 17 00:00:00 2001 From: Unknown <17joshuanewcomb@gmail.com> Date: Sun, 30 Jun 2019 20:42:26 -0700 Subject: [PATCH 2/7] Add Logic Behind interface-wide calling convention --- .../Reflection/IntrospectiveMemberBase.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs index 2c4157bc..a22488d4 100644 --- a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs +++ b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs @@ -141,8 +141,23 @@ public string GetNativeEntrypoint() /// public CallingConvention GetNativeCallingConvention() { - var metadataAttribute = GetCustomAttribute() ?? - new NativeSymbolAttribute(Name); + var metadataAttribute = GetCustomAttribute(); + + if (metadataAttribute == null || metadataAttribute.CallingConvention == default) + { + NativeSymbolsAttribute attribute; + + if (MetadataType.IsInterface) + { + attribute = MetadataType.GetCustomAttribute(); + } + else + { + attribute = MetadataType.GetInterfaces().FirstOrDefault(iface => iface.GetCustomAttribute() != null)?.GetCustomAttribute(); + } + + return attribute == null ? default : attribute.DefaultCallingConvention; + } return metadataAttribute.CallingConvention; } From edcd1ed086fbef2b7a2780c9c5faa844802875db Mon Sep 17 00:00:00 2001 From: Unknown <17joshuanewcomb@gmail.com> Date: Sun, 30 Jun 2019 20:53:40 -0700 Subject: [PATCH 3/7] Faster implementation of internal logic --- .../Reflection/IntrospectiveMemberBase.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs index a22488d4..e9afc191 100644 --- a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs +++ b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs @@ -145,7 +145,7 @@ public CallingConvention GetNativeCallingConvention() if (metadataAttribute == null || metadataAttribute.CallingConvention == default) { - NativeSymbolsAttribute attribute; + NativeSymbolsAttribute attribute = null; if (MetadataType.IsInterface) { @@ -153,7 +153,16 @@ public CallingConvention GetNativeCallingConvention() } else { - attribute = MetadataType.GetInterfaces().FirstOrDefault(iface => iface.GetCustomAttribute() != null)?.GetCustomAttribute(); + // Possibility for improvement in behavior + foreach (var iface in MetadataType.GetInterfaces()) + { + attribute = iface.GetCustomAttribute(); + + if (attribute != null) + { + break; + } + } } return attribute == null ? default : attribute.DefaultCallingConvention; From a0c590ec40a9387e9804dd530ffbe2e4fb06b455 Mon Sep 17 00:00:00 2001 From: Unknown <17joshuanewcomb@gmail.com> Date: Mon, 1 Jul 2019 11:05:26 -0700 Subject: [PATCH 4/7] Simplify internal logic --- .../Reflection/IntrospectiveMemberBase.cs | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs index e9afc191..1f9d2095 100644 --- a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs +++ b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs @@ -145,25 +145,7 @@ public CallingConvention GetNativeCallingConvention() if (metadataAttribute == null || metadataAttribute.CallingConvention == default) { - NativeSymbolsAttribute attribute = null; - - if (MetadataType.IsInterface) - { - attribute = MetadataType.GetCustomAttribute(); - } - else - { - // Possibility for improvement in behavior - foreach (var iface in MetadataType.GetInterfaces()) - { - attribute = iface.GetCustomAttribute(); - - if (attribute != null) - { - break; - } - } - } + NativeSymbolsAttribute attribute = MetadataType.GetCustomAttribute(); return attribute == null ? default : attribute.DefaultCallingConvention; } From 42a00f5ad64de51b42d644fb71748b9a238f7559 Mon Sep 17 00:00:00 2001 From: Unknown <17joshuanewcomb@gmail.com> Date: Mon, 1 Jul 2019 11:22:42 -0700 Subject: [PATCH 5/7] Basic Tests implemented --- .../Interfaces/ICallingConventionLibrary.cs | 38 ++++++++++++++ .../Integration/CallingConventionTests.cs | 50 +++++++++++++++++++ AdvancedDLSupport.Tests/c/CMakeLists.txt | 1 + .../c/src/CallingConventionTests.c | 11 ++++ .../c/src/CustomLoadingLogicTests.c | 11 ---- 5 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 AdvancedDLSupport.Tests/Data/Interfaces/ICallingConventionLibrary.cs create mode 100644 AdvancedDLSupport.Tests/Tests/Integration/CallingConventionTests.cs create mode 100644 AdvancedDLSupport.Tests/c/src/CallingConventionTests.c delete mode 100644 AdvancedDLSupport.Tests/c/src/CustomLoadingLogicTests.c diff --git a/AdvancedDLSupport.Tests/Data/Interfaces/ICallingConventionLibrary.cs b/AdvancedDLSupport.Tests/Data/Interfaces/ICallingConventionLibrary.cs new file mode 100644 index 00000000..195bc0ac --- /dev/null +++ b/AdvancedDLSupport.Tests/Data/Interfaces/ICallingConventionLibrary.cs @@ -0,0 +1,38 @@ +// +// ICallingConventionLibrary.cs +// +// Copyright (c) 2018 Firwood Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// + +using System; +using System.Collections.Generic; +using System.Text; + +using static System.Runtime.InteropServices.CallingConvention; + +#pragma warning disable SA1600, CS1591 + +namespace AdvancedDLSupport.Tests.Data.Interfaces +{ + [NativeSymbols(DefaultCallingConvention = StdCall)] + public interface ICallingConventionLibrary + { + int MultiplyBy5_STD(int num); + + [NativeSymbol(CallingConvention = Cdecl)] + int MultiplyBy5(int num); + } +} diff --git a/AdvancedDLSupport.Tests/Tests/Integration/CallingConventionTests.cs b/AdvancedDLSupport.Tests/Tests/Integration/CallingConventionTests.cs new file mode 100644 index 00000000..c35c4846 --- /dev/null +++ b/AdvancedDLSupport.Tests/Tests/Integration/CallingConventionTests.cs @@ -0,0 +1,50 @@ +// +// CallingConventionTests.cs +// +// Copyright (c) 2018 Firwood Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// + +using AdvancedDLSupport.Tests.Data.Interfaces; +using AdvancedDLSupport.Tests.TestBases; + +using Xunit; + +#pragma warning disable SA1600, CS1591 + +namespace AdvancedDLSupport.Tests.Integration +{ + public class CallingConventionTests : LibraryTestBase + { + private const string LibraryName = "CallingConventionTests"; + + public CallingConventionTests() + : base(LibraryName) + { + } + + [Fact] + public void StdCallIsDefaultInterfaceConvention() + { + Assert.Equal(20, Library.MultiplyBy5_STD(4)); + } + + [Fact] + public void NativeSymbolOverrideWorks() + { + Assert.Equal(20, Library.MultiplyBy5(4)); + } + } +} diff --git a/AdvancedDLSupport.Tests/c/CMakeLists.txt b/AdvancedDLSupport.Tests/c/CMakeLists.txt index 78346d6c..9b96d80e 100644 --- a/AdvancedDLSupport.Tests/c/CMakeLists.txt +++ b/AdvancedDLSupport.Tests/c/CMakeLists.txt @@ -42,6 +42,7 @@ list( GenericDelegateTests BooleanMarshallingTests SpanMarshallingTests + CallingConventionTests ) list( diff --git a/AdvancedDLSupport.Tests/c/src/CallingConventionTests.c b/AdvancedDLSupport.Tests/c/src/CallingConventionTests.c new file mode 100644 index 00000000..c1ec7691 --- /dev/null +++ b/AdvancedDLSupport.Tests/c/src/CallingConventionTests.c @@ -0,0 +1,11 @@ +#include + +__declspec(dllexport) int32_t __stdcall MultiplyBy5_STD(int32_t num) +{ + return num * 5; +} + +__declspec(dllexport) int32_t __cdecl MultiplyBy5(int32_t num) +{ + return num * 5; +} \ No newline at end of file diff --git a/AdvancedDLSupport.Tests/c/src/CustomLoadingLogicTests.c b/AdvancedDLSupport.Tests/c/src/CustomLoadingLogicTests.c deleted file mode 100644 index b62e907e..00000000 --- a/AdvancedDLSupport.Tests/c/src/CustomLoadingLogicTests.c +++ /dev/null @@ -1,11 +0,0 @@ -#include - -__declspec(dllexport) int32_t ReturnsOne() -{ - return 1; -} - -__declspec(dllexport) int32_t ReturnsTwo() -{ - return 2; -} \ No newline at end of file From 17611e87efc17301e039f8f005563c82c94b83e6 Mon Sep 17 00:00:00 2001 From: Unknown <17joshuanewcomb@gmail.com> Date: Tue, 2 Jul 2019 22:41:58 -0700 Subject: [PATCH 6/7] Travis Test Fix --- AdvancedDLSupport.Tests/c/src/CallingConventionTests.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AdvancedDLSupport.Tests/c/src/CallingConventionTests.c b/AdvancedDLSupport.Tests/c/src/CallingConventionTests.c index c1ec7691..9c5dd862 100644 --- a/AdvancedDLSupport.Tests/c/src/CallingConventionTests.c +++ b/AdvancedDLSupport.Tests/c/src/CallingConventionTests.c @@ -1,11 +1,12 @@ #include +#include "comp.h" __declspec(dllexport) int32_t __stdcall MultiplyBy5_STD(int32_t num) { return num * 5; } -__declspec(dllexport) int32_t __cdecl MultiplyBy5(int32_t num) +__declspec(dllexport) int32_t MultiplyBy5(int32_t num) { return num * 5; } \ No newline at end of file From df98ed7120bc7b00402b84da48a675e2f13d297b Mon Sep 17 00:00:00 2001 From: Unknown <17joshuanewcomb@gmail.com> Date: Fri, 12 Jul 2019 15:24:23 -0700 Subject: [PATCH 7/7] Fixed bug where tests would crash on linux --- AdvancedDLSupport/Attributes/NativeSymbolAttribute.cs | 1 - AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/AdvancedDLSupport/Attributes/NativeSymbolAttribute.cs b/AdvancedDLSupport/Attributes/NativeSymbolAttribute.cs index 54739888..24ed3535 100644 --- a/AdvancedDLSupport/Attributes/NativeSymbolAttribute.cs +++ b/AdvancedDLSupport/Attributes/NativeSymbolAttribute.cs @@ -49,7 +49,6 @@ public sealed class NativeSymbolAttribute : Attribute [PublicAPI] public NativeSymbolAttribute([NotNull, CallerMemberName] string entrypoint = "") { - CallingConvention = CallingConvention.Cdecl; Entrypoint = entrypoint; } } diff --git a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs index 1f9d2095..3aff9130 100644 --- a/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs +++ b/AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs @@ -147,7 +147,7 @@ public CallingConvention GetNativeCallingConvention() { NativeSymbolsAttribute attribute = MetadataType.GetCustomAttribute(); - return attribute == null ? default : attribute.DefaultCallingConvention; + return attribute == null || attribute.DefaultCallingConvention == default ? CallingConvention.Cdecl : attribute.DefaultCallingConvention; } return metadataAttribute.CallingConvention;