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

From: Andreas Rottmann (a.rottmann at gmx.at)
Date: Thu Jan 1 18:43:57 EST 2009

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.

>
>> PS: I know one is not really supposed to use `ffi-callback' directly,
>>     but I need it to provide a compatible API across all
>>     implementations.
>
> Another doc issue: I forgot to update the docs for `function-ptr' to
> say that you can provide a Scheme procedure as the first argument. So,
> you could use `function-ptr' instead of `ffi-callback'.
>
Good to hear I don't need to rely on quasi-non-public API :-).

Thanks & Regards, Rotty
-- 
Andreas Rottmann         | Rotty at ICQ      | 118634484 at ICQ | a.rottmann at gmx.at
http://rotty.uttx.net    | GnuPG Key: http://rotty.uttx.net/gpg.asc
Fingerprint              | C38A 39C5 16D7 B69F 33A3  6993 22C8 27F7 35A9 92E7
v2sw7MYChw5pr5OFma7u7Lw2m5g/l7Di6e6t5BSb7en6g3/5HZa2Xs6MSr1/2p7 hackerkey.com

To iterate is human; to recurse, divine.


Posted on the users mailing list.