2021-11-19 00:37:35 +08:00
|
|
|
#include "lua_wraper.h"
|
|
|
|
|
|
|
|
void LuaDelegate::Stop()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:42:34 +08:00
|
|
|
int LuaDelegate::DoString(std::string scr) {
|
2021-11-19 00:37:35 +08:00
|
|
|
if (mVM != nullptr){
|
|
|
|
int ret = luaL_dostring(mVM,scr.c_str());
|
|
|
|
if (ret > 0){
|
|
|
|
printf("lua error");
|
|
|
|
PrintError(mVM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 01:02:13 +08:00
|
|
|
this->mScript = scr;
|
2021-11-19 00:37:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:42:34 +08:00
|
|
|
int LuaDelegate::UpdateScript(std::string scr) {
|
2021-11-19 00:37:35 +08:00
|
|
|
if(nullptr != mVM){
|
|
|
|
lua_close(mVM);
|
|
|
|
}
|
|
|
|
free(mVM);
|
|
|
|
mVM = luaL_newstate(); //打开lua
|
2022-01-23 01:02:13 +08:00
|
|
|
if (nullptr != mVM) {
|
|
|
|
return -1;
|
2021-11-19 00:37:35 +08:00
|
|
|
}
|
|
|
|
for (auto x : mFunc) {
|
|
|
|
lua_register(mVM, x.first.c_str(), x.second);
|
|
|
|
}
|
2022-01-23 01:02:13 +08:00
|
|
|
mScript = scr;
|
2021-11-27 17:42:34 +08:00
|
|
|
if (mVM != nullptr) {
|
2021-11-19 00:37:35 +08:00
|
|
|
int ret = luaL_dostring(mVM,scr.c_str());
|
|
|
|
if (ret > 0){
|
|
|
|
printf("lua error");
|
|
|
|
PrintError(mVM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:42:34 +08:00
|
|
|
void LuaDelegate::PrintError(lua_State *L) {
|
2021-11-19 00:37:35 +08:00
|
|
|
std::cout<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1);
|
|
|
|
}
|
|
|
|
|
2022-01-23 01:02:13 +08:00
|
|
|
int LuaDelegate::BindFunction(std::string name, lua_CFunction function)
|
|
|
|
{
|
|
|
|
if ((nullptr == function) || (name == ""))
|
|
|
|
return -1;
|
|
|
|
this->mFunc[name] = function;
|
|
|
|
lua_register(mVM, "serial_send", function);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:42:34 +08:00
|
|
|
void LuaDelegate::OnSerialData(std::string data) {
|
2022-01-23 01:02:13 +08:00
|
|
|
int i = lua_getglobal(mVM,"OnUartData");
|
|
|
|
if(i == 0){
|
|
|
|
std::cout << "OnSerialData not found\r\n";
|
|
|
|
return;
|
|
|
|
}
|
2021-11-19 00:37:35 +08:00
|
|
|
lua_pushstring(mVM,data.data());
|
|
|
|
lua_call(mVM,1,0);
|
|
|
|
}
|
|
|
|
|
2021-12-29 00:17:57 +08:00
|
|
|
void LuaDelegate::DumpStack() {
|
2021-11-19 00:37:35 +08:00
|
|
|
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:
|
|
|
|
std::cout<<("%s\n", lua_tostring(mVM, i));
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
std::cout<<(lua_toboolean(mVM, i) ? "true\n" : "false\n");
|
|
|
|
break;
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
std::cout<<("%g\n", lua_tonumber(mVM, i));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cout<<("%s\n", lua_typename(mVM, t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2021-12-29 00:17:57 +08:00
|
|
|
LuaDelegate::~LuaDelegate() {
|
2021-11-19 00:37:35 +08:00
|
|
|
if(nullptr != mVM){
|
|
|
|
lua_close(mVM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaDelegate::LuaDelegate():
|
2021-12-29 00:17:57 +08:00
|
|
|
mVM(nullptr) {
|
2021-11-19 00:37:35 +08:00
|
|
|
mVM = luaL_newstate(); //打开lua
|
|
|
|
if(nullptr != mVM){
|
|
|
|
printf("shit is nullptr");
|
|
|
|
}
|
|
|
|
luaL_openlibs(mVM); //打开标准库
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaDelegate::LuaDelegate(std::map<std::string, lua_CFunction> maps) :
|
2021-12-29 00:17:57 +08:00
|
|
|
mVM(nullptr) {
|
2021-11-19 00:37:35 +08:00
|
|
|
mVM = luaL_newstate(); //打开lua
|
|
|
|
if (nullptr != mVM) {
|
|
|
|
printf("shit is nullptr");
|
|
|
|
}
|
|
|
|
luaL_openlibs(mVM); //打开标准库
|
|
|
|
|
|
|
|
for (auto x : maps) {
|
|
|
|
lua_register(mVM, x.first.c_str(), x.second);
|
|
|
|
}
|
|
|
|
mFunc = maps;
|
|
|
|
}
|
|
|
|
|
2021-12-29 00:17:57 +08:00
|
|
|
int LuaDelegate::DoFile(std::string path) {
|
2021-11-19 00:37:35 +08:00
|
|
|
if(mVM != nullptr){
|
|
|
|
int ret = luaL_dofile(mVM, path.c_str());
|
|
|
|
if (ret > 0){
|
|
|
|
printf("lua error");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|