В каком высокоуровневом языке еще есть такой FFI, или хотя бы возможность его создания?
http://github.com/Lovesan/virgil
//gcc -std=c99 -shared -o virgil_examples[.dll/.so] virgil_examples.c
#include <stdio.h>
typedef void (*matrix_callback)(float* matrix, int m, int n, void* param);
void print_matrix(float* matrix, int m, int n)
{
for(int i = 0; i<m; ++i)
{
for(int j = 0; j<n; ++j)
printf("%g ", matrix[i*n+j]);
printf("\n");
}
}
void foo (float* matrix, int m, int n, matrix_callback f, void* param)
{
print_matrix(matrix, m, n);
f(matrix, m, n, param);
}
(deftype matrix () '(simple-array single-float (* *)))
(define-foreign-library virgil-examples
(t (:default "virgil_examples")))
(use-foreign-library virgil-examples)
(define-external-function "foo"
(:cdecl virgil-examples)
(void)
(matrix (& (simple-array single-float) :inout))
(m int :aux (array-dimension matrix 0))
(n int :aux (array-dimension matrix 1))
(callback pointer)
(param (& float :in t) :optional void))
(define-callback add-number
void ((data pointer) (m int) (n int) (param (& float :in t)))
(with-value (matrix data `(simple-array single-float (,m ,n)) :inout)
(let ((param (if (voidp param) 0.0 param)))
(dotimes (i m)
(dotimes (j n)
(incf (aref matrix i j) param))))))
(defun main (matrix)
(declare (type matrix matrix))
(format t "~&Matrix:~%~a~%" matrix)
(force-output *standard-output*)
(foo matrix (get-callback 'add-number) 1.0)
(format t "~&After processing:~%~a" matrix))
* (defparameter *matrix* (make-array '(4 4) :element-type 'single-float
:initial-contents '((1.0 2.0 3.0 4.0)
(5.0 6.0 7.0 8.0)
(9.0 10.0 11.0 12.0)
(13.0 14.0 15.0 16.0))))
;;==> *MATRIX*
* (main *matrix*)
;;на stdout ==>
Matrix:
#2A((1.0 2.0 3.0 4.0)
(5.0 6.0 7.0 8.0)
(9.0 10.0 11.0 12.0)
(13.0 14.0 15.0 16.0))
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After processing:
#2A((2.0 3.0 4.0 5.0)
(6.0 7.0 8.0 9.0)
(10.0 11.0 12.0 13.0)
(14.0 15.0 16.0 17.0))