MLton 20051202 ForeignFunctionInterfaceTypes
Home  Index  
MLton's ForeignFunctionInterface only allows values of certain SML types to be passed between SML and C. The following types are allowed: bool, char, int, real, word. All of the different sizes of (fixed-sized) integers, reals, and words are supported as well: Int8.int, Int16.int, Int32.int, Int64.int, Real32.real, Real64.real, Word8.word, Word16.word, Word32.word, Word64.word. There is a special type, MLton.Pointer.t, for passing C pointers -- see MLtonPointer for details.

Arrays, refs, and vectors of the above types are also allowed. Because in MLton monomorphic arrays and vectors are exactly the same as their polymorphic counterpart, these are also allowed. Hence, string, char vector, and CharVector.vector are also allowed. Strings are not null terminated, unless you manually do so from the SML side.

Unfortunately, passing tuples or datatypes is not allowed because that would interfere with representation optimizations.

The C header file that -export-header generates includes typedefs for the C types corresponding to the SML types. Here is the mapping between SML types and C types.

SML type C typedef C type
array Pointer char *
bool Int32 long
char Int8 char
Int8.int Int8 char
Int16.int Int16 short
Int32.int Int32 long
Int64.int Int64 long long
int Int32 long
MLton.Pointer.t Pointer char *
Real32.real Real32 float
Real64.real Real64 double
real Real64 double
ref Pointer char *
string Pointer char * (read-only)
vector Pointer char * (read-only)
Word8.word Word8 unsigned char
Word16.word Word16 unsigned short
Word32.word Word32 unsigned long
Word64.word Word64 unsigned long long
word Word32 unsigned int

Because MLton assumes that vectors and strings are read-only (and will perform optimizations that, for instance, cause them to share space), you must not modify the data pointed to by the char * in C code.

Although the C type of an array, ref, or vector is always Pointer, in reality, the object has the natural C representation. Your C code should cast to the appropriate C type if you want to keep the C compiler from complaining.

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.)


Last edited on 2005-12-01 04:27:38 by StephenWeeks.