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.
This commit is contained in:
Thierry Fournier 2016-02-22 19:52:08 +01:00 committed by Willy Tarreau
parent 9ba1d024db
commit ddd8988fe5
3 changed files with 45 additions and 43 deletions

View File

@ -1,7 +1,11 @@
#ifndef _PROTO_HLUA_FCN_H #ifndef _PROTO_HLUA_FCN_H
#define _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); void *hlua_checkudata(lua_State *L, int ud, int class_ref);
int hlua_fcn_reg_core_fcn(lua_State *L); int hlua_fcn_reg_core_fcn(lua_State *L);
int hlua_dump_object(lua_State *L);
#endif /* _PROTO_HLUA_FCN_H */ #endif /* _PROTO_HLUA_FCN_H */

View File

@ -264,49 +264,6 @@ const char *hlua_get_top_error_string(lua_State *L)
return lua_tostring(L, -1); 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 /* This function check the number of arguments available in the
* stack. If the number of arguments available is not the same * stack. If the number of arguments available is not the same
* then <nb> an error is throwed. * then <nb> an error is throwed.

View File

@ -25,6 +25,47 @@
/* Contains the class reference of the concat object. */ /* Contains the class reference of the concat object. */
static int class_concat_ref; 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. */ /* Return an object of the expected type, or throws an error. */
void *hlua_checkudata(lua_State *L, int ud, int class_ref) void *hlua_checkudata(lua_State *L, int ud, int class_ref)
{ {