English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
creating a functional value from C
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-05-23 (11:21)
From: dmitry grebeniuk <gds-mlsts@m...>
Subject: creating a functional value from C

  I want to create a functional value from C world,
which can be called from OCaml.  Something to make
this code work:

external create_func_val : int -> (string -> string)
   = "c_create_func_val";
value fv = create_func_val 123;
print_string (fv "abc");
print_string (fv "def");

  Real situation is more complex -- functions
"c_create_func_val" and "fv" both has side effects,
so I can't just rewrite last lines as
"print_string (create_func_val 123 "abc");
 print_string (create_func_val 123 "def");",
moreover, the real address that will be stored in
"fv"-closure becomes known only in function
"c_create_func_val", and it is the address of
C function that follow all gc-related conventions
(takes and returns "value"s, uses CAMLparam and so on).
  As a simplification, "fv" should not hold any ML
values in its environment.

  Is it possible?  If yes, then how to do it?
  Reading OCaml sources hadn't helped me.

 dmitry                          mailto:gds-mlsts@moldavcable.com