no message
parent
0db40d2998
commit
96d7d414e2
|
@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.5)
|
|||
include_directories(third/lua/include)
|
||||
link_directories(third/lua/lib)
|
||||
link_libraries(lua)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
|
||||
project(protoDebuger LANGUAGES CXX)
|
||||
|
||||
|
@ -29,6 +31,7 @@ set(SOURCES # 待预编译的cpp代码
|
|||
qserialproto.cpp
|
||||
globalvar.cpp
|
||||
sharedata.cpp
|
||||
lua_wraper.cpp
|
||||
)
|
||||
|
||||
set(RESOURCES
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
#include "lua_wraper.h"
|
||||
|
||||
void LuaDelegate::Stop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int LuaDelegate::DoString(QString scr)
|
||||
{
|
||||
if (mVM != nullptr){
|
||||
int ret = luaL_dostring(mVM,scr.toStdString().c_str());
|
||||
if (ret > 0){
|
||||
printf("lua error");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
LuaDelegate::~LuaDelegate()
|
||||
{
|
||||
if(nullptr != mVM){
|
||||
lua_close(mVM);
|
||||
}
|
||||
}
|
||||
|
||||
LuaDelegate::LuaDelegate():
|
||||
mVM(nullptr)
|
||||
{
|
||||
mVM = luaL_newstate(); //打开lua
|
||||
if(nullptr != mVM){
|
||||
printf("shit is nullptr");
|
||||
}
|
||||
}
|
||||
|
||||
int LuaDelegate::DoFile(QString path)
|
||||
{
|
||||
if(mVM != nullptr){
|
||||
luaL_openlibs(mVM); //打开标准库
|
||||
int ret = luaL_loadfile(mVM, path.toStdString().c_str());
|
||||
if (ret > 0){
|
||||
printf("lua error");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void TestLua()
|
||||
{
|
||||
LuaDelegate v;
|
||||
qDebug()<<typeid (int).name();
|
||||
v.DoFile("Test.lua");
|
||||
v.CallFuntion<int,void*,bool>("test",13,(void *)"sds",false);
|
||||
}
|
|
@ -3,35 +3,58 @@
|
|||
|
||||
|
||||
#include <string.h>
|
||||
#include <QString>
|
||||
#include <iostream>
|
||||
#include<typeinfo>
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
}
|
||||
|
||||
void TestLua(){
|
||||
char buff[256];
|
||||
int error;
|
||||
lua_State *L = luaL_newstate(); //打开lua
|
||||
if(nullptr != L){
|
||||
printf("shit is nullptr");
|
||||
}
|
||||
luaL_openlibs(L); //打开标准库
|
||||
int ret = luaL_dofile(L, "Test.lua");
|
||||
if (ret > 0){
|
||||
printf("lua error");
|
||||
}
|
||||
int sum;
|
||||
//code5
|
||||
lua_getglobal(L, "add");
|
||||
//code6
|
||||
char * data = "hell";
|
||||
lua_pushstring(L, data);
|
||||
//code7
|
||||
lua_pushnumber(L, 4);
|
||||
//code8
|
||||
lua_call(L, 2, 1);
|
||||
|
||||
lua_close(L);
|
||||
|
||||
class LuaDelegate{
|
||||
public:
|
||||
LuaDelegate();
|
||||
int DoFile(QString);
|
||||
void Stop();
|
||||
int DoString(QString);
|
||||
|
||||
void pushstack() {
|
||||
// const std::type_info &t1 = std::bad_typeid(arg1);
|
||||
// std::cout << t1.name() << std::endl;
|
||||
}
|
||||
//单参数模板
|
||||
template <typename T,typename ... Types>
|
||||
void pushstack(T arg1,Types... rest) {
|
||||
const std::type_info &t1 = typeid(arg1);
|
||||
qDebug() <<arg1 << " "<< t1.name() << " type ";
|
||||
pushstack(rest...);
|
||||
}
|
||||
template<typename... T>//类的成员函数是模板
|
||||
void CallFuntion(QString name,T... para){
|
||||
int i = lua_getglobal(mVM,name.toStdString().c_str());
|
||||
if(i < 0){
|
||||
return ;
|
||||
}
|
||||
pushstack(para...);
|
||||
}
|
||||
void OnDataRecv(){
|
||||
|
||||
}
|
||||
|
||||
~LuaDelegate();
|
||||
private:
|
||||
lua_State *mVM;
|
||||
QString mFile;
|
||||
};
|
||||
|
||||
void TestLua();
|
||||
|
||||
|
||||
#endif // LUA_WRAPER_H
|
||||
|
||||
|
|
|
@ -39,8 +39,9 @@ int main(int argc, char *argv[])
|
|||
view.setTitle ("多用途通信协议调试器,基于lua");
|
||||
view.rootContext()->setContextProperty("DataWrap",&gGlobal);
|
||||
|
||||
view.show();
|
||||
TestLua();
|
||||
|
||||
view.show();
|
||||
QObject *qmlObject = view.findChild<QObject*>("SerialSelect",Qt::FindChildOption::FindChildrenRecursively);
|
||||
qmlObject->setProperty("comlist",comList);
|
||||
|
||||
|
|
Loading…
Reference in New Issue