lua-users home
lua-l archive

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


On Nov 22, 2013, at 10:33 AM, Andrew Starks <andrew.starks@trms.com> wrote:

> I've looked around for this and can't find it. It's not a big deal,
> but I find myself wanting to do
> 
> t[#t + 1] = v
> 
> and in C, that's actually a lot of code. Right now, I'm inlining a
> function like this:
> int luaL_returnlen(L, index) {
>  lua_len(L, index);
>  len = lua_tointeger(L,-1);
>  lua_pop(L, 1);
>  return len;
> }
> 
> so that I can inline a function like this:
> 
> void luaL_rawsetnexti(L, index){
>  lua_rawseti(L, index,luaL_returnlen(L, index) + 1);
> }
> 
> -------
> 
> So, this works. My question:
> 
> Is there a better way with the way the C API? Or is the answer, "Welcome to C!”
> 

Is there some reason you are doing rawset? Also, what about keeping everything in the stack and using lua_arith() to do the add? So all you would need to do is:

lua_len(…)
lua_pushinteger(…, 1);
lua_arith(LUA_OPADD);
lua_settable(…)

Which does the whole thing in 4 lines.

—Tim