Lua passing handle to function created with newlib

Viewed 659

I'm using luaL_newlib(luaState, afbFunction) where afbFunction is a static luaL_Reg array. Unfortunately, luaL_Reg only support two fields: name and func. As a result, there is no way to pass a handle to a function and implement something like:

static const luaL_Reg afbFunction[] = {
    {"notice" , LuaPrintMsg, MSG_NOTICE},
    {"info"   , LuaPrintMsg, MSG_INFO},
    {"warning", LuaPrintWar, MSG_WARN},
    {NULL, NULL}  /* sentinel */
};

I have two motivations for not having as many C entry points as there are Lua functions:

  1. I have a generic wrapper for all C functions to process input/output parameters value from Lua (table) to C (jsonc).
  2. The number of Lua functions to interface come from a config file and may change without having to rebuild the code. To make a long story short, the config file has a sharelib path followed by the list of function to expose.

Does someone have a solution to have a unique C entry point for multiple Lua functions? When using luaL_newlib(), the C function receives as 2nd argument the lib table, but it seems to receive the full table and not the corresponding entry, also even testing the label is not possible.

2 Answers
Related