C base type | cBaseTy | Foreign Function Interface types |
C argument type | cArgTy | cBaseTy1 * ... * cBaseTyn or unit |
C return type | cRetTy | cBaseTy or unit |
C function type | cFuncTy | cArgTy -> cRetTy |
C pointer type | cPtrTy | MLton.Pointer.t |
The type annotation and the semicolon are not optional in the syntax of ForeignFunctionInterface expressions. However, the type is lexed, parsed, and elaborated as an SML type, so any type (including type abbreviations) may be used, so long as it elaborates to a type of the correct form.
Address
_address "C function or variable name" : cPtrTy;
Denotes the address of the C function or variable.
Symbol
_symbol "C variable name" attr... : (unit -> cBaseTy) * (cBaseTy -> unit);
Denotes the getter and setter for a C variable. The cBaseTys must be identical.
attr... denotes a (possibly empty) sequence of attributes.
-
alloc : allocate storage (and export a symbol) for the C variable
_symbol * : cPtrTy -> (unit -> cBaseTy) * (cBaseTy -> unit);
Denotes the getter and setter for a C pointer to a variable. The cBaseTys must be identical.
Import
_import "CFunctionName" attr... : cFuncTy;
Denotes an SML function whose behavior is implemented by calling the C function. See Calling from SML to C for more details.
_import * attr... : cPtrTy -> cFuncTy;
Denotes a SML function whose behavior is implemented by calling a C function through a C function pointer.
attr... denotes a (possibly empty) sequence of attributes.
-
cdecl : call with the cdecl calling convention.
-
stdcall : call with the stdcall calling convention.
See Calling from SML to C function pointer for more details.
Export
_export "CFunctionName" attr... : cFuncTy -> unit;
Exports a C function with the name CFunctionName that can be used to call an SML function of the type cFuncTy. When the function denoted by the export expression is applied to an SML function f, subsequent C calls to CFunctionName will call f. It is an error to call CFunctionName before the export has been applied. The export may be applied more than once, with each application replacing any previous definition of CFunctionName.
attr... denotes a (possibly empty) sequence of attributes.
-
cdecl : call with the cdecl calling convention.
-
stdcall : call with the stdcall calling convention.
See Calling from C to SML for more details.