yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
363 stars 95 forks source link

[WIP] Add `Obj.t typ` for OCaml values #703

Open bobot opened 2 years ago

bobot commented 2 years ago

Allows to have C function with value argument and result. Should fix #643 in a more satisfactory way.

WIP because it remains ugly part in ffi

Duplicate of #569 opened 3 years ago. I hope there are now more interesting use cases for this feature.

CC @disteph @gasche

bobot commented 2 years ago

I think a version with ocaml_any: 'a typ is not needed because a version with:

  val ocaml_value :
     ?format: (Format.formatter -> 'a -> unit) -> string -> 'a typ
  (** Value representing directly the OCaml value. [ocaml_value ?format name]
     the [name] and [format] are only used for debugging *)

is simpler and more regular. First one defines the typ then use it.

If the function is polymorphic it is a little harder and needs Obj.t but it is also less needed:

https://github.com/ocamllabs/ocaml-ctypes/blob/a35e4bf1c89e502b91a41beec3979cacaac531ac/tests/test-passing-ocaml-values/stubs/functions.ml#L46-L48

and outside the functor: https://github.com/ocamllabs/ocaml-ctypes/blob/a35e4bf1c89e502b91a41beec3979cacaac531ac/tests/test-passing-ocaml-values/test_passing_ocaml_values.ml#L80-L82