[ocaml-ctypes] Setting completion function variable for Readline

Jeremy Yallop yallop at gmail.com
Wed Sep 25 04:27:23 BST 2013


On 24 September 2013 00:45, Phil Hagelberg <phil at hagelb.org> wrote:
>
> Jeremy Yallop writes:
>
>> Yes: you can use the foreign_value function[0] to retrieve the address
>> of the global, and then set it directly using the assignment operator
>> <- at .
>
> Thanks; that looks like just the trick.

There's one thing I forgot to mention: it's important to ensure that
the function you assign to the global isn't garbage collected while
there's still a possibility that it can be called by the C library.
If you use a global function everything should be fine, and you don't
need to do anything special.  It's also fine to use a non-global
function such as a closure constructed at runtime, but you'll need to
keep a reference to it around in OCaml to prevent collection.

> There's one twist which still has me stumped though. For custom
> completion functions, Readline requires an array be returned which is
> null-terminated. As far as I can tell, it's impossible to represent this
> within the OCaml type system. Is this something I'd have to dip into the
> Obj module to implement? What would that look like?

Is it an array of pointers terminated with a null pointer?  The ctypes
array type can represent that; there's a function in the fts
binding[0] in the distribution that converts an OCaml list to such an
array which might be a useful inspiration or starting point.

https://github.com/ocamllabs/ocaml-ctypes/blob/816b56081d/examples/fts/fts.ml#L234-239


More information about the Ctypes mailing list