#Using Zig std for FFI

1 messages · Page 1 of 1 (latest)

winter wagon
#

I want to use functions from Zig std in another lang and call them using FFI. My approach was to just write a Zig shared lib that wraps the functions I want in an export fn and call them the typical way. One example I was interested in was from std.net:
pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) TcpConnectToHostError!Stream
This returns an !std.net.Stream that I'm not sure how to convert to a C type my FFI can handle. Technically I don't even need to interpret the return value since I plan to have any modifications handled by Zig using a different export fn.

Any advice on how to approach this?

white laurel
#

Make your export fn tcpConnectToHost accept a pointer to the resulting Stream and return an error code. If the export fn fails, it does not touch the value behind the pointer and returns the corresponding error code. Otherwise, set the resulting value via the pointer and return some success error code (like 0, for example).

winter wagon
#

How would I create a pointer to a Stream in my other lang? Add another export fn?