4 ms·
There are several issues that I usually hit with bindings to C/C++ and other languages: - Calling a native function that itself takes callback, and that call
by malkia 10y ago
There are several issues that I usually hit with bindings to C/C++ and other languages:
- Calling a native function that itself takes callback, and that callback might be your non-native code. (trampolines?)
- Dealing with memory allocation. Who owns what.
- Exceptions or long_jmps
- Green threads, fibers, etc.
- Other?
- solipsism 10y agoCallbacks are well supported in SWIG for most languages.
- malkia 10y agoAlso: - int64 -> uint64 conversion - int64/uint64 -> double conversion
- dekhn 10y agocallbacks in swig are straightforward. you have a C/C++ reciver function that gets registered along with a pointer to the scripting language target function. I've used this design for over a decade.
- malkia 10y agoI was mentioning FFI in general, like Common Lisp's FFI, LuaJIT, others. For example for callback back, you need to have a trampoline, where the "C/C++" trampoline would call back your language runtime "engine". But this introduces gap in the "stack", and might not work with all VM's, or you may run out of trampoline slots, and not being able to allocate new, since this would require dynamic code generation, and say on iOS and certain other game consoles that's not allowed.
- dekhn 10y agothere are no trampoline slots required. it's a function pointer that's provided as an argument (user data). Can you point to an FFI that doesn't support callbacks in this way? This would be a major problen in the FFI implementation.
- ihnorton 10y agoLuaJIT limits both the number of registered callbacks [1], and re-entering the interpreter from a JIT'd callback [2] (it will try to detect and prevent JIT for such functions, but if that fails you get a panic). [1] http://stackoverflow.com/questions/31042530/why-does-luajit-produce-a-too-many-callbacks-error-from-this-simple-code http://stackoverflow.com/questions/31042530/why-does-luajit-... [2] http://stackoverflow.com/questions/25924755/c-and-lua-unprotected-error-bad-callback-how-is-this-possible http://stackoverflow.com/questions/25924755/c-and-lua-unprot...
- dekhn 10y agothat's a limitation of a specific FFI, which is apparently a deisgn choice motivated by limited memory systems.
- malkia 9y agoYes, sorry - I'm simply speaking as an user of a given language runtime connecting to "C" exported functions. I understand that given a different implementation this would've not be needed.