chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

chibi-ffi: how to return a list type

Open VincentToups opened this issue 5 years ago • 1 comments

I'm wrapping a function which returns a variable number of heap allocated pointers. The natural thing seems to be to build a list.

Looking at the code generated by chibi-ffi and trying to copy the style, I notice that to wrap a pointer into the appropriately typed chibi value, the function wrapper inspects its own declaration for the return type via something like

sexp_opcode_return_type(self)

This makes sense when returning a single value. Because these type codes aren't generated until the library is loaded, they aren't available at the time when the wrapper is defined, so the wrapper has to get the type from chibi runtime.

However, the return type of this function is pair rather than the type of thing being returned in the list, and thus I can't access the type I need to call sexp_make_cpointer.

My hunch is that I can get the type manually by calling one of the less documented ops, sexp_lookup_type_op. I know what to use as the type name, but where do I find the type id? It seems like the ID isn't generated until library init time either and thus I can't access it in the wrapper definition.

What is the right way to do this?

VincentToups avatar Jan 23 '20 01:01 VincentToups

You probably want the function to allocate and return an array, which Chibi will convert to a list automatically. If you're having trouble convincing the FFI to do what you want, then you can code this manually, but don't worry about the return type. This is used for type inference, but is a simple type system, and just setting the type to SEXP_OBJECT is fine (and is what the FFI would do here).

ashinn avatar Jan 27 '20 14:01 ashinn