Use new drop and PeekResult

This commit is contained in:
Mm2PL 2024-07-13 23:42:38 +02:00
parent 57fec4d41d
commit d97b720567
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
4 changed files with 99 additions and 45 deletions

View file

@ -8,6 +8,7 @@
# include "controllers/plugins/PluginController.hpp" # include "controllers/plugins/PluginController.hpp"
# include "messages/MessageBuilder.hpp" # include "messages/MessageBuilder.hpp"
# include "providers/twitch/TwitchIrcServer.hpp" # include "providers/twitch/TwitchIrcServer.hpp"
# include "util/drop.hpp"
extern "C" { extern "C" {
# include <lauxlib.h> # include <lauxlib.h>
@ -73,15 +74,21 @@ int c2_register_command(lua_State *L)
} }
QString name; QString name;
if (!lua::peek(L, &name, 1)) auto pres = lua::peek(L, &name, 1);
if (!pres)
{ {
luaL_error(L, "cannot get command name (1st arg of register_command, " pres.errorReason.push_back(
"expected a string)"); QString("While getting command name (1st arg of register_command, "
"expected a string got %1)")
.arg(luaL_typename(L, 1)));
drop(name);
pres.throwAsLuaError(L);
return 0; return 0;
} }
if (lua_isnoneornil(L, 2)) if (lua_isnoneornil(L, 2))
{ {
luaL_error(L, "missing argument for register_command: function " drop(name);
luaL_error(L, "Missing argument for register_command: function "
"\"pointer\""); "\"pointer\"");
return 0; return 0;
} }
@ -106,10 +113,15 @@ int c2_register_callback(lua_State *L)
return 0; return 0;
} }
EventType evtType{}; EventType evtType{};
if (!lua::peek(L, &evtType, 1)) auto pres = lua::peek(L, &evtType, 1);
if (!pres)
{ {
luaL_error(L, "cannot get event name (1st arg of register_callback, " pres.errorReason.push_back(
"expected a string)"); QString("cannot get event name (1st arg of register_callback, "
"expected a string, got %1)")
.arg(lua_typename(L, 1)));
// no types to drop yet
pres.throwAsLuaError(L);
return 0; return 0;
} }
if (lua_isnoneornil(L, 2)) if (lua_isnoneornil(L, 2))
@ -139,14 +151,25 @@ int c2_log(lua_State *L)
luaL_error(L, "c2_log: internal error: no plugin?"); luaL_error(L, "c2_log: internal error: no plugin?");
return 0; return 0;
} }
auto logc = lua_gettop(L) - 1; if (lua_gettop(L) < 2)
// This is almost the expansion of qCDebug() macro, actual thing is wrapped in a for loop
LogLevel lvl{};
if (!lua::pop(L, &lvl, 1))
{ {
luaL_error(L, "Invalid log level, use one from c2.LogLevel."); // no arguments
return luaL_error(L,
"c2.log expects at least two arguments, a log level "
"(c2.LogLevel) and an object to print "
"(usually a string)");
}
auto logc = lua_gettop(L) - 1;
LogLevel lvl{};
auto pres = lua::pop(L, &lvl, 1);
if (!pres)
{
pres.errorReason.emplace_back(
"While getting log level (1st argument of c2.log)");
pres.throwAsLuaError(L);
return 0; return 0;
} }
// This is almost the expansion of qCDebug() macro, actual thing is wrapped in a for loop
QDebug stream = qdebugStreamForLogLevel(lvl); QDebug stream = qdebugStreamForLogLevel(lvl);
logHelper(L, pl, stream, logc); logHelper(L, pl, stream, logc);
return 0; return 0;
@ -224,6 +247,8 @@ int g_load(lua_State *L)
utf8->toUnicode(data.constData(), data.size(), &state); utf8->toUnicode(data.constData(), data.size(), &state);
if (state.invalidChars != 0) if (state.invalidChars != 0)
{ {
drop(data);
drop(state);
luaL_error(L, "invalid utf-8 in load() is not allowed"); luaL_error(L, "invalid utf-8 in load() is not allowed");
return 0; return 0;
} }
@ -297,6 +322,10 @@ int loadfile(lua_State *L, const QString &str)
return 2; return 2;
} }
drop(temp);
drop(info);
drop(datadir);
drop(dir);
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
lua_tostring(L, 1), filename, lua_tostring(L, -1)); lua_tostring(L, 1), filename, lua_tostring(L, -1));
} }
@ -306,10 +335,10 @@ int searcherAbsolute(lua_State *L)
auto name = QString::fromUtf8(luaL_checkstring(L, 1)); auto name = QString::fromUtf8(luaL_checkstring(L, 1));
name = name.replace('.', QDir::separator()); name = name.replace('.', QDir::separator());
QString filename;
auto *pl = getIApp()->getPlugins()->getPluginByStatePtr(L); auto *pl = getIApp()->getPlugins()->getPluginByStatePtr(L);
if (pl == nullptr) if (pl == nullptr)
{ {
drop(name);
return luaL_error(L, "searcherAbsolute: internal error: no plugin?"); return luaL_error(L, "searcherAbsolute: internal error: no plugin?");
} }
@ -332,7 +361,7 @@ int searcherRelative(lua_State *L)
lua::push( lua::push(
L, L,
QString( QString(
"Unable to load relative to file:caller has no source file")); "Unable to load relative to file: caller has no source file"));
return 1; return 1;
} }

View file

@ -8,7 +8,7 @@
# include "messages/MessageBuilder.hpp" # include "messages/MessageBuilder.hpp"
# include "providers/twitch/TwitchChannel.hpp" # include "providers/twitch/TwitchChannel.hpp"
# include "providers/twitch/TwitchIrcServer.hpp" # include "providers/twitch/TwitchIrcServer.hpp"
# include "util/drop.hpp"
extern "C" { extern "C" {
# include <lauxlib.h> # include <lauxlib.h>
# include <lua.h> # include <lua.h>
@ -163,9 +163,15 @@ int ChannelRef::send_message(lua_State *L)
} }
QString text; QString text;
if (!lua::pop(L, &text)) auto pres = lua::pop(L, &text);
if (!pres)
{ {
luaL_error(L, "cannot get text (1st argument of Channel:send_message)"); pres.errorReason.push_back(
QString("While getting text (1st argument of Channel:send_message, "
"expected a string, got %1)")
.arg(luaL_typename(L, 1)));
drop(text);
pres.throwAsLuaError(L);
return 0; return 0;
} }
@ -192,10 +198,15 @@ int ChannelRef::add_system_message(lua_State *L)
} }
QString text; QString text;
if (!lua::pop(L, &text)) auto pres = lua::pop(L, &text);
if (!pres)
{ {
luaL_error( pres.errorReason.push_back(QString("While getting text (1st argument "
L, "cannot get text (1st argument of Channel:add_system_message)"); "of Channel:add_system_message, "
"expected a string, got %1)")
.arg(luaL_typename(L, 1)));
drop(text);
pres.throwAsLuaError(L);
return 0; return 0;
} }
ChannelPtr that = ChannelRef::getOrError(L); ChannelPtr that = ChannelRef::getOrError(L);
@ -280,25 +291,30 @@ int ChannelRef::get_by_name(lua_State *L)
{ {
luaL_error(L, "Channel.by_name needs exactly 2 arguments (channel " luaL_error(L, "Channel.by_name needs exactly 2 arguments (channel "
"name and platform)"); "name and platform)");
lua_pushnil(L); return 0;
return 1;
} }
LPlatform platform{}; LPlatform platform{};
if (!lua::pop(L, &platform)) auto pres = lua::pop(L, &platform);
if (!pres)
{ {
luaL_error(L, "cannot get platform (2nd argument of Channel.by_name, " pres.errorReason.push_back(
"expected a string)"); QString("cannot get platform (2nd argument of Channel.by_name, "
lua_pushnil(L); "expected a string, got %1)")
return 1; .arg(luaL_typename(L, 1)));
pres.throwAsLuaError(L);
return 0;
} }
QString name; QString name;
if (!lua::pop(L, &name)) pres = lua::pop(L, &name);
if (!pres)
{ {
luaL_error(L, pres.errorReason.push_back(
"cannot get channel name (1st argument of Channel.by_name, " QString("cannot get channel name (1st argument of Channel.by_name, "
"expected a string)"); "expected a string, got %1)")
lua_pushnil(L); .arg(luaL_typename(L, 1)));
return 1; drop(name);
pres.throwAsLuaError(L);
return 0;
} }
auto chn = getIApp()->getTwitchAbstract()->getChannelOrEmpty(name); auto chn = getIApp()->getTwitchAbstract()->getChannelOrEmpty(name);
lua::push(L, chn); lua::push(L, chn);
@ -312,17 +328,19 @@ int ChannelRef::get_by_twitch_id(lua_State *L)
luaL_error( luaL_error(
L, "Channel.by_twitch_id needs exactly 1 arguments (channel owner " L, "Channel.by_twitch_id needs exactly 1 arguments (channel owner "
"id)"); "id)");
lua_pushnil(L); return 0;
return 1;
} }
QString id; QString id;
if (!lua::pop(L, &id)) auto pres = lua::pop(L, &id);
if (!pres)
{ {
luaL_error(L, pres.errorReason.push_back(
"cannot get channel name (1st argument of Channel.by_name, " QString("cannot get channel name (1st argument of Channel.by_name, "
"expected a string)"); "expected a string, got %1)")
lua_pushnil(L); .arg(luaL_typename(L, 1)));
return 1; drop(id);
pres.throwAsLuaError(L);
return 0;
} }
auto chn = getIApp()->getTwitch()->getChannelOrEmptyByID(id); auto chn = getIApp()->getTwitch()->getChannelOrEmptyByID(id);

View file

@ -9,6 +9,7 @@
# include "controllers/plugins/LuaAPI.hpp" # include "controllers/plugins/LuaAPI.hpp"
# include "controllers/plugins/LuaUtilities.hpp" # include "controllers/plugins/LuaUtilities.hpp"
# include "util/DebugCount.hpp" # include "util/DebugCount.hpp"
# include "util/drop.hpp"
extern "C" { extern "C" {
# include <lauxlib.h> # include <lauxlib.h>
@ -264,12 +265,15 @@ int HTTPRequest::set_header(lua_State *L)
std::string value; std::string value;
if (!lua::pop(L, &value)) if (!lua::pop(L, &value))
{ {
drop(value);
return luaL_error( return luaL_error(
L, "cannot get value (2nd argument of HTTPRequest:set_header)"); L, "cannot get value (2nd argument of HTTPRequest:set_header)");
} }
std::string name; std::string name;
if (!lua::pop(L, &name)) if (!lua::pop(L, &name))
{ {
drop(value);
drop(name);
return luaL_error( return luaL_error(
L, "cannot get name (1st argument of HTTPRequest:set_header)"); L, "cannot get name (1st argument of HTTPRequest:set_header)");
} }

View file

@ -113,11 +113,14 @@ int io_open(lua_State *L)
{ {
// we have a mode // we have a mode
QString smode; QString smode;
if (!lua::pop(L, &smode)) auto pres = lua::pop(L, &smode);
if (!pres)
{ {
return luaL_error( pres.errorReason.push_back(
L, QString("io.open mode (2nd argument) must be a string or not "
"io.open mode (2nd argument) must be a string or not present"); "present, got %1")
.arg(luaL_typename(L, 1)));
pres.throwAsLuaError(L);
} }
mode = LuaFileMode(smode); mode = LuaFileMode(smode);
if (!mode.error.isEmpty()) if (!mode.error.isEmpty())