Add segment in readme about anonymous functions as callbacks

This commit is contained in:
Joshua Suskalo 2022-07-16 19:26:27 -05:00
parent e9a5b640ac
commit c9f30a361c
No known key found for this signature in database
GPG key ID: 9B6BA586EFF1B9F0

View file

@ -239,6 +239,13 @@ type and message in the registers section, but it's important to be aware of all
the same. Ideally you should test your callbacks before actually passing them to the same. Ideally you should test your callbacks before actually passing them to
native code. native code.
Another important thing to keep in mind is the expected lifetime of the function
that you pass to native code. For example it is perfectly fine to pass an
anonymous function to a native function if the callback will never be called
again once the native function returns. If however it saves the callback for
later use the JVM may collect it prematurely, causing a crash when the callback
is later called by native code.
### Variadic Functions ### Variadic Functions
Some native functions can take any number of arguments, and in these cases coffi Some native functions can take any number of arguments, and in these cases coffi
provides `vacfn-factory` (for "varargs C function factory"). provides `vacfn-factory` (for "varargs C function factory").