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

[Swift language feature] Implement Swift.Array support #2964

285 changes: 285 additions & 0 deletions src/Swift.Runtime/src/Swift/SwiftArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Swift;
using Swift.Runtime;
using Swift.Runtime.InteropServices;

namespace Swift;

/// <summary>
/// Represents a Swift array payload.
///
/// The following diagram illustrates the hierarchy of a Swift array type.
/// The actual implementation may differ:
///
/// struct Array
/// +-----------------------------------------------------------------------+
/// | struct ArrayBuffer |
/// | +--------------------------------------------------------------+ |
/// | | struct BridgeStorage | |
/// | | +------------------------------------------------------+ | |
/// | | | var rawValue: IntPtr | | |
/// | | +------------------------------------------------------+ | |
/// | +--------------------------------------------------------------+ |
/// +-----------------------------------------------------------------------+
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 8)]
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
public struct ArrayBuffer
{
public IntPtr storage;
}

/// <summary>
/// Represents a Swift array.
/// </summary>
/// <typeparam name="Element">The element type contained in the array.</typeparam>
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
public class SwiftArray<Element> : IDisposable, ISwiftObject
jkurdek marked this conversation as resolved.
Show resolved Hide resolved
{
static nuint _payloadSize = SwiftObjectHelper<SwiftArray<Element>>.GetTypeMetadata().Size;
static nuint _elementSize = ElementTypeMetadata.Size;

// Swift array is a value type and doesn't contain an IntPtr payload
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
private ArrayBuffer buffer;
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
bool _disposed = false;
public unsafe void Dispose()
{
if (!_disposed)
{
Arc.Release(*(IntPtr*)buffer.storage);
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
_disposed = true;
GC.SuppressFinalize(this);
}
}

unsafe ~SwiftArray()
{
Arc.Release(*(IntPtr*)buffer.storage);
}
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
public static nuint PayloadSize => _payloadSize;
public ArrayBuffer Payload => buffer;
public static nuint ElementSize => _elementSize;
static TypeMetadata ISwiftObject.GetTypeMetadata()
{
return TypeMetadata.Cache.GetOrAdd(typeof(SwiftArray<Element>), _ => SwiftArrayPInvokes.PInvoke_getMetadata(TypeMetadataRequest.Complete, ElementTypeMetadata));
}

static TypeMetadata ElementTypeMetadata
{
get => TypeMetadata.GetTypeMetadataOrThrow<Element>();
}

static ISwiftObject ISwiftObject.NewFromPayload(SwiftHandle handle)
{
return new SwiftArray<Element>(handle);
}

IntPtr ISwiftObject.MarshalToSwift(IntPtr swiftDest)
{
var metadata = SwiftObjectHelper<SwiftArray<Element>>.GetTypeMetadata();
unsafe
{
fixed (void* _payloadPtr = &buffer)
{
metadata.ValueWitnessTable->InitializeWithCopy((void*)swiftDest, (void*)_payloadPtr, metadata);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General comment about current design of MarshalToSwift - this feels very prone to buffer overruns.
It's a virtual call, so the caller technically doesn't know what the callee will be precisely. But at the same time, the caller MUST now the size of the buffer to allocate for swiftDest. That feels wrong.

What is the scenario where we have a preexisting buffer and need to marshal to it? So far the callers I've seen all need to allocate before calling this method. And they have to get the size right, otherwise we'll get buffer overruns at runtime.

I think the minimum should be that we don't pass a raw pointer, but some representation of pointer + size. Or better yet, this method would be responsible for allocating the buffer and returning it.

I also find it weird that the method takes a destination buffer, but it may choose to return another buffer. What is the memory ownership? We should at least precisely document this in comments, but ideally redesign the API to make it "secure by default".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use MemoryManager<byte> (our own implementation) which would handle the memory ownership. But it's also possible that having a simple struct of our own would be good enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should review the MarshalToSwift design. I would not tackle this in this PR though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely - created #2974 to track that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to #2851

}
}
return swiftDest;
}

/// <summary>
/// Gets the protocol conformance descriptor for the given type.
/// </summary>
/// <typeparam name="TProtocol"></typeparam>
/// <returns></returns>
static ProtocolConformanceDescriptor ISwiftObject.GetProtocolConformanceDescriptor<TProtocol>()
where TProtocol : class
{
return ProtocolConformanceDescriptor.Zero;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will have to be able to obtain the descriptor for array : Collection

}

/// <summary>
/// Constructs a new SwiftArray from the given handle.
/// </summary>
unsafe SwiftArray(SwiftHandle handle)
{
this.buffer = *(ArrayBuffer*)(handle);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just copy the memory? What about arrays of reference types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this constructor as it goes against the expected behavior on Swift side. Even if payload value is copied, it would point to the same instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't be removed, it is used in the ISwiftObject.NewFromPayload. It should perform a shallow copy of the ArrayBuffer struct.

Anyway, it doesn't have the same semantics as in Swift.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, maybe this is a future problem once we start supporting reference types. We might need to call InitializeWithCopy here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this is a future problem once we start supporting reference types

Could you provide an example?

We might need to call InitializeWithCopy here

Added to #2930.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have anything specific in my mind yet. I was think about having an array of reference types. If we just make a shallow copy of the array the ref count of those references will not be updated.

}

/// <summary>
/// Constructs a new empty SwiftArray.
/// </summary>
public SwiftArray()
{
buffer = SwiftArrayPInvokes.Init(ElementTypeMetadata);
}

/// <summary>
/// Gets the number of elements in the array.
/// </summary>
public unsafe int Count
{
get
{
return (int)SwiftArrayPInvokes.Count(buffer, ElementTypeMetadata);
}
}

/// <summary>
/// Appends the given element to the array.
/// </summary>
public unsafe void Append(Element item)
{
IntPtr T0Payload = IntPtr.Zero;
var metadata = SwiftObjectHelper<SwiftArray<Element>>.GetTypeMetadata();
try
{
T0Payload = (IntPtr)NativeMemory.Alloc(ElementSize);
SwiftMarshal.MarshalToSwift(item, (IntPtr)T0Payload);

fixed (void* _payloadPtr = &buffer)
{
SwiftArrayPInvokes.Append(T0Payload, metadata, new SwiftSelf(_payloadPtr));
}
}
finally
{
NativeMemory.Free((void*)T0Payload);
}
}

/// <summary>
/// Inserts the given element at the given index.
/// </summary>
public unsafe void Insert(int index, Element item)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index));
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved

IntPtr T0Payload = IntPtr.Zero;
var metadata = SwiftObjectHelper<SwiftArray<Element>>.GetTypeMetadata();
try
{
T0Payload = (IntPtr)NativeMemory.Alloc(ElementSize);
SwiftMarshal.MarshalToSwift(item, (IntPtr)T0Payload);
fixed (void* _payloadPtr = &buffer)
{
SwiftArrayPInvokes.Insert(new SwiftHandle(T0Payload), (nint)index, metadata, new SwiftSelf(_payloadPtr));
}
}
finally
{
NativeMemory.Free((void*)T0Payload);
}
}

/// <summary>
/// Removes the element at the given index.
/// </summary>
public unsafe void Remove(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index));

IntPtr T0Payload = IntPtr.Zero;
var metadata = SwiftObjectHelper<SwiftArray<Element>>.GetTypeMetadata();
try
{
T0Payload = (IntPtr)NativeMemory.Alloc(ElementSize);
SwiftMarshal.MarshalToSwift(index, (IntPtr)T0Payload);

fixed (void* _payloadPtr = &buffer)
{
SwiftArrayPInvokes.Remove(new SwiftIndirectResult((void*)T0Payload), (nint)index, metadata, new SwiftSelf(_payloadPtr));
}
}
finally
{
NativeMemory.Free((void*)T0Payload);
}
}

/// <summary>
/// Removes all elements from the array.
/// </summary>
public unsafe void RemoveAll()
{
var metadata = SwiftObjectHelper<SwiftArray<Element>>.GetTypeMetadata();

fixed (void* _payloadPtr = &buffer)
{
SwiftArrayPInvokes.RemoveAll(1, metadata, new SwiftSelf(_payloadPtr));
}
}

/// <summary>
/// Gets or sets the element at the given index.
/// </summary>
public unsafe Element this[int index]
{
get
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();

IntPtr T0Payload = (IntPtr)NativeMemory.Alloc(ElementSize);
SwiftArrayPInvokes.Get(new SwiftIndirectResult((void*)T0Payload), (nint)index, buffer, ElementTypeMetadata);
return SwiftMarshal.MarshalFromSwift<Element>(new SwiftHandle((IntPtr)T0Payload));
}
set
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();

var metadata = SwiftObjectHelper<SwiftArray<Element>>.GetTypeMetadata();
IntPtr T0Payload = (IntPtr)NativeMemory.Alloc(ElementSize);
SwiftMarshal.MarshalToSwift(value, T0Payload);

fixed (void* _payloadPtr = &buffer)
{
SwiftArrayPInvokes.Set(new SwiftHandle(T0Payload), (nint)index, metadata, new SwiftSelf(_payloadPtr));
}
}
}
}

internal static class SwiftArrayPInvokes
{
[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSaMa")]
public static extern TypeMetadata PInvoke_getMetadata(TypeMetadataRequest request, TypeMetadata typeMetadata);

[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sS2ayxGycfC")]
public static extern ArrayBuffer Init(TypeMetadata typeMetadata);

[UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSayxSicig")]
public static unsafe extern void Get(SwiftIndirectResult result, nint index, ArrayBuffer handle, TypeMetadata elementMetadata);

[UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSayxSicis")]
public static unsafe extern void Set(SwiftHandle value, nint index, TypeMetadata elementMetadata, SwiftSelf self);

[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSa5countSivg")]
public static extern nint Count(ArrayBuffer handle, TypeMetadata elementMetadata);

[UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSa6appendyyxnF")]
public static unsafe extern void Append(IntPtr value, TypeMetadata metadata, SwiftSelf self);

[UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSa9removeAll15keepingCapacityySb_tF")]
public static unsafe extern void RemoveAll(byte keepCapacity, TypeMetadata metadata, SwiftSelf self);

[UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSa6remove2atxSi_tF")]
public static unsafe extern void Remove(SwiftIndirectResult result, nint index, TypeMetadata metadata, SwiftSelf self);

[UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
[DllImport(KnownLibraries.SwiftCore, EntryPoint = "$sSa6insert_2atyxn_SitF")]
public static unsafe extern void Insert(SwiftHandle value, nint index, TypeMetadata metadata, SwiftSelf self);
}
Loading
Loading