lua 调用C函数
#include "tolua_fix.h" #include "WordFilter.h" #include "LuaBasicConversions.h" #include "lua_sensitive_binding.h" int lua_sensitive_loadfile(lua_State* tolua_S) { size_t sz = 0; const char *filePath = luaL_checklstring(tolua_S, 1, &sz); std::thread threadLoadFile(&CWordFilter::loadFileByThread, CWordFilter::getInstance(), filePath); threadLoadFile.detach(); return 1; } int censorStr(lua_State *tolua_S) { size_t sz = 0; std::string source = luaL_checklstring(tolua_S, 1, &sz); if (CWordFilter::getInstance()->censorStr(source)) { return 0; } else if (CWordFilter::getInstance()->censorStrWithOutSymbol(source)) { return 0; } return 1; } static luaL_Reg lua_sensitive_funcs[] = { { "loadFile", lua_sensitive_loadfile }, { "censorStr", censorStr }, { NULL, NULL } }; int init_sensitive_binding(lua_State* tolua_S){ lua_newtable(tolua_S); lua_pushvalue(tolua_S, -1); lua_setfield(tolua_S, LUA_GLOBALSINDEX, "SensitiveChecker"); luaL_Reg* lib = lua_sensitive_funcs; for (; lib->func; lib++) { lua_pushcfunction(tolua_S, lib->func); lua_setfield(tolua_S, -2, lib->name); } lua_pop(tolua_S, 1); return 1; } int register_sensitive_binding(lua_State* tolua_S) { return init_sensitive_binding(tolua_S); }
将需要用的函数插入到 名字为 SensitiveChecker 的全局变量表中, 这里我加入了两个函数。用于之前的敏感词屏蔽操作,因为只需要用到这两个接口,所以没有必要去把一个类添加到项目中去,为了省事就加了个全局的表
祝我无坚不摧,祝我百毒不侵,祝我狼心狗肺,祝我逍遥快活。