Skip to content

Test cases for validation of the SwiftIndirectResult proposal #102082

@kotlarmilos

Description

@kotlarmilos

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);

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions