Skip to content

Commit fc7f7c4

Browse files
Peter Neissclaude
andcommitted
Phase 2F: API Table Access Helpers (lapi.cpp) - Static Function Elimination
Converted 3 static table access helper functions to lua_State private methods: - auxgetstr() → lua_State::auxGetStr() - auxsetstr() → lua_State::auxSetStr() - getGlobalTable() → lua_State::getGlobalTable() **Changes**: - Added 3 private method declarations to lua_State class (lstate.h) - Added 5 friend function declarations for public API functions - Converted static functions to member methods (lapi.cpp) - Updated 6 call sites across 5 API functions **Function Details**: - auxgetstr() → auxGetStr(const TValue *t, const char *k): Gets value from table using string key, handles fast path and metamethod fallback - auxsetstr() → auxSetStr(const TValue *t, const char *k): Sets value in table using string key, handles fast path and metamethod fallback - getGlobalTable() → getGlobalTable(TValue *gt): Retrieves global table from registry[LUA_RIDX_GLOBALS] **Friend Functions** (need access to table access helpers): - lua_getglobal() - gets global variable by name - lua_getfield() - gets table field by string key - lua_setglobal() - sets global variable by name - lua_setfield() - sets table field by string key - lua_load() - loads chunk and sets up global table as first upvalue **Call Sites Updated**: 6 locations across 5 API functions - lua_getglobal: calls getGlobalTable() and auxGetStr() - lua_getfield: calls auxGetStr() - lua_setglobal: calls getGlobalTable() and auxSetStr() - lua_setfield: calls auxSetStr() - lua_load: calls getGlobalTable() to setup function environment **Impact**: Table access helpers properly encapsulated. These functions handle the common pattern of string-keyed table access with fast path optimization. **Files Changed**: 2 files (lstate.h, lapi.cpp) **Testing**: All tests pass ✅ (2.11s single run) **Benchmark**: 5-run average = 2.12s ✅ (50% faster than 4.20s baseline!) **Risk**: MEDIUM (core API functions, multiple call sites, but well-tested) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent bc662e2 commit fc7f7c4

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

src/core/lapi.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -589,17 +589,17 @@ LUA_API int lua_pushthread (lua_State *L) {
589589
*/
590590

591591

592-
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
593-
TString* const str = TString::create(L, k);
594-
LuaT tag = L->getVM().fastget(t, str, s2v(L->getTop().p), [](Table* tbl, TString* strkey, TValue* res) { return tbl->getStr(strkey, res); });
592+
int lua_State::auxGetStr(const TValue *t, const char *k) {
593+
TString* const str = TString::create(this, k);
594+
LuaT tag = getVM().fastget(t, str, s2v(getTop().p), [](Table* tbl, TString* strkey, TValue* res) { return tbl->getStr(strkey, res); });
595595
if (!tagisempty(tag))
596-
api_incr_top(L);
596+
api_incr_top(this);
597597
else {
598-
setsvalue2s(L, L->getTop().p, str);
599-
api_incr_top(L);
600-
tag = L->getVM().finishGet(t, s2v(L->getTop().p - 1), L->getTop().p - 1, tag);
598+
setsvalue2s(this, getTop().p, str);
599+
api_incr_top(this);
600+
tag = getVM().finishGet(t, s2v(getTop().p - 1), getTop().p - 1, tag);
601601
}
602-
lua_unlock(L);
602+
lua_unlock(this);
603603
return novariant(tag);
604604
}
605605

@@ -609,19 +609,19 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
609609
** table; so, an emergency collection while using the global table
610610
** cannot collect it.
611611
*/
612-
static void getGlobalTable (lua_State *L, TValue *gt) {
613-
Table* const registry = hvalue(G(L)->getRegistry());
612+
void lua_State::getGlobalTable(TValue *gt) {
613+
Table* const registry = hvalue(G(this)->getRegistry());
614614
const LuaT tag = registry->getInt(LUA_RIDX_GLOBALS, gt);
615615
(void)tag; /* avoid not-used warnings when checks are off */
616-
api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
616+
api_check(this, novariant(tag) == LUA_TTABLE, "global table must exist");
617617
}
618618

619619

620620
LUA_API int lua_getglobal (lua_State *L, const char *name) {
621621
TValue gt;
622622
lua_lock(L);
623-
getGlobalTable(L, &gt);
624-
return auxgetstr(L, &gt, name);
623+
L->getGlobalTable(&gt);
624+
return L->auxGetStr(&gt, name);
625625
}
626626

627627

@@ -639,7 +639,7 @@ LUA_API int lua_gettable (lua_State *L, int idx) {
639639

640640
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
641641
lua_lock(L);
642-
return auxgetstr(L, L->getStackSubsystem().indexToValue(L,idx), k);
642+
return L->auxGetStr(L->getStackSubsystem().indexToValue(L,idx), k);
643643
}
644644

645645

@@ -761,29 +761,29 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
761761
/*
762762
** t[k] = value at the top of the stack (where 'k' is a string)
763763
*/
764-
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
765-
TString* const str = TString::create(L, k);
766-
api_checkpop(L, 1);
767-
const int hres = L->getVM().fastset(t, str, s2v(L->getTop().p - 1), [](Table* tbl, TString* strkey, TValue* val) { return tbl->psetStr(strkey, val); });
764+
void lua_State::auxSetStr(const TValue *t, const char *k) {
765+
TString* const str = TString::create(this, k);
766+
api_checkpop(this, 1);
767+
const int hres = getVM().fastset(t, str, s2v(getTop().p - 1), [](Table* tbl, TString* strkey, TValue* val) { return tbl->psetStr(strkey, val); });
768768
if (hres == HOK) {
769-
L->getVM().finishfastset(t, s2v(L->getTop().p - 1));
770-
L->getStackSubsystem().pop(); /* pop value */
769+
getVM().finishfastset(t, s2v(getTop().p - 1));
770+
getStackSubsystem().pop(); /* pop value */
771771
}
772772
else {
773-
setsvalue2s(L, L->getTop().p, str); /* push 'str' (to make it a TValue) */
774-
api_incr_top(L);
775-
L->getVM().finishSet(t, s2v(L->getTop().p - 1), s2v(L->getTop().p - 2), hres);
776-
L->getStackSubsystem().popN(2); /* pop value and key */
773+
setsvalue2s(this, getTop().p, str); /* push 'str' (to make it a TValue) */
774+
api_incr_top(this);
775+
getVM().finishSet(t, s2v(getTop().p - 1), s2v(getTop().p - 2), hres);
776+
getStackSubsystem().popN(2); /* pop value and key */
777777
}
778-
lua_unlock(L); /* lock done by caller */
778+
lua_unlock(this); /* lock done by caller */
779779
}
780780

781781

782782
LUA_API void lua_setglobal (lua_State *L, const char *name) {
783783
TValue gt;
784-
lua_lock(L); /* unlock done in 'auxsetstr' */
785-
getGlobalTable(L, &gt);
786-
auxsetstr(L, &gt, name);
784+
lua_lock(L); /* unlock done in 'auxSetStr' */
785+
L->getGlobalTable(&gt);
786+
L->auxSetStr(&gt, name);
787787
}
788788

789789

@@ -802,8 +802,8 @@ LUA_API void lua_settable (lua_State *L, int idx) {
802802

803803

804804
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
805-
lua_lock(L); /* unlock done in 'auxsetstr' */
806-
auxsetstr(L, L->getStackSubsystem().indexToValue(L,idx), k);
805+
lua_lock(L); /* unlock done in 'auxSetStr' */
806+
L->auxSetStr(L->getStackSubsystem().indexToValue(L,idx), k);
807807
}
808808

809809

@@ -1012,7 +1012,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
10121012
if (f->getNumUpvalues() >= 1) { /* does it have an upvalue? */
10131013
/* get global table from registry */
10141014
TValue gt;
1015-
getGlobalTable(L, &gt);
1015+
L->getGlobalTable(&gt);
10161016
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
10171017
*f->getUpval(0)->getVP() = gt;
10181018
luaC_barrier(L, f->getUpval(0), &gt);

src/core/lstate.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,18 @@ struct lua_State : public GCBase<lua_State> {
760760

761761
// Phase 2E: Friend function that needs access to API helper
762762
friend void lua_rotate(lua_State *L, int idx, int n);
763+
764+
// Phase 2F: API table access helpers (lapi.cpp)
765+
int auxGetStr(const TValue *t, const char *k);
766+
void auxSetStr(const TValue *t, const char *k);
767+
void getGlobalTable(TValue *gt);
768+
769+
// Phase 2F: Friend functions that need access to table access helpers
770+
friend int lua_getglobal(lua_State *L, const char *name);
771+
friend int lua_getfield(lua_State *L, int idx, const char *k);
772+
friend void lua_setglobal(lua_State *L, const char *name);
773+
friend void lua_setfield(lua_State *L, int idx, const char *k);
774+
friend int lua_load(lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode);
763775
};
764776

765777

0 commit comments

Comments
 (0)