2024-04-12 14:36:37 +02:00
|
|
|
// Copyright (C) 2024 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "lua_global.h"
|
|
|
|
|
|
|
|
|
|
#include <extensionsystem/iplugin.h>
|
|
|
|
|
#include <extensionsystem/pluginspec.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/expected.h>
|
|
|
|
|
#include <utils/filepath.h>
|
|
|
|
|
|
|
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
|
|
|
|
|
// this needs to be included after sol/sol.hpp!
|
|
|
|
|
#include "luaqttypes.h"
|
|
|
|
|
|
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
namespace Lua {
|
|
|
|
|
class LuaEnginePrivate;
|
|
|
|
|
class LuaPluginSpec;
|
|
|
|
|
|
|
|
|
|
struct CoroutineState
|
|
|
|
|
{
|
|
|
|
|
bool isMainThread;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class LUA_EXPORT LuaEngine
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
LuaEngine();
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using PackageProvider = std::function<sol::object(sol::state_view)>;
|
|
|
|
|
|
|
|
|
|
~LuaEngine();
|
|
|
|
|
static LuaEngine &instance();
|
|
|
|
|
|
|
|
|
|
Utils::expected_str<LuaPluginSpec *> loadPlugin(const Utils::FilePath &path);
|
2024-04-23 07:47:42 +02:00
|
|
|
Utils::expected_str<void> prepareSetup(
|
|
|
|
|
sol::state_view &lua, const LuaPluginSpec &pluginSpec, sol::optional<sol::table> hookTable);
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
|
static void registerProvider(const QString &packageName, const PackageProvider &provider);
|
2024-04-19 14:03:25 +02:00
|
|
|
static void autoRegister(const std::function<void(sol::state_view)> ®isterFunction);
|
|
|
|
|
static void registerHook(QString name, const std::function<void(sol::function)> &hookProvider);
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
|
static Utils::expected_str<void> connectHooks(sol::state_view lua, const sol::table &hookTable);
|
|
|
|
|
|
|
|
|
|
static bool isCoroutine(lua_State *state);
|
|
|
|
|
|
2024-04-19 14:03:25 +02:00
|
|
|
static sol::table toTable(const sol::state_view &lua, const QJsonValue &v);
|
2024-04-12 14:36:37 +02:00
|
|
|
static QJsonValue toJson(const sol::table &t);
|
|
|
|
|
|
2024-04-19 14:03:25 +02:00
|
|
|
static QStringList variadicToStringList(const sol::variadic_args &vargs);
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
|
template<typename R, typename... Args>
|
2024-04-19 14:03:25 +02:00
|
|
|
static Utils::expected_str<R> safe_call(const sol::protected_function &function, Args &&...args)
|
2024-04-12 14:36:37 +02:00
|
|
|
{
|
|
|
|
|
sol::protected_function_result result = function(std::forward<Args>(args)...);
|
|
|
|
|
if (!result.valid()) {
|
|
|
|
|
sol::error err = result;
|
|
|
|
|
return Utils::make_unexpected(QString::fromLocal8Bit(err.what()));
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return result.get<R>();
|
|
|
|
|
} catch (std::runtime_error &e) {
|
|
|
|
|
return Utils::make_unexpected(QString::fromLocal8Bit(e.what()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename... Args>
|
2024-04-19 14:03:25 +02:00
|
|
|
static Utils::expected_str<void> void_safe_call(
|
|
|
|
|
const sol::protected_function &function, Args &&...args)
|
2024-04-12 14:36:37 +02:00
|
|
|
{
|
|
|
|
|
sol::protected_function_result result = function(std::forward<Args>(args)...);
|
|
|
|
|
if (!result.valid()) {
|
|
|
|
|
sol::error err = result;
|
|
|
|
|
return Utils::make_unexpected(QString::fromLocal8Bit(err.what()));
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Utils::expected_str<void> connectHooks(
|
2024-04-19 14:03:25 +02:00
|
|
|
sol::state_view lua, const sol::table &table, const QString &path);
|
2024-04-12 14:36:37 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::unique_ptr<LuaEnginePrivate> d;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Lua
|