When I compile the following code:
import strutils
type
Token {.exportc.} = object
typ: cint
val: cstring
proc exportc(a: Token) {.exportc.} =
let msg = "Token (type, value): ($1, $2)" % [$a.typ, $a.val]
echo msg
with
nim c --header exportc.nim
I get a header file with the following snippet:
typedef struct Token Token;
struct Token {
int typ;
NCSTRING val;
};
N_NOCONV(void, signalHandler)(int sign);
N_NIMCALL(NI, getRefcount)(void* p);
N_NIMCALL(void, exportc)(Token a);
N_CDECL(void, NimMain)(void);
which is what I expect and can then call from a c main function. However, if I change the object to a ref object
Token {.exportc.} = ref object
typ: cint
val: cstring
I get a header file with the following:
typedef struct tyObject_TokencolonObjectType___My6xn9bSiVikjAjVy7Y8xcA tyObject_TokencolonObjectType___My6xn9bSiVikjAjVy7Y8xcA;
N_NOCONV(void, signalHandler)(int sign);
N_NIMCALL(NI, getRefcount)(void* p);
N_NIMCALL(void, exportc)(tyObject_TokencolonObjectType___My6xn9bSiVikjAjVy7Y8xcA* a);
N_CDECL(void, NimMain)(void);
So for a ref object the exportc pragma doesn't stop name mangling which makes calling from c a pain. Is this a bug or is there something about reference objects I'm not comprehending.
Thanks,
Jason