expand-to-foreign-dyn ignored when structures are passed by value

Bug #1473807 reported by 3b
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
CFFI
New
Undecided
Unassigned

Bug Description

If structures are passed to/from a structure by value, it falls back to TRANSLATE-INTO-FOREIGN-MEMORY, even for other arguments.

(cffi:define-foreign-type a1 ()
  ()
  (:actual-type :pointer)
  (:simple-parser a1))

(cffi:defcstruct (a2 :class a2))

(defmethod expand-to-foreign-dyn (value var body (type a1))
  `(with-special-allocator (,var)
     (setf (cffi:mem-ref ,var a1) ,value)
     ,@body))

(third (macroexpand-1 '(cffi:defcfun "foo1" :void (a a1))))
;; =>
(DEFUN FOO1 (A)
  (WITH-SPECIAL-ALLOCATOR (#:G913)
    (SETF (MEM-REF #:G913 A1) A)
    (CFFI-SYS:%FOREIGN-FUNCALL "foo1" (:POINTER #:G913 :VOID) :CONVENTION
                               :CDECL :LIBRARY :DEFAULT)))

(macroexpand-1 (fourth (third (macroexpand-1 '(cffi:defcfun "foo2" :void (a a1) (b (:struct a2)))))))
;; =>
(CFFI:WITH-FOREIGN-OBJECTS ((CFFI::ARGVALUES :POINTER 2))
  (CFFI:WITH-FOREIGN-OBJECT (#:G643 'A1)
    (CFFI:TRANSLATE-INTO-FOREIGN-MEMORY A #<A1 {10150DC203}> #:G643)
    (CFFI:WITH-FOREIGN-OBJECT (#:G644 '(:STRUCT A2))
      (CFFI:TRANSLATE-INTO-FOREIGN-MEMORY B #<A2 A2> #:G644)
      (PROGN
       (LOOP :FOR CFFI::ARG :IN (LIST #:G643 #:G644)
             :FOR
             COUNT :FROM 0
             :DO (SETF (CFFI:MEM-AREF CFFI::ARGVALUES :POINTER COUNT)
                         CFFI::ARG))
       (CFFI::CALL
        (CFFI::PREPARE-FUNCTION "foo2" ':VOID '(:POINTER (:STRUCT A2))
                                ':DEFAULT-ABI)
        (CFFI:FOREIGN-SYMBOL-POINTER "foo2") (CFFI-SYS:NULL-POINTER)
        CFFI::ARGVALUES)
       (VALUES)))))

expected A1 to use WITH-SPECIAL-ALLOCATOR instead of TRANSLATE-INTO-FOREIGN-MEMORY

tested on sbcl win/linux x8664, cffi v0.15

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.