]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: make frrscript_call encode args based on type
authorDonald Lee <dlqs@gmx.com>
Mon, 14 Jun 2021 06:54:58 +0000 (14:54 +0800)
committerDonald Lee <dlqs@gmx.com>
Mon, 21 Jun 2021 20:51:22 +0000 (04:51 +0800)
Signed-off-by: Donald Lee <dlqs@gmx.com>
lib/frrscript.c
lib/frrscript.h

index 10d400886d835de6e55b5d8459d76b5c1e1527d1..1a9f3639dd66ed7d2be326fa7772bdb62b369f81 100644 (file)
@@ -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);
 
index f4057f531bd4b6b73d57afac34bb886cfc59d817..06d247f38d612cebd7eb005d6d4e85c6a45600df 100644 (file)
@@ -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.