[MLton-devel] RE: [MLton-user] new release of MLton

Stephen Weeks MLton@mlton.org
Mon, 14 Jul 2003 08:46:03 -0700


Hi Mike.

> Unfortunately I must first apologise for forgetting to mention a fortnight
> ago that you didn't include libgmp.a in the Cygwin experimental release,
> which problem seems to have continued.  (That library seems not to be a part
> of Cygwin.)

Yes, that was a bug in our packaging process.  It has been fixed.
Thanks.

> Second, I played around with a basic binding to some simple OpenGL functions
> back then and came up against the problem that although I can now call SML
> functions from C, I couldn't think of a way of actually passing those
> callback functions as SML side function arguments to functions which in turn
> call _ffi C functions to set callbacks, for example, setting a redraw
> callback on the C side, but doing it entirely from the SML side.

Ken Larsen described to us how they handle callbacks in mGTK
(http://mgtk.sf.net).  Here's what he said.

  Gtk+ is a signal (or event) based toolkit.  This means that you
  connect a callback function to an event from the GUI (or other
  events).

  To handle the callback we have a hash-table with all callback
  functions (implemented in pure SML).  And we have two functions that
  we need to be able to call from C: one to dispatch the call to the
  right callback function and one to remove the callback functions
  from the table when a widget is disposed.

  That is, in C all signals are connected to to the same C function
  (mgtk_callback_dispatch), then mgtk_callback_dispatch call a
  specific pre-registered SML function, dispatch, and finally dispatch
  fetches the right function from the hash-table and call it with the
  provided arguments.

  Thus, we need to know how:
    * to register a SML function so that we can call it from C
    * to call this registered function

Would a similar approach for you?  I could imagine having different
hash tables and dispatchers for different types of callbacks.

> Third, I thought I might try porting the Moscow ML dynlib library to MLton.
> This ran into a problem on Cygwin as shown by the attached archive.  Running
> "make" results in a build with no errors, but also no object file generated
> from Dynlib.sml.  Likewise "mlton dynlib.cm" produces no object file for the
> sml source.  Furthermore I can't put "dynlib_c.c" into "dynlib.cm":
> 
> $ mlton dynlib.cm
> dynlib.cm: MLton can't process Dynlib_c.c
> 
> despite the fact that MLton can do precisely that via the Makefile.

These are invalid calls to mlton.

	$(mlton) -stop o Dynlib_c.c -output Dynlib_c.o
	$(mlton) -stop o Dynlib.sml -output Dynlib.o

The flags (except for linker flags) must occur before the file to be
compiled, as in

	$(mlton) -stop o -output Dynlib_c.o Dynlib_c.c
	$(mlton) -stop o -output Dynlib.o Dynlib.sml

Also, the call to mlton on Dynlib.sml will fail because that is not
the entire (SML) program.  When I run make with your sources, I get
the error:

	Dynlib.sml:1.21-1.27 Error: unbound signature Dynlib
	compilation aborted: elaborate reported errors

I am surprised that when you run make, you get no errors.

You are correct that you cannot put .c files into .cm files.  But
there is no need, since you can link with .o files on the command
line.  So, if you change dynlib.cm to

	Group is
	Dynlib.sig
	Dynlib.sml

and change your Makefile to

	mlton = mlton

	.PHONY: all
	all: dynlib
	
	Dynlib_c.o: Dynlib_c.c
		$(mlton) -stop o Dynlib_c.c
	
	dynlib:  Dynlib.sig Dynlib.sml Dynlib_c.o
		$(mlton) dynlib.cm Dynlib_c.o -ldl
	
	clean:
		rm *.o *.exe
	
then things almost work.  At that point, if I run make, I get linker
errors.

% make
mlton -stop o Dynlib_c.c
mlton dynlib.cm Dynlib_c.o -ldl
Dynlib_c.o: In function `dynlib_dlopen':
Dynlib_c.o(.text+0x9): undefined reference to `Long_val'
Dynlib_c.o(.text+0x2c): undefined reference to `String_val'
Dynlib_c.o(.text+0x4d): undefined reference to `failwith'
Dynlib_c.o: In function `dynlib_dlsym':
Dynlib_c.o(.text+0x8d): undefined reference to `String_val'
Dynlib_c.o(.text+0xae): undefined reference to `failwith'
collect2: ld returned 1 exit status
call to system failed: gcc -o dynlib /tmp/filehkZ2lG.o /tmp/filebkzgRi.o Dynlib_c.o -ldl -L/usr/lib/mlton/self -lmlton -lgdtoa -lm /usr/lib/libgmp.a
make: *** [dynlib] Error 1



-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps1
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel