From cfb64f3518a362f248839d656160f566ff436ca3 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Fri, 27 Sep 2024 12:11:32 +0200 Subject: [PATCH] Lua: Move inspect and async into resources Change-Id: I257323861823ed7858efdb265a78cc572c2e7273 Reviewed-by: Eike Ziller --- qt_attributions.json | 9 +++---- src/plugins/lua/CMakeLists.txt | 3 ++- src/plugins/lua/lua.qbs | 1 - src/plugins/lua/luaengine.cpp | 17 +++++++++++++ src/plugins/lua/luaengine.h | 1 + src/plugins/lua/luaplugin.cpp | 5 ++-- .../{bindings => scripts}/ASYNC-LICENSE.txt | 0 .../plugins/lua/scripts}/INSPECT-LICENSE.txt | 0 .../{bindings/async.cpp => scripts/async.lua} | 25 +++---------------- .../plugins/lua/scripts}/inspect.lua | 0 10 files changed, 31 insertions(+), 30 deletions(-) rename src/plugins/lua/{bindings => scripts}/ASYNC-LICENSE.txt (100%) rename {share/qtcreator/lua-plugins/luatests => src/plugins/lua/scripts}/INSPECT-LICENSE.txt (100%) rename src/plugins/lua/{bindings/async.cpp => scripts/async.lua} (74%) rename {share/qtcreator/lua-plugins/luatests => src/plugins/lua/scripts}/inspect.lua (100%) diff --git a/qt_attributions.json b/qt_attributions.json index 987502609df..008ff353943 100644 --- a/qt_attributions.json +++ b/qt_attributions.json @@ -612,11 +612,11 @@ "QDocModule": "qtcreator", "QtParts": ["tools"], "QtUsage": "Used for async/await support in Lua modules.", - "Path": "src/plugins/lua/bindings", + "Path": "src/plugins/lua/scripts", "Description": "lua-async-await implements the async/await pattern in Lua.", "Homepage": "https://github.com/ms-jpq/lua-async-await", "License": "MIT License", - "LicenseFile": "src/plugins/lua/bindings/ASYNC-LICENSE.txt", + "LicenseFile": "src/plugins/lua/scripts/ASYNC-LICENSE.txt", "Copyright": "Copyright (c) 2008 Paul Evans" }, { @@ -625,11 +625,11 @@ "QDocModule": "qtcreator", "QtParts": ["tools"], "QtUsage": "Used for pretty printing from Lua scripts.", - "Path": "share/qtcreator/lua-plugins/luatests", + "Path": "src/plugins/lua/scripts", "Description": "inspect.lua is a library for pretty printing complex objects in Lua.", "Homepage": "https://github.com/kikito/inspect.lua", "License": "MIT License", - "LicenseFile": "share/qtcreator/lua-plugins/luatests/INSPECT-LICENSE.txt", + "LicenseFile": "src/plugins/lua/scripts/INSPECT-LICENSE.txt", "Copyright": "Copyright (c) 2022 Enrique GarcĂ­a Cota" }, { @@ -662,4 +662,3 @@ "Copyright": "Copyright 2011 The Apache Software Foundation" } ] - diff --git a/src/plugins/lua/CMakeLists.txt b/src/plugins/lua/CMakeLists.txt index 51fc92496ee..7bc0aa2efbd 100644 --- a/src/plugins/lua/CMakeLists.txt +++ b/src/plugins/lua/CMakeLists.txt @@ -4,7 +4,6 @@ add_qtc_plugin(Lua PUBLIC_DEFINES LUA_AVAILABLE SOURCES bindings/action.cpp - bindings/async.cpp bindings/async.h bindings/core.cpp bindings/fetch.cpp @@ -49,7 +48,9 @@ if(TARGET Lua) qt_add_resources(Lua lua_script_rcc PREFIX "/lua" FILES + scripts/async.lua scripts/ilua.lua + scripts/inspect.lua ) set_source_files_properties(luauibindings.cpp PROPERTY SKIP_AUTOMOC ON PROPERTY SKIP_AUTOGEN ON) diff --git a/src/plugins/lua/lua.qbs b/src/plugins/lua/lua.qbs index 65b4205332a..5e6298387af 100644 --- a/src/plugins/lua/lua.qbs +++ b/src/plugins/lua/lua.qbs @@ -39,7 +39,6 @@ QtcPlugin { files: [ "action.cpp", - "async.cpp", "core.cpp", "fetch.cpp", "gui.cpp", diff --git a/src/plugins/lua/luaengine.cpp b/src/plugins/lua/luaengine.cpp index f70b0d5e269..dca1bb25733 100644 --- a/src/plugins/lua/luaengine.cpp +++ b/src/plugins/lua/luaengine.cpp @@ -171,6 +171,23 @@ void registerProvider(const QString &packageName, const PackageProvider &provide d->m_providers[packageName] = provider; } +void registerProvider(const QString &packageName, const FilePath &path) +{ + registerProvider(packageName, [path](sol::state_view lua) -> sol::object { + auto content = path.fileContents(); + if (!content) + throw sol::error(content.error().toStdString()); + + sol::protected_function_result res + = lua.script(content->data(), path.fileName().toStdString()); + if (!res.valid()) { + sol::error err = res; + throw err; + } + return res.get(0); + }); +} + void autoRegister(const std::function ®isterFunction) { d->m_autoProviders.append(registerFunction); diff --git a/src/plugins/lua/luaengine.h b/src/plugins/lua/luaengine.h index 81efef952e3..36cd6f10e47 100644 --- a/src/plugins/lua/luaengine.h +++ b/src/plugins/lua/luaengine.h @@ -51,6 +51,7 @@ LUA_EXPORT Utils::expected_str prepareSetup( sol::state_view lua, const LuaPluginSpec &pluginSpec); LUA_EXPORT void registerProvider(const QString &packageName, const PackageProvider &provider); +LUA_EXPORT void registerProvider(const QString &packageName, const Utils::FilePath &path); LUA_EXPORT void autoRegister(const std::function ®isterFunction); LUA_EXPORT void registerHook( QString name, const std::function &hookProvider); diff --git a/src/plugins/lua/luaplugin.cpp b/src/plugins/lua/luaplugin.cpp index 56518230bd7..cb173b943d4 100644 --- a/src/plugins/lua/luaplugin.cpp +++ b/src/plugins/lua/luaplugin.cpp @@ -34,7 +34,6 @@ using namespace ExtensionSystem; namespace Lua::Internal { void setupActionModule(); -void setupAsyncModule(); void setupCoreModule(); void setupFetchModule(); void setupGuiModule(); @@ -254,8 +253,10 @@ public: { setupLuaEngine(this); + registerProvider("async", ":/lua/scripts/async.lua"); + registerProvider("inspect", ":/lua/scripts/inspect.lua"); + setupActionModule(); - setupAsyncModule(); setupCoreModule(); setupFetchModule(); setupGuiModule(); diff --git a/src/plugins/lua/bindings/ASYNC-LICENSE.txt b/src/plugins/lua/scripts/ASYNC-LICENSE.txt similarity index 100% rename from src/plugins/lua/bindings/ASYNC-LICENSE.txt rename to src/plugins/lua/scripts/ASYNC-LICENSE.txt diff --git a/share/qtcreator/lua-plugins/luatests/INSPECT-LICENSE.txt b/src/plugins/lua/scripts/INSPECT-LICENSE.txt similarity index 100% rename from share/qtcreator/lua-plugins/luatests/INSPECT-LICENSE.txt rename to src/plugins/lua/scripts/INSPECT-LICENSE.txt diff --git a/src/plugins/lua/bindings/async.cpp b/src/plugins/lua/scripts/async.lua similarity index 74% rename from src/plugins/lua/bindings/async.cpp rename to src/plugins/lua/scripts/async.lua index 54868af6f0b..7958ea5d8af 100644 --- a/src/plugins/lua/bindings/async.cpp +++ b/src/plugins/lua/scripts/async.lua @@ -1,11 +1,3 @@ -// Copyright (C) 2024 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 - -#include "../luaengine.h" - -namespace Lua::Internal { - -static const char *async_source = R"( -- From: https://github.com/ms-jpq/lua-async-await -- Licensed under MIT local co = coroutine @@ -70,13 +62,15 @@ end -- sugar over coroutine local await = function(defer) local _, isMain = coroutine.running() - assert(not isMain, "a.wait was called outside of a running coroutine. You need to start one using a.sync(my_function)() first") + assert(not isMain, + "a.wait was called outside of a running coroutine. You need to start one using a.sync(my_function)() first") assert(type(defer) == "function", "type error :: expected func :: was: " .. type(defer)) return co.yield(defer) end local await_all = function(defer) local _, isMain = coroutine.running() - assert(not isMain, "a.wait_all was called outside of a running coroutine. You need to start one using a.sync(my_function)() first") + assert(not isMain, + "a.wait_all was called outside of a running coroutine. You need to start one using a.sync(my_function)() first") assert(type(defer) == "table", "type error :: expected table") return co.yield(join(defer)) end @@ -86,14 +80,3 @@ return { wait_all = await_all, wrap = wrap, } -)"; - -void setupAsyncModule() -{ - registerProvider("async", [](sol::state_view lua) -> sol::object { - sol::protected_function_result res = lua.script(async_source, "async.cpp"); - return res.get(0); - }); -} - -} // namespace Lua::Internal diff --git a/share/qtcreator/lua-plugins/luatests/inspect.lua b/src/plugins/lua/scripts/inspect.lua similarity index 100% rename from share/qtcreator/lua-plugins/luatests/inspect.lua rename to src/plugins/lua/scripts/inspect.lua