TEST(cmsgpack, example) { int rc; lua_State *L = lua_open(); luaL_openlibs(L); rc = luaopen_cmsgpack(L); ASSERT_EQ(1, rc); std::string bin = {'\xa5', 'H', 'e', 'l', 'l', 'o'}; lua_pushlstring(L, bin.data(), bin.size()); lua_setglobal(L, "bindata"); std::string buf = "str = cmsgpack.unpack(bindata)\n"; rc = luaL_loadbuffer(L, buf.c_str(), buf.length(), "test"); ASSERT_EQ(0, rc); rc = lua_pcall(L, 0, 0, 0); LUA_ASSERT_PCALL(L, rc); lua_getglobal(L, "str"); std::string str = lua_tolstring(L, -1, nullptr); if(str != "Hello") { FAIL() << "str = " << str << " (not \"Hello\")"; } lua_pop(L, 1); lua_close(L); }
// Initializes a state and loads the source of the Lua script. // // source - The script source code. // L - A reference to where the new Lua state should be returned. // // Returns 0 if successful, otherwise returns -1. int sky_lua_initscript(bstring source, lua_State **L) { int rc; assert(source != NULL); assert(L != NULL); // Load Lua with standard library. *L = luaL_newstate(); check_mem(L); luaL_openlibs(*L); // Load Lua msgpack library. rc = luaopen_cmsgpack(*L); check(rc == 1, "Unable to load lua-cmsgpack"); //debug("--SOURCE--\n%s", bdata(source)); // Compile lua script. rc = luaL_loadstring(*L, bdata(source)); check(rc == 0, "Unable to compile Lua script: %s", lua_tostring(*L, -1)); // Call once to make the functions available. lua_call(*L, 0, 0); return 0; error: lua_close(*L); *L = NULL; return -1; }
TEST(cmsgpack, packUnpack) { int rc; lua_State *L = lua_open(); luaL_openlibs(L); rc = luaopen_cmsgpack(L); ASSERT_EQ(1, rc); std::string buf = "local t = {x = 8, y = 2}\n" "local msgpack = cmsgpack.pack(t)\n" "local t2 = cmsgpack.unpack(msgpack)\n" "x = t2['x'] + t2['y']\n"; rc = luaL_loadbuffer(L, buf.c_str(), buf.length(), "test"); ASSERT_EQ(0, rc); rc = lua_pcall(L, 0, 0, 0); LUA_ASSERT_PCALL(L, rc); lua_getglobal(L, "x"); auto num = lua_tonumber(L, -1); lua_pop(L, 1); EXPECT_DOUBLE_EQ(10, num); lua_close(L); }
void OpenLuaExport() { LuaWrapper* poWrapper = LuaWrapper::Instance(); RegLuaDebugger(NULL); luaopen_cmsgpack(poWrapper->GetLuaState()); luaopen_lpeg(poWrapper->GetLuaState()); luaopen_protobuf_c(poWrapper->GetLuaState()); luaopen_cjson(poWrapper->GetLuaState()); RegTimerMgr("GlobalExport"); RegWordFilter("GlobalExport"); poWrapper->RegFnList(_global_lua_func, "GlobalExport"); RegLuaCmd("NetworkExport"); RegLuaRpc("NetworkExport"); RegLuaPBPack("NetworkExport"); RegLuaNetwork("NetworkExport"); RegLuaSerialize("cseri"); RegClassSSDBDriver(); RegClassMysqlDriver(); RegClassRobot(); }