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.h | |
| 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.h')
| -rw-r--r-- | lib/frrscript.h | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/lib/frrscript.h b/lib/frrscript.h index df72bba4cd..a0e1176ea7 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -119,16 +119,12 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs); */ void frrscript_init(const char *scriptdir); -#define ENCODE_ARGS(name, value) \ - do { \ - ENCODE_ARGS_WITH_STATE(L, value); \ - lua_setglobal(L, name); \ - } while (0) +#define ENCODE_ARGS(name, value) ENCODE_ARGS_WITH_STATE(lfs->L, value) #define DECODE_ARGS(name, value) \ do { \ - lua_getglobal(L, name); \ - DECODE_ARGS_WITH_STATE(L, value); \ + lua_getfield(lfs->L, 1, name); \ + DECODE_ARGS_WITH_STATE(lfs->L, value); \ } while (0) /* @@ -179,7 +175,7 @@ const struct prefix * : lua_decode_noop \ * Returns: * 0 if the script ran successfully, nonzero otherwise. */ -int _frrscript_call(struct frrscript *fs); +int _frrscript_call_lua(struct lua_function_state *lfs, int nargs); /* * Wrapper for call script. Maps values passed in to their encoder @@ -191,15 +187,32 @@ int _frrscript_call(struct frrscript *fs); * Returns: * 0 if the script ran successfully, nonzero otherwise. */ -#define frrscript_call(fs, ...) \ - ({ \ - lua_State *L = fs->L; \ - MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \ - int ret = _frrscript_call(fs); \ - if (ret == 0) { \ - MAP_LISTS(DECODE_ARGS, ##__VA_ARGS__); \ - } \ - ret; \ + +#define frrscript_call(fs, f, ...) \ + ({ \ + struct lua_function_state lookup = {.name = f}; \ + struct lua_function_state *lfs; \ + lfs = hash_lookup(fs->lua_function_hash, &lookup); \ + lfs == NULL ? ({ \ + zlog_err( \ + "Lua script call: tried to call '%s' in '%s' which was not loaded", \ + f, fs->name); \ + 1; \ + }) \ + : ({ \ + MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \ + _frrscript_call_lua(lfs, PP_NARG(__VA_ARGS__)); \ + }) != 0 \ + ? ({ \ + zlog_err( \ + "Lua script call: '%s' in '%s' returned non-zero exit code", \ + f, fs->name); \ + 1; \ + }) \ + : ({ \ + MAP_LISTS(DECODE_ARGS, ##__VA_ARGS__); \ + 0; \ + }); \ }) /* |
