cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.95k stars 982 forks source link

Expose an ABI-friendly embedding interface #653

Open lambdadog opened 2 years ago

lambdadog commented 2 years ago

While it's possible to embed Chez Scheme using the generated scheme.h headers, there are problems with this since a significant portion of the interface is increasingly opaque pointer arithmetic macros. If someone wanted to embed the Chez kernel in an application written in a non-C/C++ language they would either have to translate the macros or write some C glue that wraps scheme.h's macros.

This is of course doable, but there are a number of issues with requiring this:

Ideally, instead of jumping through all these hoops, the application could simply load the Chez kernel dynamic library and embed Chez via an ABI-friendly API.

lambdadog commented 2 years ago

I didn't give much lip service to translating the macros, but this is mostly because I've been trying to do this for the last two weeks in a platform independent way and I find myself chasing obscure and confusing errors constantly -- while it may be possible for someone with enough familiarity with the in-memory layout of scheme values (something difficult to gain without documentation: #654) it shouldn't be the de-facto path to embedding with its barrier to entry.

If bindings were written in this way, of course, it would be nice to have the lower level of indirection, but I can't see it as a starting point.

porglezomp commented 4 months ago

I've been working on embedding this (as part of embedding racket) and have been approaching it by just wrapping every macro in a function, with the same name, e.g.:

#define Sfixnump(x) (((uptr)(x)&0x7)==0x0)
bool (Sfixnump) (ptr x) { return Sfixnump(x); }

which resolves the linkage issue for me but I imagine does very little for portability. But sharing this here for anyone else trying to do this—it took me a bit to figure out that I could reuse the same name.