From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: rixed@happyleptic.org Newsgroups: gmane.lisp.guile.user Subject: Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters Date: Mon, 5 Jul 2010 10:56:36 +0200 Message-ID: <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 1278320231 15462 80.91.229.12 (5 Jul 2010 08:57:11 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 5 Jul 2010 08:57:11 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Jul 05 10:57:09 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 1OVhUC-0000Of-OY for guile-user@m.gmane.org; Mon, 05 Jul 2010 10:57:09 +0200 Original-Received: from localhost ([127.0.0.1]:58855 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OVhUC-0002SC-6w for guile-user@m.gmane.org; Mon, 05 Jul 2010 04:57:08 -0400 Original-Received: from [140.186.70.92] (port=46239 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OVhTv-0002S5-BW for guile-user@gnu.org; Mon, 05 Jul 2010 04:56:52 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OVhTu-0007rL-44 for guile-user@gnu.org; Mon, 05 Jul 2010 04:56:51 -0400 Original-Received: from smtp5-g21.free.fr ([212.27.42.5]:41469) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OVhTt-0007qq-BI for guile-user@gnu.org; Mon, 05 Jul 2010 04:56:50 -0400 Original-Received: from apc.happyleptic.org (unknown [82.67.194.89]) by smtp5-g21.free.fr (Postfix) with ESMTP id ABCDCD480DA for ; Mon, 5 Jul 2010 10:56:43 +0200 (CEST) Original-Received: by apc.happyleptic.org (Postfix, from userid 1004) id 7FDFB33877; Mon, 5 Jul 2010 10:56:36 +0200 (CEST) Content-Disposition: inline User-Agent: Mutt/1.5.12-2006-07-14 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) 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:7956 Archived-At: Hello ! Frequently, our C program computes a set of approximately 50 values that it wants to make accessible for a user defined call back so that the user can choose to print some of these values (if some condition is met for instance). I first thought to make some C functions accessible that would return the values (one C getter per value), and have the C scheme procedure call these C functions for the values it's interested in. But the problem is that it's a little verbose (both in C and in scheme due to the required parenthesis). I then though of binding the values to global variables that the user callback would use but ruled this out because I was afraid this solution would be slow. Then I wanted to pass the values as regular parameters, but I don't want to impose the user to define his function with 50 parameters. So finally we came up with something like this : The user enter merely a list of the values he wants (in a "configuration" file) : (define user-fields `(foo bar (+ baz foo) (if (> foo bar) 1 2))) And then in C we evaluate : (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)))) So the user entry is minimal and we can, for each set of values, apply the hook function to the values (which is I think one of the fastest way to make available some a set of named values to scheme). The downside is : the above scheme line looks like black magic. Can anyone suggest a better way to do this ?