From: Donald Lee Date: Mon, 14 Jun 2021 06:54:58 +0000 (+0800) Subject: lib: make frrscript_call encode args based on type X-Git-Tag: base_8.1~355^2~19 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=43a5106d42b6054ca1dbdee7d455a5212c3094ca;p=mirror%2Ffrr.git lib: make frrscript_call encode args based on type Signed-off-by: Donald Lee --- diff --git a/lib/frrscript.c b/lib/frrscript.c index 10d400886d..1a9f3639dd 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -104,24 +104,8 @@ static void codec_free(struct codec *c) /* Generic script APIs */ -int frrscript_call(struct frrscript *fs, struct frrscript_env *env) +int _frrscript_call(struct frrscript *fs) { - struct frrscript_codec c = {}; - const void *arg; - const char *bindname; - - /* Encode script arguments */ - for (int i = 0; env && env[i].val != NULL; i++) { - bindname = env[i].name; - c.typename = env[i].typename; - arg = env[i].val; - - struct frrscript_codec *codec = hash_lookup(codec_hash, &c); - assert(codec && "No encoder for type"); - codec->encoder(fs->L, arg); - - lua_setglobal(fs->L, bindname); - } int ret = lua_pcall(fs->L, 0, 0, 0); diff --git a/lib/frrscript.h b/lib/frrscript.h index f4057f531b..06d247f38d 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -96,6 +96,21 @@ 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 DECODE_ARGS(name, value) \ + do { \ + lua_getglobal(L, name); \ + DECODE_ARGS_WITH_STATE(L, value) \ + } while (0) + +#define ENCODE_ARGS_WITH_STATE(L, value) _Generic((value), )(L, value); + +#define DECODE_ARGS_WITH_STATE(L, value) _Generic((value), )(L, value); /* * Call script. @@ -109,8 +124,18 @@ void frrscript_init(const char *scriptdir); * Returns: * 0 if the script ran successfully, nonzero otherwise. */ -int frrscript_call(struct frrscript *fs, struct frrscript_env *env); - +int _frrscript_call(struct frrscript *fs); + +#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; \ + }) /* * Get result from finished script.