MLton 20100608 CallingFromSMLToC
Home  Index  
MLton's ForeignFunctionInterface allows an SML program to import C functions. Suppose you would like to import from C a function with the following prototype:
int foo (double d, char c);
MLton extends the syntax of SML to allow expressions like the following:
_import "foo": real * char -> int;
This expression denotes a function of type real * char -> int whose behavior is implemented by calling the C function whose name is foo. Thinking in terms of C, imagine that there are C variables d of type double, c of type unsigned char, and i of type int. Then, the C statement i = foo (d, c) is executed and i is returned.

The general form of an _import expression is:

_import "C function name" attr... : cFuncTy;
The type and the semicolon are not optional.

The function name is followed by a (possibly empty) sequence of attributes, analogous to C __attribute__ specifiers. For now, the only attributes supported are cdecl and stdcall. These specify the calling convention of the C function on Cygwin/Windows, and are ignored on all other platforms. The default is cdecl. You must use stdcall in order to correctly call Windows API functions.

Example

import.sml imports the C function ffi and the C variable FFI_INT as follows.

(* main.sml *)

(* Declare ffi to be implemented by calling the C function ffi. *)
val ffi = _import "ffi" public: real array * int * int ref * char ref * int -> char;
open Array

val size = 10
val a = tabulate (size, fn i => real i)
val ri = ref 0
val rc = ref #"0"
val n = 17

(* Call the C function *)
val c = ffi (a, Array.length a, ri, rc, n)

(* FFI_INT is declared as public in ffi-import.c *)
val (nGet, nSet) = _symbol "FFI_INT" public: (unit -> int) * (int -> unit);

val _ = print (concat [Int.toString (nGet ()), "\n"])

val _ =
   print (if c = #"c" andalso !ri = 45 andalso !rc = c
             then "success\n"
          else "fail\n")

ffi-import.c is

#include "export.h"

Int32 FFI_INT = 13;
Word32 FFI_WORD = 0xFF;
Bool FFI_BOOL = 1;
Real64 FFI_REAL = 3.14159;

Char8 ffi (Pointer a1, Int32 a1len, Pointer a2, Pointer a3, Int32 n) {
        double *ds = (double*)a1;
        int *pi = (int*)a2;
        char *pc = (char*)a3;
        int i;
        double sum;

        sum = 0.0;
        for (i = 0; i < a1len; ++i) {
                sum += ds[i];
                ds[i] += n;
        }
        *pi = (int)sum;
        *pc = 'c';
        return 'c';
}

Compile and run the program.

% mlton -default-ann 'allowFFI true' import.sml ffi-import.c
% ./import
13
success

Download

Next Steps


Last edited on 2006-11-14 01:25:50 by MatthewFluet.