yallop / ocaml-ctypes

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

OCaml-Java backend #13

Open yallop opened 11 years ago

yallop commented 11 years ago

An OCaml-Java backend for ctypes would make it possible to write C bindings that work uniformly across OCaml and OCaml-Java.

dsheets commented 10 years ago

From the ctypes mailing list:

Adding support would mostly likely only involve changing ctypes itself, not OCaml-Java, and is likely to involve writing OCaml-Java implementations of the following components:

(1) memory access, i.e. functions for allocating blocks, for reading and writing scalar values to arbitrary addresses, and for viewing C objects as bigarrays: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/memory_stubs.ml https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/bigarray_stubs.ml

(2) functions for printing primitive (scalar) values: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/value_printing_stubs.ml

(3) implementations of signed and unsigned integer types of various sizes: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/signed.ml https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/unsigned.ml

(4) functions for converting between OCaml and C string representations https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/std_view_stubs.ml

plus one of the following approaches for calling functions

(a) a "dynamic" approach, which resolves symbols and constructs call frames at runtime, like the ctypes Foreign module. This involves two components: a dynamic loading interface along the following lines https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes-foreign-base/dl.mli and primitives for dynamically constructing and making calls: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes-foreign-base/ffi_stubs.ml

(b) a "static" approach, which generates code to be compiled by the standard toolchain: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/cstubs/cstubs.mli

Note that most of the links above are to internal Ctypes modules, not to the interface, which I'd expect to remain largely unchanged.

As Xavier suggests, JNA may be a good starting point for some or all of the above. If someone would like to look at adding OCaml-Java support, please feel free to ask questions, either on the GitHub issue tracker, or on the ctypes mailing list.

Jeremy.