Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] create a closure in external C function?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: qrczak@k...
Subject: Re: [Caml-list] create a closure in external C function?
Tue, 6 Mar 2001 18:01:28 +0100, Xavier Leroy <Xavier.Leroy@inria.fr> pisze:

> As Fabrice noted, the hard part is to deal with arbitrary many C
> function types, determined dynamically. There, you'd need some kind
> of dynamic invocation interface for C functions, and I don't know
> of any C library that provides this in a portable or semi-portable
> fashion.

What about another issue: converting a function closure to a C
function pointer? This too cannot be done portably, can be done
unportably, is needed in the functional programming context, and
I don't know any C library or compiler extension which provides
it (besides gcc's downward closures, but they are not sufficient).
A generic C interface would probably express it as partial application.

Semi-standard Haskell's foreign function interface provides this
(converting a Haskell's function closure to a C function pointer).
There is a function which frees the data assiociated with a function
pointer produced that way.

Last time I looked at this, I hadn't found this functionality in OCaml.
You can at most register OCaml's functions under textual names. This
interface is too limited. You can't even unregister them.

Should OCaml provide this? I haven't really used OCaml much, but
imagine that currently you have to rely on whatever poor man's closure
mechanism was provided by the C library you are interfacing to.
E.g. gtk+ uses the standard C trick of bundling a callback function
pointer with a void * value which is passed to the function as an
additional argument.

Designers of some other libraries were not aware of the fact that
there exist languages which treat functions as data, e.g. readline
wants just function pointers, and it even doesn't let the programmer
know when the function pointer is no longer needed.

The implementation of this conversion must of course build
heap-allocated machine code at runtime, hiding the data pointer
inside it.

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr