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

2

6

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???

Barolet answered 4/12, 2013 at 16:8 Comment(1)
The print function isn't loaded. luaL_openlibs should load it.Waldron
T
4

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.

Tutti answered 4/12, 2013 at 16:32 Comment(1)
Thankyou, I now have it working, having modified my c++ as you suggested. I now understand that for a function from a chunk to be registered, it must be 'run' not merely 'loaded' in Lua terminology. Thankyou for your answer!Barolet
S
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);
Stome answered 3/7, 2015 at 2:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.