[MLton-user] FFI type error

Stephen Weeks sweeks@sweeks.com
Thu, 3 Aug 2006 23:06:03 -0700


> I want to interface SML with a C function that returns arrays. 

You should be aware of the following:

  http://mlton.org/ForeignFunctionInterfaceTypes

  When calling an imported C function from SML that returns an array,
  ref, or vector result or when calling an exported SML function from
  C that takes an array, ref, or string argument, then the object must
  be an ML object allocated on the ML heap. (Although an array, ref,
  or vector object has the natural C representation, the object also
  has an additional header used by the SML runtime system.)

So, your code that is returning a NULL pointer from the C side is not
allowed (but of course MLton doesn't check your C code).

Nevertheless, you have also discovered a compiler bug.

> val foo = _import "cfoo" : int -> int array;
> val _ = List.map foo [3]
> 
> The List.map line causes this error during compilation:
> Type error: bad primapp
> cfoo(word32)
> Type error: analyze raised exception unhandled exception: TypeError
> 
> unhandled exception: TypeError

This is a bug in the optimizer, which gets confused by the result
imported C function being unused.  If you're interested, the bug is in
the "useless" optimization pass:

  http://mlton.org/Useless

I've committed a fix to the repository.  You can rebuild from there,
or if you want to use an old MLton, compile with

  -drop-pass useless

Let us know if you still have problems.