]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: Add frrscript_get_result
authorDonald Lee <dlqs@gmx.com>
Wed, 7 Jul 2021 13:53:38 +0000 (21:53 +0800)
committerDonald Lee <dlqs@gmx.com>
Sat, 17 Jul 2021 22:32:03 +0000 (06:32 +0800)
Signed-off-by: Donald Lee <dlqs@gmx.com>
lib/frrscript.c
lib/frrscript.h

index 1d4fe58dd151c053d6a19ef8463c1720e0e4b306..5352c6470dad119f75d48c3ad5bda98b08bb4d36 100644 (file)
@@ -194,22 +194,31 @@ done:
        return ret;
 }
 
-void *frrscript_get_result(struct frrscript *fs,
-                          const struct frrscript_env *result)
+void *frrscript_get_result(struct frrscript *fs, const char *function_name,
+                          const char *name,
+                          void *(*lua_to)(lua_State *L, int idx))
 {
-       void *r;
-       struct frrscript_codec c = {.typename = result->typename};
+       void *p;
+       struct lua_function_state *lfs;
+       struct lua_function_state lookup = {.name = function_name};
 
-       struct frrscript_codec *codec = hash_lookup(codec_hash, &c);
-       assert(codec && "No encoder for type");
+       lfs = hash_lookup(fs->lua_function_hash, &lookup);
 
-       if (!codec->decoder) {
-               zlog_err("No script decoder for type '%s'", result->typename);
+       if (lfs == NULL) {
                return NULL;
        }
 
+       /* results table is idx 1 on the stack, getfield pushes our item to idx
+        * 2*/
+       lua_getfield(lfs->L, 1, name);
+       if (lua_isnil(lfs->L, -1)) {
+               lua_pop(lfs->L, 1);
+               zlog_err("No result in results table with that name %s", name);
+               return NULL;
+       }
+       p = lua_to(lfs->L, 2);
 
-       return r;
+       return p;
 }
 
 void frrscript_register_type_codec(struct frrscript_codec *codec)
index 1e35e2ee4108e9d1c0f2b60402d7945e367b43d0..d2791a5a8b6075f4aa3a35ec28918ee368480360 100644 (file)
@@ -233,8 +233,9 @@ int _frrscript_call_lua(struct lua_function_state *lfs, int nargs);
  * Returns:
  *    The script result of the specified name and type, or NULL.
  */
-void *frrscript_get_result(struct frrscript *fs,
-                          const struct frrscript_env *result);
+void *frrscript_get_result(struct frrscript *fs, const char *function_name,
+                          const char *name,
+                          void *(*lua_to)(lua_State *L, int idx));
 
 #ifdef __cplusplus
 }