-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for exporting statically compiled libraries from C #856
Conversation
76caed0
to
e54ff39
Compare
As far as I can see, the failure in the integration tests is not due to my changes. I tried replacing them with a single whitespace change, so that the code was identical to Other than that, the changes work as expected, at least on my setup, but I think that we should probably add a function, e.g. |
e54ff39
to
c264e11
Compare
Well, it seems I no longer need this. I realized that it'd probably be best to statically link Chibi-Scheme, so that I now have to bake in the system C libraries anyway. I then simply get the table of "static libraries" from my side and copy it into a new table, adding my own libraries. This seems to work fine, but it is still something of hack, in that it is not explicitly supported. It would probably not be too hard to add a function to Chibi to "register" a set of user-supplied libraries. Since Chibi is meant to be ebeddable, this seems like a desirable feature. The biggest change that would be needed, as far as I can see, would be to change the Let me know if you'd like me to try and put together a PR along those lines. |
Thanks! That's entirely up to you. I'm afraid I don't have time to help right now, but patches are always welcome. |
c264e11
to
0649ec3
Compare
0649ec3
to
e88374a
Compare
473af92
to
61590d8
Compare
I've updated the patch with an implementation that, as far as I can see, is the simplest in terms of needed changes, while still covering all use cases. As before, the feature is based on the machinery already in place to support statically compiled C libraries. This is essentially unaltered and works as before. When it is enabled, the table of exported C libraries is initialized via clibs.c as before. The only change is that now, more functions can be added via the new Since one might possibly want to export C libraries, without having any statically baked into Chibi, an additional configuration macro has been defined, To use the feature, one would do something like: static sexp add(sexp ctx, sexp self, sexp_sint_t n, sexp s, sexp t)
{
return sexp_add(ctx, s, t);
}
sexp init_foo(
sexp ctx, sexp self, sexp_sint_t n, sexp env,
const char *version, const sexp_abi_identifier_t abi) {
sexp_define_foreign_proc_rest(ctx, env, "add", 2, add);
return SEXP_VOID;
}
int main(int argc, char** argv) {
sexp ctx;
static struct sexp_library_entry_t entries[] = {
{"foo", init_foo},
{NULL, NULL}
};
sexp_scheme_init();
sexp_add_static_libraries(entries);
...
} One can then either By the way, note that, contrary to the documentation, both in comments in |
for (table = sexp_static_libraries; ; | ||
table = (struct sexp_library_entry_t*)entry->init) { | ||
for (entry = &table[0]; entry->name; entry++); | ||
if (!entry->init) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you fix the indentation here, and either use braces for both for's or neither?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed that by "indentation" you meant the 4 spaces inadvertently used inside the if block. If you'd like me to fix something else, let me know. As far as the for statements are concerned, note that I cannot use no braces for the first (as it contains two statements) and also that the second is empty; it merely advances the entry
pointer to the end of the list. Do you want me to add an empty pair of braces to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, actually I misread this, I thought the if was inside the for block. It's better the be clear and put the semi-colon on a separate line:
for (entry = &table[0]; entry->name; entry++)
;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -1680,6 +1680,8 @@ sexp sexp_finalize_dl (sexp ctx, sexp self, sexp_sint_t n, sexp dl); | |||
#define sexp_current_source_param | |||
#endif | |||
|
|||
SEXP_API void sexp_add_static_libraries(struct sexp_library_entry_t* libraries); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment that libraries must outlive all Chibi uses?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also added a few words to explain its use, as it will be tricky for prospective users to figure it out by inspecting the code. Let me know if this is not desirable.
61590d8
to
19141ea
Compare
This uses the existing mechanism for statically compiled C libraries, to allow the user to export their own C libraries in a similar way. User exported libraries can be added on top of statically compiled C libraries or exist on their own (by setting SEXP_USE_STATIC_LIBS_EMPTY).
19141ea
to
310a04f
Compare
This PR trivially extends the existing mechanism for statically compiled libraries to allow the user to export custom libraries from C. I currently use it to export a relatively complex API from the C side as an importable library in scheme (mainly for the benefit of allowing organization into sublibraries, selective import of symbols, renaming symbols, etc.) Use goes something like this:
From the C side:
Then somewhere in the initialization function for chibi-scheme, I add:
Then from Scheme, somewhere in, say
./foo/bar.sld
:Then in the main program one can:
As it is, the design is quite elementary and doesn't make any attempt to cover up its co-opting of the statically included shared library mechanism. As such, the user is forced to bother with mimicking shared library import paths and extensions. This is not a big bother really and it is usable just fine as is, but if you prefer, we could look into further streamlining the interface.
Edit: This started off leaving the the
sexp_static_libraries
symbol undefined and simply defining it in the main program. This turned out not to be supported on Windows though. I then tried initializingsexp_static_libraries
to an empty table, but marking it__attribute__((weak)))
, but this doesn't work on MSVC. This third attempt also fails in some cases, although I'm not sure why. I'll look further into it, but if you have any comments, let me know.