X-Git-Url: https://git.distorted.org.uk/~mdw/clg/blobdiff_plain/cb8163645e1940bfe52ec24690ec048342d2159f..cd794aac76c990da71b3361e6333d3f7857d0946:/glib/gforeign.lisp diff --git a/glib/gforeign.lisp b/glib/gforeign.lisp index a8b32ba..48cd686 100644 --- a/glib/gforeign.lisp +++ b/glib/gforeign.lisp @@ -15,7 +15,7 @@ ;; License along with this library; if not, write to the Free Software ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -;; $Id: gforeign.lisp,v 1.6 2001/04/29 20:05:22 espen Exp $ +;; $Id: gforeign.lisp,v 1.13 2002/03/19 17:03:42 espen Exp $ (in-package "GLIB") @@ -53,7 +53,9 @@ (defun find-applicable-type-method (type-spec fname &optional (error t)) (flet ((find-superclass-method (class) - (when class + (when (and class (class-finalized-p class)) +; (unless (class-finalized-p class) +; (finalize-inheritance class)) (dolist (super (cdr (pcl::class-precedence-list class))) (return-if (find-type-method super fname))))) (find-expanded-type-method (type-spec) @@ -287,10 +289,11 @@ (defmacro defbinding (name lambda-list return-type-spec &rest docs/args) - (multiple-value-bind (c-name lisp-name) + (multiple-value-bind (lisp-name c-name) (if (atom name) - (values (default-alien-fname name) name) - (values-list name)) + (values name (default-alien-fname name)) + (values-list name)) + (let ((supplied-lambda-list lambda-list) (docs nil) (args nil)) @@ -312,11 +315,6 @@ c-name lisp-name (or supplied-lambda-list (nreverse lambda-list)) return-type-spec (reverse docs) (reverse args))))) -;; For backward compatibility -(defmacro define-foreign (&rest args) - `(defbinding ,@args)) - - #+cmu (defun %defbinding (foreign-name lisp-name lambda-list return-type-spec docs args) @@ -325,7 +323,7 @@ (dolist (arg args) (destructuring-bind (var expr type-spec style) arg (let ((declaration (translate-type-spec type-spec)) - (deallocation (cleanup-alien type-spec expr t))) + (deallocation (cleanup-alien type-spec var t))) (cond ((member style '(:out :in-out)) (alien-types `(* ,declaration)) @@ -348,6 +346,7 @@ (let ((alien-funcall `(alien-funcall ,lisp-name ,@(alien-parameters)))) `(defun ,lisp-name ,lambda-list ,@docs + (declare (optimize (ext:inhibit-warnings 3))) (with-alien ((,lisp-name (function ,(translate-type-spec return-type-spec) @@ -364,8 +363,52 @@ ,@(alien-deallocators) (values ,@(alien-values))))))))) - +(defun mkbinding (name return-type &rest arg-types) + (declare (optimize (ext:inhibit-warnings 3))) + (let* ((ftype + `(function + ,@(mapcar #'translate-type-spec (cons return-type arg-types)))) + (alien + (alien::%heap-alien + (alien::make-heap-alien-info + :type (alien::parse-alien-type ftype) + :sap-form (system:foreign-symbol-address name)))) + (translate-arguments + (mapcar #'intern-return-value-translator arg-types)) + (translate-return-value (intern-return-value-translator return-type)) + (cleanup-arguments (mapcar #'intern-cleanup-function arg-types))) + + #'(lambda (&rest args) + (map-into args #'funcall translate-arguments args) + (prog1 + (funcall + translate-return-value (apply #'alien:alien-funcall alien args)) + (mapc #'funcall cleanup-arguments args))))) + + +(defun type-translateable-p (type-spec) + (find-applicable-type-method type-spec 'translate-type-spec nil)) + +(defun every-type-translateable-p (type-specs) + (every #'type-translateable-p type-specs)) + +(defun mkbinding-late (name return-type &rest arg-types) + (if (every-type-translateable-p (cons return-type arg-types)) + (apply #'mkbinding name return-type arg-types) + (let ((binding nil)) + #'(lambda (&rest args) + (cond + (binding (apply binding args)) + ((every-type-translateable-p (cons return-type arg-types)) + (setq binding (apply #'mkbinding name return-type arg-types)) + (apply binding args)) + (t + (dolist (type-spec (cons return-type arg-types)) + (unless (type-translateable-p type-spec) + (error "Can't translate type ~A" type-spec))))))))) + + ;;;; Definitons and translations of fundamental types @@ -382,7 +425,6 @@ (deftype boolean (&optional (size '*)) (declare (ignore size)) `(member t nil)) -(deftype static (type) type) (deftype invalid () nil) (defun atomic-type-p (type-spec) @@ -602,11 +644,11 @@ )))) (deftype-method cleanup-alien string (type-spec c-string &optional weak-ref) - (declare (ignore type-spec)) (when weak-ref (unreference-alien type-spec c-string))) (deftype-method unreference-alien string (type-spec c-string) + (declare (ignore type-spec)) `(let ((c-string ,c-string)) (unless (null-pointer-p c-string) (deallocate-memory c-string)))) @@ -683,3 +725,9 @@ (deftype-method translate-type-spec nil (type-spec) (declare (ignore type-spec)) 'void) + +(deftype-method translate-from-alien nil (type-spec expr &optional weak-ref) + (declare (ignore type-spec weak-ref)) + `(progn + ,expr + (values)))