[ocaml-ctypes] [Caml-list] Library recompilation with OCamljava
Jeremy Yallop
yallop at gmail.com
Mon Oct 20 10:44:44 BST 2014
[+cc ctypes at lists.ocaml.org]
On Sat, Oct 18, 2014 at 5:09 AM, forum at x9c.fr <forum at x9c.fr> wrote:
>> I think a neat way to use a C library from an ocamljava-compiled
>> program would be to have a Java "backend" for Jeremy Yallop's
>> ctypes (https://github.com/ocamllabs/ocaml-ctypes). I never had
>> the time to implement that, but toyed with this idea and think the
>> best way to implement it would be to go through JNA
>> (https://github.com/twall/jna) rather than JNI. JNA includes a
>> "dlopen"-like mechanism, and automatically maps simple types from
>> Java to C. My knowledge of ctypes is quite limited, but I see no
>> showstopper.
On 18/10/2014, Kenneth Adam Miller <kennethadammiller at gmail.com> wrote:
> Precisely! ocaml-ctypes is exactly what's being used by the library
> that I'm porting to call into C sub libraries. It would be really
> sweet if the ocamljava compiler could detect the ocaml-ctypes and
> generate these mappings automatically. This would eliminate a lot of
> error prone code, since C code tends to interpret data raw at some
> point... How might I got about writing this to boot? I'm moderately
> new to ocaml, lacking deep expertise in it, but I'm an aggressive
> learner. Please explain the best path forward, I want to create a
> robust and reusable solution.
OCaml-Java support is on the wish list for ctypes
https://github.com/ocamllabs/ocaml-ctypes/issues/13
but we don't have the resources to implement it at present. 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.
More information about the Ctypes
mailing list