From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: =?utf-8?B?5a6L5paH5q2m?= <iyzsong@envs.net>
Newsgroups: gmane.lisp.guile.user
Subject: Re: Calling user defined guile functions from C?
Date: Fri, 24 Nov 2023 18:50:07 +0800
Message-ID: <87msv3o274.fsf@envs.net>
References: <CX69Z8VEXGGC.2O6N6Y5K4NF6N@stud.uni-goettingen.de>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="40134"; mail-complaints-to="usenet@ciao.gmane.io"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cc: <guile-user@gnu.org>
To: Leon Henrik Plickat <leonhenrik.plickat@stud.uni-goettingen.de>
Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Fri Nov 24 11:50:45 2023
Return-path: <guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org>
Envelope-to: guile-user@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org>)
	id 1r6Tlk-000AGL-U5
	for guile-user@m.gmane-mx.org; Fri, 24 Nov 2023 11:50:44 +0100
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <guile-user-bounces@gnu.org>)
	id 1r6TlK-0005N2-5z; Fri, 24 Nov 2023 05:50:18 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <iyzsong@envs.net>) id 1r6TlG-0005ED-LQ
 for guile-user@gnu.org; Fri, 24 Nov 2023 05:50:15 -0500
Original-Received: from mail.envs.net ([5.199.136.28])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <iyzsong@envs.net>) id 1r6TlD-0006la-BL
 for guile-user@gnu.org; Fri, 24 Nov 2023 05:50:13 -0500
Original-Received: from localhost (mail.envs.net [127.0.0.1])
 by mail.envs.net (Postfix) with ESMTP id 2A7C838A0279;
 Fri, 24 Nov 2023 10:50:08 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=envs.net; s=modoboa;
 t=1700823008; bh=pVEWuIhyE/bRawjmFTHqIOdmhNd274bmuS6xix/NirA=;
 h=From:To:Cc:Subject:In-Reply-To:References:Date:From;
 b=P8q5MCV3zD93kt7lrZWZHyfqWMOi+DKnJgNaRfwardJMH2mGcNIzT7iB7ByNweqq/
 UZNOTr78yfOzRe+IpLFstrHPChlbKh6PbRdkEEOQwk0shFI3ssofBB8Z8rgcvrtUnB
 byqOVyhI53M1DvqIL35B5WbPy6r0stjdQHVsSGO4TPZvUdBLRfRmyW3q5uU/EhAH+t
 afxblFeYW1GRP/TeH2DKZadvBI/4xIBitfIb+/hbM2YfaU6NPmP8H+1JbWtWiuJ3yc
 ms3Nf7j8D4QrBhA3RpE7j8Rx/FM9yOVXt8N16ydhMrZtS4uXpqgOFZmSMQhN3ga0kx
 IitUInrbDs5XRmz+dUNWfXlUDkgE5KRurqqjpn8VgI7cXv7BUcrXAdGf9rkgzm36Fu
 kD0fiky6w4WlhB7aAtQxz82jDKcD7mpXG/6fvh6RK8GFryXj0n/n4sARPOkVr4UOpA
 Kwea/GEEzWw/998k5qwD4eIGtO0xMOMuu+wif62r6d1UDgWtMZoIvPQgUA4P6VX/9n
 CM+aIy3pt1WJ+rmA7ii1ZtrCX/U41EaHczvehwXBzD95IxOYFNyiY4RcE13EX111Gn
 6mxk8TXta/YhFq3NVYzhmsXvujLVt/v/oqUC3cXL10skvLgyluf9LQNu/icGyw02VD
 PbZkT0Ssr3Wiu99mEGNuy 
X-Virus-Scanned: Debian amavisd-new at mail.envs.net
Original-Received: from mail.envs.net ([127.0.0.1])
 by localhost (mail.envs.net [127.0.0.1]) (amavisd-new, port 10026)
 with ESMTP id j3lEtbCJiyrR; Fri, 24 Nov 2023 10:50:05 +0000 (UTC)
Original-Received: from localhost (unknown [36.170.45.232])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest
 SHA256) (No client certificate requested)
 by mail.envs.net (Postfix) with ESMTPSA;
 Fri, 24 Nov 2023 10:50:04 +0000 (UTC)
Original-Received: from localhost (localhost [local])
 by localhost (OpenSMTPD) with ESMTPA id b260497f;
 Fri, 24 Nov 2023 10:50:07 +0000 (UTC)
In-Reply-To: <CX69Z8VEXGGC.2O6N6Y5K4NF6N@stud.uni-goettingen.de> (Leon Henrik
 Plickat's message of "Thu, 23 Nov 2023 15:53:06 +0100")
Received-SPF: pass client-ip=5.199.136.28; envelope-from=iyzsong@envs.net;
 helo=mail.envs.net
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01,
 UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: guile-user@gnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: General Guile related discussions <guile-user.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/guile-user>,
 <mailto:guile-user-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/guile-user>
List-Post: <mailto:guile-user@gnu.org>
List-Help: <mailto:guile-user-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/guile-user>,
 <mailto:guile-user-request@gnu.org?subject=subscribe>
Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org
Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.lisp.guile.user:19324
Archived-At: <http://permalink.gmane.org/gmane.lisp.guile.user/19324>

Hello!

Leon Henrik Plickat <leonhenrik.plickat@stud.uni-goettingen.de> writes:
> [...]
>
> Must the program run in guile mode the entire time, or can I enter
> it only temporarily for eval'ing the script and calling the function?

I think 'scm_init_guile' is for the entire time, and 'scm_with_guile'
is for the temporarily usage.

> What is the canonical way of eval'ing a script to get a function
> definition from C?

Use 'scm_c_primitive_load' to load and eval a script, if the script
return a function as its last value, then it's done.
Else use 'scm_c_lookup' to get the function as a varaible.

> Can I check after eval'ing the script whether the function I want the
> user to define exists? Probably with scm_c_eval_string() again, but
> I wonder if there is some other more way that is considered to be
> better.

It seems 'scm_module_variable' returns '#f' when the variable binding
doesn't exist.

>
> I am working my way through the API reference, but decided to ask
> for some input in the meantime.

Procedures needed are described in manual section "6.18.10 Accessing
Modules from C".  The flow are:

- Enter guile mode via 'scm_init_guile', 'scm_boot_guile', or 'scm_with_guile'.
- Load and eval script via 'scm_c_primitive_load'.
- Get SCM variables via 'scm_c_lookup', 'scm_module_variable'
- Dereference variables via 'scm_variable_ref' to procedures SCM.
- Call procedures via 'scm_call', 'scm_call_1, etc.

Hope it helps!