|
11 | 11 | operating system could be a `.so`, `.dylib` or `.dll` file). and a list of functions. Each |
12 | 12 | function is defined by its name, a list of the type of the arguments, and the return argument. |
13 | 13 |
|
14 | | -Types available are: `sint8`/`i8`, `uint8`/`u8`, `sint16`/`i16`, `uint16`/`u16`, `sint32`/`i32`, `uint32`/`u32`, `sint64`/`i64`, |
15 | | -`uint64`/`u64`, `f32`, `f64`, `cstr`, `void`, `bool`, `ptr` and custom structs, which can be defined |
16 | | -with `foreign_struct/2`. |
17 | | -
|
18 | | -After that, each function on the lists maps to a predicate created in the ffi module which |
19 | | -are used to call the native code. |
20 | | -The predicate takes the functor name after the function name. Then, the arguments are the input |
21 | | -arguments followed by a return argument. However, functions with return type `void` or `bool` |
22 | | -don't have that return argument. Predicates with `void` always succeed and `bool` predicates depend |
23 | | -on the return value on the native side. |
| 14 | +For each function in the list a predicate of the same name is generated in the ffi module which |
| 15 | +can then be used to call the native code. |
| 16 | +The predicates arguments are the input arguments of the foreign function and depending on the return type an extra argument for the return value. |
| 17 | +Functions with return type `void` or `bool` don't have this extra argument. |
| 18 | +Predicates for functions with return type `void` always succeed. |
| 19 | +Predicates for functions with retun type `bool` succeed iff the return value is 1. |
24 | 20 |
|
25 | 21 | ``` |
26 | 22 | ffi:FUNCTION_NAME(+InputArg1, ..., +InputArgN, -ReturnArg). % for all return types except void and bool |
27 | 23 | ffi:FUNCTION_NAME(+InputArg1, ..., +InputArgN). % for void and bool |
28 | 24 | ``` |
29 | 25 |
|
30 | | -## Notes regarding cstr |
| 26 | +## Available types are |
| 27 | +
|
| 28 | +### Basic C Types |
| 29 | +
|
| 30 | +[C Types Reference](https://en.cppreference.com/w/c/language/types.html) |
| 31 | +
|
| 32 | +- `void`, |
| 33 | +- `char`, `uchar`, `schar` |
| 34 | +- `short`, `ushort` |
| 35 | +- `int`, `uint` |
| 36 | +- `long`, `ulong`, |
| 37 | +- `longlong`, `ulonglong`, |
| 38 | +- `float`, `double` |
| 39 | +
|
| 40 | +### Fixed Width Integer Types |
| 41 | +
|
| 42 | +[C Fixed With Integer Types Reference](https://en.cppreference.com/w/c/types/integer.html) |
| 43 | +
|
| 44 | +- `sint8`/`i8`, `uint8`/`u8`, |
| 45 | +- `sint16`/`i16`, `uint16`/`u16`, |
| 46 | +- `sint32`/`i32`, `uint32`/`u32`, |
| 47 | +- `sint64`/`i64`, `uint64`/`u64`, |
| 48 | +
|
| 49 | +### Fixed Width Floating-Point Types |
| 50 | +
|
| 51 | +[C++ Fixed Width Floating-Point Types Reference](https://en.cppreference.com/w/cpp/types/floating-point.html) |
| 52 | +
|
| 53 | +- `f32`, `f64` |
| 54 | +
|
| 55 | +### Other Types |
| 56 | +
|
| 57 | +- `cstr`, |
| 58 | +- `ptr`, |
| 59 | +- `bool` and, |
| 60 | +- custom structs, which can be defined with `foreign_struct/2`. |
| 61 | +
|
| 62 | +### Notes regarding bool |
| 63 | +
|
| 64 | +- Not necessarily compatible with the fundamental C type bool. |
| 65 | +- Same as `i8` but only values 0 and 1 are valid values. |
| 66 | +
|
| 67 | +### Notes regarding cstr |
31 | 68 |
|
32 | 69 | - When using `cstr` as an argument type the string will be deallocated once the function returns. |
33 | 70 | - When using `cstr` as a return type the string will be copied and won't be deallocated. |
34 | 71 |
|
35 | | -
|
36 | 72 | ## Example |
37 | 73 |
|
38 | 74 | For example, let's see how to define a function from the [raylib](https://www.raylib.com/) library. |
39 | 75 |
|
40 | 76 | ``` |
41 | | -?- use_foreign_module("./libraylib.so", ['InitWindow'([sint32, sint32, cstr], void)]). |
| 77 | +?- use_foreign_module("./libraylib.so", ['InitWindow'([int, int, cstr], void)]). |
42 | 78 | ``` |
43 | 79 |
|
44 | 80 | This creates a `'InitWindow'` predicate under the ffi module. Now, we can call it: |
|
143 | 179 | % |
144 | 180 | % Read a value of Type from the pointer Ptr and unify the read value with Value |
145 | 181 | % |
146 | | -% For type cstr take read a nul-terminated utf-8 string starting at Ptr. |
| 182 | +% For type cstr read a nul-terminated utf-8 string starting at Ptr. |
147 | 183 | % |
148 | 184 | read_ptr(Type, Ptr, Value) :- |
149 | 185 | must_be(atom, Type), |
|
163 | 199 |
|
164 | 200 | %% array_type(+ElemType, +Len, -ArrayType) |
165 | 201 | % |
166 | | -% unify the ffi type for an array of lenth Len with element type ElemType with ArrayType |
| 202 | +% unify the ffi type for an array of length Len with element type ElemType with ArrayType |
167 | 203 | % |
168 | 204 | array_type(ElemType, Len, ArrayType) :- |
169 | 205 | (Len =< 0 -> domain_error(greater_than_zero, Len, array_type/3); true), |
|
183 | 219 | % Allocate the Locals, evaluate the Goal and deallocate the Locals. |
184 | 220 | % The Locals will also be cleandup when Goal fails or throws an error. |
185 | 221 | % |
186 | | -% Locals is a list of local variable definitions let(-Ptr, +Type, +Args). |
| 222 | +% Locals is a list of local variable definitions `let(-Ptr, +Type, +Args)`. |
187 | 223 | % Ptr will be unified with the pointer to the local of Type initialized with Args. |
188 | 224 | % |
189 | 225 | with_locals(Locals, Goal) :- |
|
0 commit comments