Skip to content

Commit

Permalink
Use new drop and PeekResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Mm2PL committed Jul 13, 2024
1 parent 57fec4d commit 3e9e61f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 43 deletions.
53 changes: 41 additions & 12 deletions src/controllers/plugins/LuaAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# include "controllers/plugins/PluginController.hpp"
# include "messages/MessageBuilder.hpp"
# include "providers/twitch/TwitchIrcServer.hpp"
# include "util/drop.hpp"

extern "C" {
# include <lauxlib.h>
Expand Down Expand Up @@ -73,15 +74,21 @@ int c2_register_command(lua_State *L)
}

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, "
"expected a string)");
pres.errorReason.push_back(
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;
}
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\"");
return 0;
}
Expand All @@ -106,10 +113,15 @@ int c2_register_callback(lua_State *L)
return 0;
}
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, "
"expected a string)");
pres.errorReason.push_back(
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;
}
if (lua_isnoneornil(L, 2))
Expand Down Expand Up @@ -139,14 +151,25 @@ int c2_log(lua_State *L)
luaL_error(L, "c2_log: internal error: no plugin?");
return 0;
}
if (lua_gettop(L) < 2)
{
// 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;
// This is almost the expansion of qCDebug() macro, actual thing is wrapped in a for loop
LogLevel lvl{};
if (!lua::pop(L, &lvl, 1))
auto pres = lua::pop(L, &lvl, 1);
if (!pres)
{
luaL_error(L, "Invalid log level, use one from c2.LogLevel.");
pres.errorReason.emplace_back(
"While getting log level (1st argument of c2.log)");
pres.throwAsLuaError(L);
return 0;
}
// This is almost the expansion of qCDebug() macro, actual thing is wrapped in a for loop
QDebug stream = qdebugStreamForLogLevel(lvl);
logHelper(L, pl, stream, logc);
return 0;
Expand Down Expand Up @@ -224,6 +247,8 @@ int g_load(lua_State *L)
utf8->toUnicode(data.constData(), data.size(), &state);
if (state.invalidChars != 0)
{
drop(data);
drop(state);
luaL_error(L, "invalid utf-8 in load() is not allowed");
return 0;
}
Expand Down Expand Up @@ -297,6 +322,10 @@ int loadfile(lua_State *L, const QString &str)
return 2;
}

drop(temp);
drop(info);
drop(datadir);
drop(dir);
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
lua_tostring(L, 1), filename, lua_tostring(L, -1));
}
Expand All @@ -306,10 +335,10 @@ int searcherAbsolute(lua_State *L)
auto name = QString::fromUtf8(luaL_checkstring(L, 1));
name = name.replace('.', QDir::separator());

QString filename;
auto *pl = getIApp()->getPlugins()->getPluginByStatePtr(L);
if (pl == nullptr)
{
drop(name);
return luaL_error(L, "searcherAbsolute: internal error: no plugin?");
}

Expand All @@ -332,7 +361,7 @@ int searcherRelative(lua_State *L)
lua::push(
L,
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;
}

Expand Down
72 changes: 45 additions & 27 deletions src/controllers/plugins/api/ChannelRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# include "messages/MessageBuilder.hpp"
# include "providers/twitch/TwitchChannel.hpp"
# include "providers/twitch/TwitchIrcServer.hpp"

# include "util/drop.hpp"
extern "C" {
# include <lauxlib.h>
# include <lua.h>
Expand Down Expand Up @@ -163,9 +163,15 @@ int ChannelRef::send_message(lua_State *L)
}

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;
}

Expand All @@ -192,10 +198,15 @@ int ChannelRef::add_system_message(lua_State *L)
}

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:add_system_message)");
pres.errorReason.push_back(QString("While getting text (1st argument "
"of Channel:add_system_message, "
"expected a string, got %1)")
.arg(luaL_typename(L, 1)));
drop(text);
pres.throwAsLuaError(L);
return 0;
}
ChannelPtr that = ChannelRef::getOrError(L);
Expand Down Expand Up @@ -280,25 +291,30 @@ int ChannelRef::get_by_name(lua_State *L)
{
luaL_error(L, "Channel.by_name needs exactly 2 arguments (channel "
"name and platform)");
lua_pushnil(L);
return 1;
return 0;
}
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, "
"expected a string)");
lua_pushnil(L);
return 1;
pres.errorReason.push_back(
QString("cannot get platform (2nd argument of Channel.by_name, "
"expected a string, got %1)")
.arg(luaL_typename(L, 1)));
pres.throwAsLuaError(L);
return 0;
}
QString name;
if (!lua::pop(L, &name))
pres = lua::pop(L, &name);
if (!pres)
{
luaL_error(L,
"cannot get channel name (1st argument of Channel.by_name, "
"expected a string)");
lua_pushnil(L);
return 1;
pres.errorReason.push_back(
QString("cannot get channel name (1st argument of Channel.by_name, "
"expected a string, got %1)")
.arg(luaL_typename(L, 1)));
drop(name);
pres.throwAsLuaError(L);
return 0;
}
auto chn = getIApp()->getTwitchAbstract()->getChannelOrEmpty(name);
lua::push(L, chn);
Expand All @@ -312,17 +328,19 @@ int ChannelRef::get_by_twitch_id(lua_State *L)
luaL_error(
L, "Channel.by_twitch_id needs exactly 1 arguments (channel owner "
"id)");
lua_pushnil(L);
return 1;
return 0;
}
QString id;
if (!lua::pop(L, &id))
auto pres = lua::pop(L, &id);
if (!pres)
{
luaL_error(L,
"cannot get channel name (1st argument of Channel.by_name, "
"expected a string)");
lua_pushnil(L);
return 1;
pres.errorReason.push_back(
QString("cannot get channel name (1st argument of Channel.by_name, "
"expected a string, got %1)")
.arg(luaL_typename(L, 1)));
drop(id);
pres.throwAsLuaError(L);
return 0;
}
auto chn = getIApp()->getTwitch()->getChannelOrEmptyByID(id);

Expand Down
11 changes: 7 additions & 4 deletions src/controllers/plugins/api/IOWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ int io_open(lua_State *L)
{
// we have a mode
QString smode;
if (!lua::pop(L, &smode))
auto pres = lua::pop(L, &smode);
if (!pres)
{
return luaL_error(
L,
"io.open mode (2nd argument) must be a string or not present");
pres.errorReason.push_back(
QString("io.open mode (2nd argument) must be a string or not "
"present, got %1")
.arg(luaL_typename(L, 1)));
pres.throwAsLuaError(L);
}
mode = LuaFileMode(smode);
if (!mode.error.isEmpty())
Expand Down

0 comments on commit 3e9e61f

Please sign in to comment.