caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Kenneth Adam Miller <kennethadammiller@gmail.com>
Cc: caml-list@inria.fr, ctypes@lists.ocaml.org
Subject: Re: [Caml-list] Library recompilation with OCamljava
Date: Mon, 20 Oct 2014 10:44:44 +0100	[thread overview]
Message-ID: <CAAxsn=EO-ZfK03+i2s5DQRfmLv0OK4SbhJyiDVyrvSLb8Zkp6A@mail.gmail.com> (raw)
In-Reply-To: <CAK7rcp8PgQzdu4ZDRGaqutzvYqCdyR0wE_APQTTrmj7iS=kyEw@mail.gmail.com>

[+cc ctypes@lists.ocaml.org]

On Sat, Oct 18, 2014 at 5:09 AM, forum@x9c.fr <forum@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@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.

      parent reply	other threads:[~2014-10-20  9:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-16  8:08 Kenneth Adam Miller
2014-10-17 21:33 ` Kenneth Adam Miller
2014-10-18  9:09   ` forum
2014-10-18 10:04     ` Kenneth Adam Miller
2014-10-18 23:36       ` Kenneth Adam Miller
2014-10-20  9:44       ` Jeremy Yallop [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAxsn=EO-ZfK03+i2s5DQRfmLv0OK4SbhJyiDVyrvSLb8Zkp6A@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=ctypes@lists.ocaml.org \
    --cc=kennethadammiller@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).