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

[LibraryImportGenerator] Basic stateless value marshaller support #71116

Merged
merged 8 commits into from
Jun 22, 2022

Conversation

elinor-fung
Copy link
Member

@elinor-fung elinor-fung commented Jun 22, 2022

  • Separate out previous custom marshalling support into _V1 classes
  • Add basic support for new stateless marshaller shape for values

This only adds really basic support for stateless value marshalling. It doesn't deal with:

  • GetPinnableReference on the marshaller
  • caller-allocated buffer support
  • linear collection marshalling

As I was doing this, I realized a lot of the initial work - splitting out V1, adding new shapes - would probably collide with what @AaronRobinsonMSFT needs when he starts the analyzer side, so I wanted to get just some of the basics out first (also wanted to avoid mixing together so much renaming/reorganization with new support).

Generated example
public static partial void ReverseStrings(ref global::SharedTypes.StringContainer strings)
{
    global::SharedTypes.StringContainerMarshaller.StringContainerNative __strings_native = default;
    try
    {
        // Marshal - Convert managed data to native data.
        __strings_native = global::SharedTypes.StringContainerMarshaller.Ref.ConvertToUnmanaged(strings);
        {
            __PInvoke(&__strings_native);
        }

        // Unmarshal - Convert native data to managed data.
        strings = global::SharedTypes.StringContainerMarshaller.Ref.ConvertToManaged(__strings_native);
    }
    finally
    {
        // Cleanup - Perform required cleanup.
        global::SharedTypes.StringContainerMarshaller.Ref.Free(__strings_native);
    }

    // Local P/Invoke
    [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "stringcontainer_reverse_strings", ExactSpelling = true)]
    static extern unsafe void __PInvoke(global::SharedTypes.StringContainerMarshaller.StringContainerNative* strings);
}

@elinor-fung elinor-fung added area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature labels Jun 22, 2022
@ghost
Copy link

ghost commented Jun 22, 2022

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details
  • Separate out previous custom marshalling support into _V1 classes
  • Add basic support for new stateless marshaller shape for values

This only adds really basic support for stateless value marshalling. It doesn't deal with:

  • GetPinnableReference on the marshaller
  • caller-allocated buffer support
  • linear collection marshalling

As I was doing this, I realized a lot of the initial work - splitting out V1, adding new shapes - would probably collide with what needs when he starts the analyzer side @AaronRobinsonMSFT, so I wanted to get just the basics out first (also wanted to avoid mixing together so much rename/reorganize with new support).

Generated example
public static partial void ReverseStrings(ref global::SharedTypes.StringContainer strings)
{
    global::SharedTypes.StringContainerMarshaller.StringContainerNative __strings_native = default;
    try
    {
        // Marshal - Convert managed data to native data.
        __strings_native = global::SharedTypes.StringContainerMarshaller.Ref.ConvertToUnmanaged(strings);
        {
            __PInvoke(&__strings_native);
        }

        // Unmarshal - Convert native data to managed data.
        strings = global::SharedTypes.StringContainerMarshaller.Ref.ConvertToManaged(__strings_native);
    }
    finally
    {
        // Cleanup - Perform required cleanup.
        global::SharedTypes.StringContainerMarshaller.Ref.Free(__strings_native);
    }

    // Local P/Invoke
    [System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "stringcontainer_reverse_strings", ExactSpelling = true)]
    static extern unsafe void __PInvoke(global::SharedTypes.StringContainerMarshaller.StringContainerNative* strings);
}
Author: elinor-fung
Assignees: -
Labels:

area-System.Runtime.InteropServices, source-generator

Milestone: -

@ghost ghost assigned elinor-fung Jun 22, 2022
Copy link
Member

@jkoritzinsky jkoritzinsky left a comment

Choose a reason for hiding this comment

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

I think it might be nice to split out the CustomTypeMarshalling tests in LibraryImportGenerator.Tests into a class/file for the V1 shapes and a new class/file for the new shapes instead of having them in the same file.

Other than that, LGTM

elinor-fung and others added 3 commits June 22, 2022 12:50
…erop.SourceGeneration/Marshalling/ICustomTypeMarshallingStrategy.cs

Co-authored-by: Aaron Robinson <arobins@microsoft.com>
@elinor-fung
Copy link
Member Author

Failures are #70010 and #70969

@elinor-fung elinor-fung merged commit f4cf69e into dotnet:main Jun 22, 2022
@elinor-fung elinor-fung deleted the basicStateless branch June 22, 2022 23:03
@ghost ghost locked as resolved and limited conversation to collaborators Jul 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants