Commits

Armin Rigo committed 08b8c2d

Functions and function pointers as arguments.

Comments (0)

Files changed (2)

             converter = '_cffi_to_c_%s' % (tp.name.replace(' ', '_'),)
             errvalue = '-1'
         #
-        elif isinstance(tp, (model.PointerType, model.FunctionPtrType)):
-            if (isinstance(tp, model.PointerType) and
-                    isinstance(tp.totype, model.PrimitiveType) and
+        elif isinstance(tp, model.PointerType):
+            if (isinstance(tp.totype, model.PrimitiveType) and
                     tp.totype.name == 'char'):
                 converter = '_cffi_to_c_char_p'
             else:
             self.prnt('    %s;' % errcode)
             return
         #
+        elif isinstance(tp, model.BaseFunctionType):
+            if isinstance(tp, model.RawFunctionType):
+                tp = tp.as_function_pointer()
+            converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('')
+            extraarg = ', _cffi_type(%d)' % self.gettypenum(tp)
+            errvalue = 'NULL'
+        #
         else:
             raise NotImplementedError(tp)
         #
         prnt('{')
         #
         for i, type in enumerate(tp.args):
+            if isinstance(type, model.RawFunctionType):
+                type = type.as_function_pointer()
             prnt('  %s;' % type.get_c_name(' x%d' % i))
         if not isinstance(tp.result, model.VoidType):
             result_code = 'result = '

testing/test_verify.py

             int (*compar)(const void *, const void *));
     """)
     ffi.verify("#include <stdlib.h>")
+
+def test_func_as_argument():
+    ffi = FFI()
+    ffi.cdef("""
+        void qsort(void *base, size_t nel, size_t width,
+            int compar(const void *, const void *));
+    """)
+    ffi.verify("#include <stdlib.h>")