From 5c25cbbf0afe046088b61e4ecca14518ee143d56 Mon Sep 17 00:00:00 2001 From: harrand Date: Sat, 5 Aug 2023 16:48:08 +0100 Subject: [PATCH] * [tz::lua] state::execute now has a new assert_on_failure param, which debug asserts if the code execution returns an error --- src/tz/lua/state.cpp | 18 +++++++++++++----- src/tz/lua/state.hpp | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/tz/lua/state.cpp b/src/tz/lua/state.cpp index 524c5a48fe..ea2764b10e 100644 --- a/src/tz/lua/state.cpp +++ b/src/tz/lua/state.cpp @@ -1,11 +1,13 @@ #include "tz/lua/state.hpp" +#include "tz/core/debug.hpp" +#include + extern "C" { #include "lauxlib.h" #include "lua.h" #include "lualib.h" } -#include namespace tz::lua { @@ -17,14 +19,20 @@ namespace tz::lua return this->lstate != nullptr; } - bool state::execute_file(const char* path) const + bool state::execute_file(const char* path, bool assert_on_failure) const { - return luaL_dofile(static_cast(this->lstate), path) == false; + auto* s = static_cast(this->lstate); + bool ret = luaL_dofile(s, path) == false; + tz::assert(!assert_on_failure || ret, "Lua Error: %s", lua_tostring(s, -1)); + return ret; } - bool state::execute(const char* lua_src) const + bool state::execute(const char* lua_src, bool assert_on_failure) const { - return luaL_dostring(static_cast(this->lstate), lua_src) == false; + auto* s = static_cast(this->lstate); + bool ret = luaL_dostring(s, lua_src) == false; + tz::assert(!assert_on_failure || ret, "Lua Error: %s", lua_tostring(s, -1)); + return ret; } state defstate = {}; diff --git a/src/tz/lua/state.hpp b/src/tz/lua/state.hpp index a42e5af034..7dcabe9f6d 100644 --- a/src/tz/lua/state.hpp +++ b/src/tz/lua/state.hpp @@ -10,8 +10,8 @@ namespace tz::lua state() = default; state(void* lstate); bool valid() const; - bool execute_file(const char* path) const; - bool execute(const char* lua_src) const; + bool execute_file(const char* path, bool assert_on_failure = true) const; + bool execute(const char* lua_src, bool assert_on_failure = true) const; private: void* lstate = nullptr; };