From fdccc8026061806cef9c6f5cc2a4d9f752b4d00d Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 3 Dec 2025 19:12:59 +0100 Subject: [PATCH] Blua: 'Protect' in table operations is not needed in the fast track https://github.com/lua/lua/commit/df8b996bccd3830d6fe9d7ba80e8fccd6ac3cabf --- src/blua/lvm.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/blua/lvm.c b/src/blua/lvm.c index 484329825..7540bcdec 100644 --- a/src/blua/lvm.c +++ b/src/blua/lvm.c @@ -393,6 +393,21 @@ static void Arith (lua_State *L, StkId ra, TValue *rb, } +/* +** copy of 'luaV_gettable', but protecting call to potential metamethod +** (which can reallocate the stack) +*/ +#define gettableProtected(L,t,k,v) { TValue *aux; \ + if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ + else Protect(luaV_finishget(L,t,k,v,aux)); } + + +/* same for 'luaV_settable' */ +#define settableProtected(L,t,k,v) { TValue *slot; \ + if (!luaV_fastset(L,t,k,slot,luaH_set,v)) \ + Protect(luaV_finishset(L,t,k,v,slot)); } + + void luaV_execute (lua_State *L, int nexeccalls) { LClosure *cl; @@ -402,9 +417,9 @@ void luaV_execute (lua_State *L, int nexeccalls) { reentry: /* entry point */ lua_assert(isLua(L->ci)); pc = L->savedpc; - cl = &clvalue(L->ci->func)->l; - base = L->base; - k = cl->p->k; + cl = &clvalue(L->ci->func)->l; /* local reference to function's closure */ + k = cl->p->k; /* local reference to function's constant table */ + base = L->base; /* local copy of function's base */ /* main loop of interpreter */ for (;;) { const Instruction i = *pc++; @@ -464,7 +479,7 @@ void luaV_execute (lua_State *L, int nexeccalls) { StkId ra = RA(i); StkId rb = RB(i); TValue *rc = RKC(i); - Protect(luaV_gettable(L, rb, rc, ra)); + gettableProtected(L, rb, rc, ra); continue; } case OP_SETGLOBAL: { @@ -486,7 +501,7 @@ void luaV_execute (lua_State *L, int nexeccalls) { StkId ra = RA(i); TValue *rb = RKB(i); TValue *rc = RKC(i); - Protect(luaV_settable(L, ra, rb, rc)); + settableProtected(L, ra, rb, rc); continue; } case OP_NEWTABLE: { @@ -500,8 +515,9 @@ void luaV_execute (lua_State *L, int nexeccalls) { case OP_SELF: { StkId ra = RA(i); StkId rb = RB(i); - setobjs2s(L, ra+1, rb); - Protect(luaV_gettable(L, rb, RKC(i), ra)); + TValue *rc = RKC(i); + setobjs2s(L, ra + 1, rb); + gettableProtected(L, rb, rc, ra); continue; } case OP_ADD: {