mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-19 16:42:48 +00:00
@ -15,11 +15,13 @@
|
||||
#define NAPI_UNLOADLIB_FAIL 5012 // 释放的库未被加载
|
||||
#define NAPI_LUAFUN_NOFOUND 5013 //调用不符合规定,没有遵守单入参单出参约定
|
||||
#define NAPI_LUA_STACK_ERROR 5014 //函数调用时,栈大小小于约定最小大小
|
||||
#define NAPI_LUA_INITFAIL 5015 //lua状态机初始化错误
|
||||
#define NAPI_LUA_INITFILE 5015 //lua状态机初始化错误
|
||||
#define NAPI_LUALIB_LOAD_OVER_STACK 5016 //加载太多lib
|
||||
#define NAPI_LUA_CLASS_LOST 5017 //luarunner对象丢失
|
||||
#define NAPI_LUA_MISS_REDIRECT 5018 //漏传用于重定向的文件路径
|
||||
#define NAPI_RESET_INPUT_FILE_ERROR 5019 //重置输入文件失败,可能造成输入污染
|
||||
#define NAPI_ERROR_FUNCS 5020
|
||||
#define NAPI_ERROR_FUNCS 5020 //函数调用错误
|
||||
#define NAPI_CHDIR_ERROR 5021 //切换工作目录失败,可能导致包搜索错误,日志写入路径错误等
|
||||
#define NAPI_NOCHUNK_FOUND 5022 //没有找到待调用的函数
|
||||
|
||||
#endif
|
||||
@ -168,57 +168,44 @@ int Lua_runner::clean()//进行新一轮调用前一定要先clean清除上个
|
||||
//TODO 支持更多类型的参数
|
||||
int Lua_runner::run(const char *path,const char *arg)
|
||||
{
|
||||
if(!this->check_luastatue())
|
||||
if(!this->check_luastatue())//检查lua状态机状态
|
||||
return -1;
|
||||
|
||||
if(path != NULL)//加载脚本到栈
|
||||
{
|
||||
if(this->load_lib(path,path) != 0)
|
||||
return -1;
|
||||
}
|
||||
if(arg != NULL)//压参数入栈
|
||||
{
|
||||
lua_pushlstring(this->L,arg,strlen(arg));
|
||||
}
|
||||
if(this->pkg_cont<0)//检查是否有可调用chunk
|
||||
{
|
||||
errno = NAPI_NOCHUNK_FOUND;
|
||||
return -1;
|
||||
}
|
||||
if(path !=NULL){
|
||||
if(luaL_loadfile(this->L, path) != LUA_OK) {
|
||||
const char *err = lua_tostring(this->L, -1);
|
||||
this->reslt = err ? err : "load error";
|
||||
errno = NAPI_SCRIPT_ERROR;
|
||||
lua_pop(this->L, 1); // 清理栈上的错误信息
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else{
|
||||
this->pkg_cont--;
|
||||
}
|
||||
if(arg !=NULL)//压栈参数
|
||||
lua_pushstring(this->L,arg);
|
||||
if(lua_type(L, -1) == LUA_TFUNCTION)
|
||||
int argcount = lua_gettop(this->L) - this->pkgs[this->pkg_cont-1].ref;//获取入参数量
|
||||
if(argcount<0)
|
||||
{
|
||||
if(lua_pcall(this->L,0,1,0) !=LUA_OK)//若栈顶为函数,直接调用
|
||||
{
|
||||
const char *ret = lua_tostring(this->L, -1);
|
||||
this->reslt = ret;//存储返回值
|
||||
errno =NAPI_SCRIPT_ERROR;
|
||||
return -1;
|
||||
}
|
||||
errno = NAPI_LUA_STACK_ERROR;
|
||||
return -1;
|
||||
}//检查参数数量
|
||||
if(lua_pcall(this->L,argcount,LUA_MULTRET,0)== LUA_OK)
|
||||
{
|
||||
if(lua_isstring(this->L,-1))
|
||||
this->reslt = lua_tostring(this->L,-1);
|
||||
this->pkg_cont--;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(lua_pcall(this->L,1,1,0) !=LUA_OK)//执行
|
||||
{
|
||||
const char *ret = lua_tostring(this->L, -1);
|
||||
this->reslt = ret;//存储返回值
|
||||
errno =NAPI_SCRIPT_ERROR;
|
||||
return -1;
|
||||
}
|
||||
errno = NAPI_SCRIPT_ERROR;
|
||||
this->reslt = lua_tostring(this->L,-1);
|
||||
this->pkg_cont--;
|
||||
lua_pop(L,1);//错误出栈
|
||||
return -1;
|
||||
}
|
||||
const char *ret = NULL;
|
||||
if(!lua_isstring(this->L,lua_gettop(this->L)))
|
||||
{
|
||||
if(lua_gettop(this->L)<2)//后续不会再有调用
|
||||
{
|
||||
errno = NAPI_SCRIPT_BAD_RET;
|
||||
goto NO_RETURN;
|
||||
}
|
||||
}
|
||||
ret = lua_tostring(this->L, -1);
|
||||
this->reslt = ret;//存储返回值
|
||||
NO_RETURN:
|
||||
return 0;
|
||||
}
|
||||
//TODO 实现函数调用 achieve func call via file
|
||||
/*
|
||||
|
||||
@ -90,6 +90,8 @@ public class LuaError <: Exception {
|
||||
case 5018 => "Missing redirect file path"
|
||||
case 5019 => "Reset input file failed, may cause input pollution"
|
||||
case 5020 => "Error in callback functions"
|
||||
case 5021 => "change workdir error"
|
||||
case 5022 => "Calling func no found"
|
||||
case _ => "Unknown error"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user