diff options
| author | Donald Lee <dlqs@gmx.com> | 2021-07-04 23:08:18 +0800 |
|---|---|---|
| committer | Donald Lee <dlqs@gmx.com> | 2021-07-18 06:32:03 +0800 |
| commit | 40d038d2a1c8329c408c4fffd88e674413dfd427 (patch) | |
| tree | ec28b87470d1fd460f567fd367ddaf52e1f4d05c /lib/frrscript.c | |
| parent | f0cddf950f928b6f9a8d56c688235cea728fda41 (diff) | |
lib: Change frrscript_call to call function instead
There is some rather heavy error checking logic in frrscript_call. Normally
I'd put this in the _frrscript_call function, but the error checking needs
to happen before the encoders/decoders in the macro are called.
The error checking looks messy but its really just nested ternary
expressions insite a larger statement expression.
Signed-off-by: Donald Lee <dlqs@gmx.com>
Diffstat (limited to 'lib/frrscript.c')
| -rw-r--r-- | lib/frrscript.c | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/lib/frrscript.c b/lib/frrscript.c index ed9043d8d2..8a93d36da1 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -138,41 +138,57 @@ static void lua_function_free(struct lua_function_state *lfs) /* Generic script APIs */ -int _frrscript_call(struct frrscript *fs) +int _frrscript_call_lua(struct lua_function_state *lfs, int nargs) { - int ret = lua_pcall(fs->L, 0, 0, 0); + int ret; + ret = lua_pcall(lfs->L, nargs, 1, 0); switch (ret) { case LUA_OK: break; case LUA_ERRRUN: - zlog_err("Script '%s' runtime error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : runtime error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; case LUA_ERRMEM: - zlog_err("Script '%s' memory error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : memory error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; case LUA_ERRERR: - zlog_err("Script '%s' error handler error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : error handler error: %s", + lfs->name, lua_tostring(lfs->L, -1)); break; case LUA_ERRGCMM: - zlog_err("Script '%s' garbage collector error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : garbage collector error: %s", + lfs->name, lua_tostring(lfs->L, -1)); break; default: - zlog_err("Script '%s' unknown error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : unknown error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; } if (ret != LUA_OK) { - lua_pop(fs->L, 1); + lua_pop(lfs->L, 1); goto done; } + if (lua_gettop(lfs->L) != 1) { + zlog_err( + "Lua hook call '%s': Lua function should return only 1 result", + lfs->name); + ret = 1; + goto done; + } + + if (lua_istable(lfs->L, 1) != 1) { + zlog_err( + "Lua hook call '%s': Lua function should return a Lua table", + lfs->name); + ret = 1; + } + done: /* LUA_OK is 0, so we can just return lua_pcall's result directly */ return ret; |
