Lua: Add temporary appDataDir to runScript

Change-Id: Idd458df7c06d3c7d93ed692c873b94f5eb7c74fe
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-08-29 15:26:10 +02:00
parent 68074eae53
commit 167c98cc74

View File

@@ -18,6 +18,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QStandardPaths> #include <QStandardPaths>
#include <QTemporaryDir>
using namespace Utils; using namespace Utils;
@@ -52,10 +53,14 @@ class LuaStateImpl : public Utils::LuaState
{ {
public: public:
sol::state lua; sol::state lua;
QTemporaryDir appDataDir;
}; };
void prepareLuaState( void prepareLuaState(
sol::state &lua, const QString &name, const std::function<void(sol::state &)> &customizeState) sol::state &lua,
const QString &name,
const std::function<void(sol::state &)> &customizeState,
const FilePath &appDataPath)
{ {
lua.open_libraries( lua.open_libraries(
sol::lib::base, sol::lib::base,
@@ -86,7 +91,7 @@ void prepareLuaState(
return self.name; return self.name;
})); }));
lua["PluginSpec"] = ScriptPluginSpec{name, {}, std::make_unique<QObject>()}; lua["PluginSpec"] = ScriptPluginSpec{name, appDataPath, std::make_unique<QObject>()};
for (const auto &[name, func] : d->m_providers.asKeyValueRange()) { for (const auto &[name, func] : d->m_providers.asKeyValueRange()) {
lua["package"]["preload"][name.toStdString()] = [func = func](const sol::this_state &s) { lua["package"]["preload"][name.toStdString()] = [func = func](const sol::this_state &s) {
@@ -107,7 +112,8 @@ std::unique_ptr<Utils::LuaState> runScript(
{ {
std::unique_ptr<LuaStateImpl> opaque = std::make_unique<LuaStateImpl>(); std::unique_ptr<LuaStateImpl> opaque = std::make_unique<LuaStateImpl>();
prepareLuaState(opaque->lua, name, customizeState); prepareLuaState(
opaque->lua, name, customizeState, FilePath::fromUserInput(opaque->appDataDir.path()));
auto result auto result
= opaque->lua = opaque->lua
@@ -129,7 +135,7 @@ sol::protected_function_result runFunction(
const QString &name, const QString &name,
std::function<void(sol::state &)> customizeState) std::function<void(sol::state &)> customizeState)
{ {
prepareLuaState(lua, name, customizeState); prepareLuaState(lua, name, customizeState, {});
return lua.safe_script(script.toStdString(), sol::script_pass_on_error, name.toStdString()); return lua.safe_script(script.toStdString(), sol::script_pass_on_error, name.toStdString());
} }