___call_fn(fn, args)
The ___call_fn
function enable C code to call any function implemented in Slogan. ___call_fn
take two arguments -
the Slogan function to call (fn
) and its arguments. The arguments are passed as a single list. ___call_fn
will return a ___slogan_obj
which is the return value of the Slogan function.
The following C program use ___call_fn
to invoke a Slogan function passed to it:
// call_fn_test.c
#include <stdio.h>
#include "slogan.h"
___slogan_obj call_slogan_fn(___slogan_obj *args)
{
int error = 0;
___slogan_obj result;
___slogan_obj f = args[0];
___ON_THROW(result = ___call_fn(f, ___pair(___fix(100), ___pair(___fix(200), ___NUL))), error = 1);
if (error == 1)
{
printf("Slogan function raised error\n");
return ___FAL;
}
return result;
}
This can be compiled to a shared library on Linux by,
$ gcc -Wall -shared -fPIC -I ${SLOGAN_HOME}/src/include/-I ${SLOGAN_HOME}/platform/gsc/include \
-o call_fn_test.so call_fn_test.c
and on OS X by,
$ gcc -dynamiclib -Wl,-undefined -Wl,dynamic_lookup -I ${SLOGAN_HOME}/src/include/ \
-I ${SLOGAN_HOME}/platform/gsc/include -o call_fn_test.so call_fn_test.c
After the shared library call_fn_test.so
is generated, the function call_slogan_fn
can
be invoked from Slogan as shown below:
let clib = ffi_open("./call_fn_test.so")
let f = ffi_fn(clib, "call_slogan_fn")
ffi_call_obj(f, #[add])
// 300
ffi_close(clib)