chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Accessing C macro definitions from scheme?


From: felix
Subject: Re: [Chicken-users] Accessing C macro definitions from scheme?
Date: Sun, 17 Aug 2003 23:09:50 +0200
User-agent: Opera7.11/Linux M2 build 406

On Sat, 16 Aug 2003 10:08:03 -0700, Burton Samograd <address@hidden> wrote:

Hi,

I'm trying to write a simple interface wrapper for a small library I
would like to use and I can seem to be able to acess constant #define's
in a way that seems reasonable.  What I'm looking for is something
like this:

-----------------------------------------------------------------------

/* header.h */
#define MY_DEFINED_VALUE (0L)

-----------------------------------------------------------------------

;;; wrapper.scm
(declare
(export
my-defined-value))

; include the header so we can get at MY_DEFINED_VALUE
(declare (foreign-declare "#include \"header.h\""))

(define my-defined-value
( +++ WHAT GOES HERE TO GET THE VALUE OF MY_DEFINED_VALUE? ***))


Try:

(define-foreign-variable _my-defined-value int "MY_DEFINED_VALUE")
(define my-defined-value _my-defined-value)

The foreign variable defined `define-foreign-variable' is only visible
in the currently compiled file, so the second definition is needed.
Another approach would be something like:

(define-macro (define-exported-foreign-variable name type text)
 (let ([tmp (gensym)])
   `(begin
      (define-foreign-variable ,tmp ,type ,text)
      (define ,name ,tmp) ) ) )

(define-exported-foreign-variable my-defined-value int "MY_DEFINED_VALUE")

[Courtesy of Peter Wang]

This doesn't work for mutable variables. But a "parameter"-like approach
would work for that, like:

(define-macro (define-exported-foreign-variable name type text)
 (let ([tmp (gensym)])
   `(begin
      (define-foreign-variable ,tmp ,type ,text)
      ,(if (pair? name)
         `(define (,name . val) (if (pair? val) (set! ,tmp val) ,tmp) )
         `(define ,name ,tmp) ) ) ) )

So for example,

(declare (foreign-declare "int foo;"))

(define-exported-foreign-variable (foo) int "foo")

(foo 33)
(foo)  ; ==> 33


-----------------------------------------------------------------------

I would like to be able to compile the wrapper.scm using 'csc -s
wrapper.scm' to produce wrapper.so, be able to load it into csi with a
(require 'wrapper) and then be able to access my-defined-value, but so
far I haven't be able to get it to work.

##core#inline looked promising, but also didn't seem to do what I
wanted unless I defined a macro that took a parameter which returned
the define, such as this:

(declare (foreign-declare "#define CONST(arg) arg"))
(define my-defined-value (##core#inline CONST MY_DEFINED_VALUE))

Yep, ##core#inline is really more a tool for hacking internals.


cheers,
felix





reply via email to

[Prev in Thread] Current Thread [Next in Thread]