Context for shared library

62 views
Skip to first unread message

AB

unread,
Aug 16, 2021, 5:27:21 PM8/16/21
to chibi-scheme
How can a shared library written in c get the context from the scheme page that loaded it?

Lukas Böger

unread,
Aug 16, 2021, 6:40:56 PM8/16/21
to chibi-scheme
> How can a shared library written in c get the context from the scheme page that loaded it?

The shared library must define a function (with external linkage) named sexp_init_library. The exact signature can be found in the docs, but its first argument is the context of type sexp.

Hope this helps.

Lukas

AB

unread,
Aug 16, 2021, 8:42:14 PM8/16/21
to chibi-scheme
That helps thanks. 
earlier I copied the ctx and env from sexp_init_library to global variables.
I had it running but would get segmentation faults or the program would just close.
right now I am trying to use a variable with sexp_env_define, it finishes with no errors,
but the next commands gives undefined variable exception.



Alex Shinn

unread,
Aug 16, 2021, 9:51:30 PM8/16/21
to chibi-scheme
Usually you want to use the FFI which will generate this for you.
ast.c is an example shared library written by hand without the FFI.

If you want to copy Scheme variables to globals in C, you should use
sexp_preserve_object to be sure they don't get gc'ed. But in general
this is advanced usage and should be done with caution - sexp_env_define
is safer.

--
Alex

AB

unread,
Aug 17, 2021, 6:20:22 PM8/17/21
to chibi-scheme
I got it working except I get an occasional segmentation fault right at startup. I used
FFI and tried to use sexp_env_define, but the c functions didn't see it.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages