lua-users home
lua-l archive

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


Hi all,

What is the environment of a Lua function when called from C via lua_call or lua_pcall? I'm writing a C++ app that occasionally calls functions on the Lua side, and I hit a wall when I realized that these functions don't seem to have any of the standard Lua functions defined in their environments. The global environment seems to be missing. Nothing in my code calls setfenv or lua_setfenv.

Example from my code:

The calling C++ snippet:

...
    lua_getglobal(_lua, "OnNewMessage");

    // ... two arguments pushed here...

    int status = lua_pcall(_lua, 2, 0, 0);
...

The Lua function (which trips up when called from C, because "pairs" turns out to be nil):

_OnNewMessage_ = function(someargs, moreargs)
    for key, val in pairs(myapp.sometable) do
        val(someargs, moreargs)
    end
end

-> config/environment.lua:18: attempt to call global 'pairs' (a nil value)

Can the environment be clobbered as a side-effect of improper stack management, or anything like that? If anyone can shed some light, I'd be much obliged. Haven't found anything in the manual addressing this issue.

Thanks,

Evan D.