Skip to content

Commit

Permalink
* [tz::lua] state::execute now has a new assert_on_failure param, whi…
Browse files Browse the repository at this point in the history
…ch debug asserts if the code execution returns an error
  • Loading branch information
harrand committed Aug 5, 2023
1 parent 9f6f639 commit 5c25cbb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/tz/lua/state.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "tz/lua/state.hpp"
#include "tz/core/debug.hpp"
#include <cstdint>

extern "C"
{
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
}
#include <cstdint>

namespace tz::lua
{
Expand All @@ -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<lua_State*>(this->lstate), path) == false;
auto* s = static_cast<lua_State*>(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<lua_State*>(this->lstate), lua_src) == false;
auto* s = static_cast<lua_State*>(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 = {};
Expand Down
4 changes: 2 additions & 2 deletions src/tz/lua/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit 5c25cbb

Please sign in to comment.