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 ?


Russ McManus <russell.mcmanus@gs.com> writes:

> Please check out this section of the guile FAQ, and let me know if
> it helps:
> 
> http://www.gnu.org/software/guile/docs/faq/guile-faq.html#Calling%20out%20to%20Scheme

Well, this does not even work within scheme because 'foo' is not defined in
guile-user but inside the scope of the lambda expression:

====
(define bar (lambda (foo) (module-ref (resolve-module '(guile-user)) 'foo)))
(bar "whatever")
====

My problem is more that I want to do something like the following:

====
(let* ((y "whatever-this-will-be") (retval #f)
       (callback-func (lambda (x) (do-something-useful-with-it x y)))
       (set-the-retval (lambda (new-value) (set! retval new-value)))
      )
  (some-nice-c-function "some-parameters-for-the-c-func")
  (retval)
)
====

And in the C code:

====
static void (*callback) (int) = NULL;

void
callback_hook (int x)
{
  char buffer [BUFSIZ];

  sprintf (buffer, "(set-the-retval (callback-func %d))", x);
  gh_eval_str (buffer);
}

void
we_may_not_modify_this_function (void)
{
   ....

   if (callback)
     callback (some_integer_value);

   ....
}

SCM some_nice_c_function (SCM parameters)
{
   we_may_not_modify_this_function ();
}

void
init (void)
{
  gh_new_procedure ("some-nice-c-function", some_nice_c_function, 1, 0, 0);
  callback = callback_hook;
}
====

The `callback_hook' in C should call the `callback-func' and `set-the-retval'
lambda expression which are defined inside the (let* ...) where the
`some_nice_c_function' is called.

                                   * * *

In short:

====
(define bar (lambda (foo) (eval (+ foo 10))))
(bar 8)
====

works without problems, but

====
SCM testfunc (void)
{
  return gh_eval_str ("(+ foo 10)");
}

void init (void)
{
  gh_new_procedure ("testfunc", testfunc, 1, 0, 0);
}
====

and

====
(define bar (lambda (foo) (testfunc)))
(bar 8)
====

will not.

-- 
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]