Skip to content
Draft
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
Expand Up @@ -105,6 +105,16 @@ ExpressionSyntax GetExpressionForParam(TypePositionInfo paramInfo)

public abstract StatementSyntax GenerateUnmarshalStatement(StubIdentifierContext context);
public abstract StatementSyntax GenerateElementCleanupStatement(StubIdentifierContext context);

/// <summary>
/// <code>
/// &lt;numElements&gt; = &lt;GetManagedValuesSource&gt;.Length;
/// </code>
/// </summary>
public StatementSyntax GenerateNumElementsAssignmentFromManagedValuesSource(StubIdentifierContext context)
{
return CollectionSource.GetNumElementsAssignmentFromManagedValuesSource(CollectionSource.TypeInfo, context);
}
}

file static class ElementsMarshallingCollectionSourceExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,9 @@ public IEnumerable<StatementSyntax> GenerateCleanupCallerAllocatedResourcesState
if (countInfo is NoCountInfo && MarshallerHelpers.GetMarshalDirection(TypeInfo, CodeContext) == MarshalDirection.ManagedToUnmanaged)
{
// When marshalling from managed to unmanaged, we may not have count info.
// For now, just set <numElements> to 0.
// See https://github.com/dotnet/runtime/issues/93423 for a tracking issue.

// <numElements> = 0;
yield return ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName(numElementsIdentifier),
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))));
// Use the managed collection's length to determine the number of elements to clean up.
// <numElements> = <GetManagedValuesSource>.Length;
yield return elementsMarshalling.GenerateNumElementsAssignmentFromManagedValuesSource(context);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,13 @@ public static IEnumerable<object[]> CustomCollectionsManagedToUnmanaged(Generato
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyOutParameter<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NativeToManagedOnlyReturnValue<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NonBlittableElementByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NonBlittableElementWithFreeByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NonBlittableElementNativeToManagedOnlyOutParameter };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateless.NonBlittableElementNativeToManagedOnlyReturnValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyOutParameter<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NativeToManagedOnlyReturnValue<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementWithFreeByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter };
yield return new[] { ID(), customCollectionMarshallingCodeSnippetsManagedToUnmanaged.Stateful.NonBlittableElementNativeToManagedOnlyReturnValue };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public struct Native { }
public static Element ConvertToManaged(Native n) => throw null;
}
""";
public const string ElementInWithFree = """
[CustomMarshaller(typeof(Element), MarshalMode.ElementIn, typeof(ElementMarshaller))]
static class ElementMarshaller
{
public struct Native { }
public static Native ConvertToUnmanaged(Element e) => throw null;
public static Element ConvertToManaged(Native n) => throw null;
public static void Free(Native unmanaged) { }
}
""";
public const string ElementOut = """
[CustomMarshaller(typeof(Element), MarshalMode.ElementOut, typeof(ElementMarshaller))]
static class ElementMarshaller
Expand Down Expand Up @@ -252,6 +262,10 @@ public string NestedMarshallerParametersAndModifiers(string elementType) => _pro
+ NonBlittableElement
+ ElementIn;

public string NonBlittableElementWithFreeByValue => ByValue("Element")
+ NonBlittableElement
+ ElementInWithFree;

public string NonBlittableElementNativeToManagedOnlyOutParameter => NativeToManagedOnlyOutParameter("Element")
+ NonBlittableElement
+ ElementOut;
Expand Down Expand Up @@ -508,6 +522,10 @@ public string NativeToManagedFinallyOnlyReturnValue(string elementType) => _snip
+ NonBlittableElement
+ ElementIn;

public string NonBlittableElementWithFreeByValue => ByValue("Element")
+ NonBlittableElement
+ ElementInWithFree;

public string NonBlittableElementNativeToManagedOnlyOutParameter => NativeToManagedOnlyOutParameter("Element")
+ NonBlittableElement
+ ElementOut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ public static IEnumerable<object[]> CustomCollections()
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NativeToManagedFinallyOnlyReturnValue<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NestedMarshallerParametersAndModifiers<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementWithFreeByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementParametersAndModifiers };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedOnlyOutParameter };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateless.NonBlittableElementNativeToManagedFinallyOnlyOutParameter };
Expand Down Expand Up @@ -432,6 +433,7 @@ public static IEnumerable<object[]> CustomCollections()
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NativeToManagedOnlyReturnValue<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NativeToManagedFinallyOnlyReturnValue<int>() };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementWithFreeByValue };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementParametersAndModifiers };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedOnlyOutParameter };
yield return new[] { ID(), customCollectionMarshallingCodeSnippets.Stateful.NonBlittableElementNativeToManagedFinallyOnlyOutParameter };
Expand Down
Loading