Skip to content

Commit 15c3451

Browse files
author
Peter Neiss
committed
Some enum cleanup
1 parent f54c5c6 commit 15c3451

File tree

10 files changed

+79
-7462
lines changed

10 files changed

+79
-7462
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ cmake-build-*/
2222
coverage_html/
2323
coverage_*.info
2424
build_coverage/
25+
tags

include/lua.h

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,9 @@ typedef struct lua_State lua_State;
6565
** basic types
6666
*/
6767

68-
#ifdef __cplusplus
69-
// C++ version: Use enum class for type safety
70-
enum class LuaType : int {
71-
None = -1,
72-
Nil = 0,
73-
Boolean = 1,
74-
LightUserdata = 2,
75-
Number = 3,
76-
String = 4,
77-
Table = 5,
78-
Function = 6,
79-
Userdata = 7,
80-
Thread = 8,
81-
NumTypes = 9
82-
};
83-
84-
// Backward compatibility macros that use the enum
85-
#define LUA_TNONE static_cast<int>(LuaType::None)
86-
#define LUA_TNIL static_cast<int>(LuaType::Nil)
87-
#define LUA_TBOOLEAN static_cast<int>(LuaType::Boolean)
88-
#define LUA_TLIGHTUSERDATA static_cast<int>(LuaType::LightUserdata)
89-
#define LUA_TNUMBER static_cast<int>(LuaType::Number)
90-
#define LUA_TSTRING static_cast<int>(LuaType::String)
91-
#define LUA_TTABLE static_cast<int>(LuaType::Table)
92-
#define LUA_TFUNCTION static_cast<int>(LuaType::Function)
93-
#define LUA_TUSERDATA static_cast<int>(LuaType::Userdata)
94-
#define LUA_TTHREAD static_cast<int>(LuaType::Thread)
95-
#define LUA_NUMTYPES static_cast<int>(LuaType::NumTypes)
96-
97-
#else
98-
// C version: Traditional integer constants
9968
#define LUA_TNONE (-1)
10069
#define LUA_TNIL 0
101-
#define LUA_TBOOLEAN 1
70+
#define LUA_TBOOLEAN 1
10271
#define LUA_TLIGHTUSERDATA 2
10372
#define LUA_TNUMBER 3
10473
#define LUA_TSTRING 4
@@ -107,6 +76,23 @@ enum class LuaType : int {
10776
#define LUA_TUSERDATA 7
10877
#define LUA_TTHREAD 8
10978
#define LUA_NUMTYPES 9
79+
80+
#ifdef __cplusplus
81+
// C++ version: Use enum class for type safety
82+
enum class LuaType : int
83+
{
84+
None = LUA_TNONE,
85+
Nil = LUA_TNIL,
86+
Boolean = LUA_TBOOLEAN,
87+
LightUserdata = LUA_TLIGHTUSERDATA,
88+
Number = LUA_TNUMBER,
89+
String = LUA_TSTRING,
90+
Table = LUA_TTABLE,
91+
Function = LUA_TFUNCTION,
92+
Userdata = LUA_TUSERDATA,
93+
Thread = LUA_TTHREAD,
94+
NumTypes = LUA_NUMTYPES
95+
};
11096
#endif
11197

11298

src/compiler/llex.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "lprefix.h"
1111

12+
#include <span>
1213

1314
#include <clocale>
1415
#include <cstring>
@@ -62,13 +63,16 @@ void LexState::saveAndNext() {
6263
next();
6364
}
6465

65-
void luaX_init (lua_State *L) {
66-
TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */
67-
obj2gco(e)->fix(L); /* Phase 25c: never collect this name */
68-
for (int i=0; i<NUM_RESERVED; i++) {
69-
TString *ts = TString::create(L, luaX_tokens[i]);
70-
obj2gco(ts)->fix(L); /* Phase 25c: reserved words are never collected */
71-
ts->setExtra(cast_byte(i+1)); /* reserved word */
66+
void luaX_init (lua_State *L)
67+
{
68+
TString *envName = luaS_newliteral(L, LUA_ENV);
69+
envName->fix(L);
70+
lu_byte tokenIndex = 0;
71+
for (auto const& token : std::span(luaX_tokens))
72+
{
73+
auto ts = TString::create(L, token);
74+
ts->fix(L);
75+
ts->setExtra(++tokenIndex);
7276
}
7377
}
7478

src/core/lstate.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "lua.h"
1111

12+
#include <utility>
1213

1314
/* Some header files included here need this definition */
1415
typedef struct CallInfo CallInfo;
@@ -164,7 +165,10 @@ enum class GCState : lu_byte {
164165
CallFin = 7,
165166
Pause = 8
166167
};
167-
168+
/*
169+
inline bool operator<(GCState lhs, GCState rhs)
170+
{ return std::to_underlying(lhs) < std::to_underlying(rhs); }
171+
*/
168172
/*
169173
** Kinds of Garbage Collection
170174
*/

src/core/ltm.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,15 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
104104
void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
105105
const TValue *p2, const TValue *p3) {
106106
StkId func = L->getTop().p;
107-
L->getStackSubsystem().setSlot(func, f); /* push function (assume EXTRA_STACK) */
108-
L->getStackSubsystem().setSlot(func + 1, p1); /* 1st argument */
109-
L->getStackSubsystem().setSlot(func + 2, p2); /* 2nd argument */
110-
L->getStackSubsystem().setSlot(func + 3, p3); /* 3rd argument */
111-
L->getStackSubsystem().setTopPtr(func + 4);
107+
auto& stack = L->getStackSubsystem();
108+
stack.setSlot(func, f); /* push function (assume EXTRA_STACK) */
109+
stack.setSlot(func + 1, p1); /* 1st argument */
110+
stack.setSlot(func + 2, p2); /* 2nd argument */
111+
stack.setSlot(func + 3, p3); /* 3rd argument */
112+
stack.adjust(4);
112113
/* metamethod may yield only when called from Lua code */
113114
if (L->getCI()->isLuaCode())
114-
L->call( func, 0);
115+
L->call(func, 0);
115116
else
116117
L->callNoYield( func, 0);
117118
}
@@ -121,13 +122,14 @@ LuaT luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
121122
const TValue *p2, StkId res) {
122123
ptrdiff_t result = L->saveStack(res);
123124
StkId func = L->getTop().p;
124-
L->getStackSubsystem().setSlot(func, f); /* push function (assume EXTRA_STACK) */
125-
L->getStackSubsystem().setSlot(func + 1, p1); /* 1st argument */
126-
L->getStackSubsystem().setSlot(func + 2, p2); /* 2nd argument */
127-
L->getStackSubsystem().adjust(3);
125+
auto& stack = L->getStackSubsystem();
126+
stack.setSlot(func, f); /* push function (assume EXTRA_STACK) */
127+
stack.setSlot(func + 1, p1); /* 1st argument */
128+
stack.setSlot(func + 2, p2); /* 2nd argument */
129+
stack.adjust(3);
128130
/* metamethod may yield only when called from Lua code */
129131
if (L->getCI()->isLuaCode())
130-
L->call( func, 1);
132+
L->call(func, 1);
131133
else
132134
L->callNoYield( func, 1);
133135
res = L->restoreStack(result);

src/memory/lgc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
*/
6565

6666
inline bool global_State::keepInvariant() const noexcept {
67-
return static_cast<lu_byte>(getGCState()) <= static_cast<lu_byte>(GCState::Atomic);
67+
return getGCState() <= GCState::Atomic;
6868
}
6969

7070
// Phase 47: Check if GC is in sweep phase

src/objects/lobject.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ enum class GCAge : lu_byte;
4545
** their real delta is always the maximum value that fits in
4646
** that field.
4747
*/
48+
// old version of StackValue
4849
typedef union StackValue {
4950
TValue val;
5051
struct {
@@ -54,7 +55,13 @@ typedef union StackValue {
5455
} tbclist;
5556
} StackValue;
5657

57-
58+
/* new version
59+
clase StackValue: public TValue
60+
{
61+
TValue val;
62+
unsigned short delta;
63+
} StackValue;
64+
*/
5865
/* index to stack elements */
5966
typedef StackValue *StkId;
6067

src/objects/lobject_core.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
** See Copyright Notice in lua.h
55
*/
66

7-
87
#ifndef lobject_core_h
98
#define lobject_core_h
109

11-
1210
#include <cstdarg>
1311

1412
#include "llimits.h"
@@ -26,11 +24,10 @@ class lua_State;
2624
*/
2725
inline constexpr int LUA_TDEADKEY = (LUA_NUMTYPES+2); /* removed keys in tables */
2826

29-
3027
/*
3128
** number of all possible types (including LUA_TNONE but excluding DEADKEY)
3229
*/
33-
inline constexpr int LUA_TOTALTYPES = (LUA_TPROTO + 2);
30+
inline constexpr int LUA_TOTALTYPES = (LUA_NUMTYPES + 3);
3431

3532

3633
/*

src/objects/ltvalue.h

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llimits.h"
1111
#include "lua.h"
1212

13+
#include <utility>
1314

1415
/*
1516
** tags for Tagged Values have the following use of bits:
@@ -20,13 +21,7 @@
2021

2122
/* add variant bits to a type */
2223
constexpr int makevariant(int t, int v) noexcept { return (t | (v << 4)); }
23-
24-
25-
/*
26-
** Extra types for collectable non-values
27-
*/
28-
inline constexpr int LUA_TUPVAL = LUA_NUMTYPES; /* upvalues */
29-
inline constexpr int LUA_TPROTO = (LUA_NUMTYPES+1); /* function prototypes */
24+
constexpr int makevariant(LuaType t, int v) noexcept { return (std::to_underlying(t) | (v << 4)); }
3025

3126

3227
/*
@@ -37,43 +32,43 @@ inline constexpr int LUA_TPROTO = (LUA_NUMTYPES+1); /* function prototypes */
3732

3833
enum class LuaT : lu_byte {
3934
/* Nil variants */
40-
NIL = makevariant(LUA_TNIL, 0),
41-
EMPTY = makevariant(LUA_TNIL, 1),
42-
ABSTKEY = makevariant(LUA_TNIL, 2),
43-
NOTABLE = makevariant(LUA_TNIL, 3),
35+
NIL = makevariant(LuaType::Nil, 0),
36+
EMPTY = makevariant(LuaType::Nil, 1),
37+
ABSTKEY = makevariant(LuaType::Nil, 2),
38+
NOTABLE = makevariant(LuaType::Nil, 3),
4439

4540
/* Boolean variants */
46-
VFALSE = makevariant(LUA_TBOOLEAN, 0),
47-
VTRUE = makevariant(LUA_TBOOLEAN, 1),
41+
VFALSE = makevariant(LuaType::Boolean, 0),
42+
VTRUE = makevariant(LuaType::Boolean, 1),
4843

4944
/* Number variants */
50-
NUMINT = makevariant(LUA_TNUMBER, 0), /* integer numbers */
51-
NUMFLT = makevariant(LUA_TNUMBER, 1), /* float numbers */
45+
NUMINT = makevariant(LuaType::Number, 0), /* integer numbers */
46+
NUMFLT = makevariant(LuaType::Number, 1), /* float numbers */
5247

5348
/* String variants */
54-
SHRSTR = makevariant(LUA_TSTRING, 0), /* short strings */
55-
LNGSTR = makevariant(LUA_TSTRING, 1), /* long strings */
49+
SHRSTR = makevariant(LuaType::String, 0), /* short strings */
50+
LNGSTR = makevariant(LuaType::String, 1), /* long strings */
5651

5752
/* Table variant */
58-
TABLE = makevariant(LUA_TTABLE, 0),
53+
TABLE = makevariant(LuaType::Table, 0),
5954

6055
/* Function variants */
61-
LCL = makevariant(LUA_TFUNCTION, 0), /* Lua closure */
62-
LCF = makevariant(LUA_TFUNCTION, 1), /* light C function */
63-
CCL = makevariant(LUA_TFUNCTION, 2), /* C closure */
56+
LCL = makevariant(LuaType::Function, 0), /* Lua closure */
57+
LCF = makevariant(LuaType::Function, 1), /* light C function */
58+
CCL = makevariant(LuaType::Function, 2), /* C closure */
6459

6560
/* Userdata variants */
66-
LIGHTUSERDATA = makevariant(LUA_TLIGHTUSERDATA, 0),
67-
USERDATA = makevariant(LUA_TUSERDATA, 0),
61+
LIGHTUSERDATA = makevariant(LuaType::LightUserdata, 0),
62+
USERDATA = makevariant(LuaType::Userdata, 0),
6863

6964
/* Thread variant */
70-
THREAD = makevariant(LUA_TTHREAD, 0),
65+
THREAD = makevariant(LuaType::Thread, 0),
7166

7267
/* Upvalue variant (collectable non-value) */
73-
UPVAL = makevariant(LUA_TUPVAL, 0),
68+
UPVAL = makevariant(LUA_NUMTYPES, 0),
7469

7570
/* Proto variant (collectable non-value) */
76-
PROTO = makevariant(LUA_TPROTO, 0)
71+
PROTO = makevariant(LUA_NUMTYPES + 1, 0)
7772
};
7873

7974
/* }================================================================== */

0 commit comments

Comments
 (0)