Calling Koka functions from C #787
-
I have seen several questions on using Koka's C ffi (e.g. here and in some issues) and I am currently reading through examples of it in the standard library. However, I have a reverse question: How do I properly call Koka's functions in a C project? What parts of the codegen are kind of "set in stone", what parts might change in the future, how to deal with the context properly, where do I actually find information on how context works, etc. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Name generation might change to address these issues #586, #763, but likely in a reasonable way. The reference counting, data layout, etc, are stable as far as I know. To call a Koka function you just have to pass it the arguments of the right type, and the context. Line 428 in 0554632
Because the context has to do with effect handler machinery, you need to ensure that the effects that the function you are calling are the same as the ones expected from the function that called into C (assuming no Koka code was run in-between). If you need anything more complex, you'll likely have to deal with the effect handler runtime, (https://github.com/koka-lang/koka/blob/dev/lib/std/core/hnd.kk, https://github.com/koka-lang/koka/tree/dev/lib/std/core/inline/hnd.h). To really understand this you probably would want to read https://www.microsoft.com/en-us/research/publication/generalized-evidence-passing-for-effect-handlers-or-efficient-compilation-of-effect-handlers-to-c/ |
Beta Was this translation helpful? Give feedback.
Name generation might change to address these issues #586, #763, but likely in a reasonable way. The reference counting, data layout, etc, are stable as far as I know.
To call a Koka function you just have to pass it the arguments of the right type, and the context.
Here is the documentation on
kk_context_t
.koka/kklib/include/kklib.h
Line 428 in 0554632
kk_get_context()
allows you to get the thread-local context without an explicitly passed reference to it. I believe the passing ofkk_context_t _ctx
is to force the compiler to always keep it in a register.Because the context has to do with effect handler machinery, you need to ensure that …