working example with explicitly accessing internal blob pasted by C-Keen on Sun Jan 15 14:05:44 2012
(import foreign) (foreign-declare #<<EOF typedef struct { double u; double v; double w; double x; double y; void* hah; double z; } c_struct_t; void fill_struct (c_struct_t* s, double val){ s->z = val; } EOF ) (define-foreign-variable sizeof-c-struct size_t "sizeof(c_struct_t)") (define-record c-struct-type buffer) (let ((maker make-c-struct-type)) (set! make-c-struct-type (lambda () (maker (make-blob sizeof-c-struct))))) (define-foreign-type c-struct-t scheme-pointer) (define fill-struct (foreign-lambda void "fill_struct" c-struct-t double)) (define c-struct-t-z (foreign-lambda* double ((c-struct-t t)) "C_return(((c_struct_t*)t)->z);")) (print "sizeof c struct: " sizeof-c-struct) (let loop ((i 0.23)) (let ((s (c-struct-type-buffer (make-c-struct-type)))) ;; <-- Buffer accessor used here (fill-struct s i) (print i " == " (c-struct-t-z s))) (loop (add1 i)))
I think this should work added by sjamaan on Sun Jan 15 14:20:22 2012
(import foreign) (foreign-declare #<<EOF typedef struct { double x; double y; int foo; } c_struct_t; void fill_struct (c_struct_t* s, int val){ s->foo = val; } EOF ) (define-foreign-variable sizeof-c-struct size_t "sizeof(c_struct_t)") (define-record c-struct-type buffer) (define make-c-struct-type* #f) (let ((maker make-c-struct-type)) (set! make-c-struct-type* maker) (set! make-c-struct-type (lambda () (maker (make-blob sizeof-c-struct))))) (define-foreign-type c-struct scheme-pointer c-struct-type-buffer make-c-struct-type*) (define fill-struct (foreign-lambda void "fill_struct" c-struct int)) (define c-struct-t-foo (foreign-lambda* int ((c-struct t)) "C_return(((c_struct_t*)t)->foo);")) (print "sizeof c struct: " sizeof-c-struct) (let loop ((i 0)) (let ((s (make-c-struct-type))) (fill-struct s i) (print i " == " (c-struct-t-foo s))) (loop (add1 i)))