Move plugins to Sol (#5622)

Co-authored-by: Nerixyz <nerixdev@outlook.de>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Mm2PL
2024-10-20 11:57:05 +02:00
committed by GitHub
parent 9345050868
commit 352a4ec132
42 changed files with 2120 additions and 2208 deletions
+26 -105
View File
@@ -2,77 +2,28 @@
# include "controllers/plugins/api/HTTPResponse.hpp"
# include "common/network/NetworkResult.hpp"
# include "controllers/plugins/LuaAPI.hpp"
# include "controllers/plugins/SolTypes.hpp"
# include "util/DebugCount.hpp"
extern "C" {
# include <lauxlib.h>
}
# include <sol/raii.hpp>
# include <sol/types.hpp>
# include <utility>
namespace chatterino::lua::api {
// NOLINTBEGIN(*vararg)
// NOLINTNEXTLINE(*-avoid-c-arrays)
static const luaL_Reg HTTP_RESPONSE_METHODS[] = {
{"data", &HTTPResponse::data_wrap},
{"status", &HTTPResponse::status_wrap},
{"error", &HTTPResponse::error_wrap},
{nullptr, nullptr},
};
void HTTPResponse::createMetatable(lua_State *L)
void HTTPResponse::createUserType(sol::table &c2)
{
lua::StackGuard guard(L, 1);
c2.new_usertype<HTTPResponse>( //
"HTTPResponse", sol::no_constructor,
// metamethods
sol::meta_method::to_string, &HTTPResponse::to_string, //
luaL_newmetatable(L, "c2.HTTPResponse");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); // clone metatable
lua_settable(L, -3); // metatable.__index = metatable
// Generic ISharedResource stuff
lua_pushstring(L, "__gc");
lua_pushcfunction(L, (&SharedPtrUserData<UserData::Type::HTTPResponse,
HTTPResponse>::destroy));
lua_settable(L, -3); // metatable.__gc = SharedPtrUserData<...>::destroy
luaL_setfuncs(L, HTTP_RESPONSE_METHODS, 0);
}
std::shared_ptr<HTTPResponse> HTTPResponse::getOrError(lua_State *L,
StackIdx where)
{
if (lua_gettop(L) < 1)
{
// The nullptr is there just to appease the compiler, luaL_error is no return
luaL_error(L, "Called c2.HTTPResponse method without a request object");
return nullptr;
}
if (lua_isuserdata(L, where) == 0)
{
luaL_error(L, "Called c2.HTTPResponse method with a non-userdata "
"'self' argument");
return nullptr;
}
// luaL_checkudata is no-return if check fails
auto *checked = luaL_checkudata(L, where, "c2.HTTPResponse");
auto *data =
SharedPtrUserData<UserData::Type::HTTPResponse, HTTPResponse>::from(
checked);
if (data == nullptr)
{
luaL_error(L, "Called c2.HTTPResponse method with an invalid pointer");
return nullptr;
}
lua_remove(L, where);
if (data->target == nullptr)
{
luaL_error(
L,
"Internal error: SharedPtrUserData<UserData::Type::HTTPResponse, "
"HTTPResponse>::target was null. This is a Chatterino bug!");
return nullptr;
}
return data->target;
"data", &HTTPResponse::data, //
"status", &HTTPResponse::status, //
"error", &HTTPResponse::error //
);
}
HTTPResponse::HTTPResponse(NetworkResult res)
@@ -85,60 +36,30 @@ HTTPResponse::~HTTPResponse()
DebugCount::decrease("lua::api::HTTPResponse");
}
int HTTPResponse::data_wrap(lua_State *L)
QByteArray HTTPResponse::data()
{
lua::StackGuard guard(L, 0); // 1 in, 1 out
auto ptr = HTTPResponse::getOrError(L, 1);
return ptr->data(L);
return this->result_.getData();
}
int HTTPResponse::data(lua_State *L)
std::optional<int> HTTPResponse::status()
{
lua::push(L, this->result_.getData().toStdString());
return 1;
return this->result_.status();
}
int HTTPResponse::status_wrap(lua_State *L)
QString HTTPResponse::error()
{
lua::StackGuard guard(L, 0); // 1 in, 1 out
auto ptr = HTTPResponse::getOrError(L, 1);
return ptr->status(L);
return this->result_.formatError();
}
int HTTPResponse::status(lua_State *L)
QString HTTPResponse::to_string()
{
lua::push(L, this->result_.status());
return 1;
if (this->status().has_value())
{
return QStringView(u"<c2.HTTPResponse status %1>")
.arg(QString::number(*this->status()));
}
return "<c2.HTTPResponse no status>";
}
int HTTPResponse::error_wrap(lua_State *L)
{
lua::StackGuard guard(L, 0); // 1 in, 1 out
auto ptr = HTTPResponse::getOrError(L, 1);
return ptr->error(L);
}
int HTTPResponse::error(lua_State *L)
{
lua::push(L, this->result_.formatError());
return 1;
}
// NOLINTEND(*vararg)
} // namespace chatterino::lua::api
namespace chatterino::lua {
StackIdx push(lua_State *L, std::shared_ptr<api::HTTPResponse> request)
{
using namespace chatterino::lua::api;
// Prepare table
SharedPtrUserData<UserData::Type::HTTPResponse, HTTPResponse>::create(
L, std::move(request));
luaL_getmetatable(L, "c2.HTTPResponse");
lua_setmetatable(L, -2);
return lua_gettop(L);
}
} // namespace chatterino::lua
#endif