| I currently ran in the need for an Arc FFI. I started to develop something useful for me. How do you feel the following ? Imagine you've got a matrix.so offering the following C functions : typedef double * * matrix;
matrix zeros (int lines, int cols);
double get (matrix m, int line, int col);
void set (matrix m, int line, int col, double value);
matrix multiply (matrix a, matrix b);
Now, you want to use it in Arc, this way : (= m (zeros 3 3))
(= n (mul m m))
(set n 1 1 (+ (get n 1 1) 1.0))
(prn:get n 1 1)
-> 1.0
To do so, what about the following declaration before the Arc code above : (loadffi "matrix"
(ctype mat (cptr (cptr cdouble)))
(cdef set "set" void (mat cint cint cdouble))
(cdef get "get" cdouble (mat cint cint))
(cdef zeros "zeros" mat (cint cint))
(cdef mul "multiply" mat (mat mat)))
The macro loadffi loads the "first-arg" file (appending .so or .dll depending on the OS), then lets the user describe types and functions he wants to "import" : cdef lets him define the Arc name, the foreign name, the return type (or void) and a list of arg-types. A few primitive C types are imported (int, short, long, float, double, char), plus the possibility to do typedefs, to express pointers and structs.All of this is then dealt by mzscheme's FFI, and the rest can be defined directly within Arc. |