2 ;;; Copyright (C) 2013 Andy Wingo <wingo@pobox.com>
4 ;;; Figl is free software: you can redistribute it and/or modify it
5 ;;; under the terms of the GNU Lesser General Public License as
6 ;;; published by the Free Software Foundation, either version 3 of the
7 ;;; License, or (at your option) any later version.
9 ;;; Figl is distributed in the hope that it will be useful, but WITHOUT
10 ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 ;;; Public License for more details.
14 ;;; You should have received a copy of the GNU Lesser General Public
15 ;;; License along with this program. If not, see
16 ;;; <http://www.gnu.org/licenses/>.
20 ;; figl is the Foreign Interface to GL.
24 (define-module (figl runtime)
25 #:use-module (system foreign)
26 #:export (define-foreign-procedure
27 define-foreign-procedures
29 define-simple-foreign-type))
31 (define-syntax foreign-trampoline
33 ((_ trampoline resolve-name (pname ptype) ... -> type)
36 (pointer->procedure (type)
39 (trampoline pname ...)))))
41 (define-syntax define-foreign-procedure
43 ((define-foreign-procedure (name (pname ptype) ... -> type)
48 (foreign-trampoline trampoline resolve-name
49 (pname ptype) ... -> type))
50 (name (lambda (pname ...)
52 (let ((pname (ptype #:unwrap pname))
54 (type #:wrap (trampoline pname ...))))))
57 (define-syntax define-foreign-procedures
59 ((define-foreign-procedures ((name prototype ...) ...)
63 (define-foreign-procedure (name prototype ...)
68 (define-syntax define-foreign-type
70 ((_ name ffi-type unwrap wrap)
74 ((_ #:wrap x) (wrap x))
75 ((_ #:unwrap x) (unwrap x)))))))
77 (define-syntax define-simple-foreign-type
80 ;; We could dispatch through to define-foreign-type via:
82 ;; (define-foreign-type name
87 ;; However the resulting wrap expression:
89 ;; ((lambda (x) x) (trampoline arg ...))
91 ;; would not be in tail position, as the optimizer doesn't know
92 ;; that the foreign function just returns one value. This hack
93 ;; just passes the wrapped expression through, allowing it to be in
99 ((_ #:unwrap x) x))))))