| 1 |
(* main.sml *) |
| 2 |
|
| 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; |
| 6 |
open Array |
| 7 |
|
| 8 |
val size = 10 |
| 9 |
val a = tabulate (size, fn i => real i) |
| 10 |
val ri = ref 0 |
| 11 |
val rc = ref #"0" |
| 12 |
val n = 17 |
| 13 |
|
| 14 |
(* Call the C function *) |
| 15 |
val c = ffi_schema ffi_addr (a, Array.length a, ri, rc, n) |
| 16 |
|
| 17 |
val _ = |
| 18 |
print (if c = #"c" andalso !ri = 45 andalso !rc = c |
| 19 |
then "success\n" |
| 20 |
else "fail\n") |
| 21 |
|
| 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"]) |
| 30 |
|
| 31 |
signature OPAQUE = |
| 32 |
sig |
| 33 |
type t |
| 34 |
val toString : t -> string |
| 35 |
end |
| 36 |
|
| 37 |
structure OpaqueInt :> OPAQUE = |
| 38 |
struct |
| 39 |
type t = Int.int |
| 40 |
val toString = Int.toString |
| 41 |
end |
| 42 |
structure OpaqueWord :> OPAQUE = |
| 43 |
struct |
| 44 |
type t = Word.word |
| 45 |
val toString = Word.toString |
| 46 |
end |
| 47 |
structure OpaqueBool :> OPAQUE = |
| 48 |
struct |
| 49 |
type t = Bool.bool |
| 50 |
val toString = Bool.toString |
| 51 |
end |
| 52 |
structure OpaqueReal :> OPAQUE = |
| 53 |
struct |
| 54 |
type t = Real.real |
| 55 |
val toString = Real.toString |
| 56 |
end |
| 57 |
|
| 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"]) |
| 66 |
|
| 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"]) |