From 24ec7710a6ca987d5b12ba5ed742bd9566cd2828 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Mon, 7 Oct 2024 15:41:55 +0200 Subject: [PATCH] Move load() to sol --- src/controllers/plugins/LuaAPI.cpp | 50 +++++--------------- src/controllers/plugins/LuaAPI.hpp | 2 +- src/controllers/plugins/PluginController.cpp | 13 ++--- 3 files changed, 16 insertions(+), 49 deletions(-) diff --git a/src/controllers/plugins/LuaAPI.cpp b/src/controllers/plugins/LuaAPI.cpp index 72844174a..e6acf0393 100644 --- a/src/controllers/plugins/LuaAPI.cpp +++ b/src/controllers/plugins/LuaAPI.cpp @@ -17,8 +17,11 @@ # include # include # include +# include # include +# include +# include # include 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 } diff --git a/src/controllers/plugins/LuaAPI.hpp b/src/controllers/plugins/LuaAPI.hpp index 7e7f96d0d..75f2af0fa 100644 --- a/src/controllers/plugins/LuaAPI.hpp +++ b/src/controllers/plugins/LuaAPI.hpp @@ -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) diff --git a/src/controllers/plugins/PluginController.cpp b/src/controllers/plugins/PluginController.cpp index 3f30e82dc..3b0c2da13 100644 --- a/src/controllers/plugins/PluginController.cpp +++ b/src/controllers/plugins/PluginController.cpp @@ -24,7 +24,9 @@ # include # include # include +# include # include +# include # include # include @@ -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",