From d32dd4c31999811be9cf35c50fbc0110a353e8e2 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 30 Dec 2023 01:53:07 -0600 Subject: [PATCH] added startScript() --- src/lua/Manager.cpp | 15 +++++++++++++-- src/lua/Thread.hpp | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lua/Manager.cpp b/src/lua/Manager.cpp index 6d7f162..8dee296 100644 --- a/src/lua/Manager.cpp +++ b/src/lua/Manager.cpp @@ -10,10 +10,21 @@ void LuaManager::init() { lua_State *L = luaL_newstate(); luaL_openlibs(L); - if (luaL_dostring(L, "print(\"Hello from Lua!\")")) { +static LuaThread *startScript(const char *script) { + lua_State *L = lua_newthread(globalState); + LuaThread *T = new LuaThread(L, luaL_ref(L, LUA_REGISTRYINDEX)); + + if (luaL_dostring(L, script)) { std::cout << "Lua error: " << lua_tostring(L, -1) << std::endl; lua_pop(L, 1); } - globalState = L; + return T; +} + +void LuaManager::init() { + globalState = luaL_newstate(); + luaL_openlibs(globalState); + + delete startScript("print(\"Hello from Lua!\")"); } diff --git a/src/lua/Thread.hpp b/src/lua/Thread.hpp index 93b0329..bcc1a68 100644 --- a/src/lua/Thread.hpp +++ b/src/lua/Thread.hpp @@ -11,4 +11,7 @@ struct LuaThread { int ref; LuaThread(lua_State *L, int ref) : L(L), ref(ref) {} + ~LuaThread() { + luaL_unref(L, LUA_REGISTRYINDEX, ref); + } }; \ No newline at end of file