简体   繁体   中英

C++ & Lua - attempt to index a nil value error

I'm currently learning how to make Lua work with C++ and i stumbled upon this problem. Here is my little app that I'm currently using:

#include <lua.5.2.3\src\lua.hpp>
#include <iostream>
#include <string>

int pluacall(lua_State *L){
    std::cout << "Called from inside Lua." << std::endl;
    return 0;
}

int main(){
    std::string x;
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    luaL_Reg _funcs[] = { { "CppFunc", pluacall }, {} };

    if (luaL_dofile(L, "test.lua")){
        std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
        lua_pop(L, 1);
    }

    luaL_setfuncs(L, _funcs, 0);

    lua_getglobal(L, "sup");
    if (lua_pcall(L, 0, 0, 0)){
        std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
        lua_pop(L, 1);
    }


    std::cout << "Test";
    std::getline(std::cin, x);

    lua_close(L);
    return 0;
}

Everything worked flawlessly until I added the luaL_setfuncs(...) .

Now the app crashes with the following error:

PANIC: Unprotected error in call to Lua API (attempt to index a nil value)

I'm gonna be completely honest I have absolutely no idea why it doesn't work or what this error even means (google wasn't my friend today). Any ideas?

It's probably also worth mentioning that I do not link the Lua library with my app, I compile them together (all Lua source added to project)

Edit: here's the Lua script I'm testing it with

function sup()
    io.write("Hey.\n")
    CppFunc()
    return 1
end
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);

Registers all functions in the array l (see luaL_Reg) into the table on the top of the stack (below optional upvalues, see next).


void luaL_newlib (lua_State *L, const luaL_Reg *l);

Creates a new table and registers there the functions in list l. It is implemented as the following macro:

 (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))

Either insert luaL_newlibtable(L, _funcs) before luaL_setfuncs , or use luaL_newlib(L, _funcs) instead. If you want to register functions not into separate table, but into global namespace, you may use following approach:

lua_pushvalue(L, LUA_GLOBALSINDEX); // push _G table
luaL_setfuncs(L, _funcs, 0); // set funcs on _G
lua_pop(L, 1); // pop _G

Try explicitly setting the sentinel that terminates funcs array:

luaL_Reg _funcs[] = { { "CppFunc", pluacall }, {NULL, NULL} };

I don't think that the compiler gives them default 0 values (but I'm not 100%), so if you don't set them you could end up with garbage pointers hence crash.

EDIT after comment that the above does not help:

If this is not the issue, try using luaL_newlib instead of luaL_setfuncs: it appears to use luaL_newlibtable which creates a table on the stack then calls setfuncs to register functions in it:

luaL_newlib(L, _funcs);

(I'm surprised at how difficult it is to find clear docs/examples about this). See this very good answer to similar question on SO .

So I did not exactly solve this problem how I thought I would, however it's better than nothing.

I changed luaL_setfuncs() to lua_register() and everything seems to work now. What was the problem - i have no idea.

I thought luaL_setfuncs() was the preffered way of registering C functions with Lua now, well it doesn't work so I'll have to stick with registering everything through lua_register() .

If I get any more info on this issue I'll edit this post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM