[plt-scheme] foreign.ss: callbacks in cstructs?

From: Jim Meier (jdm352 at mail.usask.ca)
Date: Tue May 15 18:37:53 EDT 2007

Hello PLTers,

I'm building  a wrapper for the Subversion version control libraries  
using the foreign.ss foreign interface, and I've run into a snag:

Subversions libsvn_client library stores several common callback  
functions in a context structure; it's this context structure that is  
passed to the library functions, rather than the callback itself.  
Unfortunately, when I attempt to set a callback field to a scheme  
procedure, I get the error:

    ptr-set!: setting fpointer value with extra arguments

I've tracked this down to src/foreign/foreign.ssc line 1318, in the  
definition of ptr-set!. It seems to be checking that callers aren't  
using offsets/indexes with function pointers; unfortunately, offsets  
are rather important for struct field access. A similar check is done  
in ptr-ref on line 1275.

My subversion wrapper is rather large, but here's a representative  
example of how I'm going about this:

(require (lib "foreign.ss")) (unsafe!)
(define _callback_t
   (_fun (opaque_baton : _pointer) -> _int))

(define-cstruct _struct_t
   ((optional-callback _callback_t)
    (optional-opaque-baton _pointer)))

(define (my-callback baton)
   (display "Hello!") (newline))

(define struct-instance (make-struct_t 1 2 my-callback #f))
(set-struct_t-optional-callback! struct-instance #f)
(display (struct_t-optional-callback struct-instance)) (newline)

Is this a sensible way of declaring and setting these callbacks? Do  
foreign.ss cstructs support such callback fields? If not, could that  
be fixed simply by removing the two checks described above?

Thanks for any advice,

Posted on the users mailing list.