]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: Enable consecutive frrscript_call
authorDonald Lee <dlqs@gmx.com>
Mon, 9 Aug 2021 23:07:06 +0000 (07:07 +0800)
committerDonald Lee <dlqs@gmx.com>
Mon, 9 Aug 2021 23:35:32 +0000 (07:35 +0800)
Previous:
 - frrscript_load: push Lua function onto stack
 - frrscript_call: calls Lua function

Now:
 - frrscript_load: checks Lua function
 - frrscript_call: pushes and calls Lua function (first clear the stack)

So now we just need one frrscript_load for consecutive frrscript_call.
frrscript_call does not recompile or reload the script file, it just
keys it from the global Lua table (where it should already be).

Signed-off-by: Donald Lee <dlqs@gmx.com>
lib/frrscript.c
lib/frrscript.h

index c9fc9389971f07a9c9d69ea46c184c2510c81326..0e0d3c030c8237842936773aa0f8768d51b98b45 100644 (file)
@@ -288,13 +288,16 @@ int frrscript_load(struct frrscript *fs, const char *function_name,
                goto fail;
        }
 
-       /* Push the Lua function we want */
+       /* To check the Lua function, we get it from the global table */
        lua_getglobal(L, function_name);
        if (lua_isfunction(L, lua_gettop(L)) == 0) {
                zlog_err("frrscript: loaded script '%s.lua' but %s not found",
                         script_name, function_name);
                goto fail;
        }
+       /* Then pop the function (frrscript_call will push it when it needs it)
+        */
+       lua_pop(L, 1);
 
        if (load_cb && (*load_cb)(fs) != 0) {
                zlog_err(
index 540676c099463c36c7a8761dcaf06d1f4efaeb91..c089df61fc4fc8ba9713ee3a5d59607c66d960aa 100644 (file)
@@ -203,8 +203,18 @@ const struct prefix * : lua_decode_noop                         \
 int _frrscript_call_lua(struct lua_function_state *lfs, int nargs);
 
 /*
- * Wrapper for calling Lua function state. Maps values passed in to their
- * encoder and decoder types.
+ * Wrapper for calling Lua function state.
+ *
+ * The Lua function name (f) to run should have already been checked by
+ * frrscript_load. So this wrapper will:
+ * 1) Find the Lua function state, which contains the Lua state
+ * 2) Clear the Lua state (there may be leftovers items from previous call)
+ * 3) Push the Lua function (f)
+ * 4) Map frrscript_call arguments onto their encoder and decoders, push those
+ * 5) Call _frrscript_call_lua (Lua execution takes place)
+ * 6) Write back to frrscript_call arguments using their decoders
+ *
+ * This wrapper can be called multiple times (after one frrscript_load).
  *
  * fs
  *    The struct frrscript in which the Lua fuction was loaded into
@@ -226,6 +236,8 @@ int _frrscript_call_lua(struct lua_function_state *lfs, int nargs);
                        1;                                                                                                                                         \
                })                                                                                                                                                 \
                            : ({                                                                                                                                   \
+                                     lua_settop(lfs->L, 0);                                                                                                       \
+                                     lua_getglobal(lfs->L, f);                                                                                                    \
                                      MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__);                                                                                       \
                                      _frrscript_call_lua(                                                                                                         \
                                              lfs, PP_NARG(__VA_ARGS__));                                                                                          \