Skip to content
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

Merged
merged 1 commit into from
Feb 6, 2016
Merged

Conversation

melted
Copy link
Contributor

@melted melted commented Oct 4, 2015

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.

@david-christiansen
Copy link
Contributor

Cool!

@melted melted force-pushed the callbacks branch 4 times, most recently from 14fc0cf to 9449860 Compare October 4, 2015 18:31
@Melvar
Copy link
Collaborator

Melvar commented Oct 4, 2015

What is the role of CVoid? Why do functions need to be wrapped in CFnPtr? Are you planning to add support to the executor as well?

@melted
Copy link
Contributor Author

melted commented Oct 4, 2015

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.

@melted
Copy link
Contributor Author

melted commented Oct 4, 2015

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.

@david-christiansen
Copy link
Contributor

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.

@Melvar
Copy link
Collaborator

Melvar commented Oct 24, 2015

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 ()
Copy link

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.

@Melvar
Copy link
Collaborator

Melvar commented Oct 24, 2015

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.

@melted
Copy link
Contributor Author

melted commented Feb 6, 2016

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.

@melted melted force-pushed the callbacks branch 2 times, most recently from 7ad65be to 3c179f6 Compare February 6, 2016 15:21
@melted
Copy link
Contributor Author

melted commented Feb 6, 2016

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.

melted added a commit that referenced this pull request Feb 6, 2016
Implement support for C callbacks
@melted melted merged commit 8d3cf2d into idris-lang:master Feb 6, 2016
@jfdm
Copy link
Contributor

jfdm commented Feb 7, 2016

Cool. However, two things:

  • The CHANGELOG needs updating.
  • It might be useful to take the ffi007 test and write a brief tutorial/guide on how to get the callbacks working.
  • It might also be useful to add some text to the reference manual.

@melted
Copy link
Contributor Author

melted commented Feb 7, 2016

Yes, those are good points. I'm working on documentation.

----- Ursprungligt meddelande -----
Från: "Jan de Muijnck-Hughes" notifications@github.com
Skickat: ‎2016-‎02-‎07 11:11
Till: "idris-lang/Idris-dev" Idris-dev@noreply.github.com
Kopia: "Niklas Larsson" metaniklas@gmail.com
Ämne: Re: [Idris-dev] Implement support for C callbacks (#2695)

Cool. However, two things:
The CHANGELOG needs updating.
It might be useful to take the ffi007 test and write a brief tutorial/guide on how to get the callbacks working.
It might also be useful to add some text to the reference manual.

Reply to this email directly or view it on GitHub.

@melted melted deleted the callbacks branch February 8, 2016 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants