[MLton] Callback to function pointer?

Wesley W. Terpstra wesley@terpstra.ca
Tue, 12 Jul 2005 15:27:01 +0200


On Tue, Jul 12, 2005 at 08:16:54AM -0400, Matthew Fluet wrote:
> > MLton can already call methods by function pointer, but it cannot export
> > methods _to_ function pointer. Would this be difficult? Many C libraries
> > (especially GUIs and event libraries) use callbacks to C function pointers.
> 
> Why do you think that MLton cannot export functions that may be called 
> indirectly?  An "_export"-ed function is a legitimate C function in the 
> object file.  If you want a function pointer to that function, take its 
> address with "&".

I meant something like this:

fun clicker1 () = print "clicked callback 1!\n"
fun clicker2 () = print "clicked callback 2!\n"

val gui_clicked = _import "gui_clicked": MLton.Pointer.t -> unit;
fun setupclick f = gui_clicked 
	(_export *: (unit -> unit) -> MLton.Pointer.t; f)

val () = setupclick clicker1
val () = setupclick clicker2

val () = enterTheGuiMainLoop ()

Where the output should be:
	clicked callback 1!
	clicked callback 2!
when the GUI is clicked, not:
	clicked callback 2!
	clicked callback 2!
... which is what I'd get if I used a name instead of '*'.

Ie: The same '_export' creates two distinct entry points. I believe _export
normally assigns the last given function to the named symbol.  So, you can
change what gets called, but for every _export only one distinct function.

Even if the above behaviour is too hard, being able to get a pointer to an
exported function would be nice. Otherwise, you'll have to have C files
	extern void sml_exported_method ();
	void* getPtr() { return &sml_exported_method; }
when all you really wanted was to pass the pointer into a C function.

-- 
Wesley W. Terpstra