Skip to content

Commit

Permalink
加个tostring
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Dec 12, 2024
1 parent 784c7c9 commit c40713b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions binding/lua_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ namespace bee::lua_socket {
std::unreachable();
}
}
static int mt_tostring(lua_State* L) {
const auto& ep = lua::checkudata<net::endpoint>(L, 1);
switch (ep.get_family()) {
case net::family::inet: {
auto [ip, port] = ep.get_inet();
lua_pushfstring(L, "%s:%d", ip.data(), port);
return 1;
}
case net::family::inet6: {
auto [ip, port] = ep.get_inet6();
lua_pushfstring(L, "%s:%d", ip.data(), port);
return 1;
}
case net::family::unix: {
auto [type, path] = ep.get_unix();
lua_pushlstring(L, path.data(), path.size());
return 1;
}
case net::family::unknown:
lua_pushstring(L, "<unknown>");
return 1;
default:
std::unreachable();
}
}
static int mt_eq(lua_State* L) {
const auto& a = lua::checkudata<net::endpoint>(L, 1);
const auto& b = lua::checkudata<net::endpoint>(L, 2);
Expand All @@ -55,6 +80,7 @@ namespace bee::lua_socket {
luaL_setfuncs(L, lib, 0);
lua_setfield(L, -2, "__index");
luaL_Reg mt[] = {
{ "__tostring", mt_tostring },
{ "__eq", mt_eq },
{ NULL, NULL },
};
Expand Down

0 comments on commit c40713b

Please sign in to comment.