MINOR: lua: core: create "core" class and object
This object provides main HAProxy functions. This first version
creates an empty object. It will be enhanced later.
diff --git a/include/types/hlua.h b/include/types/hlua.h
index cfeb767..b2c3997 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -3,6 +3,8 @@
#include <lua.h>
+#define CLASS_CORE "Core"
+
enum hlua_state {
HLUA_STOP = 0,
HLUA_RUN,
diff --git a/include/types/log.h b/include/types/log.h
index c680663..345a09f 100644
--- a/include/types/log.h
+++ b/include/types/log.h
@@ -33,6 +33,8 @@
#define SYSLOG_PORT 514
#define UNIQUEID_LEN 128
+/* The array containing the names of the log levels. */
+extern const char *log_levels[];
/* lists of fields that can be logged */
enum {
diff --git a/src/hlua.c b/src/hlua.c
index dd03784..00b88ca 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -36,6 +36,12 @@
*/
struct eb_root hlua_ctx = EB_ROOT_UNIQUE;
+/* The following variables contains the reference of the different
+ * Lua classes. These references are useful for identify metadata
+ * associated with an object.
+ */
+static int class_core_ref;
+
/* Used to check an Lua function type in the stack. It creates and
* returns a reference of the function. This function throws an
* error if the rgument is not a "function".
@@ -490,6 +496,8 @@
void hlua_init(void)
{
+ int i;
+
/* Initialise com signals pool session. */
pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
@@ -507,4 +515,39 @@
/* Initialise lua. */
luaL_openlibs(gL.T);
+
+ /*
+ *
+ * Create "core" object.
+ *
+ */
+
+ /* This integer entry is just used as base value for the object "core". */
+ lua_pushinteger(gL.T, 0);
+
+ /* Create and fill the metatable. */
+ lua_newtable(gL.T);
+
+ /* Create and fill the __index entry. */
+ lua_pushstring(gL.T, "__index");
+ lua_newtable(gL.T);
+
+ /* Push the loglevel constants. */
+ for (i=0; i<NB_LOG_LEVELS; i++)
+ hlua_class_const_int(gL.T, log_levels[i], i);
+
+ /* Store the table __index in the metable. */
+ lua_settable(gL.T, -3);
+
+ /* Register previous table in the registry with named entry. */
+ lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
+ lua_setfield(gL.T, LUA_REGISTRYINDEX, CLASS_CORE); /* register class session. */
+
+ /* Register previous table in the registry with reference. */
+ lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */
+ class_core_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); /* reference class session. */
+
+ /* Create new object with class Core. */
+ lua_setmetatable(gL.T, -2);
+ lua_setglobal(gL.T, "core");
}