lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This is a great tool and will help plenty, as I am just learning Lua C API. I think however there is an issue with the following:

In:
print(math.pi)

Out:
static int lua_openmodule(lua_State* L) {
    lua_getglobal(L, "print");
    lua_getglobal(L, "math");
    lua_getfield(L, 1, "pi");
    lua_call(L, 1, 0);
    return 0;
}


The above appears to fail on 2 points:
1) lua_getfield(L, 1, "pi"); should be: lua_getfield(L, -1, "pi"); as we don't nesc. know what is 1st on Stack
2) There is a need to remove "math" from the Stack. i.e. lua_remove(L, -2); so the lua_call will act on func: "print" and val of: "pi"

Working:
static int lua_openmodule(lua_State* L) {
    lua_getglobal(L, "print");
    lua_getglobal(L, "math");
    lua_getfield(L, -1, "pi");
    lua_remove(L, -2);
    lua_call(L, 1, 0);
    return 0;
}

--
Sam