Idea/proposal/question DEF-FOREIGN-VAR

Edi Weitz <[email protected]> 14 Aug 2003 22:17:36 +0200
Newsgroups gmane.lisp.uffi.general
Message-ID <[email protected]>
If possible I'd like to have something like DEF-FOREIGN-VAR in
UFFI. This macro should provide access to variables in foreign
code. Below is a preliminary version to show how it might look
like. However, this version doesn't quite do what I want.

It seems to work on CMUCL (and probably also on SCL and SBCL) while on
LW it only works for primitive types. I guess I have to try the
:ADDRESS-OF value for the ACCESSOR keyword in
FLI:DEFINE-FOREIGN-VARIABLE.

For AllegroCL, I have no idea at all. I thought that
FF:DEF-FOREIGN-VARIABLE was what I wanted but it seems to be intended
for integer access only if I understand its behaviour correctly. If
you macro-expand it you can see how they get at the variable's address
but I don't think it's right to use internal, undocumented functions.

LW creates an accessor function while all other implementations I have
here use symbol macros. Thus the somewhat strange definition for LW.

Any comments or ideas?

Thanks,
Edi.



(defmacro def-foreign-var (names type module)
  #-lispworks (declare (ignore module))
  #+allegro (declare (ignore type))
  (let ((foreign-name (if (atom names) names (first names)))
        (lisp-name (if (atom names) (uffi::make-lisp-name names) (second names)))
        #-allegro
        (var-type (uffi::convert-from-uffi-type type :type)))
    #+(or cmu scl)
    `(alien:def-alien-variable (,foreign-name ,lisp-name) ,var-type)
    #+sbcl
    `(sb-alien:define-alien-variable (,foreign-name ,lisp-name) ,var-type)
    #+allegro
    `(ff:def-foreign-variable (,lisp-name ,foreign-name) :convention :c)
    #+lispworks
    (let ((temp-name (gensym)))
      `(progn
        (fli:define-foreign-variable (,temp-name ,foreign-name) :type ,var-type :module ,module)
        (define-symbol-macro ,lisp-name (,temp-name))))
    #-(or allegro cmu scl sbcl lispworks)
    `(define-symbol-macro ,lisp-name
      '(error "DEF-FOREIGN-VAR not (yet) defined for ~A"
        (lisp-implementation-type)))))