| How would C track references for heap-allocated data originating from (A)RC-based language? I don't think what you say on .NET correlates with reality - you are not supposed to pass managed objects across FFI (you just can't, it won't let you unless you do unsafe cast shenanigans) - the correct way is to either marshal the data or use blittable representation (plain structs can be passed as is, either by reference or value, same goes for arrays/pointers of primitives). On the rare occasion where unmanaged code needs to keep a GC object alive, it is passed via special GCHandle but that's an exception not the rule. Swift has its own high level representation for such objects which are very much not FFI compatible. ARC in Swift is a feature that requires compiler involvement to work correctly, and its structs are not FFI compatible unless they are "trivial". Swift also has its own more advanced Swift Library Evolution ABI, which is strictly not C ABI you expect out of your regular library and has its own calling convention and semantics. Overall, there seem to be strange misconceptions about how these languages work so it would be best to check with the documentation first: .NET P/Invoke (FFI): - https://learn.microsoft.com/en-us/dotnet/standard/native-int... - https://learn.microsoft.com/en-us/dotnet/core/deploying/nati... Swift non-C ABI: - https://github.com/apple/swift/blob/main/docs/LibraryEvoluti... - https://www.swift.org/blog/library-evolution/ Swift ARC: - https://docs.swift.org/swift-book/documentation/the-swift-pr... - https://github.com/apple/swift/blob/main/docs/ARCOptimizatio... (I don't actually know if any other other platform, besides Swift itself, implements Swift ABI support, but .NET is going to have it in .NET 9 - likely the first non-Swift platform to do so) |
I have lost all love for the platform, but I still have to hand it to Microsoft: no FFI has come anywhere close to .Net in the 25-odd years of its existence.
> How would C track references for heap-allocated data originating from (A)RC-based language?
Intentionally designed FF interfaces do not at all. C either delegates allocation to the host language, or the host language needs to let C know when it's done with things. I think Lua is an example of the former (it has been a while), the Vulkano crate is a living example of the latter.