Le 22/09/2022 à 18:25, Olivier Dion via General Guile related
discussions a écrit :
Hi,
`exit-hook' seems to only be executed when in a REPL. Is there a way for
it to be executed at the end of a none interactive program?
My intend here is that I have a module that load a foreign extension and
initialize its runtime so that other modules of that extension can be
used. I then add a hook for finalizing the extension, which is
important to save data.
Here's the snippet:
--8<---------------cut here---------------start------------->8---
(define-module (jami)
#:use-module (system foreing-library))
(let* ((libjami (load-foreign-library "libguile-jami"))
(init (foreign-library-function libjami "init"))
(fini (foreign-library-function libjami "fini")))
(init)
(add-hook! exit-hook fini))
--8<---------------cut here---------------end--------------->8---
So I guess the trivial solution would be to ask the user to manually
call the `init' and `fini' procedure before using any procedures of the
extension.
Something like this:
--8<---------------cut here---------------start------------->8---
(define-syntax-rule (with-jami body body* ...)
(dynamic-wind
init
(lambda () body body* ...)
fini))
--8<---------------cut here---------------end--------------->8---
But it would be nice to a have a hook for finalization like `exit-hook'
that is hidden from the user.
Though?
It looks like Guile only uses exit-hook for (ice-9 readline), so it
doesn't bother to call it when not running as REPL.
Maybe you could use a finalizer attached to a C global variable?
(I didn't test.)