From 1da9c4bdbb5ffdaf83af482af031fa97ed47eb15 Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Sat, 24 Jul 2021 00:17:33 +0800 Subject: [PATCH] lib: Use negative indices and add comments Signed-off-by: Donald Lee --- lib/frrscript.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index 3f2544f4a0..8fe8a93f05 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -208,10 +208,14 @@ void *frrscript_get_result(struct frrscript *fs, const char *function_name, if (lfs == NULL) return NULL; - /* results table is idx 1 on the stack, getfield pushes our item to idx - * 2 + /* At this point, the Lua state should have only the returned table. + * We will then search the table for the key/value we're interested in. + * Then if the value is present (i.e. non-nil), call the lua_to* + * decoder. */ - lua_getfield(lfs->L, 1, name); + assert(lua_gettop(lfs->L) == 1); + assert(lua_istable(lfs->L, -1) == 1); + lua_getfield(lfs->L, -1, name); if (lua_isnil(lfs->L, -1)) { lua_pop(lfs->L, 1); zlog_warn( @@ -221,6 +225,11 @@ void *frrscript_get_result(struct frrscript *fs, const char *function_name, } p = lua_to(lfs->L, 2); + /* At the end, the Lua state should be same as it was at the start + * i.e. containing soley the returned table. */ + assert(lua_gettop(lfs->L) == 1); + assert(lua_istable(lfs->L, -1) == 1); + return p; } -- 2.39.5