From 395e0792d8db4a35138c9f85c101eda547f5540d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 20 Nov 2022 13:53:41 +0100 Subject: [PATCH] Use luaL_loadbufferx to pass name directly to interpreter Signed-off-by: DL6ER --- src/lua/ftl_lua.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lua/ftl_lua.c b/src/lua/ftl_lua.c index e8130a1b..72c371c3 100644 --- a/src/lua/ftl_lua.c +++ b/src/lua/ftl_lua.c @@ -99,14 +99,15 @@ LUAMOD_API int luaopen_pihole(lua_State *L) { return LUA_YIELD; } -const char *script = inspect_lua; - -static bool ftl_lua_load_embedded_script(lua_State *L, const char *name, const char *script, const bool make_global) +static bool ftl_lua_load_embedded_script(lua_State *L, const char *name, const char *script, const size_t script_len, const bool make_global) { - if (luaL_dostring(L, script) != 0) + // Explanation: + // luaL_dostring(L, script) expands to (luaL_loadstring(L, script) || lua_pcall(L, 0, LUA_MULTRET, 0)) + // luaL_loadstring(L, script) calls luaL_loadbuffer(L, s, strlen(s), s) + if (luaL_loadbufferx(L, script, script_len, name, NULL) || lua_pcall(L, 0, LUA_MULTRET, 0) != 0) { const char *lua_err = lua_tostring(L, -1); - logg("LUA error: %s", lua_err); + printf("LUA error while trying to import %s.lua: %s\n", name, lua_err); return false; } @@ -122,5 +123,5 @@ static bool ftl_lua_load_embedded_script(lua_State *L, const char *name, const c // Load bundled libraries and make the available globally void ftl_lua_init(lua_State *L) { - ftl_lua_load_embedded_script(L, "inspect", inspect_lua, true); + ftl_lua_load_embedded_script(L, "inspect", inspect_lua, sizeof(inspect_lua), true); }