Lua 5.2 issue: 'attempt to call a nil value' from lua_pcall

丶灬走出姿态 提交于 2020-01-02 03:17:34

问题


I'm having problems getting a Lua 5.2 function to get called from C++.

This is the Lua chunk (named test.lua):

function testFunction ()
print "Hello World"
end

And this is the C++:

int iErr = 0;

//Create a lua state
lua_State *lua = luaL_newstate();

// Load io library
luaopen_io (lua);

//load the chunk we want to execute (test.lua)
iErr = luaL_loadfile(lua, "test.lua");
if (iErr == 0) {
    printf("successfully loaded test.lua\n");

    // Push the function name onto the stack
    lua_getglobal(lua, "testFunction");
    printf("called lua_getglobal. lua stack height is now %d\n", lua_gettop(lua));

    //Call our function
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

} else {
    printf("Error loading test.lua. Error code: %s\n", lua_tostring(lua, -1));        
}
lua_close (lua);

When I trace, I see that it loads the test.lua script fine (no error is returned), it then shows that the stack height is 3 after calling the lua_getglobal with the function name.

However, it fails at lua_pcall with the error code 2: 'attempt to call a nil value'.

I've read plenty of examples of Lua 5.2 code, and can't seem to see where I'm going wrong. This looks like it should definitely work (according to what I've read).

I've checked spelling and case sensitivity, and it all matches up.

Have I misunderstood something???


回答1:


luaL_loadfile just loads the file, it does not run it. Try luaL_dofile instead.

You'll still get an error because print is defined in the base library, not in the io library. So call luaopen_base instead.




回答2:


You need call "priming lua_pacll()" before lua_getglobal(). Please refer Calling Lua From a C Program. The whole code should like this:

int iErr = 0;

//Create a lua state
lua_State *lua = luaL_newstate();

// Load base library
luaopen_base (lua);

//load the chunk we want to execute (test.lua)
iErr = luaL_loadfile(lua, "test.lua");
if (iErr == 0) {
    printf("successfully loaded test.lua\n");

    //Call priming lua_pcall
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

    // Push the function name onto the stack
    lua_getglobal(lua, "testFunction");
    printf("called lua_getglobal. lua stack height is now %d\n", lua_gettop(lua));

    //Call our function
    iErr = lua_pcall(lua, 0, 0, 0);
    if (iErr != 0) {
        printf("Error code %i attempting to call function: '%s'\n", iErr, lua_tostring(lua, -1));
    }

} else {
    printf("Error loading test.lua. Error code: %s\n", lua_tostring(lua, -1));        
}
lua_close (lua);


来源:https://stackoverflow.com/questions/20380232/lua-5-2-issue-attempt-to-call-a-nil-value-from-lua-pcall

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!