Skip to content

Commit

Permalink
added startScript()
Browse files Browse the repository at this point in the history
  • Loading branch information
CPunch committed Dec 30, 2023
1 parent c5de584 commit d32dd4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/lua/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!\")");
}
3 changes: 3 additions & 0 deletions src/lua/Thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

0 comments on commit d32dd4c

Please sign in to comment.