FFI API を考える

make-c-function 相当を手続きで書いたが、Ypsilon のようにマクロのマッチング機能をフルに使った方がきれいに書ける気がしなくもない。
どちらにせよ、いちいち quote するのは面倒なのでマクロで wrap はした方が良さそうだ。(定義はヘッダからコピペするのだし)

(define sub (make-c-function libtest 'int "sub" '(int int)))
(display (sub 5 -3))

今の make-c-function 実装。

(define (make-c-function lib ret-type name arg-types)
  (let ([func (%ffi-lookup lib name)]
        [stub (hashtable-ref stub-ht ret-type #f)]
        [checkers (map (lambda (type) (hashtable-ref checker-ht type #f)) arg-types)])
    (unless stub
      (assertion-violation 'make-c-function "wrong ret type" ret-type))
    (unless func
      (assertion-violation 'make-c-function "c-function not found" name))
    (lambda args
      (unless (= (length arg-types) (length args))
        (assertion-violation 'make-c-function "wrong arguments number" args))
      (for-each
       (lambda (checker arg)
         (unless (checker arg)
           (assertion-violation name "wrong argument " arg))
         )
       checkers
       args)
      (apply stub func args))))