Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//

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);
}
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//

using AdvancedDLSupport.Tests.Data.Interfaces;
using AdvancedDLSupport.Tests.TestBases;

using Xunit;

#pragma warning disable SA1600, CS1591

namespace AdvancedDLSupport.Tests.Integration
{
public class CallingConventionTests : LibraryTestBase<ICallingConventionLibrary>
{
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));
}
}
}
1 change: 1 addition & 0 deletions AdvancedDLSupport.Tests/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ list(
GenericDelegateTests
BooleanMarshallingTests
SpanMarshallingTests
CallingConventionTests
)

list(
Expand Down
12 changes: 12 additions & 0 deletions AdvancedDLSupport.Tests/c/src/CallingConventionTests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdint.h>
#include "comp.h"

__declspec(dllexport) int32_t __stdcall MultiplyBy5_STD(int32_t num)
{
return num * 5;
}

__declspec(dllexport) int32_t MultiplyBy5(int32_t num)
{
return num * 5;
}
11 changes: 0 additions & 11 deletions AdvancedDLSupport.Tests/c/src/CustomLoadingLogicTests.c

This file was deleted.

1 change: 0 additions & 1 deletion AdvancedDLSupport/Attributes/NativeSymbolAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public sealed class NativeSymbolAttribute : Attribute
[PublicAPI]
public NativeSymbolAttribute([NotNull, CallerMemberName] string entrypoint = "")
{
CallingConvention = CallingConvention.Cdecl;
Entrypoint = entrypoint;
}
}
Expand Down
10 changes: 8 additions & 2 deletions AdvancedDLSupport/Reflection/IntrospectiveMemberBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@ public string GetNativeEntrypoint()
/// <inheritdoc />
public CallingConvention GetNativeCallingConvention()
{
var metadataAttribute = GetCustomAttribute<NativeSymbolAttribute>() ??
new NativeSymbolAttribute(Name);
var metadataAttribute = GetCustomAttribute<NativeSymbolAttribute>();

if (metadataAttribute == null || metadataAttribute.CallingConvention == default)
{
NativeSymbolsAttribute attribute = MetadataType.GetCustomAttribute<NativeSymbolsAttribute>();

return attribute == null || attribute.DefaultCallingConvention == default ? CallingConvention.Cdecl : attribute.DefaultCallingConvention;
}

return metadataAttribute.CallingConvention;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//

using System;
using System.Runtime.InteropServices;
using JetBrains.Annotations;

namespace AdvancedDLSupport
Expand All @@ -40,6 +41,12 @@ public class NativeSymbolsAttribute : Attribute
[PublicAPI]
public SymbolTransformationMethod SymbolTransformationMethod { get; set; }

/// <summary>
/// Gets or sets the default calling convention for symbols of the interface.
/// </summary>
[PublicAPI]
public CallingConvention DefaultCallingConvention { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="NativeSymbolsAttribute"/> class.
/// </summary>
Expand Down