From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Neil Jerram Newsgroups: gmane.lisp.guile.user Subject: Re: Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters Date: Mon, 05 Jul 2010 21:18:30 +0100 Message-ID: <87bpalof6x.fsf@ossau.uklinux.net> References: <20100705085635.GB9492@apc> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1278361128 31003 80.91.229.12 (5 Jul 2010 20:18:48 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 5 Jul 2010 20:18:48 +0000 (UTC) Cc: guile-user@gnu.org To: rixed@happyleptic.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Jul 05 22:18:47 2010 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OVs7m-0006hN-2S for guile-user@m.gmane.org; Mon, 05 Jul 2010 22:18:42 +0200 Original-Received: from localhost ([127.0.0.1]:33012 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OVs7l-0005CI-Fs for guile-user@m.gmane.org; Mon, 05 Jul 2010 16:18:41 -0400 Original-Received: from [140.186.70.92] (port=59912 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OVs7g-0005CD-5L for guile-user@gnu.org; Mon, 05 Jul 2010 16:18:37 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OVs7e-0007CM-Qw for guile-user@gnu.org; Mon, 05 Jul 2010 16:18:36 -0400 Original-Received: from mail3.uklinux.net ([80.84.72.33]:56172) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OVs7e-0007CB-Mk for guile-user@gnu.org; Mon, 05 Jul 2010 16:18:34 -0400 Original-Received: from arudy (unknown [92.29.72.223]) by mail3.uklinux.net (Postfix) with ESMTP id 017431F66B8; Mon, 5 Jul 2010 21:18:33 +0100 (BST) Original-Received: from arudy (unknown [192.168.11.8]) by arudy (Postfix) with ESMTP id C965238013; Mon, 5 Jul 2010 21:18:30 +0100 (BST) In-Reply-To: <20100705085635.GB9492@apc> (rixed@happyleptic.org's message of "Mon, 5 Jul 2010 10:56:36 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4-2.6 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:7961 Archived-At: rixed@happyleptic.org writes: > (define (hook-helper %s) (lambda () #\t)) > > where %s is the long list of parameters (foo bar baz...) that's inserted by the C program. > And : > > (define (hook . args) (local-eval (cons print-user-fields user-fields) (procedure-environment (apply hook-helper args)))) Using local-eval and procedure-environment like this won't work in Guile 1.9/2.0, I believe. But you could get a similar effect - which I think will still work in 1.9/2.0 - by creating a module, defining values in it, and then evaluating in that module. (In Guile, module == top level environment.) (Note that the bindings in a module are conceptually similar to an alist, so this is actually not so different from what Thien-Thi suggested.) To create a module, in Scheme: (define-module (xxx)) Then your C code would access that using SCM module = scm_c_resolve_module ("xxx"); and define each value in it using scm_c_module_define (module, name, value); and finally evaluate in that module using scm_c_eval_string_in_module (expression, module); It would also seem (at least to me) a bit less magical to define the user-fields as a procedure, e.g.: (define (calc-user-fields) (list foo bar (+ baz foo) (if (> foo bar) 1 2))) and then the "expression" above would be "(calc-user-fields)". All completely untested, of course! :-) Regards, Neil