lua-users home
lua-l archive

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


> What exactly does lua_load() push onto the stack for popping
> at execution?  ie. Since I'm passing 0 'nargs' to lua_pcall()
> I presume that I don't have to push any arguments onto the
> stack, so what is lua_pcall() requiring and how can I manually
> derive this magic value myself so that I may push it onto the
> stack every time I wish to invoke a particular chunk?


Basically lua_load() pushes a function object representing the chunk of code
that is loaded from disk.

Probably the best thing is to store this in either a global variable, or
possibly better still in the Lua Registry (that way it is a lot less likely
any code can kill your chunk by setting a global variable).  You can then
retrieve it and call it whenever you like.

Something like:

    lua_pushstring(L, "MyChunkFunc");
    lua_load (L, ...);
    lua_settable(L, LUA_REGISTRYINDEX);

Then when you wish to call it:

    lua_pushstring(L, "MyChunkFunc");
    lua_gettable(L, LUA_REGISTRYINDEX);
    lua_pcall(L, 0, LUA_MULTRET, 0);


To use a global, simply store and retrieve it using LUA_GLOBALSINDEX
instead.


Note that specifying LUA_MULTRET means the stack will contain any number of
values returned from the chunk; if you do not need this, and don't want to
deal with cleaning up the stack afterwards, you can pass 0 for this arg.


Love, Light and Peace,
- Peter Loveday
Director of Development, eyeon Software