[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua C-API - create two tables parallel
- From: Andrew Gierth <andrew@...>
- Date: Sat, 07 Oct 2023 14:36:09 +0100
>>>>> "Ervin" == Ervin Hegedüs <airween@gmail.com> writes:
Ervin> As you can see I iterate through the list twice, because I don't
Ervin> know how can I handle in two tables parallel.
I don't see why you think this is a problem - you can easily have
multiple tables on the stack.
untested:
lua_newtable(L); // lineerrors
lua_newtable(L); // lineerrorspos
l.lineerrpool.currptr = l.lineerrpool.pool;
for (int c = 0; c < l.lineerrcnt; ++c)
{
read_mylog_err(&l.lineerrpool, &logerr);
// stack: lineerrors \ lineerrorspos
lua_pushstring(L, logerr.errmsg);
// stack: lineerrors \ lineerrorspos \ errmsg
lua_seti(L, -3, c);
lua_newtable(L);
// stack: lineerrors \ lineerrorspos \ subtable
lua_pushinteger(L, *logerr.startpos);
lua_seti(L, -2, 1);
lua_pushinteger(L, *logerr.endpos);
lua_seti(L, -2, 1);
// stack: lineerrors \ lineerrorspos \ subtable
lua_seti(L, -2, c);
}
// stack: lineerrors \ lineerrorspos
lua_setfield(L, maintable_idx, "lineerrorspos");
lua_setfield(L, maintable_idx, "lineerrors");
If you're stuck on Lua 5.1, then you can easily define lua_seti yourself
(in terms of lua_settable and lua_insert), or rewrite as
lua_pushinteger(L, c);
lua_pushstring(L, logerr.errmsg);
// stack: lineerrors \ lineerrorspos \ c \ errmsg
lua_settable(L, -4);
etc.
--
Andrew.