Move load() to sol

This commit is contained in:
Mm2PL 2024-10-07 15:41:55 +02:00
parent a8438bfa50
commit 24ec7710a6
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
3 changed files with 16 additions and 49 deletions

View file

@ -17,8 +17,11 @@
# include <QUrl>
# include <sol/forward.hpp>
# include <sol/state_view.hpp>
# include <sol/types.hpp>
# include <sol/variadic_args.hpp>
# include <sol/variadic_results.hpp>
# include <string>
# include <utility>
namespace {
@ -156,49 +159,18 @@ int c2_later(lua_State *L)
return 0;
}
int g_load(lua_State *L)
sol::variadic_results g_load(sol::this_state s, sol::object data)
{
# ifdef NDEBUG
luaL_error(L, "load() is only usable in debug mode");
return 0;
throw std::runtime_error("load() is only usable in debug mode");
# 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++)
{
lua_seti(L, LUA_REGISTRYINDEX, i);
}
// fetch load and call it
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);
// If you're modifying this PLEASE verify it works, Sol is very annoying about serialization
// - Mm2PL
sol::state_view lua(s);
auto load = lua.registry()["real_load"];
sol::protected_function_result ret = load(data, "=(load)", "t");
return ret;
# endif
}

View file

@ -126,7 +126,7 @@ void c2_log(sol::this_state L, Plugin *pl, LogLevel lvl,
int c2_later(lua_State *L);
// 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);
// NOLINTEND(readability-identifier-naming)

View file

@ -24,7 +24,9 @@
# include <QJsonDocument>
# include <sol/forward.hpp>
# include <sol/sol.hpp>
# include <sol/types.hpp>
# include <sol/variadic_args.hpp>
# include <sol/variadic_results.hpp>
# include <memory>
# include <utility>
@ -169,17 +171,9 @@ void PluginController::openLibrariesFor(Plugin *plugin, const QDir &pluginDir)
// possibly randomize this name at runtime to prevent some attacks?
# ifndef NDEBUG
lua_getfield(L, gtable, "load");
lua_setfield(L, LUA_REGISTRYINDEX, "real_load");
lua.registry()["real_load"] = lua.globals()["load"];
# 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_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) {
lua::api::g_print(s, plugin, args);
});
g.set_function("load", &lua::api::g_load);
sol::table c2 = g["c2"];
c2.set_function("register_command",