From 45e78d7aa992c36e9ea92b6b9cafe4e341d5695c Mon Sep 17 00:00:00 2001 From: Thierry Fournier Date: Fri, 19 Feb 2016 18:34:46 +0100 Subject: [PATCH] MINOR: lua: refactor the Lua object registration All the HAProxy Lua object are declared with the same pattern: - Add the function __tosting which dumps the object name - Register the name in the Lua REGISTRY - Register the reference ID These action are refactored in on function. This remove some lines of code. --- include/proto/hlua_fcn.h | 1 + src/hlua.c | 95 ++++++---------------------------------- src/hlua_fcn.c | 35 +++++++++++++++ 3 files changed, 49 insertions(+), 82 deletions(-) diff --git a/include/proto/hlua_fcn.h b/include/proto/hlua_fcn.h index 560d1f4ed..9c5d9081b 100644 --- a/include/proto/hlua_fcn.h +++ b/include/proto/hlua_fcn.h @@ -5,6 +5,7 @@ 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_register_metatable(struct lua_State *L, char *name); int hlua_fcn_reg_core_fcn(lua_State *L); int hlua_dump_object(lua_State *L); diff --git a/src/hlua.c b/src/hlua.c index 402595a06..1fd3b2728 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -6592,12 +6592,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_MAP); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6608,11 +6602,12 @@ void hlua_init(void) lua_rawset(gL.T, -3); - /* Register previous table in the registry with reference and named entry. */ + /* Register previous table in the registry with reference and named entry. + * The function hlua_register_metatable() pops the stack, so we + * previously create a copy of the table. + */ lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_MAP); /* register class session. */ - class_map_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); /* Assign the metatable to the mai Map object. */ lua_setmetatable(gL.T, -2); @@ -6629,12 +6624,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_CHANNEL); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6653,9 +6642,7 @@ void hlua_init(void) lua_rawset(gL.T, -3); /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CHANNEL); /* register class session. */ - class_channel_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); /* * @@ -6666,12 +6653,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_FETCHES); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6709,9 +6690,7 @@ void hlua_init(void) lua_rawset(gL.T, -3); /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_FETCHES); /* register class session. */ - class_fetches_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); /* * @@ -6722,12 +6701,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_CONVERTERS); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fill the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6762,9 +6735,7 @@ void hlua_init(void) lua_rawset(gL.T, -3); /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CONVERTERS); /* register class session. */ - class_converters_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); /* * @@ -6775,12 +6746,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_HTTP); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6808,9 +6773,7 @@ void hlua_init(void) lua_rawset(gL.T, -3); /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_HTTP); /* register class session. */ - class_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); /* * @@ -6821,12 +6784,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_APPLET_TCP); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6841,9 +6798,7 @@ void hlua_init(void) lua_settable(gL.T, -3); /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_APPLET_TCP); /* register class session. */ - class_applet_tcp_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); /* * @@ -6854,12 +6809,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_APPLET_HTTP); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6877,9 +6826,7 @@ void hlua_init(void) lua_settable(gL.T, -3); /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_APPLET_HTTP); /* register class session. */ - class_applet_http_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); /* * @@ -6890,12 +6837,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_TXN); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6919,9 +6860,7 @@ void hlua_init(void) lua_rawset(gL.T, -3); /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_TXN); /* register class session. */ - class_txn_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */ + class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); /* * @@ -6932,12 +6871,6 @@ void hlua_init(void) /* Create and fill the metatable. */ lua_newtable(gL.T); - /* Create the __tostring identifier */ - lua_pushstring(gL.T, "__tostring"); - lua_pushstring(gL.T, CLASS_SOCKET); - lua_pushcclosure(gL.T, hlua_dump_object, 1); - lua_rawset(gL.T, -3); - /* Create and fille the __index entry. */ lua_pushstring(gL.T, "__index"); lua_newtable(gL.T); @@ -6962,9 +6895,7 @@ void hlua_init(void) lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ /* Register previous table in the registry with reference and named entry. */ - lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ - lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_SOCKET); /* register class socket. */ - class_socket_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class socket. */ + class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); /* Proxy and server configuration initialisation. */ memset(&socket_proxy, 0, sizeof(socket_proxy)); diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c index ac8f03316..1dcd650f2 100644 --- a/src/hlua_fcn.c +++ b/src/hlua_fcn.c @@ -60,6 +60,41 @@ int hlua_dump_object(struct lua_State *L) return 1; } +/* This function register a table as metatable and. It names + * the metatable, and returns the associated reference. + * The original table is poped from the top of the stack. + * "name" is the referenced class name. + */ +int hlua_register_metatable(struct lua_State *L, char *name) +{ + /* Check the type of the top element. it must be + * a table. + */ + if (lua_type(L, -1) != LUA_TTABLE) + luaL_error(L, "hlua_register_metatable() requires a type Table " + "in the top of the stack"); + + /* Add the __tostring function which identify the + * created object. + */ + lua_pushstring(L, "__tostring"); + lua_pushstring(L, name); + lua_pushcclosure(L, hlua_dump_object, 1); + lua_rawset(L, -3); + + /* Register a named entry for the table. The table + * reference is copyed first because the function + * lua_setfield() pop the entry. + */ + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, name); + + /* Creates the reference of the object. The + * function luaL_ref pop the top of the stack. + */ + return luaL_ref(L, LUA_REGISTRYINDEX); +} + /* Return an object of the expected type, or throws an error. */ void *hlua_checkudata(lua_State *L, int ud, int class_ref) {