Skip to content

Commit 99366a0

Browse files
authored
Merge pull request #70 from NiceAndPeter/claude/refactor-lua-constants-enum-01EMQjo9ZBYwAxYwqtPyap9n
Refactor Lua version constants to enum class
2 parents eaceeaa + 99033d9 commit 99366a0

33 files changed

+419
-402
lines changed

src/compiler/lcode.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int FuncState::addk(Proto *proto, TValue *v) {
337337
int FuncState::k2proto(TValue *key, TValue *v) {
338338
TValue val;
339339
Proto *proto = getProto();
340-
int tag = luaH_get(getKCache(), key, &val); /* query scanner table */
340+
LuaT tag = luaH_get(getKCache(), key, &val); /* query scanner table */
341341
if (!tagisempty(tag)) { /* is there an index there? */
342342
int k = cast_int(ivalue(&val));
343343
/* collisions can happen only for float keys */
@@ -466,22 +466,22 @@ void FuncState::floatCode(int reg, lua_Number flt) {
466466
*/
467467
static void const2exp (TValue *v, expdesc *e) {
468468
switch (ttypetag(v)) {
469-
case LUA_VNUMINT:
469+
case LuaT::NUMINT:
470470
e->setKind(VKINT); e->setIntValue(ivalue(v));
471471
break;
472-
case LUA_VNUMFLT:
472+
case LuaT::NUMFLT:
473473
e->setKind(VKFLT); e->setFloatValue(fltvalue(v));
474474
break;
475-
case LUA_VFALSE:
475+
case LuaT::VFALSE:
476476
e->setKind(VFALSE);
477477
break;
478-
case LUA_VTRUE:
478+
case LuaT::VTRUE:
479479
e->setKind(VTRUE);
480480
break;
481-
case LUA_VNIL:
481+
case LuaT::NIL:
482482
e->setKind(VNIL);
483483
break;
484-
case LUA_VSHRSTR: case LUA_VLNGSTR:
484+
case LuaT::SHRSTR: case LuaT::LNGSTR:
485485
e->setKind(VKSTR); e->setStringValue(tsvalue(v));
486486
break;
487487
default: lua_assert(0);

src/compiler/llex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ l_noret LexState::syntaxError(const char *msg) {
119119
TString* LexState::anchorStr(TString *ts) {
120120
lua_State *luaState = getLuaState();
121121
TValue oldts;
122-
int tag = luaH_getstr(getTable(), ts, &oldts);
122+
LuaT tag = luaH_getstr(getTable(), ts, &oldts);
123123
if (!tagisempty(tag)) /* string already present? */
124124
return tsvalue(&oldts); /* use stored value */
125125
else { /* create a new entry */

src/core/lapi.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
383383
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
384384
const TValue *o = L->getStackSubsystem().indexToValue(L,idx);
385385
switch (ttypetag(o)) {
386-
case LUA_VSHRSTR: return static_cast<lua_Unsigned>(tsvalue(o)->length());
387-
case LUA_VLNGSTR: return static_cast<lua_Unsigned>(tsvalue(o)->length());
388-
case LUA_VUSERDATA: return static_cast<lua_Unsigned>(uvalue(o)->getLen());
389-
case LUA_VTABLE: {
386+
case LuaT::SHRSTR: return static_cast<lua_Unsigned>(tsvalue(o)->length());
387+
case LuaT::LNGSTR: return static_cast<lua_Unsigned>(tsvalue(o)->length());
388+
case LuaT::USERDATA: return static_cast<lua_Unsigned>(uvalue(o)->getLen());
389+
case LuaT::TABLE: {
390390
lua_Unsigned res;
391391
lua_lock(L);
392392
res = luaH_getn(L, hvalue(o));
@@ -438,8 +438,8 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
438438
LUA_API const void *lua_topointer (lua_State *L, int idx) {
439439
const TValue *o = L->getStackSubsystem().indexToValue(L,idx);
440440
switch (ttypetag(o)) {
441-
case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
442-
case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
441+
case LuaT::LCF: return cast_voidp(cast_sizet(fvalue(o)));
442+
case LuaT::USERDATA: case LuaT::LIGHTUSERDATA:
443443
return touserdata(o);
444444
default: {
445445
if (iscollectable(o))
@@ -612,7 +612,7 @@ LUA_API int lua_pushthread (lua_State *L) {
612612

613613

614614
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
615-
lu_byte tag;
615+
LuaT tag;
616616
TString *str = TString::create(L, k);
617617
tag = luaV_fastget(t, str, s2v(L->getTop().p), luaH_getstr);
618618
if (!tagisempty(tag))
@@ -634,7 +634,7 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
634634
*/
635635
static void getGlobalTable (lua_State *L, TValue *gt) {
636636
Table *registry = hvalue(G(L)->getRegistry());
637-
lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
637+
LuaT tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
638638
(void)tag; /* avoid not-used warnings when checks are off */
639639
api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
640640
}
@@ -652,7 +652,7 @@ LUA_API int lua_gettable (lua_State *L, int idx) {
652652
lua_lock(L);
653653
api_checkpop(L, 1);
654654
TValue *t = L->getStackSubsystem().indexToValue(L,idx);
655-
lu_byte tag = luaV_fastget(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 1), luaH_get);
655+
LuaT tag = luaV_fastget(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 1), luaH_get);
656656
if (tagisempty(tag))
657657
tag = luaV_finishget(L, t, s2v(L->getTop().p - 1), L->getTop().p - 1, tag);
658658
lua_unlock(L);
@@ -669,7 +669,7 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
669669
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
670670
lua_lock(L);
671671
TValue *t = L->getStackSubsystem().indexToValue(L,idx);
672-
lu_byte tag;
672+
LuaT tag;
673673
luaV_fastgeti(t, n, s2v(L->getTop().p), tag);
674674
if (tagisempty(tag)) {
675675
TValue key;
@@ -682,7 +682,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
682682
}
683683

684684

685-
static int finishrawget (lua_State *L, lu_byte tag) {
685+
static int finishrawget (lua_State *L, LuaT tag) {
686686
if (tagisempty(tag)) /* avoid copying empty items to the stack */
687687
setnilvalue(s2v(L->getTop().p));
688688
api_incr_top(L);
@@ -702,7 +702,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
702702
lua_lock(L);
703703
api_checkpop(L, 1);
704704
Table *t = gettable(L, idx);
705-
lu_byte tag = luaH_get(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 1));
705+
LuaT tag = luaH_get(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 1));
706706
L->getStackSubsystem().pop(); /* pop key */
707707
return finishrawget(L, tag);
708708
}
@@ -711,7 +711,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
711711
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
712712
lua_lock(L);
713713
Table *t = gettable(L, idx);
714-
lu_byte tag;
714+
LuaT tag;
715715
luaH_fastgeti(t, n, s2v(L->getTop().p), tag);
716716
return finishrawget(L, tag);
717717
}
@@ -1290,15 +1290,15 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
12901290
static const char *aux_upvalue (TValue *fi, int n, TValue **val,
12911291
GCObject **owner) {
12921292
switch (ttypetag(fi)) {
1293-
case LUA_VCCL: { /* C closure */
1293+
case LuaT::CCL: { /* C closure */
12941294
CClosure *f = clCvalue(fi);
12951295
if (!(cast_uint(n) - 1u < cast_uint(f->getNumUpvalues())))
12961296
return nullptr; /* 'n' not in [1, f->getNumUpvalues()] */
12971297
*val = f->getUpvalue(n-1);
12981298
if (owner) *owner = obj2gco(f);
12991299
return "";
13001300
}
1301-
case LUA_VLCL: { /* Lua closure */
1301+
case LuaT::LCL: { /* Lua closure */
13021302
LClosure *f = clLvalue(fi);
13031303
TString *name;
13041304
Proto *p = f->getProto();
@@ -1364,16 +1364,16 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
13641364
LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
13651365
TValue *fi = L->getStackSubsystem().indexToValue(L,fidx);
13661366
switch (ttypetag(fi)) {
1367-
case LUA_VLCL: { /* lua closure */
1367+
case LuaT::LCL: { /* lua closure */
13681368
return *getupvalref(L, fidx, n, nullptr);
13691369
}
1370-
case LUA_VCCL: { /* C closure */
1370+
case LuaT::CCL: { /* C closure */
13711371
CClosure *f = clCvalue(fi);
13721372
if (1 <= n && n <= f->getNumUpvalues())
13731373
return f->getUpvalue(n - 1);
13741374
/* else */
13751375
} /* FALLTHROUGH */
1376-
case LUA_VLCF:
1376+
case LuaT::LCF:
13771377
return nullptr; /* light C functions have no upvalues */
13781378
default: {
13791379
api_check(L, 0, "function expected");

src/core/ldebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535
/* Both CClosure and LClosure have tt at same offset (from GCBase) */
36-
#define LuaClosure(f) ((f) != nullptr && (f)->c.getType() == LUA_VLCL)
36+
#define LuaClosure(f) ((f) != nullptr && (f)->c.getType() == ctb(LuaT::LCL))
3737

3838
static const char strlocal[] = "local";
3939
static const char strupval[] = "upvalue";

src/core/ldo.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,11 @@ int lua_State::preTailCall(CallInfo *ci_arg, StkId func,
582582
unsigned status_val = LUA_MULTRET + 1;
583583
retry:
584584
switch (ttypetag(s2v(func))) {
585-
case LUA_VCCL: /* C closure */
585+
case LuaT::CCL: /* C closure */
586586
return preCallC(func, status_val, clCvalue(s2v(func))->getFunction());
587-
case LUA_VLCF: /* light C function */
587+
case LuaT::LCF: /* light C function */
588588
return preCallC(func, status_val, fvalue(s2v(func)));
589-
case LUA_VLCL: { /* Lua function */
589+
case LuaT::LCL: { /* Lua function */
590590
Proto *p = clLvalue(s2v(func))->getProto();
591591
int fsize = p->getMaxStackSize(); /* frame size */
592592
int nfixparams = p->getNumParams();
@@ -628,13 +628,13 @@ CallInfo* lua_State::preCall(StkId func, int nresults) {
628628
lua_assert(status_val <= MAXRESULTS + 1);
629629
retry:
630630
switch (ttypetag(s2v(func))) {
631-
case LUA_VCCL: /* C closure */
631+
case LuaT::CCL: /* C closure */
632632
preCallC(func, status_val, clCvalue(s2v(func))->getFunction());
633633
return nullptr;
634-
case LUA_VLCF: /* light C function */
634+
case LuaT::LCF: /* light C function */
635635
preCallC(func, status_val, fvalue(s2v(func)));
636636
return nullptr;
637-
case LUA_VLCL: { /* Lua function */
637+
case LuaT::LCL: { /* Lua function */
638638
CallInfo *ci_new;
639639
Proto *p = clLvalue(s2v(func))->getProto();
640640
int narg = cast_int(getTop().p - func) - 1; /* number of real arguments */

src/core/lstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
272272
lua_lock(L);
273273
luaC_checkGC(L);
274274
/* create new thread */
275-
o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), lxOffset());
275+
o = luaC_newobjdt(L, ctb(LuaT::THREAD), sizeof(LX), lxOffset());
276276
L1 = gco2th(o);
277277
/* anchor it on L stack */
278278
setthvalue2s(L, L->getTop().p, L1);
@@ -335,7 +335,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
335335
(*f)(ud, nullptr, LUA_TTHREAD, sizeof(global_State)));
336336
if (g == nullptr) return nullptr;
337337
L = &g->getMainThread()->l;
338-
L->setType(LUA_VTHREAD);
338+
L->setType(ctb(LuaT::THREAD));
339339
g->setCurrentWhite(bitmask(WHITE0BIT));
340340
L->setMarked(g->getWhite());
341341
preinit_thread(L, g);

src/core/lstate.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ struct lua_State : public GCBase<lua_State> {
655655
void finishOp();
656656
void concat(int total);
657657
void objlen(StkId ra, const TValue *rb);
658-
lu_byte finishGet(const TValue *t, TValue *key, StkId val, lu_byte tag);
658+
LuaT finishGet(const TValue *t, TValue *key, StkId val, LuaT tag);
659659
void finishSet(const TValue *t, TValue *key, TValue *val, int aux);
660660

661661
// Arithmetic operation methods (formerly luaV_* functions, implemented in lvm.cpp)
@@ -1229,17 +1229,17 @@ inline TString* gco2ts(GCObject* o) noexcept {
12291229
}
12301230

12311231
inline Udata* gco2u(GCObject* o) noexcept {
1232-
lua_assert(o->getType() == LUA_VUSERDATA);
1232+
lua_assert(o->getType() == ctb(LuaT::USERDATA));
12331233
return reinterpret_cast<Udata*>(o);
12341234
}
12351235

12361236
inline LClosure* gco2lcl(GCObject* o) noexcept {
1237-
lua_assert(o->getType() == LUA_VLCL);
1237+
lua_assert(o->getType() == ctb(LuaT::LCL));
12381238
return reinterpret_cast<LClosure*>(o);
12391239
}
12401240

12411241
inline CClosure* gco2ccl(GCObject* o) noexcept {
1242-
lua_assert(o->getType() == LUA_VCCL);
1242+
lua_assert(o->getType() == ctb(LuaT::CCL));
12431243
return reinterpret_cast<CClosure*>(o);
12441244
}
12451245

@@ -1249,22 +1249,22 @@ inline Closure* gco2cl(GCObject* o) noexcept {
12491249
}
12501250

12511251
inline Table* gco2t(GCObject* o) noexcept {
1252-
lua_assert(o->getType() == LUA_VTABLE);
1252+
lua_assert(o->getType() == ctb(LuaT::TABLE));
12531253
return reinterpret_cast<Table*>(o);
12541254
}
12551255

12561256
inline Proto* gco2p(GCObject* o) noexcept {
1257-
lua_assert(o->getType() == LUA_VPROTO);
1257+
lua_assert(o->getType() == ctb(LuaT::PROTO));
12581258
return reinterpret_cast<Proto*>(o);
12591259
}
12601260

12611261
inline lua_State* gco2th(GCObject* o) noexcept {
1262-
lua_assert(o->getType() == LUA_VTHREAD);
1262+
lua_assert(o->getType() == ctb(LuaT::THREAD));
12631263
return reinterpret_cast<lua_State*>(o);
12641264
}
12651265

12661266
inline UpVal* gco2upv(GCObject* o) noexcept {
1267-
lua_assert(o->getType() == LUA_VUPVAL);
1267+
lua_assert(o->getType() == ctb(LuaT::UPVAL));
12681268
return reinterpret_cast<UpVal*>(o);
12691269
}
12701270

src/core/ltm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
117117
}
118118

119119

120-
lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
120+
LuaT luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
121121
const TValue *p2, StkId res) {
122122
ptrdiff_t result = L->saveStack(res);
123123
StkId func = L->getTop().p;
@@ -144,7 +144,7 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2,
144144
if (notm(tm))
145145
return -1; /* tag method not found */
146146
else /* call tag method and return the tag of the result */
147-
return luaT_callTMres(L, tm, p1, p2, res);
147+
return static_cast<int>(luaT_callTMres(L, tm, p1, p2, res));
148148
}
149149

150150

src/core/ltm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ LUAI_FUNC void luaT_init (lua_State *L);
9696

9797
LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
9898
const TValue *p2, const TValue *p3);
99-
LUAI_FUNC lu_byte luaT_callTMres (lua_State *L, const TValue *f,
99+
LUAI_FUNC LuaT luaT_callTMres (lua_State *L, const TValue *f,
100100
const TValue *p1, const TValue *p2, StkId p3);
101101
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
102102
StkId res, TMS event);

0 commit comments

Comments
 (0)