Skip to content

Commit 81bf683

Browse files
Peter Neissclaude
andcommitted
Phase 111: Replace cast() macro with proper C++ casts - modernize 48 instances
Replace generic cast() macro with type-safe C++ casts throughout codebase: **Cast Replacements (48 instances):** - static_cast (36): Numeric conversions, void* to typed pointer, enums - reinterpret_cast (8): Type punning, dlsym pointers, memory layout - const_cast (1): Removing const from static invalidinstruction - ULONG_MAX (3): Replaced cast(unsigned long, ~0L) **Files Modified (19):** - Core: lapi.cpp, ldo.cpp, ldebug.cpp, lstate.cpp, lstack.h, lstate.h - Objects: lobject.h, lobject.cpp, ltable.cpp, lfunc.cpp, lstring.cpp - Memory: lmem.cpp, lgc.cpp, gc_core.cpp, gc_marking.cpp - Compiler: lcode.cpp, ldump.cpp - Testing: ltests.cpp - Interpreter: lua.cpp **Documentation Enhancements:** - Added GC type safety documentation (lobject.h) - Enhanced Node/Limbox memory layout docs with C++17 standard refs (ltable.cpp) - Documented why reinterpret_cast is safe for GC pointer conversions **Performance:** - Average: 2.23s (5 runs: 2.22s, 2.21s, 2.24s, 2.26s, 2.23s) - Zero regression - maintains baseline performance - All tests passing: "final OK !!!" Benefits: Better type safety, clearer intent, enhanced documentation, zero performance impact. Macro definitions for memory allocation preserved. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bac1db5 commit 81bf683

File tree

19 files changed

+92
-50
lines changed

19 files changed

+92
-50
lines changed

src/compiler/lcode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Instruction *FuncState::previousinstruction() {
8181
if (getPC() > getLastTarget())
8282
return &getProto()->getCode()[getPC() - 1]; /* previous instruction */
8383
else
84-
return cast(Instruction*, &invalidinstruction);
84+
return const_cast<Instruction*>(&invalidinstruction);
8585
}
8686

8787
/*

src/core/lapi.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
391391
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
392392
const TValue *o = L->getStackSubsystem().indexToValue(L,idx);
393393
switch (ttypetag(o)) {
394-
case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->length());
395-
case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->length());
396-
case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->getLen());
394+
case LUA_VSHRSTR: return static_cast<lua_Unsigned>(tsvalue(o)->length());
395+
case LUA_VLNGSTR: return static_cast<lua_Unsigned>(tsvalue(o)->length());
396+
case LUA_VUSERDATA: return static_cast<lua_Unsigned>(uvalue(o)->getLen());
397397
case LUA_VTABLE: {
398398
lua_Unsigned res;
399399
lua_lock(L);
@@ -1021,7 +1021,7 @@ struct CallS { /* data to 'f_call' */
10211021

10221022

10231023
static void f_call (lua_State *L, void *ud) {
1024-
struct CallS *c = cast(struct CallS *, ud);
1024+
CallS *c = static_cast<CallS*>(ud);
10251025
L->callNoYield( c->func, c->nresults);
10261026
}
10271027

@@ -1153,7 +1153,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
11531153
}
11541154
case LUA_GCSTEP: {
11551155
lu_byte oldstp = g->getGCStp();
1156-
l_mem n = cast(l_mem, va_arg(argp, size_t));
1156+
l_mem n = static_cast<l_mem>(va_arg(argp, size_t));
11571157
int work = 0; /* true if GC did some work */
11581158
g->setGCStp(0); /* allow GC to run (other bits must be zero here) */
11591159
if (n <= 0)
@@ -1307,7 +1307,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
13071307
Udata *u;
13081308
lua_lock(L);
13091309
api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value");
1310-
u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
1310+
u = luaS_newudata(L, size, static_cast<unsigned short>(nuvalue));
13111311
setuvalue(L, s2v(L->getTop().p), u);
13121312
api_incr_top(L);
13131313
luaC_checkGC(L);

src/core/ldebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ static const char *funcnamefromcode (lua_State *L, const Proto *p,
640640
tm = TMS::TM_NEWINDEX;
641641
break;
642642
case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
643-
tm = cast(TMS, InstructionView(i).c());
643+
tm = static_cast<TMS>(InstructionView(i).c());
644644
break;
645645
}
646646
case OP_UNM: tm = TMS::TM_UNM; break;

src/core/ldo.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ TStatus lua_State::rawRunProtected(Pfunc f, void *ud) {
226226
lj.status = ex.status();
227227
}
228228
catch (...) { /* non-Lua exception */
229-
lj.status = cast(TStatus, -1); /* create some error code */
229+
lj.status = static_cast<TStatus>(-1); /* create some error code */
230230
}
231231

232232
setErrorJmp(lj.previous); /* restore old error handler */
@@ -717,12 +717,12 @@ void lua_State::callNoYield(StkId func, int nResults) {
717717
*/
718718
// Convert to private lua_State method
719719
TStatus lua_State::finishPCallK(CallInfo *ci_arg) {
720-
TStatus status_val = cast(TStatus, ci_arg->getRecoverStatus()); /* get original status */
720+
TStatus status_val = static_cast<TStatus>(ci_arg->getRecoverStatus()); /* get original status */
721721
if (l_likely(status_val == LUA_OK)) /* no error? */
722722
status_val = LUA_YIELD; /* was interrupted by an yield */
723723
else { /* error */
724724
StkId func = this->restoreStack(ci_arg->getFuncIdx());
725-
setAllowHook(cast(lu_byte, ci_arg->getOAH())); /* restore 'allowhook' */
725+
setAllowHook(static_cast<lu_byte>(ci_arg->getOAH())); /* restore 'allowhook' */
726726
func = luaF_close(this, func, status_val, 1); /* can yield or raise an error */
727727
setErrorObj(status_val, func);
728728
shrinkStack(); /* restore stack size in case of overflow */
@@ -840,7 +840,7 @@ static int resume_error (lua_State *L, const char *msg, int narg) {
840840
** coroutine.
841841
*/
842842
static void resume (lua_State *L, void *ud) {
843-
int n = *(cast(int*, ud)); /* number of arguments */
843+
int n = *static_cast<int*>(ud); /* number of arguments */
844844
StkId firstArg = L->getTop().p - n; /* first argument */
845845
CallInfo *ci = L->getCI();
846846
if (L->getStatus() == LUA_OK) /* starting a coroutine? */
@@ -973,7 +973,7 @@ struct CloseP {
973973
** Auxiliary function to call 'luaF_close' in protected mode.
974974
*/
975975
static void closepaux (lua_State *L, void *ud) {
976-
struct CloseP *pcl = cast(struct CloseP *, ud);
976+
CloseP *pcl = static_cast<CloseP*>(ud);
977977
luaF_close(L, pcl->level, pcl->status, 0);
978978
}
979979

@@ -1054,7 +1054,7 @@ static void checkmode (lua_State *L, const char *mode, const char *x) {
10541054

10551055
static void f_parser (lua_State *L, void *ud) {
10561056
LClosure *cl;
1057-
struct SParser *p = cast(struct SParser *, ud);
1057+
SParser *p = static_cast<SParser*>(ud);
10581058
const char *mode = p->mode ? p->mode : "bt";
10591059
int c = zgetc(p->z); /* read first character */
10601060
if (c == LUA_SIGNATURE[0]) {

src/core/lstack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class LuaStack {
121121

122122
/* Convert offset to stack pointer */
123123
inline StkId restore(ptrdiff_t n) const noexcept {
124-
return cast(StkId, cast_charp(stack.p) + n);
124+
return reinterpret_cast<StkId>(cast_charp(stack.p) + n);
125125
}
126126

127127
/*

src/core/lstate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ inline constexpr size_t lxOffset() noexcept {
3838
}
3939

4040
inline LX* fromstate(lua_State* L) noexcept {
41-
return cast(LX *, cast(lu_byte *, (L)) - lxOffset());
41+
return reinterpret_cast<LX*>(reinterpret_cast<lu_byte*>(L) - lxOffset());
4242
}
4343

4444

@@ -239,7 +239,7 @@ static void preinit_thread (lua_State *L, global_State *g) {
239239

240240

241241
lu_mem luaE_threadsize (lua_State *L) {
242-
lu_mem sz = cast(lu_mem, sizeof(LX))
242+
lu_mem sz = static_cast<lu_mem>(sizeof(LX))
243243
+ cast_uint(L->getNCI()) * sizeof(CallInfo);
244244
if (L->getStack().p != NULL)
245245
sz += cast_uint(L->getStackSize() + EXTRA_STACK) * sizeof(StackValue);
@@ -331,7 +331,7 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) {
331331
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
332332
int i;
333333
lua_State *L;
334-
global_State *g = cast(global_State*,
334+
global_State *g = static_cast<global_State*>(
335335
(*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State)));
336336
if (g == NULL) return NULL;
337337
L = &g->getMainThread()->l;

src/core/lstate.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,5 +1296,14 @@ LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
12961296
LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status);
12971297

12981298

1299+
/*
1300+
** GC Type Safety for lua_State
1301+
** lua_State inherits from GCBase<lua_State> and participates in the GC system.
1302+
** Like other GC types, it uses reinterpret_cast for pointer conversions which
1303+
** are safe due to common initial sequence, type tag checking, and CRTP design.
1304+
** See lobject.h for detailed explanation of GC type safety.
1305+
*/
1306+
1307+
12991308
#endif
13001309

src/interpreter/lua.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,11 @@ static void lua_initreadline (lua_State *L) {
501501
if (lib == NULL)
502502
lua_warning(L, "library '" LUA_READLINELIB "' not found", 0);
503503
else {
504-
const char **name = cast(const char**, dlsym(lib, "rl_readline_name"));
504+
const char **name = static_cast<const char**>(dlsym(lib, "rl_readline_name"));
505505
if (name != NULL)
506506
*name = "lua";
507-
l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline")));
508-
l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history")));
507+
l_readline = reinterpret_cast<l_readlineT>(cast_func(dlsym(lib, "readline")));
508+
l_addhist = reinterpret_cast<l_addhistT>(cast_func(dlsym(lib, "add_history")));
509509
if (l_readline == NULL)
510510
lua_warning(L, "unable to load 'readline'", 0);
511511
}

src/memory/gc/gc_core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ l_mem GCCore::objsize(GCObject* o) {
6868
}
6969
default: res = 0; lua_assert(0);
7070
}
71-
return cast(l_mem, res);
71+
return static_cast<l_mem>(res);
7272
}
7373

7474

src/memory/gc/gc_marking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ l_mem GCMarking::traversetable(global_State* g, Table* h) {
118118
linkgclistTable(h, *g->getAllWeakPtr());
119119
break;
120120
}
121-
return cast(l_mem, 1 + 2 * h->nodeSize() + h->arraySize());
121+
return static_cast<l_mem>(1 + 2 * h->nodeSize() + h->arraySize());
122122
}
123123

124124
/*

0 commit comments

Comments
 (0)