Hello. I have that code:
typedef struct st_h2o_handler_t {
size_t _config_slot;
void (*on_context_init)(struct st_h2o_handler_t *self, h2o_context_t *ctx);
void (*on_context_dispose)(struct st_h2o_handler_t *self, h2o_context_t *ctx);
void (*dispose)(struct st_h2o_handler_t *self);
int (*on_req)(struct st_h2o_handler_t *self, h2o_req_t *req);
} h2o_handler_t;
After convertion to Nim there is:
type
h2o_handler_t* = object
_config_slot*: csize
on_context_init*: proc (self: ptr st_h2o_handler_t; ctx: ptr h2o_context_t)
on_context_dispose*: proc (self: ptr st_h2o_handler_t; ctx: ptr h2o_context_t)
dispose*: proc (self: ptr st_h2o_handler_t)
on_req*: proc (self: ptr st_h2o_handler_t; req: ptr h2o_req_t): cint
Nim gives error:
Error: invalid token: _
What is a way to change _config_slot so that it would be correct Nim code and back compatible with C code.
And second:
Is converted code correct? I mean functions inside typedef struct st_h2o_handler_t (or it is pointers to functions?)?
What is a way in Nim to handle C pointers to functions?
Nim don't like underscore leading for identifier, change _config_slot to config_slot or other name. Nim also don't care the name/identifier as long as the type is compatible. configSlot is also ok.
pointer to function in Nim has several calling convention, such as nimcall, closure, cdecl, stdcall, etc.
it depends on your compiler/library calling convention. but usually C has cdecl calling convention(or stdcall for Windows API)
just add pragma {.cdecl.} to your function pointer, for example:
type
abc* {.pure, final.} = object
dispose*: proc (self: ptr abc) {.cdecl.}
{.pure.} pragma will strip unnecassary runtime info and translate it into plain C struct
{.final.} -> non inheritable