diff options
| author | Donald Lee <dlqs@gmx.com> | 2021-08-10 04:41:25 +0800 |
|---|---|---|
| committer | Quentin Young <qlyoung@qlyoung.net> | 2023-11-20 20:45:02 -0500 |
| commit | d2acf63f16837caaadd093927c236b742c19fd75 (patch) | |
| tree | 9a6b11c90de0c01a40ceb8e8d5fa60afb22bbb3e | |
| parent | ac91b343d635f056e8de47224719ed6218e79dfb (diff) | |
lib: Create encoders for int and rename stuff
Create encoders/decoders for int and rename current int encoders
/decoders to long long.
Signed-off-by: Donald Lee <dlqs@gmx.com>
| -rw-r--r-- | lib/frrlua.c | 28 | ||||
| -rw-r--r-- | lib/frrlua.h | 19 | ||||
| -rw-r--r-- | lib/frrscript.h | 7 | ||||
| -rw-r--r-- | tests/lib/test_frrlua.c | 12 |
4 files changed, 56 insertions, 10 deletions
diff --git a/lib/frrlua.c b/lib/frrlua.c index 720e95491a..0e95e49c6f 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -259,12 +259,12 @@ void *lua_tosockunion(lua_State *L, int idx) return su; } -void lua_pushintegerp(lua_State *L, const long long *num) +void lua_pushintegerp(lua_State *L, const int *num) { lua_pushinteger(L, *num); } -void lua_decode_integerp(lua_State *L, int idx, long long *num) +void lua_decode_integerp(lua_State *L, int idx, int *num) { int isnum; *num = lua_tonumberx(L, idx, &isnum); @@ -274,7 +274,7 @@ void lua_decode_integerp(lua_State *L, int idx, long long *num) void *lua_tointegerp(lua_State *L, int idx) { - long long *num = XCALLOC(MTYPE_SCRIPT_RES, sizeof(long long)); + int *num = XCALLOC(MTYPE_TMP, sizeof(int)); lua_decode_integerp(L, idx, num); return num; @@ -332,6 +332,28 @@ void lua_pushnexthop_group(lua_State *L, const struct nexthop_group *ng) } } +void lua_pushlonglongp(lua_State *L, const long long *num) +{ + /* lua library function; this can take a long long */ + lua_pushinteger(L, *num); +} + +void lua_decode_longlongp(lua_State *L, int idx, long long *num) +{ + int isnum; + *num = lua_tonumberx(L, idx, &isnum); + lua_pop(L, 1); + assert(isnum); +} + +void *lua_tolonglongp(lua_State *L, int idx) +{ + long long *num = XCALLOC(MTYPE_TMP, sizeof(long long)); + + lua_decode_longlongp(L, idx, num); + return num; +} + void lua_decode_stringp(lua_State *L, int idx, char *str) { strlcpy(str, lua_tostring(L, idx), strlen(str) + 1); diff --git a/lib/frrlua.h b/lib/frrlua.h index 61fab328fe..dc0f4d9986 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -121,9 +121,9 @@ void lua_pushnexthop(lua_State *L, const struct nexthop *nexthop); /* * Converts an int to a Lua value and pushes it on the stack. */ -void lua_pushintegerp(lua_State *L, const long long *num); +void lua_pushintegerp(lua_State *L, const int *num); -void lua_decode_integerp(lua_State *L, int idx, long long *num); +void lua_decode_integerp(lua_State *L, int idx, int *num); /* * Converts the Lua value at idx to an int. @@ -133,6 +133,21 @@ void lua_decode_integerp(lua_State *L, int idx, long long *num); */ void *lua_tointegerp(lua_State *L, int idx); +/* + * Converts a long long to a Lua value and pushes it on the stack. + */ +void lua_pushlonglongp(lua_State *L, const long long *num); + +void lua_decode_longlongp(lua_State *L, int idx, long long *num); + +/* + * Converts the Lua value at idx to a long long. + * + * Returns: + * long long allocated with MTYPE_TMP. + */ +void *lua_tolonglongp(lua_State *L, int idx); + void lua_decode_stringp(lua_State *L, int idx, char *str); /* diff --git a/lib/frrscript.h b/lib/frrscript.h index f842dd9f45..ce313a1b63 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -197,8 +197,9 @@ void _lua_decode_noop(lua_State *, ...); #define ENCODE_ARGS_WITH_STATE(L, value) \ _Generic((value), \ int : lua_pushinteger, \ +int * : lua_pushintegerp, \ long long : lua_pushinteger, \ -long long * : lua_pushintegerp, \ +long long * : lua_pushlonglongp, \ struct prefix * : lua_pushprefix, \ struct interface * : lua_pushinterface, \ struct in_addr * : lua_pushinaddr, \ @@ -217,8 +218,8 @@ struct zebra_dplane_ctx * : lua_pushzebra_dplane_ctx \ #define DECODE_ARGS_WITH_STATE(L, value) \ _Generic((value), \ -int : lua_decode_integer_noop, \ -long long * : lua_decode_integerp, \ +int * : lua_decode_integerp, \ +long long * : lua_decode_longlongp, \ struct prefix * : lua_decode_prefix, \ struct interface * : lua_decode_interface, \ struct in_addr * : lua_decode_inaddr, \ diff --git a/tests/lib/test_frrlua.c b/tests/lib/test_frrlua.c index 701e171c84..2760a273bd 100644 --- a/tests/lib/test_frrlua.c +++ b/tests/lib/test_frrlua.c @@ -13,14 +13,22 @@ static void test_encode_decode(void) { lua_State *L = luaL_newstate(); - long long a = 123; - long long b = a; + int a = 123; + int b = a; lua_pushintegerp(L, &a); lua_decode_integerp(L, -1, &a); assert(a == b); assert(lua_gettop(L) == 0); + long long ll_a = 123L; + long long ll_b = a; + + lua_pushlonglongp(L, &ll_a); + lua_decode_longlongp(L, -1, &ll_a); + assert(ll_a == ll_b); + assert(lua_gettop(L) == 0); + time_t time_a = 100; time_t time_b; |
