mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Introduce PeekResult
This commit is contained in:
parent
5f9268811f
commit
9155242f76
2 changed files with 40 additions and 0 deletions
|
@ -266,5 +266,29 @@ QString toString(lua_State *L, StackIdx idx)
|
||||||
const auto *ptr = luaL_tolstring(L, idx, &len);
|
const auto *ptr = luaL_tolstring(L, idx, &len);
|
||||||
return QString::fromUtf8(ptr, int(len));
|
return QString::fromUtf8(ptr, int(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PeekResult::throwAsLuaError(lua_State *L)
|
||||||
|
{
|
||||||
|
// Note that this uses lua buffers to ensure deallocation of the error string
|
||||||
|
luaL_Buffer buf;
|
||||||
|
luaL_buffinit(L, &buf);
|
||||||
|
bool first = true;
|
||||||
|
for (const auto &s : this->errorReason)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
{
|
||||||
|
luaL_addchar(&buf, '\n');
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
luaL_addstring(&buf, s.toStdString().c_str());
|
||||||
|
}
|
||||||
|
// Remove our copy
|
||||||
|
this->errorReason.clear();
|
||||||
|
|
||||||
|
luaL_pushresult(&buf);
|
||||||
|
lua_error(L); // This call never returns
|
||||||
|
assert(false && "unreachable");
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace chatterino::lua
|
} // namespace chatterino::lua
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -69,6 +69,22 @@ StackIdx push(lua_State *L, const bool &b);
|
||||||
StackIdx push(lua_State *L, const int &b);
|
StackIdx push(lua_State *L, const int &b);
|
||||||
StackIdx push(lua_State *L, const api::CompletionEvent &ev);
|
StackIdx push(lua_State *L, const api::CompletionEvent &ev);
|
||||||
|
|
||||||
|
struct PeekResult {
|
||||||
|
bool ok = true;
|
||||||
|
std::vector<QString> errorReason = {};
|
||||||
|
|
||||||
|
operator bool() const
|
||||||
|
{
|
||||||
|
return this->ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combines errorReason into a single string and then calls luaL_error.
|
||||||
|
* As luaL_error never returns this function does not either.
|
||||||
|
*/
|
||||||
|
[[noreturn]] void throwAsLuaError(lua_State *L);
|
||||||
|
};
|
||||||
|
|
||||||
// returns OK?
|
// returns OK?
|
||||||
bool peek(lua_State *L, int *out, StackIdx idx = -1);
|
bool peek(lua_State *L, int *out, StackIdx idx = -1);
|
||||||
bool peek(lua_State *L, bool *out, StackIdx idx = -1);
|
bool peek(lua_State *L, bool *out, StackIdx idx = -1);
|
||||||
|
|
Loading…
Reference in a new issue