mirror of
https://gitcode.com/JianFeeeee/LuaCangjia_api.git
synced 2026-09-20 00:48:47 +00:00
添加单元测试,修复部分错误
This commit is contained in:
@ -12,19 +12,25 @@ Lua_runner::Lua_runner(const char *path)
|
||||
this->L =NULL;
|
||||
this->L = luaL_newstate();
|
||||
errno = 0;//清除老错误信息
|
||||
if(this->L == NULL)
|
||||
if(this->L == NULL){
|
||||
errno = NAPI_LUA_STATE_ERROR;
|
||||
return ;
|
||||
}
|
||||
luaL_openlibs(this->L);//创建lua状态机,打开标准库
|
||||
char buf[64];
|
||||
const char* save_stdlib_code =
|
||||
"local std = {} "
|
||||
"for k, v in pairs(_G) do std[k] = true end "
|
||||
"_G.__STD_LIBS = std"; // 将列表存到全局变量 __STD_LIBS 中
|
||||
|
||||
if(luaL_dostring(this->L,
|
||||
luaL_dostring(this->L, save_stdlib_code);
|
||||
/*if(luaL_dostring(this->L,
|
||||
"package.path = \";/data/storage/el2/luapkg/?/init.lua\"") != LUA_OK)//TODO 修改加载库的默认路径指向沙盒内路径
|
||||
errno = NAPI_LUA_INITFAIL;//加载默认路径
|
||||
errno = NAPI_LUA_INITFAIL;//加载默认路径*/
|
||||
if(path !=NULL)
|
||||
{
|
||||
char buf[1024];
|
||||
snprintf(buf, sizeof(buf),
|
||||
"package.path = package.path .. \";%s\"", /* 追加风格 */
|
||||
"package.path = package.path .. \";%s\"",
|
||||
path);
|
||||
if(luaL_dostring(this->L,buf) != LUA_OK)//加载用户路径
|
||||
errno = NAPI_LUA_INITFAIL;
|
||||
@ -104,8 +110,8 @@ int Lua_runner::load_lib(const char *path,const char *name)
|
||||
|
||||
int Lua_runner::unload_lib(const char *name)
|
||||
{
|
||||
int sig = 0;
|
||||
for(int i;i<this->pkg_cont;i++)
|
||||
int sig = -1;
|
||||
for(int i=0;i<this->pkg_cont;i++)
|
||||
{
|
||||
if(strcmp(this->pkgs[i].name,name) == 0)
|
||||
{
|
||||
@ -139,12 +145,22 @@ int Lua_runner::clean()//进行新一轮调用前一定要先clean清除上个
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
const char* clean_globals_code =
|
||||
"if _G.__STD_LIBS then "
|
||||
" local std = _G.__STD_LIBS "
|
||||
" for k, v in pairs(_G) do "
|
||||
" if not std[k] then _G[k] = nil end "
|
||||
" end "
|
||||
"end";
|
||||
|
||||
luaL_dostring(this->L, clean_globals_code);
|
||||
luaL_dostring(this->L, "package.loaded = {}");//清空当前引用的包
|
||||
for(int i = 0 ;i<this->pkg_cont;i++)
|
||||
{
|
||||
lua_remove(this->L,this->pkgs[i].ref);
|
||||
}
|
||||
this->pkg_cont = 0;
|
||||
this->reslt ="nil";
|
||||
lua_settop(this->L, 0);//清空栈
|
||||
return 0;
|
||||
}
|
||||
@ -155,8 +171,15 @@ int Lua_runner::run(const char *path,const char *arg)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if(path !=NULL)//加载脚本
|
||||
luaL_loadfile(this->L,path);
|
||||
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--;
|
||||
}
|
||||
@ -182,17 +205,18 @@ int Lua_runner::run(const char *path,const char *arg)
|
||||
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;
|
||||
return -1;
|
||||
errno = NAPI_SCRIPT_BAD_RET;
|
||||
goto NO_RETURN;
|
||||
}
|
||||
}
|
||||
const char *ret = lua_tostring(this->L, -1);
|
||||
ret = lua_tostring(this->L, -1);
|
||||
this->reslt = ret;//存储返回值
|
||||
NO_RETURN:
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user