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

Suggest: Capable native pointer marshaling on IL2C interoperability. #67

Open
kekyo opened this issue Mar 9, 2019 · 2 comments
Open

Comments

@kekyo
Copy link
Owner

kekyo commented Mar 9, 2019

For example, C# with P/Invoke way:

struct Foo
{
}

struct Bar
{
   // Foo* field1;    // C# and P/Invoke can't handle native pointer on the structure.
   IntPtr field1;      // And formal way is manually marshaling...
}

If IL2C/Invoke can handle directly it, very easier in the situation.
We discussed how to implement this way for first inspirations:

[NativeType(...)]
struct Foo
{
}

[NativeType(...)]
struct Bar
{
   [NativePointer(...)]
   Foo field1;
}

Foo foo = new Foo();
Bar bar = new Bar();
foo.field1 = bar;

In Roslyn, the code fragment understands the bar will copy instance into foo.field1.
But IL2C will wirte the bar variable is the pointer:

// Bar is value type.
Bar bar = ...
foo->field1 = &bar;    // store the bar's pointer (stfld opcode)

We have to think more deep things:

  • Does the pointer have to track for GC?
  • Can IL2C handle automate for marshler?
  • Can IL2C do type interence switch from raw type to pointer type?
    • For example, the field1 is pointer so IL2C has to access with arrow expression (field1->inner1) instead dot-notation (field1.inner1)

@chameleonhead Thanks discussed and suggested at Center CLR Try development meetup No.6.

@chameleonhead
Copy link

I wanted to write code like ...

        [NativeValue("winsock2.h", SymbolName = "WSADESCRIPTION_LEN")]
        static readonly int WSADESCRIPTION_LEN = 256;

        [NativeValue("winsock2.h", SymbolName = "WSASYS_STATUS_LEN")]
        static readonly int WSASYS_STATUS_LEN = 128;

        [NativeType("winsock2.h", SymbolName = "WORD")]
        struct WORD
        {
            public byte bLow;
            public byte bHigh;
        }

        [NativeType("winsock2.h", SymbolName = "WSADATA")]
        struct WSADATA
        {
            WORD wVersion;
            WORD wHighVersion;
            byte[] szDescription;
            byte szSystemStatus;
            ushort iMaxSockets;
            ushort iMaxUdpDg;
            IntPtr lpVendorInfo;
        };

        [NativeMethod("winsock2.h", SymbolName = "socket")]
        static extern WSADATA WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);

But I could not know how to write LPWSADATA (which is WSADATA*).

Thank you for taking up.

@kekyo
Copy link
Owner Author

kekyo commented Mar 9, 2019

Memoized for me: the ref keyword in C#7, can we handle the field pointer type natural expressions?

@kekyo kekyo added the difficult label Mar 9, 2019
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