3 (* Declare ffi to be implemented by calling the C function ffi
. *)
4 val ffi_addr
= _address
"ffi" public
: MLton
.Pointer
.t
;
5 val ffi_schema
= _import
* : MLton
.Pointer
.t
-> real array
* int * int ref
* char ref
* int -> char
;
9 val a
= tabulate (size
, fn i
=> real i
)
14 (* Call the C function
*)
15 val c
= ffi_schema
ffi_addr (a
, Array
.length a
, ri
, rc
, n
)
18 print (if c
= #
"c" andalso !ri
= 45 andalso !rc
= c
22 val n
= #
1 (_symbol
"FFI_INT" public
: (unit
-> int) * (int -> unit
);) ()
23 val _
= print (concat
[Int.toString n
, "\n"])
24 val w
= #
1 (_symbol
"FFI_WORD" public
: (unit
-> word) * (word -> unit
);) ()
25 val _
= print (concat
[Word.toString w
, "\n"])
26 val b
= #
1 (_symbol
"FFI_BOOL" public
: (unit
-> bool) * (bool -> unit
);) ()
27 val _
= print (concat
[Bool.toString b
, "\n"])
28 val r
= #
1 (_symbol
"FFI_REAL" public
: (unit
-> real) * (real -> unit
);) ()
29 val _
= print (concat
[Real.toString r
, "\n"])
34 val toString
: t
-> string
37 structure OpaqueInt
:> OPAQUE
=
40 val toString
= Int.toString
42 structure OpaqueWord
:> OPAQUE
=
45 val toString
= Word.toString
47 structure OpaqueBool
:> OPAQUE
=
50 val toString
= Bool.toString
52 structure OpaqueReal
:> OPAQUE
=
55 val toString
= Real.toString
58 val (n
, _
) = _symbol
"FFI_INT" public
: (unit
-> OpaqueInt
.t
) * (OpaqueInt
.t
-> unit
);
59 val _
= print (concat
[OpaqueInt
.toString (n ()), "\n"])
60 val (w
, _
) = _symbol
"FFI_WORD" public
: (unit
-> OpaqueWord
.t
) * (OpaqueWord
.t
-> unit
);
61 val _
= print (concat
[OpaqueWord
.toString (w ()), "\n"])
62 val (b
, _
) = _symbol
"FFI_BOOL" public
: (unit
-> OpaqueBool
.t
) * (OpaqueBool
.t
-> unit
);
63 val _
= print (concat
[OpaqueBool
.toString (b ()), "\n"])
64 val (r
, _
) = _symbol
"FFI_REAL" public
: (unit
-> OpaqueReal
.t
) * (OpaqueReal
.t
-> unit
);
65 val _
= print (concat
[OpaqueReal
.toString (r ()), "\n"])
67 val n_addr
= _address
"FFI_INT" public
: MLton
.Pointer
.t
;
68 val n
= MLton
.Pointer
.getInt32 (n_addr
, 0);
69 val _
= print (concat
[Int.toString n
, "\n"])
70 val w_addr
= _address
"FFI_WORD" public
: MLton
.Pointer
.t
;
71 val w
= MLton
.Pointer
.getWord32 (w_addr
, 0);
72 val _
= print (concat
[Word.toString w
, "\n"])
73 val b_addr
= _address
"FFI_BOOL" public
: MLton
.Pointer
.t
;
74 val b
= (MLton
.Pointer
.getInt32 (n_addr
, 0)) <> 0
75 val _
= print (concat
[Bool.toString b
, "\n"])
76 val r_addr
= _address
"FFI_REAL" public
: MLton
.Pointer
.t
;
77 val r
= MLton
.Pointer
.getReal64 (r_addr
, 0)
78 val _
= print (concat
[Real.toString r
, "\n"])