;;;; ;;;; ffi.stk -- FFI support ;;;; ;;;; Copyright © 2007-2011 Erick Gallesio - I3S-CNRS/ESSI ;;;; ;;;; ;;;; This program is free software; you can redistribute it and/or modify ;;;; it under the terms of the GNU General Public License as published by ;;;; the Free Software Foundation; either version 2 of the License, or ;;;; (at your option) any later version. ;;;; ;;;; This program is distributed in the hope that it will be useful, ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;;; GNU General Public License for more details. ;;;; ;;;; You should have received a copy of the GNU General Public License ;;;; along with this program; if not, write to the Free Software ;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, ;;;; USA. ;;;; ;;;; Author: Erick Gallesio [eg@essi.fr] ;;;; Creation date: 14-Bun-2007 09:24 (eg) ;;;; Last file update: 4-Dec-2011 18:10 (eg) ;;;; (define make-external-function #f) (define make-callback #f) (let ((table '((:void 0) (:char 1) (:short 2) (:ushort 3) (:int 4) (:uint 5) (:long 6) (:ulong 7) (:lonlong 8) (:ulonlong 9) (:float 10) (:double 11) (:boolean 12) (:pointer 13) (:string 14) (:int8 15) (:int16 16) (:int32 17) (:int64 18) (:obj 19)))) (define (arg-type->number k argument?) (let ((info (assoc k table))) (if info (let ((r (cadr info))) (if (and (zero? r) argument?) (error 'define-external "parameter of type :void are forbidden") (cadr info))) (error 'define-external "bad type name ~S" k)))) (define (parse-parameters lst) (map (lambda (x) (cond ((keyword? x) (arg-type->number x #t)) ((pair? x) (if (and (symbol? (car x)) (keyword? (cadr x)) (null? (cddr x))) (arg-type->number (cadr x) #t) (error 'make-external-function "bad parameter description: ~S" x))) (else (error 'make-external-function "bad parameter description: ~S" x)))) lst)) ;; make-external-function (set! make-external-function (lambda (entry-name parameters return-type lib-name) (%make-ext-func entry-name (parse-parameters parameters) (arg-type->number return-type #f) lib-name))) ;; make-callback (set! make-callback (lambda (proc types data) (%make-callback proc (parse-parameters types) data)))) #| |# (define-macro (define-external name parameters . args) (let* ((lib (key-get args :library-name "")) (lib-name (if (and (equal? lib "") (equal? (running-os) 'cygwin-windows)) "cygwin1.dll" lib)) (entry-name (key-get args :entry-name (symbol->string name))) (return-type (key-get args :return-type :void))) `(define ,name (make-external-function ,entry-name ',parameters ,return-type ,lib-name)))) (provide "ffi")