From da04d7f55a41d8eb71bc62a69d70c3af47b04242 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 21 Sep 2026 17:23:48 +0200 Subject: [PATCH] wipe all the lua crap cba fixing any of this or breaking shit really --- src/blua/lapi.c | 25 +++++----------- src/blua/lgc.c | 1 - src/blua/llimits.h | 30 ------------------- src/blua/lstate.c | 8 ++++- src/blua/lstate.h | 1 - src/blua/lstring.c | 60 ------------------------------------- src/blua/lstring.h | 8 ++--- src/blua/lvm.c | 74 ++++++++++++++-------------------------------- src/blua/lvm.h | 26 ++-------------- 9 files changed, 42 insertions(+), 191 deletions(-) diff --git a/src/blua/lapi.c b/src/blua/lapi.c index c12f383be..6a33983fe 100644 --- a/src/blua/lapi.c +++ b/src/blua/lapi.c @@ -519,22 +519,6 @@ LUA_API int lua_pushthread (lua_State *L) { ** get functions (Lua -> stack) */ -static void auxgetstr (lua_State *L, TValue *t, const char *k) { - TValue *slot; - TValue key; - TString *str = luaS_new(L, k); - api_checkvalidindex(L, t); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top, slot); - } - else { - setsvalue(L, &key, str); - luaV_finishget(L, t, &key, L->top, slot); - } - api_incr_top(L); - lua_unlock(L); -} - LUA_API void lua_gettable (lua_State *L, int idx) { StkId t; @@ -547,8 +531,15 @@ LUA_API void lua_gettable (lua_State *L, int idx) { LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { + StkId t; + TValue key; lua_lock(L); - auxgetstr(L, index2adr(L, idx), k); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + setsvalue(L, &key, luaS_new(L, k)); + luaV_gettable(L, t, &key, L->top); + api_incr_top(L); + lua_unlock(L); } diff --git a/src/blua/lgc.c b/src/blua/lgc.c index 26af8135b..1d1087e5b 100644 --- a/src/blua/lgc.c +++ b/src/blua/lgc.c @@ -544,7 +544,6 @@ static void atomic (lua_State *L) { marktmu(g); /* mark `preserved' userdata */ udsize += propagateall(g); /* remark, to propagate `preserveness' */ cleartable(g->weak); /* remove collected objects from weak tables */ - luaS_clearcache(g); /* flip current white */ g->currentwhite = cast_byte(otherwhite(g)); g->sweepstrgc = 0; diff --git a/src/blua/llimits.h b/src/blua/llimits.h index e35931ab0..20475d41f 100644 --- a/src/blua/llimits.h +++ b/src/blua/llimits.h @@ -41,25 +41,6 @@ typedef unsigned char lu_byte; */ #define IntPoint(p) ((unsigned int)(lu_mem)(p)) -/* -** conversion of pointer to unsigned integer: this is for hashing only; -** there is no problem if the integer cannot hold the whole pointer -** value. (In strict ISO C this may cause undefined behavior, but no -** actual machine seems to bother.) -*/ -#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ - __STDC_VERSION__ >= 199901L -#include -#if defined(UINTPTR_MAX) /* even in C99 this type is optional */ -#define L_P2I uintptr_t -#else /* no 'intptr'? */ -#define L_P2I uintmax_t /* use the largest available integer */ -#endif -#else /* C89 option */ -#define L_P2I size_t -#endif - -#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX)) /* type to ensure maximum alignment */ @@ -119,17 +100,6 @@ typedef lu_int32 Instruction; #endif -/* -** Size of cache for strings in the API. 'N' is the number of -** sets (better be a prime) and "M" is the size of each set (M == 1 -** makes a direct cache.) -*/ -#ifndef STRCACHE_N -#define STRCACHE_N 53 -#define STRCACHE_M 2 -#endif - - /* minimum size for string buffer */ #ifndef LUA_MINBUFFER #define LUA_MINBUFFER 32 diff --git a/src/blua/lstate.c b/src/blua/lstate.c index 633724ed7..b3482c6d6 100644 --- a/src/blua/lstate.c +++ b/src/blua/lstate.c @@ -64,6 +64,9 @@ static void freestack (lua_State *L, lua_State *L1) { } +#define MEMERRMSG "not enough memory" + + /* ** open parts that may cause memory-allocation errors */ @@ -73,9 +76,12 @@ static void f_luaopen (lua_State *L, void *ud) { stack_init(L, L); /* init stack */ sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ - luaS_init(L); + luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ luaT_init(L); luaX_init(L); + /* pre-create memory-error message */ + g->memerrmsg = luaS_newliteral(L, MEMERRMSG); + luaS_fix(g->memerrmsg); /* it should never be collected */ g->GCthreshold = 4*g->totalbytes; } diff --git a/src/blua/lstate.h b/src/blua/lstate.h index c3f96b869..224119f06 100644 --- a/src/blua/lstate.h +++ b/src/blua/lstate.h @@ -92,7 +92,6 @@ typedef struct global_State { UpVal uvhead; /* head of double-linked list of all open upvalues */ struct Table *mt[NUM_TAGS]; /* metatables for basic types */ TString *tmname[TM_N]; /* array with tag-method names */ - TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ } global_State; diff --git a/src/blua/lstring.c b/src/blua/lstring.c index 860347134..b1249238b 100644 --- a/src/blua/lstring.c +++ b/src/blua/lstring.c @@ -18,9 +18,6 @@ #include "lstring.h" -#define MEMERRMSG "not enough memory" - - void luaS_resize (lua_State *L, int newsize) { GCObject **newhash; @@ -75,36 +72,6 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, } -/* -** Clear API string cache. (Entries cannot be empty, so fill them with -** a non-collectable string.) -*/ -void luaS_clearcache (global_State *g) { - int i, j; - for (i = 0; i < STRCACHE_N; i++) - for (j = 0; j < STRCACHE_M; j++) { - if (luaS_iswhite(g->strcache[i][j])) /* will entry be collected? */ - g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ - } -} - - -/* -** Initialize the string table and the string cache -*/ -void luaS_init (lua_State *L) { - global_State *g = G(L); - int i, j; - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ - /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaS_fix(g->memerrmsg); /* it should never be collected */ - for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ - for (j = 0; j < STRCACHE_M; j++) - g->strcache[i][j] = g->memerrmsg; -} - - // string hash taken from luaJIT /* Unaligned load of uint32_t. */ @@ -161,10 +128,6 @@ static inline __attribute__((always_inline)) unsigned luaS_hash (const char *str #endif } - -/* -** new string (with explicit length) -*/ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { GCObject *o; unsigned int h = luaS_hash(str, l); @@ -184,29 +147,6 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { } -/* -** Create or reuse a zero-terminated string, first checking in the -** cache (using the string address as a key). The cache can contain -** only zero-terminated strings, so it is safe to use 'strcmp' to -** check hits. -*/ -TString *luaS_new (lua_State *L, const char *str) { - unsigned int i = point2uint(str) % STRCACHE_N; /* hash */ - int j; - TString **p = G(L)->strcache[i]; - for (j = 0; j < STRCACHE_M; j++) { - if (strcmp(str, getstr(p[j])) == 0) /* hit? */ - return p[j]; /* that is it */ - } - /* normal route */ - for (j = STRCACHE_M - 1; j > 0; j--) - p[j] = p[j - 1]; /* move out last element */ - /* new element is first in the list */ - p[0] = luaS_newlstr(L, str, strlen(str)); - return p[0]; -} - - Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { Udata *u; if (l_unlikely(s > MAX_SIZET - sizeof(Udata))) diff --git a/src/blua/lstring.h b/src/blua/lstring.h index 42e88d538..73a2ff8b3 100644 --- a/src/blua/lstring.h +++ b/src/blua/lstring.h @@ -17,19 +17,15 @@ #define sizeudata(u) (sizeof(union Udata)+(u)->len) -//#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) +#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) -#define luaS_iswhite(x) test2bits((x)->tsv.marked, WHITE0BIT, WHITE1BIT) -#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) +#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) LUAI_FUNC void luaS_resize (lua_State *L, int newsize); -LUAI_FUNC void luaS_clearcache (global_State *g); -LUAI_FUNC void luaS_init (lua_State *L); LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); -LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); #endif diff --git a/src/blua/lvm.c b/src/blua/lvm.c index fbc4ae755..7afbbd572 100644 --- a/src/blua/lvm.c +++ b/src/blua/lvm.c @@ -105,42 +105,27 @@ static void callTM (lua_State *L, const TValue *f, const TValue *p1, } -/* -** Finish the table access 'val = t[key]'. -** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to -** t[k] entry (which must be nil). -*/ -void luaV_finishget (lua_State *L, TValue *t, TValue *key, StkId val, - TValue *slot) { - int loop; /* counter to avoid infinite loops */ - TValue *tm; /* metamethod */ +void luaV_gettable (lua_State *L, TValue *t, TValue *key, StkId val) { + int loop; for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (slot == NULL) { /* 't' is not a table? */ - lua_assert(!ttistable(t)); - tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (l_unlikely(ttisnil(tm))) - luaG_typeerror(L, t, "index"); /* no metamethod */ - /* else will try the metamethod */ - } - else { /* 't' is a table */ - lua_assert(ttisnil(slot)); - tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ - if (tm == NULL) { /* no metamethod? */ - setnilvalue(val); /* result is nil */ + TValue *tm; + if (ttistable(t)) { /* `t' is a table? */ + Table *h = hvalue(t); + const TValue *res = luaH_get(h, key); /* do a primitive get */ + if (!ttisnil(res) || /* result is no nil? */ + (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ + setobj2s(L, val, res); return; } - /* else will try the metamethod */ + /* else will try the tag method */ } - if (ttisfunction(tm)) { /* is metamethod a function? */ - callTMres(L, val, tm, t, key); /* call it */ + else if (l_unlikely(ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))) + luaG_typeerror(L, t, "index"); + if (ttisfunction(tm)) { + callTMres(L, val, tm, t, key); return; } - t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */ - setobj2s(L, val, slot); /* done */ - return; - } - /* else repeat (tail call 'luaV_finishget') */ + t = tm; /* else repeat with `tm' */ } luaG_runerror(L, "loop in gettable"); } @@ -154,12 +139,8 @@ void luaV_settable (lua_State *L, TValue *t, TValue *key, StkId val) { Table *h = hvalue(t); TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ /* oldval is nil=> look for newindex, oldval is not nil => look for usedindex */ - if (ttisnil(oldval)) { - tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ - } else { - tm = fasttm(L, h->metatable, TM_USEDINDEX); - } - if (tm == NULL) { /* no metamethod? */ + if (!((ttisnil(oldval) && ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) != NULL)) || + ((!ttisnil(oldval)) && ((tm = fasttm(L, h->metatable, TM_USEDINDEX)) != NULL)))) { setobj2t(L, oldval, val); luaC_barriert(L, h, val); return; @@ -397,14 +378,6 @@ 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 *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else Protect(luaV_finishget(L,t,k,v,slot)); } - void luaV_execute (lua_State *L, int nexeccalls) { LClosure *cl; @@ -414,9 +387,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; /* 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 */ + cl = &clvalue(L->ci->func)->l; + base = L->base; + k = cl->p->k; /* main loop of interpreter */ for (;;) { const Instruction i = *pc++; @@ -476,7 +449,7 @@ void luaV_execute (lua_State *L, int nexeccalls) { StkId ra = RA(i); StkId rb = RB(i); TValue *rc = RKC(i); - gettableProtected(L, rb, rc, ra); + Protect(luaV_gettable(L, rb, rc, ra)); continue; } case OP_SETGLOBAL: { @@ -512,9 +485,8 @@ void luaV_execute (lua_State *L, int nexeccalls) { case OP_SELF: { StkId ra = RA(i); StkId rb = RB(i); - TValue *rc = RKC(i); - setobjs2s(L, ra + 1, rb); - gettableProtected(L, rb, rc, ra); + setobjs2s(L, ra+1, rb); + Protect(luaV_gettable(L, rb, RKC(i), ra)); continue; } case OP_ADD: { diff --git a/src/blua/lvm.h b/src/blua/lvm.h index 129a84bc4..f9226ee5d 100644 --- a/src/blua/lvm.h +++ b/src/blua/lvm.h @@ -22,34 +22,12 @@ (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) -/* -** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, -** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, -** return 0 (meaning it will have to check metamethod) with 'slot' -** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). -** 'f' is the raw get function to use. -*/ -#define luaV_fastget(L,t,k,slot,f) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = f(hvalue(t), k), /* else, do raw access */ \ - !ttisnil(slot))) /* result not nil? */ - -/* -** standard implementation for 'gettable' -*/ -#define luaV_gettable(L,t,k,v) { TValue *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else luaV_finishget(L,t,k,v,slot); } - - - LUAI_FUNC int luaV_lessthan (lua_State *L, TValue *l, TValue *r); LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); -LUAI_FUNC void luaV_finishget (lua_State *L, TValue *t, TValue *key, - StkId val, TValue *tm); +LUAI_FUNC void luaV_gettable (lua_State *L, TValue *t, TValue *key, + StkId val); LUAI_FUNC void luaV_settable (lua_State *L, TValue *t, TValue *key, StkId val); LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);