Skip to content

Commit 930febf

Browse files
committed
Phase 122: Refactor LUA_V* to enum class LuaT (WIP - headers)
Major changes: - Changed enum class LuaT from : int to : lu_byte for memory efficiency - Changed TValue::tt_ from lu_byte to LuaT type - Changed GCObject::tt and GCBase::tt from lu_byte to LuaT - Updated all type-checking functions (checktag, ctb, etc.) to use LuaT - Updated TValue getters to return LuaT instead of lu_byte - Added overloads for backward compatibility Header files updated: - ltvalue.h: Core LuaT enum and TValue type changes - lobject_core.h: Updated GCObject, GCBase, type helpers - lobject.h: Updated TValue setters and equality operator - lstring.h: Updated string type checks - lfunc.h: Updated function/closure type checks - ltable.h: Updated table type checks and Node constructor Status: Headers updated, implementation files still need work Build status: In progress, compiler-driven migration
1 parent e03b75f commit 930febf

File tree

6 files changed

+93
-101
lines changed

6 files changed

+93
-101
lines changed

src/objects/lfunc.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ typedef union StackValue *StkId;
2525
*/
2626

2727
constexpr bool ttisfunction(const TValue* o) noexcept { return checktype(o, LUA_TFUNCTION); }
28-
constexpr bool ttisLclosure(const TValue* o) noexcept { return checktag(o, ctb(LUA_VLCL)); }
29-
constexpr bool ttislcf(const TValue* o) noexcept { return checktag(o, LUA_VLCF); }
30-
constexpr bool ttisCclosure(const TValue* o) noexcept { return checktag(o, ctb(LUA_VCCL)); }
28+
constexpr bool ttisLclosure(const TValue* o) noexcept { return checktag(o, ctb(LuaT::LCL)); }
29+
constexpr bool ttislcf(const TValue* o) noexcept { return checktag(o, LuaT::LCF); }
30+
constexpr bool ttisCclosure(const TValue* o) noexcept { return checktag(o, ctb(LuaT::CCL)); }
3131
constexpr bool ttisclosure(const TValue* o) noexcept { return ttisLclosure(o) || ttisCclosure(o); }
3232

3333
constexpr bool TValue::isFunction() const noexcept { return checktype(this, LUA_TFUNCTION); }
34-
constexpr bool TValue::isLClosure() const noexcept { return checktag(this, ctb(LUA_VLCL)); }
35-
constexpr bool TValue::isLightCFunction() const noexcept { return checktag(this, LUA_VLCF); }
36-
constexpr bool TValue::isCClosure() const noexcept { return checktag(this, ctb(LUA_VCCL)); }
34+
constexpr bool TValue::isLClosure() const noexcept { return checktag(this, ctb(LuaT::LCL)); }
35+
constexpr bool TValue::isLightCFunction() const noexcept { return checktag(this, LuaT::LCF); }
36+
constexpr bool TValue::isCClosure() const noexcept { return checktag(this, ctb(LuaT::CCL)); }
3737
constexpr bool TValue::isClosure() const noexcept { return isLClosure() || isCClosure(); }
3838

3939
inline constexpr bool isLfunction(const TValue* o) noexcept {

src/objects/lobject.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,28 @@ constexpr const TValue* s2v(const StackValue* o) noexcept { return &(o)->val; }
9797
** TValue member function implementations
9898
** These must be defined here after all type constants are available
9999
*/
100-
inline void TValue::setNil() noexcept { tt_ = LUA_VNIL; }
101-
inline void TValue::setFalse() noexcept { tt_ = LUA_VFALSE; }
102-
inline void TValue::setTrue() noexcept { tt_ = LUA_VTRUE; }
100+
inline void TValue::setNil() noexcept { setType(LuaT::NIL); }
101+
inline void TValue::setFalse() noexcept { setType(LuaT::VFALSE); }
102+
inline void TValue::setTrue() noexcept { setType(LuaT::VTRUE); }
103103

104104
inline void TValue::setInt(lua_Integer i) noexcept {
105105
value_.i = i;
106-
tt_ = LUA_VNUMINT;
106+
setType(LuaT::NUMINT);
107107
}
108108

109109
inline void TValue::setFloat(lua_Number n) noexcept {
110110
value_.n = n;
111-
tt_ = LUA_VNUMFLT;
111+
setType(LuaT::NUMFLT);
112112
}
113113

114114
inline void TValue::setPointer(void* p) noexcept {
115115
value_.p = p;
116-
tt_ = LUA_VLIGHTUSERDATA;
116+
setType(LuaT::LIGHTUSERDATA);
117117
}
118118

119119
inline void TValue::setFunction(lua_CFunction f) noexcept {
120120
value_.f = f;
121-
tt_ = LUA_VLCF;
121+
setType(LuaT::LCF);
122122
}
123123

124124
inline void TValue::setString(lua_State* L, TString* s) noexcept {
@@ -129,31 +129,31 @@ inline void TValue::setString(lua_State* L, TString* s) noexcept {
129129

130130
inline void TValue::setUserdata(lua_State* L, Udata* u) noexcept {
131131
value_.gc = reinterpret_cast<GCObject*>(u);
132-
tt_ = ctb(LUA_VUSERDATA);
132+
tt_ = ctb(LuaT::USERDATA);
133133
(void)L;
134134
}
135135

136136
inline void TValue::setTable(lua_State* L, Table* t) noexcept {
137137
value_.gc = reinterpret_cast<GCObject*>(t);
138-
tt_ = ctb(LUA_VTABLE);
138+
tt_ = ctb(LuaT::TABLE);
139139
(void)L;
140140
}
141141

142142
inline void TValue::setLClosure(lua_State* L, LClosure* cl) noexcept {
143143
value_.gc = reinterpret_cast<GCObject*>(cl);
144-
tt_ = ctb(LUA_VLCL);
144+
tt_ = ctb(LuaT::LCL);
145145
(void)L;
146146
}
147147

148148
inline void TValue::setCClosure(lua_State* L, CClosure* cl) noexcept {
149149
value_.gc = reinterpret_cast<GCObject*>(cl);
150-
tt_ = ctb(LUA_VCCL);
150+
tt_ = ctb(LuaT::CCL);
151151
(void)L;
152152
}
153153

154154
inline void TValue::setThread(lua_State* L, lua_State* th) noexcept {
155155
value_.gc = reinterpret_cast<GCObject*>(th);
156-
tt_ = ctb(LUA_VTHREAD);
156+
tt_ = ctb(LuaT::THREAD);
157157
(void)L;
158158
}
159159

@@ -379,17 +379,17 @@ inline bool operator==(const TValue& l, const TValue& r) noexcept {
379379
else if (ttypetag(&l) != ttypetag(&r)) {
380380
/* Different variants - only numbers and strings can be equal across variants */
381381
switch (ttypetag(&l)) {
382-
case LUA_VNUMINT: { /* int == float? */
382+
case LuaT::NUMINT: { /* int == float? */
383383
lua_Integer i2;
384384
return (luaV_flttointeger(fltvalue(&r), &i2, F2Imod::F2Ieq) &&
385385
ivalue(&l) == i2);
386386
}
387-
case LUA_VNUMFLT: { /* float == int? */
387+
case LuaT::NUMFLT: { /* float == int? */
388388
lua_Integer i1;
389389
return (luaV_flttointeger(fltvalue(&l), &i1, F2Imod::F2Ieq) &&
390390
i1 == ivalue(&r));
391391
}
392-
case LUA_VSHRSTR: case LUA_VLNGSTR: {
392+
case LuaT::SHRSTR: case LuaT::LNGSTR: {
393393
/* Compare strings with different variants */
394394
return tsvalue(&l)->equals(tsvalue(&r));
395395
}
@@ -399,21 +399,21 @@ inline bool operator==(const TValue& l, const TValue& r) noexcept {
399399
}
400400
else { /* same variant */
401401
switch (ttypetag(&l)) {
402-
case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
402+
case LuaT::NIL: case LuaT::VFALSE: case LuaT::VTRUE:
403403
return true;
404-
case LUA_VNUMINT:
404+
case LuaT::NUMINT:
405405
return ivalue(&l) == ivalue(&r);
406-
case LUA_VNUMFLT:
406+
case LuaT::NUMFLT:
407407
return fltvalue(&l) == fltvalue(&r);
408-
case LUA_VLIGHTUSERDATA:
408+
case LuaT::LIGHTUSERDATA:
409409
return pvalue(&l) == pvalue(&r);
410-
case LUA_VSHRSTR:
410+
case LuaT::SHRSTR:
411411
return eqshrstr(tsvalue(&l), tsvalue(&r));
412-
case LUA_VLNGSTR:
412+
case LuaT::LNGSTR:
413413
return tsvalue(&l)->equals(tsvalue(&r));
414-
case LUA_VUSERDATA:
414+
case LuaT::USERDATA:
415415
return uvalue(&l) == uvalue(&r);
416-
case LUA_VLCF:
416+
case LuaT::LCF:
417417
return fvalue(&l) == fvalue(&r);
418418
default: /* other collectable types (tables, closures, threads) */
419419
return gcvalue(&l) == gcvalue(&r);

src/objects/lobject_core.h

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ constexpr bool tagisempty(int tag) noexcept { return novariant(tag) == LUA_TNIL;
5555

5656

5757
/* macro to test for a standard nil */
58-
constexpr bool ttisstrictnil(const TValue* o) noexcept { return checktag(o, LUA_VNIL); }
58+
constexpr bool ttisstrictnil(const TValue* o) noexcept { return checktag(o, LuaT::NIL); }
5959

60-
constexpr bool TValue::isStrictNil() const noexcept { return checktag(this, LUA_VNIL); }
60+
constexpr bool TValue::isStrictNil() const noexcept { return checktag(this, LuaT::NIL); }
6161

6262
inline void setnilvalue(TValue* obj) noexcept { obj->setNil(); }
6363

6464

65-
constexpr bool isabstkey(const TValue* v) noexcept { return checktag(v, LUA_VABSTKEY); }
65+
constexpr bool isabstkey(const TValue* v) noexcept { return checktag(v, LuaT::ABSTKEY); }
6666

67-
constexpr bool TValue::isAbstKey() const noexcept { return checktag(this, LUA_VABSTKEY); }
67+
constexpr bool TValue::isAbstKey() const noexcept { return checktag(this, LuaT::ABSTKEY); }
6868

6969

7070
/*
@@ -89,11 +89,11 @@ constexpr bool TValue::isEmpty() const noexcept { return isNil(); }
8989

9090

9191
/* macro defining a value corresponding to an absent key */
92-
#define ABSTKEYCONSTANT {nullptr}, LUA_VABSTKEY
92+
#define ABSTKEYCONSTANT {nullptr}, LuaT::ABSTKEY
9393

9494

9595
/* mark an entry as empty */
96-
inline void setempty(TValue* v) noexcept { settt_(v, LUA_VEMPTY); }
96+
inline void setempty(TValue* v) noexcept { settt_(v, LuaT::EMPTY); }
9797

9898
/* }================================================================== */
9999

@@ -106,15 +106,16 @@ inline void setempty(TValue* v) noexcept { settt_(v, LUA_VEMPTY); }
106106
*/
107107

108108
constexpr bool ttisboolean(const TValue* o) noexcept { return checktype(o, LUA_TBOOLEAN); }
109-
constexpr bool ttisfalse(const TValue* o) noexcept { return checktag(o, LUA_VFALSE); }
110-
constexpr bool ttistrue(const TValue* o) noexcept { return checktag(o, LUA_VTRUE); }
109+
constexpr bool ttisfalse(const TValue* o) noexcept { return checktag(o, LuaT::VFALSE); }
110+
constexpr bool ttistrue(const TValue* o) noexcept { return checktag(o, LuaT::VTRUE); }
111111

112112
constexpr bool TValue::isBoolean() const noexcept { return checktype(this, LUA_TBOOLEAN); }
113-
constexpr bool TValue::isFalse() const noexcept { return checktag(this, LUA_VFALSE); }
114-
constexpr bool TValue::isTrue() const noexcept { return checktag(this, LUA_VTRUE); }
113+
constexpr bool TValue::isFalse() const noexcept { return checktag(this, LuaT::VFALSE); }
114+
constexpr bool TValue::isTrue() const noexcept { return checktag(this, LuaT::VTRUE); }
115115

116116
constexpr bool l_isfalse(const TValue* o) noexcept { return ttisfalse(o) || ttisnil(o); }
117-
constexpr bool tagisfalse(int t) noexcept { return (t == LUA_VFALSE || novariant(t) == LUA_TNIL); }
117+
constexpr bool tagisfalse(LuaT t) noexcept { return (t == LuaT::VFALSE || novariant(t) == LUA_TNIL); }
118+
constexpr bool tagisfalse(int t) noexcept { return (t == static_cast<int>(LuaT::VFALSE) || novariant(t) == LUA_TNIL); }
118119

119120
constexpr bool TValue::isFalseLike() const noexcept { return isFalse() || isNil(); }
120121

@@ -133,9 +134,9 @@ inline void setbtvalue(TValue* obj) noexcept { obj->setTrue(); }
133134
** Note: LUA_VTHREAD now defined in ltvalue.h
134135
*/
135136

136-
constexpr bool ttisthread(const TValue* o) noexcept { return checktag(o, ctb(LUA_VTHREAD)); }
137+
constexpr bool ttisthread(const TValue* o) noexcept { return checktag(o, ctb(LuaT::THREAD)); }
137138

138-
constexpr bool TValue::isThread() const noexcept { return checktag(this, ctb(LUA_VTHREAD)); }
139+
constexpr bool TValue::isThread() const noexcept { return checktag(this, ctb(LuaT::THREAD)); }
139140

140141
inline lua_State* thvalue(const TValue* o) noexcept { return o->threadValue(); }
141142

@@ -152,16 +153,16 @@ inline lua_State* thvalue(const TValue* o) noexcept { return o->threadValue(); }
152153
*/
153154

154155
constexpr bool ttisnumber(const TValue* o) noexcept { return checktype(o, LUA_TNUMBER); }
155-
constexpr bool ttisfloat(const TValue* o) noexcept { return checktag(o, LUA_VNUMFLT); }
156-
constexpr bool ttisinteger(const TValue* o) noexcept { return checktag(o, LUA_VNUMINT); }
156+
constexpr bool ttisfloat(const TValue* o) noexcept { return checktag(o, LuaT::NUMFLT); }
157+
constexpr bool ttisinteger(const TValue* o) noexcept { return checktag(o, LuaT::NUMINT); }
157158

158159
constexpr bool TValue::isNumber() const noexcept { return checktype(this, LUA_TNUMBER); }
159-
constexpr bool TValue::isFloat() const noexcept { return checktag(this, LUA_VNUMFLT); }
160-
constexpr bool TValue::isInteger() const noexcept { return checktag(this, LUA_VNUMINT); }
160+
constexpr bool TValue::isFloat() const noexcept { return checktag(this, LuaT::NUMFLT); }
161+
constexpr bool TValue::isInteger() const noexcept { return checktag(this, LuaT::NUMINT); }
161162

162-
// TValue::numberValue() implementation (needs LUA_VNUMINT constant)
163+
// TValue::numberValue() implementation (needs NUMINT constant)
163164
inline lua_Number TValue::numberValue() const noexcept {
164-
return (tt_ == LUA_VNUMINT) ? static_cast<lua_Number>(value_.i) : value_.n;
165+
return (tt_ == LuaT::NUMINT) ? static_cast<lua_Number>(value_.i) : value_.n;
165166
}
166167

167168
inline lua_Number nvalue(const TValue* o) noexcept { return o->numberValue(); }
@@ -190,7 +191,7 @@ constexpr lua_Integer ivalueraw(const Value& v) noexcept { return v.i; }
190191
class GCObject {
191192
protected:
192193
mutable GCObject* next; /* GC list linkage (mutable for GC bookkeeping) */
193-
lu_byte tt; /* Type tag (immutable) */
194+
LuaT tt; /* Type tag (immutable) */
194195
mutable lu_byte marked; /* GC mark bits (mutable for GC bookkeeping) */
195196

196197
public:
@@ -199,8 +200,9 @@ class GCObject {
199200
void setNext(GCObject* n) const noexcept { next = n; } /* const - next is mutable */
200201
// Pointer-to-pointer for efficient GC list manipulation (allows in-place removal)
201202
GCObject** getNextPtr() const noexcept { return &next; } /* const - next is mutable */
202-
lu_byte getType() const noexcept { return tt; }
203-
void setType(lu_byte t) noexcept { tt = t; }
203+
LuaT getType() const noexcept { return tt; }
204+
void setType(LuaT t) noexcept { tt = t; }
205+
void setType(lu_byte t) noexcept { tt = static_cast<LuaT>(t); } /* for legacy code */
204206
lu_byte getMarked() const noexcept { return marked; }
205207
void setMarked(lu_byte m) const noexcept { marked = m; } /* const - marked is mutable */
206208
bool isMarked() const noexcept { return marked != 0; }
@@ -276,8 +278,9 @@ class GCBase: public GCObject {
276278
constexpr GCObject* getNext() const noexcept { return next; }
277279
constexpr void setNext(GCObject* n) const noexcept { next = n; } /* const - next is mutable */
278280

279-
constexpr lu_byte getType() const noexcept { return tt; }
280-
constexpr void setType(lu_byte t) noexcept { tt = t; }
281+
constexpr LuaT getType() const noexcept { return tt; }
282+
constexpr void setType(LuaT t) noexcept { tt = t; }
283+
constexpr void setType(lu_byte t) noexcept { tt = static_cast<LuaT>(t); } /* for legacy code */
281284

282285
constexpr lu_byte getMarked() const noexcept { return marked; }
283286
constexpr void setMarked(lu_byte m) const noexcept { marked = m; } /* const - marked is mutable */
@@ -297,9 +300,9 @@ class GCBase: public GCObject {
297300
}
298301
};
299302

300-
constexpr bool iscollectable(const TValue* o) noexcept { return (rawtt(o) & BIT_ISCOLLECTABLE) != 0; }
303+
constexpr bool iscollectable(const TValue* o) noexcept { return (static_cast<int>(rawtt(o)) & BIT_ISCOLLECTABLE) != 0; }
301304

302-
constexpr bool TValue::isCollectable() const noexcept { return (tt_ & BIT_ISCOLLECTABLE) != 0; }
305+
constexpr bool TValue::isCollectable() const noexcept { return (static_cast<int>(tt_) & BIT_ISCOLLECTABLE) != 0; }
303306

304307
inline GCObject* gcvalue(const TValue* o) noexcept { return o->gcValue(); }
305308

@@ -322,11 +325,11 @@ inline bool TValue::hasRightType() const noexcept { return typeTag() == gcValue(
322325
** Note: LUA_VLIGHTUSERDATA, LUA_VUSERDATA now defined in ltvalue.h
323326
*/
324327

325-
constexpr bool ttislightuserdata(const TValue* o) noexcept { return checktag(o, LUA_VLIGHTUSERDATA); }
326-
constexpr bool ttisfulluserdata(const TValue* o) noexcept { return checktag(o, ctb(LUA_VUSERDATA)); }
328+
constexpr bool ttislightuserdata(const TValue* o) noexcept { return checktag(o, LuaT::LIGHTUSERDATA); }
329+
constexpr bool ttisfulluserdata(const TValue* o) noexcept { return checktag(o, ctb(LuaT::USERDATA)); }
327330

328-
constexpr bool TValue::isLightUserdata() const noexcept { return checktag(this, LUA_VLIGHTUSERDATA); }
329-
constexpr bool TValue::isFullUserdata() const noexcept { return checktag(this, ctb(LUA_VUSERDATA)); }
331+
constexpr bool TValue::isLightUserdata() const noexcept { return checktag(this, LuaT::LIGHTUSERDATA); }
332+
constexpr bool TValue::isFullUserdata() const noexcept { return checktag(this, ctb(LuaT::USERDATA)); }
330333

331334
inline void* pvalue(const TValue* o) noexcept { return o->pointerValue(); }
332335

src/objects/lstring.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class global_State;
4141
*/
4242

4343
constexpr bool ttisstring(const TValue* o) noexcept { return checktype(o, LUA_TSTRING); }
44-
constexpr bool ttisshrstring(const TValue* o) noexcept { return checktag(o, ctb(LUA_VSHRSTR)); }
45-
constexpr bool ttislngstring(const TValue* o) noexcept { return checktag(o, ctb(LUA_VLNGSTR)); }
44+
constexpr bool ttisshrstring(const TValue* o) noexcept { return checktag(o, ctb(LuaT::SHRSTR)); }
45+
constexpr bool ttislngstring(const TValue* o) noexcept { return checktag(o, ctb(LuaT::LNGSTR)); }
4646

4747
constexpr bool TValue::isString() const noexcept { return checktype(this, LUA_TSTRING); }
48-
constexpr bool TValue::isShortString() const noexcept { return checktag(this, ctb(LUA_VSHRSTR)); }
49-
constexpr bool TValue::isLongString() const noexcept { return checktag(this, ctb(LUA_VLNGSTR)); }
48+
constexpr bool TValue::isShortString() const noexcept { return checktag(this, ctb(LuaT::SHRSTR)); }
49+
constexpr bool TValue::isLongString() const noexcept { return checktag(this, ctb(LuaT::LNGSTR)); }
5050

5151
inline TString* tsvalue(const TValue* o) noexcept { return o->stringValue(); }
5252

@@ -302,7 +302,7 @@ inline bool isreserved(const TString* s) noexcept {
302302
** equality for short strings, which are always internalized
303303
*/
304304
inline bool eqshrstr(const TString* a, const TString* b) noexcept {
305-
return check_exp((a)->getType() == LUA_VSHRSTR, (a) == (b));
305+
return check_exp((a)->getType() == LuaT::SHRSTR, (a) == (b));
306306
}
307307

308308

src/objects/ltable.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ typedef union StackValue *StkId;
2424
** Note: LUA_VTABLE now defined in ltvalue.h
2525
*/
2626

27-
constexpr bool ttistable(const TValue* o) noexcept { return checktag(o, ctb(LUA_VTABLE)); }
27+
constexpr bool ttistable(const TValue* o) noexcept { return checktag(o, ctb(LuaT::TABLE)); }
2828

29-
constexpr bool TValue::isTable() const noexcept { return checktag(this, ctb(LUA_VTABLE)); }
29+
constexpr bool TValue::isTable() const noexcept { return checktag(this, ctb(LuaT::TABLE)); }
3030

3131
inline Table* hvalue(const TValue* o) noexcept { return o->tableValue(); }
3232

@@ -54,7 +54,7 @@ class Node {
5454

5555
public:
5656
// Default constructor
57-
constexpr Node() noexcept : u{{0}, LUA_VNIL, LUA_TNIL, 0, {0}} {}
57+
constexpr Node() noexcept : u{{0}, static_cast<lu_byte>(LuaT::NIL), LUA_TNIL, 0, {0}} {}
5858

5959
// Constructor for initializing with explicit values
6060
constexpr Node(Value val, lu_byte val_tt, lu_byte key_tt, int next_val, Value key_val) noexcept

0 commit comments

Comments
 (0)