unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Calling user defined guile functions from C?
@ 2023-11-23 14:53 Leon Henrik Plickat
  2023-11-24 10:50 ` 宋文武
  0 siblings, 1 reply; 3+ messages in thread
From: Leon Henrik Plickat @ 2023-11-23 14:53 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1253 bytes --]

I intend on using guile as a scripting language in a C program (a
Wayland client in this case). All documentation and tutorials I have
found focus on extending guile with C code, however I explicitly want
to drive the code from C.

What I ideally want to do is to eval a guile script on startup of the
program in which a user can define a function. The script basically
functions as configuration. Afterwards, enter the main loop and in
response to certain events call the function defined by the user
(with scm_c_eval_string("(name-of-function)") I suppose) and use its
return value (a list of lists containg 4 integers each) to respond to
the event.

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?
What is the canonical way of eval'ing a script to get a function
definition from C?
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.

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

Friendly greetings,
Leon Henrik Plickat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Calling user defined guile functions from C?
  2023-11-23 14:53 Calling user defined guile functions from C? Leon Henrik Plickat
@ 2023-11-24 10:50 ` 宋文武
  2023-11-26  9:07   ` Leon Henrik Plickat
  0 siblings, 1 reply; 3+ messages in thread
From: 宋文武 @ 2023-11-24 10:50 UTC (permalink / raw)
  To: Leon Henrik Plickat; +Cc: guile-user

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!



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Calling user defined guile functions from C?
  2023-11-24 10:50 ` 宋文武
@ 2023-11-26  9:07   ` Leon Henrik Plickat
  0 siblings, 0 replies; 3+ messages in thread
From: Leon Henrik Plickat @ 2023-11-26  9:07 UTC (permalink / raw)
  To: 宋文武; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 278 bytes --]

Thank you! With your input this was mostly straight-forward.

I'll leave the repository link here, should anyone else wanting to do
something similar stumbles uppon this exchange:
https://git.sr.ht/~leon_plickat/riverguile/

-- 
Friendly greetings,
Leon Henrik Plickat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-26  9:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-23 14:53 Calling user defined guile functions from C? Leon Henrik Plickat
2023-11-24 10:50 ` 宋文武
2023-11-26  9:07   ` Leon Henrik Plickat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).