diff mbox series

[02/13] Lua: move out state initialization from run_lua_script

Message ID 20240221082221.11997-3-stefano.babic@swupdate.org
State Accepted
Delegated to: Stefano Babic
Headers show
Series Extend Lua Environemnt and post-failure scripts | expand

Commit Message

Stefano Babic Feb. 21, 2024, 8:22 a.m. UTC
This is in preparation for using global state. Do not create the state
in the run_lua_script(), but accepts a L state as parameter. It is duty
of the caller to provide a valid state.

Change accordingly the Lua script handler.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 corelib/lua_interface.c      | 12 +-----------
 handlers/lua_scripthandler.c | 14 +++++++++++---
 include/lua_util.h           |  2 +-
 3 files changed, 13 insertions(+), 15 deletions(-)

--
2.34.1
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 33be8d3b..72190383 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -204,18 +204,13 @@  static void lua_report_exception(lua_State *L)
 	} while (*s++);
 }

-int run_lua_script(const char *script, const char *function, char *parms)
+int run_lua_script(lua_State *L, const char *script, const char *function, char *parms)
 {
 	int ret;
 	const char *output;

-	lua_State *L = luaL_newstate(); /* opens Lua */
-	luaL_openlibs(L); /* opens the standard libraries */
-	luaL_requiref(L, "swupdate", luaopen_swupdate, 1 );
-
 	if (luaL_loadfile(L, script)) {
 		ERROR("ERROR loading %s", script);
-		lua_close(L);
 		return -1;
 	}

@@ -224,13 +219,11 @@  int run_lua_script(const char *script, const char *function, char *parms)
 		LUAstackDump(L);
 		ERROR("ERROR preparing Lua script %s %d",
 			script, ret);
-		lua_close(L);
 		return -1;
 	}

 	lua_getglobal(L, function);
 	if(!lua_isfunction(L,lua_gettop(L))) {
-		lua_close(L);
 		TRACE("Script : no %s in %s script, exiting", function, script);
 		return 0;
 	}
@@ -241,7 +234,6 @@  int run_lua_script(const char *script, const char *function, char *parms)
 	if (lua_pcall(L, 1, 2, 0)) {
 		LUAstackDump(L);
 		ERROR("ERROR Calling Lua script %s", script);
-		lua_close(L);
 		return -1;
 	}

@@ -257,8 +249,6 @@  int run_lua_script(const char *script, const char *function, char *parms)
 		TRACE("Script output: %s script end", output);
 	}

-	lua_close(L);
-
 	return ret;
 }

diff --git a/handlers/lua_scripthandler.c b/handlers/lua_scripthandler.c
index 509b360d..d4ac421e 100644
--- a/handlers/lua_scripthandler.c
+++ b/handlers/lua_scripthandler.c
@@ -29,13 +29,20 @@  static int start_lua_script(struct img_type *img, void *data)
 	int ret;
 	const char *fnname;
 	struct script_handler_data *script_data;
-
+	lua_State *L;
 	const char* tmp = get_tmpdirscripts();
 	char filename[MAX_IMAGE_FNAME + strlen(tmp) + 2 + strlen(img->type_data)];

 	if (!data)
 		return -1;

+	L = lua_init(img->bootloader);
+
+	if (!L) {
+		ERROR("Lua state cannot be instantiated");
+		return -1;
+	}
+
 	script_data = data;

 	switch (script_data->scriptfn) {
@@ -54,10 +61,11 @@  static int start_lua_script(struct img_type *img, void *data)
 		"%s%s", tmp, img->fname);
 	TRACE("Calling Lua %s", filename);

-	ret = run_lua_script(filename, fnname, img->type_data);
+	ret = run_lua_script(L, filename, fnname, img->type_data);

-	return ret;
+	lua_close(L);

+	return ret;
 }

  __attribute__((constructor))
diff --git a/include/lua_util.h b/include/lua_util.h
index 5f5e67a7..ff603b5c 100644
--- a/include/lua_util.h
+++ b/include/lua_util.h
@@ -20,7 +20,7 @@  typedef enum {
 } root_dev_type;

 void LUAstackDump (lua_State *L);
-int run_lua_script(const char *script, const char *function, char *parms);
+int run_lua_script(lua_State *L, const char *script, const char *function, char *parms);
 lua_State *lua_init(struct dict *bootenv);
 int lua_load_buffer(lua_State *L, const char *buf);
 int lua_parser_fn(lua_State *L, const char *fcn, struct img_type *img);