From ddd8988fe5c70c98df9d845920877d4ccd13df4f Mon Sep 17 00:00:00 2001 From: Thierry Fournier Date: Mon, 22 Feb 2016 19:52:08 +0100 Subject: [PATCH] MINOR: lua: move class registration facilities The functions - hlua_class_const_int() - hlua_class_const_str() - hlua_class_function() are use for common class registration actions. The function 'hlua_dump_object()' is generic dump name function. These functions can be used by all the HAProxy objects, so I move it into the safe functions file. --- include/proto/hlua_fcn.h | 4 ++++ src/hlua.c | 43 ---------------------------------------- src/hlua_fcn.c | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/include/proto/hlua_fcn.h b/include/proto/hlua_fcn.h index 179d40cc3..560d1f4ed 100644 --- a/include/proto/hlua_fcn.h +++ b/include/proto/hlua_fcn.h @@ -1,7 +1,11 @@ #ifndef _PROTO_HLUA_FCN_H #define _PROTO_HLUA_FCN_H +void hlua_class_const_int(lua_State *L, const char *name, int value); +void hlua_class_const_str(lua_State *L, const char *name, const char *value); +void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L)); void *hlua_checkudata(lua_State *L, int ud, int class_ref); int hlua_fcn_reg_core_fcn(lua_State *L); +int hlua_dump_object(lua_State *L); #endif /* _PROTO_HLUA_FCN_H */ diff --git a/src/hlua.c b/src/hlua.c index 54fbc4847..402595a06 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -264,49 +264,6 @@ const char *hlua_get_top_error_string(lua_State *L) return lua_tostring(L, -1); } -/* The three following functions are useful for adding entries - * in a table. These functions takes a string and respectively an - * integer, a string or a function and add it to the table in the - * top of the stack. - * - * These functions throws an error if no more stack size is - * available. - */ -__LJMP static inline void hlua_class_const_int(lua_State *L, const char *name, - int value) -{ - if (!lua_checkstack(L, 2)) - WILL_LJMP(luaL_error(L, "full stack")); - lua_pushstring(L, name); - lua_pushinteger(L, value); - lua_rawset(L, -3); -} -__LJMP static inline void hlua_class_const_str(lua_State *L, const char *name, - const char *value) -{ - if (!lua_checkstack(L, 2)) - WILL_LJMP(luaL_error(L, "full stack")); - lua_pushstring(L, name); - lua_pushstring(L, value); - lua_rawset(L, -3); -} -__LJMP static inline void hlua_class_function(lua_State *L, const char *name, - int (*function)(lua_State *L)) -{ - if (!lua_checkstack(L, 2)) - WILL_LJMP(luaL_error(L, "full stack")); - lua_pushstring(L, name); - lua_pushcclosure(L, function, 0); - lua_rawset(L, -3); -} - -__LJMP static int hlua_dump_object(struct lua_State *L) -{ - const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1)); - lua_pushfstring(L, "HAProxy class %s", name); - return 1; -} - /* This function check the number of arguments available in the * stack. If the number of arguments available is not the same * then an error is throwed. diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c index 5adb12815..c649aece6 100644 --- a/src/hlua_fcn.c +++ b/src/hlua_fcn.c @@ -25,6 +25,47 @@ /* Contains the class reference of the concat object. */ static int class_concat_ref; +/* The three following functions are useful for adding entries + * in a table. These functions takes a string and respectively an + * integer, a string or a function and add it to the table in the + * top of the stack. + * + * These functions throws an error if no more stack size is + * available. + */ +void hlua_class_const_int(lua_State *L, const char *name, int value) +{ + if (!lua_checkstack(L, 2)) + luaL_error(L, "full stack"); + lua_pushstring(L, name); + lua_pushinteger(L, value); + lua_rawset(L, -3); +} +void hlua_class_const_str(lua_State *L, const char *name, const char *value) +{ + if (!lua_checkstack(L, 2)) + luaL_error(L, "full stack"); + lua_pushstring(L, name); + lua_pushstring(L, value); + lua_rawset(L, -3); +} +void hlua_class_function(lua_State *L, const char *name, int (*function)(lua_State *L)) +{ + if (!lua_checkstack(L, 2)) + luaL_error(L, "full stack"); + lua_pushstring(L, name); + lua_pushcclosure(L, function, 0); + lua_rawset(L, -3); +} + +/* This function returns a string containg the HAProxy object name. */ +int hlua_dump_object(struct lua_State *L) +{ + const char *name = (const char *)lua_tostring(L, lua_upvalueindex(1)); + lua_pushfstring(L, "HAProxy class %s", name); + return 1; +} + /* Return an object of the expected type, or throws an error. */ void *hlua_checkudata(lua_State *L, int ud, int class_ref) {