#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; } void LuaDelegate::PrintError(lua_State *L) { printf("\nFATAL ERROR:%s\n\n", lua_tostring(L, -1)); } void LuaDelegate::OnDataRecv(QString data){ int i = lua_getglobal(mVM,"OnDataReady"); lua_pushstring(mVM,data.toStdString().data()); lua_call(mVM,1,0); } void LuaDelegate::DumpStack() { static int count = 0; printf("begin dump lua stack:%d\n", count); int top = lua_gettop(mVM); for (int i = top; i > 0; --i) { int t = lua_type(mVM, i); switch (t) { case LUA_TSTRING: qDebug()<<("%s\n", lua_tostring(mVM, i)); break; case LUA_TBOOLEAN: qDebug()<<(lua_toboolean(mVM, i) ? "true\n" : "false\n"); break; case LUA_TNUMBER: qDebug()<<("%g\n", lua_tonumber(mVM, i)); break; default: qDebug()<<("%s\n", lua_typename(mVM, t)); break; } } ++count; } LuaDelegate::~LuaDelegate() { if(nullptr != mVM){ lua_close(mVM); } } LuaDelegate::LuaDelegate(): mVM(nullptr) { mVM = luaL_newstate(); //打开lua if(nullptr != mVM){ printf("shit is nullptr"); } luaL_openlibs(mVM); //打开标准库 lua_register(mVM, "showbuffer", LuaShowData); lua_register(mVM, "resp", LuaWriteUart); } int LuaDelegate::DoFile(QString path) { if(mVM != nullptr){ int ret = luaL_dofile(mVM, path.toStdString().c_str()); if (ret > 0){ printf("lua error"); return -1; } } return 0; }