mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-20 17:08:12 +00:00
@ -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
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user