diff mbox series

[05/13] Lua Script: runs with global Lua state

Message ID 20240221082221.11997-6-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
Add a property "global-state" to run the script with a per installation
Lua state or creating a new local as in the past.

If global-state = "true" (as property, value is always a string), Lua
state is reused and the script can call any Lua function that was loaded
before during the installation.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 handlers/lua_scripthandler.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

--
2.34.1
diff mbox series

Patch

diff --git a/handlers/lua_scripthandler.c b/handlers/lua_scripthandler.c
index d4ac421e..213099de 100644
--- a/handlers/lua_scripthandler.c
+++ b/handlers/lua_scripthandler.c
@@ -36,7 +36,14 @@  static int start_lua_script(struct img_type *img, void *data)
 	if (!data)
 		return -1;

-	L = lua_init(img->bootloader);
+	bool global  = strtobool(dict_get_value(&img->properties, "global-state"));
+
+	if (global) {
+		TRACE("Executing with global state");
+		L = img->L;
+	} else {
+		L = lua_init(img->bootloader);
+	}

 	if (!L) {
 		ERROR("Lua state cannot be instantiated");
@@ -63,7 +70,8 @@  static int start_lua_script(struct img_type *img, void *data)

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

-	lua_close(L);
+	if (!global)
+		lua_close(L);

 	return ret;
 }