It appears that calli callsites cannot be decorated with the
“platformapi” calling convention like DllImport signatures can. This is
problematic since Windows uses stdcall by default and most other
platforms use cdecl.
There are three approaches to this issue, without going back to
delegate calls: (a) generate an unmanaged thunk that cleans up the
stack after a GL call; (b) use libFFI; (c) use cdecl *or* stdcall
everywhere and hope that the runtime can cope.
.Net 2.0 can detect and fix stdcall functions invoked through a cdecl
callsite. .Net 4.0 adds a configuration option to enable or disable
this fixup (faster p/invoke if disabled) and raise a MDA exception when
this condition is detected. (This affects x86 only.)
Mono appears to be able to cope with cdecl functions invoked through a
stdcall callsite.
More testing is required.
This includes arrays of primitives and arrays of generics. Our code is
similar to the code generated by the Mono C# compiler for the "fixed"
construct. The .Net compiler produces slightly different code (two local
variables instead of one) - more research is required.
Default results in a managed calling convention which does not generate
unmanaged thunking code for parameter marshaling.
System.Runtime.InteropServices.CallingConvention.Winapi appears to
correspond to StdCall for calli callsites (this might be different for
pinvoke, which supports an unmanaged "platformapi" calling convention.)
Needs more testing to prove this is doing the right thing on non-Windows
platforms.
.Net will happily execute a calli with a generic return type, whereas
Mono will refuse to. Mono is probably doing the right thing here. Fixed
by resolving the generic return into a concrete type.