mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Move load() to sol
This commit is contained in:
parent
a8438bfa50
commit
24ec7710a6
3 changed files with 16 additions and 49 deletions
|
@ -17,8 +17,11 @@
|
||||||
# include <QUrl>
|
# include <QUrl>
|
||||||
# include <sol/forward.hpp>
|
# include <sol/forward.hpp>
|
||||||
# include <sol/state_view.hpp>
|
# include <sol/state_view.hpp>
|
||||||
|
# include <sol/types.hpp>
|
||||||
# include <sol/variadic_args.hpp>
|
# include <sol/variadic_args.hpp>
|
||||||
|
# include <sol/variadic_results.hpp>
|
||||||
|
|
||||||
|
# include <string>
|
||||||
# include <utility>
|
# include <utility>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -156,49 +159,18 @@ int c2_later(lua_State *L)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int g_load(lua_State *L)
|
sol::variadic_results g_load(sol::this_state s, sol::object data)
|
||||||
{
|
{
|
||||||
# ifdef NDEBUG
|
# ifdef NDEBUG
|
||||||
luaL_error(L, "load() is only usable in debug mode");
|
throw std::runtime_error("load() is only usable in debug mode");
|
||||||
return 0;
|
|
||||||
# else
|
# else
|
||||||
auto countArgs = lua_gettop(L);
|
|
||||||
QByteArray data;
|
|
||||||
if (lua::peek(L, &data, 1))
|
|
||||||
{
|
|
||||||
auto *utf8 = QTextCodec::codecForName("UTF-8");
|
|
||||||
QTextCodec::ConverterState state;
|
|
||||||
utf8->toUnicode(data.constData(), data.size(), &state);
|
|
||||||
if (state.invalidChars != 0)
|
|
||||||
{
|
|
||||||
luaL_error(L, "invalid utf-8 in load() is not allowed");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
luaL_error(L, "using reader function in load() is not allowed");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < countArgs; i++)
|
// If you're modifying this PLEASE verify it works, Sol is very annoying about serialization
|
||||||
{
|
// - Mm2PL
|
||||||
lua_seti(L, LUA_REGISTRYINDEX, i);
|
sol::state_view lua(s);
|
||||||
}
|
auto load = lua.registry()["real_load"];
|
||||||
|
sol::protected_function_result ret = load(data, "=(load)", "t");
|
||||||
// fetch load and call it
|
return ret;
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, "real_load");
|
|
||||||
|
|
||||||
for (int i = 0; i < countArgs; i++)
|
|
||||||
{
|
|
||||||
lua_geti(L, LUA_REGISTRYINDEX, i);
|
|
||||||
lua_pushnil(L);
|
|
||||||
lua_seti(L, LUA_REGISTRYINDEX, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_call(L, countArgs, LUA_MULTRET);
|
|
||||||
|
|
||||||
return lua_gettop(L);
|
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -126,7 +126,7 @@ void c2_log(sol::this_state L, Plugin *pl, LogLevel lvl,
|
||||||
int c2_later(lua_State *L);
|
int c2_later(lua_State *L);
|
||||||
|
|
||||||
// These ones are global
|
// These ones are global
|
||||||
int g_load(lua_State *L);
|
sol::variadic_results g_load(sol::this_state s, sol::object data);
|
||||||
void g_print(sol::this_state L, Plugin *pl, sol::variadic_args args);
|
void g_print(sol::this_state L, Plugin *pl, sol::variadic_args args);
|
||||||
// NOLINTEND(readability-identifier-naming)
|
// NOLINTEND(readability-identifier-naming)
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,9 @@
|
||||||
# include <QJsonDocument>
|
# include <QJsonDocument>
|
||||||
# include <sol/forward.hpp>
|
# include <sol/forward.hpp>
|
||||||
# include <sol/sol.hpp>
|
# include <sol/sol.hpp>
|
||||||
|
# include <sol/types.hpp>
|
||||||
# include <sol/variadic_args.hpp>
|
# include <sol/variadic_args.hpp>
|
||||||
|
# include <sol/variadic_results.hpp>
|
||||||
|
|
||||||
# include <memory>
|
# include <memory>
|
||||||
# include <utility>
|
# include <utility>
|
||||||
|
@ -169,17 +171,9 @@ void PluginController::openLibrariesFor(Plugin *plugin, const QDir &pluginDir)
|
||||||
// possibly randomize this name at runtime to prevent some attacks?
|
// possibly randomize this name at runtime to prevent some attacks?
|
||||||
|
|
||||||
# ifndef NDEBUG
|
# ifndef NDEBUG
|
||||||
lua_getfield(L, gtable, "load");
|
lua.registry()["real_load"] = lua.globals()["load"];
|
||||||
lua_setfield(L, LUA_REGISTRYINDEX, "real_load");
|
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
// NOLINTNEXTLINE(*-avoid-c-arrays)
|
|
||||||
static const luaL_Reg replacementFuncs[] = {
|
|
||||||
{"load", lua::api::g_load},
|
|
||||||
{nullptr, nullptr},
|
|
||||||
};
|
|
||||||
luaL_setfuncs(L, replacementFuncs, 0);
|
|
||||||
|
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_setfield(L, gtable, "loadfile");
|
lua_setfield(L, gtable, "loadfile");
|
||||||
|
|
||||||
|
@ -282,6 +276,7 @@ void PluginController::initSol(sol::state_view &lua, Plugin *plugin)
|
||||||
[plugin](sol::this_state s, sol::variadic_args args) {
|
[plugin](sol::this_state s, sol::variadic_args args) {
|
||||||
lua::api::g_print(s, plugin, args);
|
lua::api::g_print(s, plugin, args);
|
||||||
});
|
});
|
||||||
|
g.set_function("load", &lua::api::g_load);
|
||||||
|
|
||||||
sol::table c2 = g["c2"];
|
sol::table c2 = g["c2"];
|
||||||
c2.set_function("register_command",
|
c2.set_function("register_command",
|
||||||
|
|
Loading…
Reference in a new issue