caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Teller <David.Teller@ens-lyon.org>
To: Nathan Mishra Linger <nathan.mishralinger@gmail.com>
Cc: Julien Moutinho <julien.moutinho@gmail.com>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] OCaml/C interface
Date: Sun, 28 Oct 2007 13:36:43 +0100	[thread overview]
Message-ID: <1193575003.6383.19.camel@Blefuscu> (raw)
In-Reply-To: <ab0c618e0710261113j7b5f0f44xef68881842b49546@mail.gmail.com>

I'm wondering how hard it would be to write a generic module for calling
C functions without having to write a specific wrapper. Something along
the lines of (with revised syntax)


type c_type = 
 [ TVoid
 | TChar
 | TInt32
 | TInt64
 | ...
 | TStruct  of list c_type
 | TUnion   of list c_type
 | TPointer of c_type
 | TArray   of array c_type
 | TString
 | TWString
 | ...
 | TReclaimable of c_type
 | TNotReclaimable of c_type
 ];

type c_value =
 [ Void
 | TChar of char
 | TInt32 of int32
 | ...
 ];

type t; (*The type of a C function*)

(** 
 Perform a dynamic link with a C function.
 Evalutes to None in case of dynamic linking error.
 *)
value acquire_function : ~name:string -> ~args:list c_type -> option t; 

(**
 Actually perform the call.
*)
value call : t -> ~args:list c_type -> c_type;

value release_value : c_value -> unit;
value release_function : t -> unit;

Etc.

Come to think about it, we could draw some inspiration from Python's 
CTypes [1]. While it wouldn't be as safe as the current manner of
calling C from OCaml, it would be much more convenient at least for
prototyping. Plus, with a little Camlp4, it would make it possible to
write something like

external "C" my_native_function = "char* stuff(char* a, char* b)" 

That was just a random thought, I have no particular interest in
implementing this, but what do you think about it ?

Cheers,
 David

[1] http://docs.python.org/lib/module-ctypes.html


  reply	other threads:[~2007-10-28 12:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-25 22:17 Nathan Mishra Linger
2007-10-26  9:18 ` [Caml-list] " Julien Moutinho
2007-10-26 18:13   ` Nathan Mishra Linger
2007-10-28 12:36     ` David Teller [this message]
2007-10-28 13:34       ` skaller
2007-10-28 13:48       ` Christopher L Conway
2007-10-28 15:39         ` David Teller

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=1193575003.6383.19.camel@Blefuscu \
    --to=david.teller@ens-lyon.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=julien.moutinho@gmail.com \
    --cc=nathan.mishralinger@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).