| 1 |
= CallingFromCToSML |
| 2 |
|
| 3 |
MLton's <<ForeignFunctionInterface#>> allows programs to _export_ SML |
| 4 |
functions to be called from C. Suppose you would like export from SML |
| 5 |
a function of type `real * char \-> int` as the C function `foo`. |
| 6 |
MLton extends the syntax of SML to allow expressions like the |
| 7 |
following: |
| 8 |
---- |
| 9 |
_export "foo": (real * char -> int) -> unit; |
| 10 |
---- |
| 11 |
The above expression exports a C function named `foo`, with |
| 12 |
prototype |
| 13 |
[source,c] |
| 14 |
---- |
| 15 |
Int32 foo (Real64 x0, Char x1); |
| 16 |
---- |
| 17 |
The `_export` expression denotes a function of type |
| 18 |
`(real * char \-> int) \-> unit` that when called with a function |
| 19 |
`f`, arranges for the exported `foo` function to call `f` |
| 20 |
when `foo` is called. So, for example, the following exports and |
| 21 |
defines `foo`. |
| 22 |
[source,sml] |
| 23 |
---- |
| 24 |
val e = _export "foo": (real * char -> int) -> unit; |
| 25 |
val _ = e (fn (x, c) => 13 + Real.floor x + Char.ord c) |
| 26 |
---- |
| 27 |
|
| 28 |
The general form of an `_export` expression is |
| 29 |
---- |
| 30 |
_export "C function name" attr... : cFuncTy -> unit; |
| 31 |
---- |
| 32 |
The type and the semicolon are not optional. As with `_import`, a |
| 33 |
sequence of attributes may follow the function name. |
| 34 |
|
| 35 |
MLton's `-export-header` option generates a C header file with |
| 36 |
prototypes for all of the functions exported from SML. Include this |
| 37 |
header file in your C files to type check calls to functions exported |
| 38 |
from SML. This header file includes ``typedef``s for the |
| 39 |
<<ForeignFunctionInterfaceTypes#,types that can be passed between SML and C>>. |
| 40 |
|
| 41 |
|
| 42 |
== Example |
| 43 |
|
| 44 |
Suppose that `export.sml` is |
| 45 |
|
| 46 |
[source,sml] |
| 47 |
---- |
| 48 |
include::https://raw.github.com/MLton/mlton/master/doc/examples/ffi/export.sml[indent=0] |
| 49 |
---- |
| 50 |
|
| 51 |
Note that the the `reentrant` attribute is used for `_import`-ing the |
| 52 |
C functions that will call the `_export`-ed SML functions. |
| 53 |
|
| 54 |
Create the header file with `-export-header`. |
| 55 |
---- |
| 56 |
% mlton -default-ann 'allowFFI true' \ |
| 57 |
-export-header export.h \ |
| 58 |
-stop tc \ |
| 59 |
export.sml |
| 60 |
---- |
| 61 |
|
| 62 |
`export.h` now contains the following C prototypes. |
| 63 |
---- |
| 64 |
Int8 f (Int32 x0, Real64 x1, Int8 x2); |
| 65 |
Pointer f2 (Word8 x0); |
| 66 |
void f3 (); |
| 67 |
void f4 (Int32 x0); |
| 68 |
extern Int32 zzz; |
| 69 |
---- |
| 70 |
|
| 71 |
Use `export.h` in a C program, `ffi-export.c`, as follows. |
| 72 |
|
| 73 |
[source,c] |
| 74 |
---- |
| 75 |
include::https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-export.c[indent=0] |
| 76 |
---- |
| 77 |
|
| 78 |
Compile `ffi-export.c` and `export.sml`. |
| 79 |
---- |
| 80 |
% gcc -c ffi-export.c |
| 81 |
% mlton -default-ann 'allowFFI true' \ |
| 82 |
export.sml ffi-export.o |
| 83 |
---- |
| 84 |
|
| 85 |
Finally, run `export`. |
| 86 |
---- |
| 87 |
% ./export |
| 88 |
g starting |
| 89 |
... |
| 90 |
g4 (0) |
| 91 |
success |
| 92 |
---- |
| 93 |
|
| 94 |
|
| 95 |
== Download |
| 96 |
* https://raw.github.com/MLton/mlton/master/doc/examples/ffi/export.sml[`export.sml`] |
| 97 |
* https://raw.github.com/MLton/mlton/master/doc/examples/ffi/ffi-export.c[`ffi-export.c`] |