diff mbox series

[2/3] Lua: Sessionize Lua Handlers

Message ID 91ADC143-2DAA-4624-A1FE-51DD729FF720@siemens.com
State Accepted
Headers show
Series [1/3] Lua: Rename lua_init() to lua_session_init() | expand

Commit Message

Storm, Christian June 13, 2024, 4:11 p.m. UTC
Initialize the global Lua state and check whether built-in or
external file Lua handlers do load successfully, session scoped.

With this change, Lua handlers are loaded into Lua session
states and no longer (on startup) into the global Lua state.

The global Lua state is still around to be able to register
global Lua Handlers from the C realm, programmatically.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 core/swupdate.c         |  2 +-
 corelib/lua_interface.c | 18 +++++++++++++-----
 include/lua_util.h      |  2 ++
 3 files changed, 16 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/core/swupdate.c b/core/swupdate.c
index 80f2229a..009abbbc 100644
--- a/core/swupdate.c
+++ b/core/swupdate.c
@@ -861,7 +861,7 @@  int main(int argc, char **argv)
 		}
 	}
 
-	lua_handlers_init(NULL);
+	lua_init();
 
 	if(!get_hw_revision(&swcfg.hw))
 		INFO("Running on %s Revision %s", swcfg.hw.boardname, swcfg.hw.revision);
diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index cf13d685..8de2e3cd 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -1541,12 +1541,8 @@  int lua_handlers_init(lua_State *L)
 #endif
 	int ret = -1;
 
-	if (!L) {
-		gL = luaL_newstate();
-		L = gL;
-	}
 	if (L) {
-		/* prime gL as LUA_TYPE_HANDLER */
+		/* prime L as LUA_TYPE_HANDLER */
 		lua_pushlightuserdata(L, (void*)LUA_TYPE_HANDLER);
 		lua_setglobal(L, "SWUPDATE_LUA_TYPE");
 		/* load standard libraries */
@@ -1603,6 +1599,18 @@  lua_State *lua_session_init(struct dict *bootenv)
 	return L;
 }
 
+int lua_init(void)
+{
+	lua_State *L = luaL_newstate();
+	int res = lua_handlers_init(L);
+	unregister_session_handlers();
+	lua_close(L);
+	if (!gL) {
+		gL = luaL_newstate();
+	}
+	return res;
+}
+
 int lua_load_buffer(lua_State *L, const char *buf)
 {
 	if (luaL_loadstring(L, buf) || lua_pcall(L, 0, 0, 0)) {
diff --git a/include/lua_util.h b/include/lua_util.h
index 1cbd4b1a..d3dd1cd8 100644
--- a/include/lua_util.h
+++ b/include/lua_util.h
@@ -22,6 +22,7 @@  typedef enum {
 void LUAstackDump (lua_State *L);
 int run_lua_script(lua_State *L, const char *script, bool load, const char *function, char *parms);
 lua_State *lua_session_init(struct dict *bootenv);
+int lua_init(void);
 int lua_load_buffer(lua_State *L, const char *buf);
 int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img);
 int lua_handler_fn(lua_State *L, const char *fcn, const char *parms);
@@ -101,6 +102,7 @@  struct img_type;
 #define lua_exit(L)
 #define lua_close(L)
 static inline lua_State *lua_session_init(struct dict __attribute__ ((__unused__)) *bootenv) { return NULL;}
+static inline int lua_init() { return 0; }
 static inline int lua_load_buffer(lua_State __attribute__ ((__unused__)) *L, 
 					const char __attribute__ ((__unused__)) *buf) {return 1;}
 static inline int lua_parser_fn(lua_State __attribute__ ((__unused__)) *L,