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

Test cases for validation of the SwiftIndirectResult proposal #102082

Open
Tracked by #93631
kotlarmilos opened this issue May 10, 2024 · 0 comments
Open
Tracked by #93631

Test cases for validation of the SwiftIndirectResult proposal #102082

kotlarmilos opened this issue May 10, 2024 · 0 comments

Comments

@kotlarmilos
Copy link
Member

Description

This issue presents test cases for the validation of the SwiftIndirectResult proposal. This proposal is a dependency for the projection tooling to invoke initializers/methods that return non-frozen structs. In C#, these projections shouldn't return reference, but allocate memory and pass it via SwiftIndirectResult.

Test cases for validation

Below are examples of initializers/methods that return non-frozen structs in C#. When the proposal gets approved, the existing runtime tests should be expanded with such test cases.

Example of a Swift non-frozen struct projected into C# from the proposal:

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Swift;

public class Point
{
    private void* _payload;
    private static readonly int _payloadSize = /* Metadata information from value witness table */;

    public Point(double x, double y)
    {
        _payload = Marshal.AllocHGlobal(_payloadSize).ToPointer();
        try
        {
            PIfunc_init(x, y, new SwiftIndirectResult(_payload));
        }
        catch
        {
            Marshal.FreeHGlobal(new IntPtr(payload));
            throw;
        }
    }

    ~Point()
    {
        if (_payload != null)
        {
            Marshal.FreeHGlobal(_payload);
            _payload = IntPtr.Zero;
        }
    }

    [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
    [DllImport("libSwiftPoint.dylib", EntryPoint = "PIfunc_init")]
    private static extern void PIfunc_init(double x, double y, SwiftIndirectResult payload);
}

Example of a Swift function that returns non-frozen struct projected into C# from the proposal:

public static Point CreateWithMagnitudeAndAngle(double magnitude, double angle)
{
    void* payload = Marshal.AllocHGlobal(_payloadSize).ToPointer();
    try
    {
        PIfunc_createFromMagnitudeAndAngle(magnitude, angle, new SwiftIndirectResult(payload));
        return new Point(payload);
    }
    catch
    {
        Marshal.FreeHGlobal(new IntPtr(payload));
        throw;
    }
}

[UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })]
    [DllImport("libSwiftPoint.dylib")]
    private static extern void PIfunc_createFromMagnitudeAndAngle(double magnitude, double angle, SwiftIndirectResult payload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants