Hello, I've got a tricky question : Is Nim able to reliably bind function defined as macro in C code or am I wasting my time ?
I'm talking stuff like :
extern void coolFuncDontLookAtExplosion(void *);
#define coolFuncDontLookAtExplosion(arg1)
\ // define your super macro function because they are totally cool and not at all unreliable
\ //Do stuff ...
My Nim code looks like this :
proc coolFuncDontLookAtExplosion(arg1: pointer) {.cdecl, importc, inline.}
I've already spent some times debugging before figuring out that I needed to inline the proc. But is that enough to be reliable ? Is there is more pragma / precaution I should take when binding C function macros ?
There is nothing particularly different when it comes to binding C functions vs macros in nim - in the end it is just a codegen.
Though as you mentioned in your comment this can be extremely unreliable due to codegen and C macros in the same place - stray comma can completely mess up everything.
{.emit: """
#define hell(arg) puts(arg)
""".}
proc hell(arg: cstring) {.importc: "hell", header: "stdio.h".}
let str = "123123"
hell(str.cstring)
PS: I also tried using .importcpp. (because it has more feature-full syntax for describing codegen patterns), but it seems like this is not possible (linker error).
use nodecl not cdecl so that no definition is produced.
I'll test it out.
There is nothing particularly different when it comes to binding C functions vs macros in nim - in the end it is just a codegen.
Perhaps I need to explain a bit more :). I'm making a Nim wrapper to the Julia programming language, so I have the pleasure of dealing their GCs when passing Julia value around in Nim.
Now the way their C API works for rooting value in the GCs is that it's a macro function that needs to stay in the same scope (because it uses the stack pointers at some point I think ? - I'm not sure exactly). My bindings without inline were causing Julia's GC corruption because it wasn't inline'd. That wasn't exactly fun to debug so I'm looking to see if there's more "catch" like these that could potentially make the bindings unstable.