lua-users home
lua-l archive

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


On 22 November 2013 18:33, 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 [...]
>
> So, this works. My question:
>
> Is there a better way with the way the C API? Or is the answer, "Welcome to C!"

If you're not supporting Lua 5.1 (assumed from your use of lua_len),
you can remove luaL_returnlen and change luaL_rawsetnexti to:

  static void rawsetnexti(L, index) {
    lua_rawseti(L, index, lua_rawlen(L, index) + 1);
  }

If you do need to support 5.1 at some point, you can use this for
compatibility purposes (with a warning that it's only equivalent in
*some* cases, not all):

#if LUA_VERSION_NUM < 502
#define lua_rawlen lua_objlen
#endif

Also, you should definitely avoid prefixing your own functions with
lua_/luaL_. Those namespaces are "owned" by Lua itself. If you declare
your functions "static", you don't really need any prefix, since
they're then local to the translation unit (C file).