mirror of
https://github.com/x-tools-author/x-tools.git
synced 2025-09-15 15:28:40 +08:00
chore: add lua files to project
This commit is contained in:
parent
bfeaaa522a
commit
9f008feb04
@ -59,6 +59,8 @@ message(STATUS "[xTools]CMAKE_CXX_COMPILER_VERSION: ${CMAKE_CXX_COMPILER_VERSION
|
||||
# --------------------------------------------------------------------------------------------------
|
||||
# Global variables
|
||||
set(X_BIN ${CMAKE_CURRENT_SOURCE_DIR}/bin/${CMAKE_SYSTEM_NAME}/${CMAKE_BUILD_TYPE})
|
||||
set(tmp ${CMAKE_CURRENT_SOURCE_DIR}/libs)
|
||||
set(X_LIBS ${tmp}/${CMAKE_BUILD_TYPE}/${CMAKE_CXX_COMPILER_ID}/${CMAKE_CXX_COMPILER_VERSION})
|
||||
option(X_MAGIC "The magic option..." OFF)
|
||||
if(X_MAGIC)
|
||||
add_compile_definitions(X_MAGIC)
|
||||
@ -171,6 +173,16 @@ include(${CMAKE_SOURCE_DIR}/cmake/libqrencode.cmake)
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/libiconv.cmake)
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/qcustomplot.cmake)
|
||||
|
||||
# --------------------------------------------------------------------------------------------------
|
||||
# Lua module
|
||||
if(NOT X_LUA)
|
||||
file(GLOB LUA_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/common/luarunner*")
|
||||
foreach(file ${LUA_FILES})
|
||||
list(REMOVE_ITEM X_SOURCES ${file})
|
||||
message(STATUS "[Lua]Remove file: ${file}")
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# --------------------------------------------------------------------------------------------------
|
||||
# Qt SerialPort module
|
||||
option(X_ENABLE_SERIAL_PORT "Enable SerialPort module" ON)
|
||||
@ -297,9 +309,11 @@ if(X_PLOT)
|
||||
list(APPEND X_LIBS QCustomPlot)
|
||||
endif()
|
||||
if(X_LUA)
|
||||
list(APPEND X_LIBS ${xLuaLib})
|
||||
list(APPEND X_LIBS ${X_LUA_LIB})
|
||||
endif()
|
||||
|
||||
message(STATUS "[xTools]Link libraries: ${X_LIBS}")
|
||||
|
||||
# --------------------------------------------------------------------------------------------------
|
||||
# xApplications selector(just for Qt6.8.0 or later)
|
||||
option(X_APPS_ENABLED "Enable xApplications selector" OFF)
|
||||
|
||||
@ -24,22 +24,30 @@ if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/3rd/lua-${lua_version})
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/3rd)
|
||||
endif()
|
||||
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rd/lua-${lua_version}/src)
|
||||
set(lua_lib_file ${CMAKE_CURRENT_SOURCE_DIR}/libs/lua/${CMAKE_C_COMPILER_VERSION}/lua.lib)
|
||||
set(lua_root ${CMAKE_CURRENT_SOURCE_DIR}/3rd/lua-${lua_version})
|
||||
include_directories(${lua_root})
|
||||
message(STATUS "[lua] Include dir: ${lua_root}")
|
||||
set(lua_lib_file ${X_LIBS}/lua-${lua_version}/lua.lib)
|
||||
get_filename_component(lua_lib_dir ${lua_lib_file} DIRECTORY)
|
||||
|
||||
if(EXISTS ${lua_lib_file})
|
||||
link_directories(${lua_lib_dir})
|
||||
else()
|
||||
file(GLOB LUA_SRC ${CMAKE_CURRENT_SOURCE_DIR}/3rd/lua-${lua_version}/src/*.*)
|
||||
list(REMOVE_ITEM LUA_SRC ${CMAKE_CURRENT_SOURCE_DIR}/3rd/lua-${lua_version}/src/lua.c)
|
||||
add_library(lua STATIC ${LUA_SRC})
|
||||
set_target_properties(lua PROPERTIES FOLDER "3rd")
|
||||
set_target_properties(lua PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${lua_lib_dir})
|
||||
file(GLOB LUA_H ${lua_root}/*.h)
|
||||
file(GLOB LUA_C ${lua_root}/*.c)
|
||||
list(REMOVE_ITEM LUA_C ${lua_root}/lua.c)
|
||||
list(REMOVE_ITEM LUA_C ${lua_root}/onelua.c)
|
||||
set(LUA_FILES ${LUA_H} ${LUA_C})
|
||||
|
||||
if(ON)
|
||||
if(EXISTS ${lua_lib_file})
|
||||
link_directories(${lua_lib_dir})
|
||||
else()
|
||||
add_library(lua STATIC ${LUA_FILES})
|
||||
set_target_properties(lua PROPERTIES FOLDER "3rd")
|
||||
set_target_properties(lua PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${lua_lib_dir})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# cmake-format: off
|
||||
set(X_LUA ON CACHE BOOL "Use Lua" FORCE)
|
||||
set(xLuaLib "lua" CACHE STRING "Lua library name" FORCE)
|
||||
set(X_LUA ON)
|
||||
set(X_LUA_LIB "lua")
|
||||
add_compile_definitions(X_LUA)
|
||||
# cmake-format: on
|
||||
|
||||
70
src/common/luarunner.cpp
Normal file
70
src/common/luarunner.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2025-2025 x-tools-author(x-tools@outlook.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of xTools project.
|
||||
*
|
||||
* xTools is licensed according to the terms in the file LICENCE(GPL V3) in the root of the source
|
||||
* code directory.
|
||||
**************************************************************************************************/
|
||||
#include "luarunner.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QEventLoop>
|
||||
|
||||
LuaRunner::LuaRunner(QObject *parent)
|
||||
: QThread(parent)
|
||||
{}
|
||||
|
||||
LuaRunner::~LuaRunner()
|
||||
{
|
||||
if (isRunning()) {
|
||||
quit();
|
||||
wait();
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray LuaRunner::execute(const QString &script, const QByteArray &data)
|
||||
{
|
||||
emit invokeExecute(script, data);
|
||||
|
||||
QEventLoop loop;
|
||||
connect(this, &LuaRunner::executed, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
|
||||
if (!m_error.isEmpty()) {
|
||||
qWarning() << "Lua execution error:" << m_error;
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
return m_result;
|
||||
}
|
||||
|
||||
void LuaRunner::run()
|
||||
{
|
||||
m_lua = luaL_newstate();
|
||||
luaL_openlibs(m_lua);
|
||||
|
||||
QObject *obj = new QObject();
|
||||
connect(this,
|
||||
&LuaRunner::invokeExecute,
|
||||
obj,
|
||||
[this](const QString &functionName, const QByteArray &data) {
|
||||
executeInThread(functionName, data);
|
||||
});
|
||||
|
||||
exec();
|
||||
lua_close(m_lua);
|
||||
obj->deleteLater();
|
||||
obj = Q_NULLPTR;
|
||||
}
|
||||
|
||||
void LuaRunner::executeInThread(const QString &script, const QByteArray &data)
|
||||
{
|
||||
// The function name is 'process' and it should be defined in the script string.
|
||||
// the 'process' function will be called with the data as an argument.
|
||||
lua_getglobal(m_lua, script.toUtf8().constData());
|
||||
lua_pushlstring(m_lua, data.constData(), data.size());
|
||||
lua_pcall(m_lua, 1, 1, 0);
|
||||
m_result = lua_tostring(m_lua, -1);
|
||||
lua_pop(m_lua, 1);
|
||||
}
|
||||
41
src/common/luarunner.h
Normal file
41
src/common/luarunner.h
Normal file
@ -0,0 +1,41 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2025-2025 x-tools-author(x-tools@outlook.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of xTools project.
|
||||
*
|
||||
* xTools is licensed according to the terms in the file LICENCE(GPL V3) in the root of the source
|
||||
* code directory.
|
||||
**************************************************************************************************/
|
||||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "lualib.h"
|
||||
}
|
||||
|
||||
#include <QThread>
|
||||
|
||||
class LuaRunner : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit LuaRunner(QObject *parent = nullptr);
|
||||
~LuaRunner() override;
|
||||
|
||||
QByteArray execute(const QString &script, const QByteArray &data);
|
||||
|
||||
protected:
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
QByteArray m_result;
|
||||
QString m_error;
|
||||
lua_State *m_lua{nullptr};
|
||||
|
||||
private:
|
||||
Q_SIGNAL void invokeExecute(const QString &functionName, const QByteArray &data);
|
||||
Q_SIGNAL void executed();
|
||||
|
||||
void executeInThread(const QString &script, const QByteArray &data);
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user