2
votes

In our menu system, we define menus in xml with lua chunks used for callbacks for menu component events. Currently, everytime a script callback is called, we call lua_loadstring which is quite slow. I'm trying to make it so this only happens once, when the menu is loaded.

My initial thought was to maintain a lua table per menu component and to do the following to add a new callback function to the table:

//create lua code that will assign a function to our table
std::string callback = "temp." + callbackName + " = function (" + params + ")" + luaCode + "end";

//push table onto stack
lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);

//pop table from stack and set it as value of global "temp"
lua_setglobal(L, "temp");

//push new function onto stack
int error = luaL_loadstring(L, callback.c_str());
if ( error )
{   
    const char* errorMsg = lua_tostring(L, -1);
    Dbg::Printf("error loading the script '%s' : %s\n", callbackName, errorMsg);
    lua_pop(L,1);
    return;
}

//call the lua code to insert the loaded function into the global temp table
if (lua_pcall(L, 0, 0, 0)) 
{
    Dbg::Printf("luascript: error running the script '%s'\n", lua_tostring(L, -1));
    lua_pop(L, 1);
}

//table now has function in it

This seems kind of dirty. Is there a better way that allows me to assign the function to the table directly from a lua chunk without having to use a temp global variable and running lua_pcall?

1

1 Answers

3
votes

If you want to put the function in a table, then put the function in the table. It seems that your Lua-stack-fu is not strong; consider studying the manual a bit more closely.

Anyway, I'd say the biggest problem you have is your insistence on params. Callback functions should expect to be varadic; they take ... as their parameters. If they want to get those values, they should use locals like this:

local param1, param2 = ...;

But if you insist on allowing them to specify a list of parameters, you may do the following:

std::string luaChunk =
    //The ; is here instead of a \n so that the line numbering
    //won't be broken by the addition of this code.
    "local " + params + " = ...; " +
    luaCode;

lua_checkstack(L, 3);
lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);
if(lua_isnil(L, -1))
{
    //Create the table if it doesn't already exist.
    lua_newtable(L);

    //Put it in the registry.
    lua_rawseti(L, LUA_REGISTRYINDEX, luaTableRef_);

    //Get it back, since setting it popped it.
    lua_rawgeti(L, LUA_REGISTRYINDEX, luaTableRef_);
}

//The table is on the stack. Now put the key on the stack.
lua_pushlstring(L, callbackName.c_str(), callbackName.size());

//Load up our function.
int error = luaL_loadbuffer(L, luaChunk.c_str(), luaChunk.size(),
    callbackName.c_str());
if( error )
{   
    const char* errorMsg = lua_tostring(L, -1);
    Dbg::Printf("error loading the script '%s' : %s\n", callbackName, errorMsg);
    //Pop the function name and the table.
    lua_pop(L, 2);
    return;
}

//Put the function in the table.
lua_settable(L, -3);

//Remove the table from the stack.
lua_pop(L, 1);