-
Notifications
You must be signed in to change notification settings - Fork 642
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
Implement support for C callbacks #2695
Conversation
Cool! |
14fc0cf
to
9449860
Compare
What is the role of |
CVoid is there because a function taking just () as an argument will be reduced, so there's nothing to wrap in that case. So we need it to create wrappers taking nothing. CFnPtr and those parts are modeled on the Javascript FFI for function arguments. |
As for the executor, that should be possible using libffi, ffi_prep_closure can be used to build them at runtime, and then only one target function that can sort out the arguments and execute the idris is needed, data can also be passed on the side via a pointer for user data. The only problem is that isn't included in the Haskell bindings, which really only supports calling functions, it's unmaintained and has had no releases in six years so that likely won't change. |
We could call libffi using Haskell's C FFI directly if we had to, or we could update the bindings and take over maintenance of them. It would be nice to keep the executor and the C FFI as close as possible, though maybe we should also just bite the bullet and split them. There's already a couple of differences regarding handling of pointers, after all, and this can be highly surprising. |
I am beginning to put together a sort of picture how the implementation in the executor could work. |
getInt : Int -> Int | ||
getInt _ = 8 | ||
|
||
test : IO () |
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.
Lets named that one test1
for consistency with test2
etc.
Say, instead of taking CVoid, couldn’t one take a universally quantified type? That ensures its behavior can’t depend on the argument, but doesn’t feel as troublesome as a function taking an empty type actually being executed. |
I'm going to revisit this now. I wonder if it would be a great hardship to require that the generated C program is linked against libffi. Then we could use that for the wrappers. |
7ad65be
to
3c179f6
Compare
I have removed CVoid as it isn't needed. Sending IO functions still doesn't work, the equivalent of unsafePerformIO needs to be done and it's too late to add unsafePerformIO to the program when we're in codegen. I have added a helpful error message that mentions unsafePerformIO. |
Implement support for C callbacks
Cool. However, two things:
|
Yes, those are good points. I'm working on documentation. ----- Ursprungligt meddelande ----- Cool. However, two things: |
This adds support for wrapping up Idris functions so they can be passed to C as callbacks.
It also has functionality to get the function pointer in case they need to be stuffed into structures.
Functions in IO doesn't work yet. That will need to be fixed before it can be merged.