When working with the Lua C API, it is important to establish convenient work with the virtual stack - all important language boundary interactions occur there. If you look at the code snippet, it doesnβt look like you are correctly translating the data into C.
When writing a lua C function, you basically have to do 3 things:
- Convert lua input to something you can work with in C.
- Do the processing or something else that the function should perform.
- Converts and returns the result if it returns to lua.
As an example, here is what your findImgProxy should look like:
static int findImgProxy(lua_State *L) { // discard any extra arguments passed in lua_settop(L, 1); luaL_checktype(L, 1, LUA_TTABLE); // Now to get the data out of the table // 'unpack' the table by putting the values onto // the stack first. Then convert those stack values // into an appropriate C type. lua_getfield(L, 1, "imagePath"); lua_getfield(L, 1, "fuzzy"); lua_getfield(L, 1, "ignoreColor"); // stack now has following: // 1 = {imagePath="/var/image.png", fuzzy=0.5, ignoreColor=0xffffff} // -3 = "/var/image.png" // -2 = 0.5 // -1 = 0xffffff const char *imagePath = luaL_checkstring(L, -3); double fuzzy = luaL_checknumber(L, -2); int ignoreColor = luaL_checkint(L, -1); // we can pop fuzzy and ignoreColor off the stack // since we got them by value lua_pop(L, 2); // do function processing // ... return 1; }
Note that we must store imagePath on the stack since we are holding const char * . Turning this line off will invalidate *imagePath , since lua can compile it.
Alternatively, you can copy the string returned by luaL_checkstring to another buffer. Turning off the string in this case is performed, since we no longer point to the internal buffer belonging to lua.
Edit: If some of the keys in the table are optional, you can use the luaL_opt* functions and provide default values. For example, if fuzzy and ignoreColor are optional:
// ... const char *imagePath = luaL_checkstring(L, -3); double fuzzy = luaL_optnumber(L, -2, 0.0); // defaults to 0.0 if no fuzzy int ignoreColor = luaL_optint(L, -1, 0); // defaults to 0 if no ignoreColor // ...
So, if the calling code provides a meaningless value for the key, it will still throw an error. OTOH, if it is absent, then the value is nil and is used by default.