lua-users home
lua-l archive

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


On Thu, 13 Feb 2020 at 00:27, Sean Conner <sean@conman.org> wrote:
It was thus said that the Great Lee Shallis once stated:
>
> Never mind I figured it out, wasn't pushing the table as I thought I had
> been, new code:

  Some commentary on the code:

> int lua_proc_locate_name( lua_State *L ) {
>   int ret = EXIT_SUCCESS, underId = 0;
>   char const *name;
>   nodes_t nodes = {0};
>   proc_notice_t *notice;
>   node_t i;
>
>   if ( lua_isinteger(L,2) || lua_isnumber(L,2) )
>     underId = lua_tonumber(L,2);
>
>   if ( !lua_isstring(L,1) ) {
>     lua_error_cb( L, "Invalid name" );
>     return 0;
>   }
>   name = lua_tostring(L,1);

  You can replace the above with:

        name    = luaL_checkstring(L,1);
        underId = luaL_optinteger(L,2,0);

  luaL_checkstring() and luaL_optinteger() will do the appropriate error
checking for you.

>   notice = proc_locate_name( &ret, name, &nodes, underId );
>   lua_newtable( L );

  You might want to change the above line to:

        lua_createtable(L,nodes.count,0);

  This will size the array portion and might be a bit faster.

>   for ( i = 0; i < nodes.count; ++i ) {
>     lua_pushinteger(L,i+1);
>     lua_newtable( L );

  And here, you might want:

        lua_createtable(L,0,5);

since you are creating a table with five fields.  Again, might be a bit
faster.

>     push_branch_bool( L, "self", notice->self );
>     push_branch_int( L, "entryId", notice->entryId );
>     push_branch_int( L, "ownerId", notice->ownerId );
>     push_branch_str( L, "name", (char*)(notice->name.block) );
>     push_branch_str( L, "cmdl", (char*)(notice->cmdl.block) );
>     lua_settable(L,-3);
>     proc_notice_zero( notice );
>     ++notice;
>   }
>   free_nodes( proc_notice_t, &ret, &nodes );
>   return 1;
> }

  -spc
Awesome, thanks!