lib: Add frrscript_get_result

Signed-off-by: Donald Lee <dlqs@gmx.com>
This commit is contained in:
Donald Lee 2021-07-07 21:53:38 +08:00
parent 24ff8520af
commit 06947ddeac
2 changed files with 21 additions and 11 deletions

View File

@ -194,22 +194,31 @@ done:
return ret; return ret;
} }
void *frrscript_get_result(struct frrscript *fs, void *frrscript_get_result(struct frrscript *fs, const char *function_name,
const struct frrscript_env *result) const char *name,
void *(*lua_to)(lua_State *L, int idx))
{ {
void *r; void *p;
struct frrscript_codec c = {.typename = result->typename}; struct lua_function_state *lfs;
struct lua_function_state lookup = {.name = function_name};
struct frrscript_codec *codec = hash_lookup(codec_hash, &c); lfs = hash_lookup(fs->lua_function_hash, &lookup);
assert(codec && "No encoder for type");
if (!codec->decoder) { if (lfs == NULL) {
zlog_err("No script decoder for type '%s'", result->typename);
return 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) void frrscript_register_type_codec(struct frrscript_codec *codec)

View File

@ -233,8 +233,9 @@ int _frrscript_call_lua(struct lua_function_state *lfs, int nargs);
* Returns: * Returns:
* The script result of the specified name and type, or NULL. * The script result of the specified name and type, or NULL.
*/ */
void *frrscript_get_result(struct frrscript *fs, void *frrscript_get_result(struct frrscript *fs, const char *function_name,
const struct frrscript_env *result); const char *name,
void *(*lua_to)(lua_State *L, int idx));
#ifdef __cplusplus #ifdef __cplusplus
} }