[cffi-devel] Re: Need foreign string method for translate-into-foreign-memory?

Liam Healy lnp at healy.washington.dc.us
Thu Oct 17 12:58:18 UTC 2013


On Thu, Oct 17, 2013 at 6:27 AM, Luís Oliveira <luismbo at gmail.com> wrote:
> Liam Healy <lnp at healy.washington.dc.us> writes:
>
>> Yes. How does this look?
>>
>> (defmethod expand-to-foreign-dyn-indirect
>>           (value var body (type foreign-string-type))
>>         (expand-to-foreign-dyn value var body type))
>
> Looks better. Is there a test we can try to see if it works and to see
> what the macro-expansion looks like?
>
> Luís
>
>


Yes; I don't have an example, but the original complaint on #lisp had
something like this

(defcstruct point (x :float) (y :float))

(defcfun ("render" c-render) :void
  (point (:struct point))
  (width :int)
  (height :int)
  (info :string)
  (world :pointer))

The expansion is

(PROGN
 NIL
 (DEFUN C-RENDER (POINT WIDTH HEIGHT INFO WORLD)
   (WITH-FOREIGN-OBJECT (#:G1363 '(:STRUCT POINT))
     (TRANSLATE-INTO-FOREIGN-MEMORY POINT #<POINT-TCLASS POINT> #:G1363)
     (WITH-FOREIGN-OBJECT (#:G1364 :POINTER)
       (TRANSLATE-INTO-FOREIGN-MEMORY WIDTH #<FOREIGN-BUILT-IN-TYPE
:INT> #:G1364)
       (WITH-FOREIGN-OBJECT (#:G1365 :POINTER)
         (TRANSLATE-INTO-FOREIGN-MEMORY HEIGHT #<FOREIGN-BUILT-IN-TYPE :INT>
                                        #:G1365)
         (MULTIPLE-VALUE-BIND (#:G1366 #:PARAM1368)
             (TRANSLATE-TO-FOREIGN INFO #<FOREIGN-STRING-TYPE :UTF-8>)
           (UNWIND-PROTECT
               (PROGN
                (WITH-FOREIGN-OBJECT (#:G1367 :POINTER)
                  (TRANSLATE-INTO-FOREIGN-MEMORY WORLD

#<FOREIGN-POINTER-TYPE :POINTER>
                                                 #:G1367)
                  (WITH-FOREIGN-OBJECTS ((ARGVALUES :POINTER 5))
                    (LOOP :FOR ARG :IN (LIST #:G1363 #:G1364 #:G1365 #:G1366
                                             #:G1367)
                          :FOR
                          COUNT :FROM 0
                          :DO (SETF (MEM-AREF ARGVALUES :POINTER COUNT) ARG))
                    (CALL
                     (PREPARE-FUNCTION "render" ':VOID
                                       '((:STRUCT POINT) :INT :INT :POINTER
                                         :POINTER)
                                       ':DEFAULT-ABI)
                     (FOREIGN-SYMBOL-POINTER "render") (NULL-POINTER) ARGVALUES)
                    (VALUES))))
             (FREE-TRANSLATED-OBJECT #:G1366 #<FOREIGN-STRING-TYPE :UTF-8>
                                     #:PARAM1368))))))))

This expansion looks right to me, and it compiles, but I can't test it
as I don't have the library.

I'm not even sure why e-t-f-d-indirect doesn't always just call
e-t-f-d except where the type is directly represented, but I'm not
going to mess with something that works.

Liam



More information about the cffi-devel mailing list