From a10b34dc94da6ede266420e91d75e6b0e4bddbd0 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Thu, 10 Jul 2025 12:16:54 +0200 Subject: [PATCH] fix(lua): detect and print syntax errors --- core/src/objects/script.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/core/src/objects/script.cpp b/core/src/objects/script.cpp index 86a975c..f5bfb38 100644 --- a/core/src/objects/script.cpp +++ b/core/src/objects/script.cpp @@ -27,6 +27,7 @@ void Script::Run() { std::shared_ptr scriptContext = dataModel().value()->GetService(); lua_State* L = scriptContext->state; + int top = lua_gettop(L); // Create thread this->thread = lua_newthread(L); @@ -54,13 +55,23 @@ void Script::Run() { lua_pop(Lt, 1); // _G - // Load source and push onto thread stack as upvalue for wrapper closure - luaL_loadbuffer(Lt, source.c_str(), source.size(), this->GetFullName().c_str()); + // Load source code and push onto thread as upvalue for wrapper + int status = luaL_loadbuffer(Lt, source.c_str(), source.size(), this->GetFullName().c_str()); + if (status != LUA_OK) { + // Failed to parse/load chunk + Logger::error(lua_tostring(Lt, -1)); + + lua_settop(L, top); + return; + } + + // Push wrapper as thread function lua_pushcclosure(Lt, script_wrapper, 1); lua_resume(Lt, 0); lua_pop(L, 1); // Pop the thread + lua_settop(L, top); } void Script::Stop() {