Skip to content

Commit

Permalink
LMail: implement __close(), mark the object "stale"
Browse files Browse the repository at this point in the history
    
This prevents creating new XattrTable instances after the connection
was closed
  • Loading branch information
MaxKellermann committed Dec 4, 2023
1 parent 76c6179 commit bb7bf9e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/LMail.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ class IncomingMail : public MutableMail {
auto_close(&_auto_close),
cred(_peer_cred)
{
auto_close->Add(L, Lua::RelativeStackIndex{-1});

lua_newtable(L);
lua_setfenv(L, -2);
}

bool IsStale() const noexcept {
return auto_close == nullptr;
}

bool HavePeerCred() const noexcept {
return cred.pid >= 0;
}
Expand All @@ -67,6 +73,11 @@ class IncomingMail : public MutableMail {
return cred.gid;
}

int Close(lua_State *) {
auto_close = nullptr;
return 0;
}

int Index(lua_State *L);
int NewIndex(lua_State *L);
};
Expand Down Expand Up @@ -257,6 +268,9 @@ IncomingMail::Index(lua_State *L)

const char *const name = luaL_checkstring(L, 2);

if (IsStale())
return luaL_error(L, "Stale object");

for (const auto *i = mail_methods; i->name != nullptr; ++i) {
if (strcmp(i->name, name) == 0) {
Lua::Push(L, i->func);
Expand Down Expand Up @@ -335,6 +349,9 @@ IncomingMail::NewIndex(lua_State *L)
if (lua_gettop(L) != 3)
return luaL_error(L, "Invalid parameters");

if (IsStale())
return luaL_error(L, "Stale object");

const char *name = luaL_checkstring(L, 2);
if (StringIsEqual(name, "sender")) {
const char *new_value = luaL_checkstring(L, 3);
Expand All @@ -353,6 +370,7 @@ RegisterLuaMail(lua_State *L)
using namespace Lua;

LuaMail::Register(L);
SetField(L, RelativeStackIndex{-1}, "__close", LuaMail::WrapMethod<&IncomingMail::Close>());
SetField(L, RelativeStackIndex{-1}, "__index", LuaMail::WrapMethod<&IncomingMail::Index>());
SetField(L, RelativeStackIndex{-1}, "__newindex", LuaMail::WrapMethod<&IncomingMail::NewIndex>());
lua_pop(L, 1);
Expand Down

0 comments on commit bb7bf9e

Please sign in to comment.