This is the mail archive of the guile@cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: (lambda (foo) (some-c-function)) - can that C function get foo ?


Maciej Stachowiak <mstachow@alum.mit.edu> writes:

> It's working as advertised, gh_eval_str evaluates in the top-level
> environment. There are other functions to evaluate in the local
> environment, but are you sure you really need to do that?

Oh, I forgot ... should have looked at the source code first.

gh_eval_str () calls scm_eval_x () and that uses
`scm_top_level_env (SCM_CDR (scm_top_level_lookup_closure_var))'
to get the environment.

The problem is that I can't pass any additional parameters to the callback
function (so I can't call scm_eval_3 () with another environment), but I
think I found a good solution for this problem (see below).

> > In general, when you call a C function inside a (let ...) or a (lambda ...)
> > expressions is it possible to access the variables that are defined locally
> > in the scope of that expression ?
> > 
> 
> Well, why do you want to do that anyway? Surely there is a cleaner
> solution to your problem.

Hmm, I think I found a solution for it.

I'll define some reasonable defaults for the callbacks in a module global
variable and use that variable in the C code.

====
(define-module (gdb callbacks))

(define-public gdb-callbacks #f)

(define-public (gdb-call-with-callback-vector func args cbv)
  (let ((old-callbacks #f))

    (dynamic-wind

     ;; called at entry.
     (lambda ()
       (set! old-callbacks gdb-callbacks)
       (set! gdb-callbacks cbv))

     ;; the protected thunk.
     (lambda ()
       (func args))

     ;; called at exit.
     (lambda ()
       (set! gdb-callbacks old-callbacks)))))
====

This way you can either just say

====
(gdb-file "gdb")
====

or somethink like this:

====
(let* ((new-callbacks '((annotate-source . (lambda (x) #f))))
       (new-file (lambda (file) (gdb-file file))))
  (gdb-call-with-callback-vector new-file "gdb" new-callbacks))
====

-- 
Martin Baulig - martin@home-of-linux.org - http://www.home-of-linux.org

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]