[plt-scheme] scheme/foreign, callbacks and NULL

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sat Jan 3 13:51:15 EST 2009

At Thu, 1 Jan 2009 16:49:43 -0700, Matthew Flatt wrote:
> At Fri, 02 Jan 2009 00:43:57 +0100, Andreas Rottmann wrote:
> > Matthew Flatt <mflatt at cs.utah.edu> writes:
> > 
> > > At Thu, 01 Jan 2009 15:08:26 +0100, Andreas Rottmann wrote:
> > [example snipped]
> > >> 
> > >> This causes the following error when run:
> > >> Scheme->C: expects argument of type <cpointer>; given #f
> > >
> > > You can use `_pointer' instead of `_fpointer' as the last argument to
> > > `g-idle-add-full%'.
> > >
> > Updating my example accordingly:
> > 
> > #lang scheme
> > 
> > (require scheme/foreign
> >          (only-in '#%foreign ffi-callback))
> > 
> > (unsafe!)
> > 
> > (define libglib (ffi-lib "libglib-2.0" '("0")))
> > (define g-idle-add-full% (get-ffi-obj "g_idle_add_full" libglib _fpointer))
> > 
> > (define g-idle-add-full
> >   (function-ptr g-idle-add-full%
> >                 (_cprocedure (list _int _fpointer _pointer _pointer) _uint)))
> > 
> > (define my-idle-callback
> >   (function-ptr (lambda args
> >                   (printf "idle, args: ~s~n" args)
> >                   1)
> >                 (_cprocedure (list _pointer) _int)))
> > 
> > (define my-destroy-callback
> >   (function-ptr (lambda args
> >                   (printf "destroy, args: ~s~n" args))
> >                 (_cprocedure (list _pointer) _void)))
> > 
> > (g-idle-add-full 200 my-idle-callback #f #f)
> > (g-idle-add-full 200 my-idle-callback #f my-destroy-callback)
> > ;; EOF
> > 
> > I now get:
> > 
> > Scheme->C: expects argument of type <pointer>; given #<ffi-callback>
> > 
> > > I'll fix the docs to clarify that `_fpointer' doesn't allow NULL
> > > pointers. Or maybe it should allow NULLs?
> > >
> > Either it should, or (preferably) you would be able to pass
> > #<ffi-callback> objects for _pointer arguments.
> 
> Hm... yes, I think ffi-callback objects probably should be allowed as
> pointers. I'll wait for Eli to weigh in, though.

Well, I've made the change for now, and we'll see what Eli says. The
existing docs for `cpointer?' suggest that callbacks were meant to be
allowed as pointers.

While I was at it, I changed `_fpointer' to treat #f as NULL and
vice-versa.


Matthew



Posted on the users mailing list.