Scryer Prolog

Module ffi

:- use_module(library(ffi)).

Foreign Function Interface

This module contains predicates used to call native code (exposed by the C ABI). It uses libffi under the hood. The bridge is very simple and is very unsafe and should be used with care. FFI isn't the only way to communicate with the outside world in Prolog: sockets, pipes and HTTP may be good enough for your use case.

The main predicate is use_foreign_module/2. It takes a library name (which depending on the operating system could be a .so, .dylib or .dll file). and a list of functions. Each function is defined by its name, a list of the type of the arguments, and the return argument.

For each function in the list a predicate of the same name is generated in the ffi module which can then be used to call the native code. The predicates arguments are the input arguments of the foreign function and depending on the return type an extra argument for the return value. Functions with return type void or bool don't have this extra argument. Predicates for functions with return type void always succeed. Predicates for functions with retun type bool succeed iff the return value is 1.


ffi:FUNCTION_NAME(+InputArg1, ..., +InputArgN, -ReturnArg). % for all return types except void and bool
ffi:FUNCTION_NAME(+InputArg1, ..., +InputArgN). % for void and bool

Available types are

Basic C Types

C Types Reference

  • void,

  • char, uchar, schar

  • short, ushort

  • int, uint

  • long, ulong,

  • longlong, ulonglong,

  • float, double

Fixed Width Integer Types

C Fixed With Integer Types Reference

  • sint8/i8, uint8/u8,

  • sint16/i16, uint16/u16,

  • sint32/i32, uint32/u32,

  • sint64/i64, uint64/u64,

Fixed Width Floating-Point Types

C++ Fixed Width Floating-Point Types Reference

  • f32, f64

Other Types

  • cstr,

  • ptr,

  • bool and,

  • custom structs, which can be defined with foreign_struct/2.

Notes regarding bool

  • Not necessarily compatible with the fundamental C type bool.

  • Same as i8 but only values 0 and 1 are valid values.

Notes regarding cstr

  • When using cstr as an argument type the string will be deallocated once the function returns.

  • When using cstr as a return type the string will be copied and won't be deallocated.

Example

For example, let's see how to define a function from the raylib library.


?- use_foreign_module("./libraylib.so", ['InitWindow'([int, int, cstr], void)]).

This creates a 'InitWindow' predicate under the ffi module. Now, we can call it:


?- ffi:'InitWindow'(800, 600, "Scryer Prolog + Raylib").

And a new window should pop up!

foreign_struct(+Name, +Elements).

Defines a new struct type with name Name, composed of the elements Elements, which is a list of other types.

The name of the types doesn't matter, but the order of Elements must match the ones in the native code.

Example:


?- foreign_struct(color, [uint8, uint8, uint8, uint8]).

use_foreign_module(+LibName, +Predicates)

  • LibName the path to the shared library to load/bind

  • Predicates list of function definitions

    Each function definition is a functor of arity 2. The functor name is the name of the function to bind, the first argument is the list of arguments of the function, the second argument is the return type of the function.

    This will define a predicate in the ffi module with the defined name, for void and bool return type functions the arity will match the length of the arguments list, for other return types there will be an additional out parameter.

allocate(+Allocator, +Type, +Args, -Ptr)

Using the Allocator allocate Type initialized with Args and unify Ptr with a pointer to that allocation.

read_ptr(+Type, +Ptr, -Value)

Read a value of Type from the pointer Ptr and unify the read value with Value

For type cstr read a nul-terminated utf-8 string starting at Ptr.

deallocate(+Allocator, +Type, +Ptr)

Deallocate the allocation at Ptr of Type allocated with Allocator

array_type(+ElemType, +Len, -ArrayType)

unify the ffi type for an array of length Len with element type ElemType with ArrayType

with_locals(+Locals, :Goal)

Allocate the Locals, evaluate the Goal and deallocate the Locals. The Locals will also be cleandup when Goal fails or throws an error.

Locals is a list of local variable definitions let(-Ptr, +Type, +Args). Ptr will be unified with the pointer to the local of Type initialized with Args.