Skip to content

Commit c2bda03

Browse files
authored
Merge pull request #68 from NiceAndPeter/claude/refactor-luaS-to-tstring-01WJ3LCFyV6gjhcuM8ptn18h
Convert luaS functions to TString methods
2 parents 8fa3882 + 49d832a commit c2bda03

File tree

19 files changed

+93
-121
lines changed

19 files changed

+93
-121
lines changed

src/compiler/llex.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void luaX_init (lua_State *L) {
6666
TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */
6767
obj2gco(e)->fix(L); /* Phase 25c: never collect this name */
6868
for (int i=0; i<NUM_RESERVED; i++) {
69-
TString *ts = luaS_new(L, luaX_tokens[i]);
69+
TString *ts = TString::create(L, luaX_tokens[i]);
7070
obj2gco(ts)->fix(L); /* Phase 25c: reserved words are never collected */
7171
ts->setExtra(cast_byte(i+1)); /* reserved word */
7272
}
@@ -138,7 +138,7 @@ TString* LexState::anchorStr(TString *ts) {
138138
** Creates a new string and anchors it in scanner's table.
139139
*/
140140
TString *LexState::newString(const char *str, size_t l) {
141-
return anchorStr(luaS_newlstr(getLuaState(), str, l));
141+
return anchorStr(TString::create(getLuaState(), str, l));
142142
}
143143

144144
/*
@@ -542,7 +542,7 @@ int LexState::lex(SemInfo *seminfo) {
542542
saveAndNext();
543543
} while (lislalnum(getCurrentChar()));
544544
/* find or create string */
545-
ts = luaS_newlstr(getLuaState(), luaZ_buffer(getBuffer()),
545+
ts = TString::create(getLuaState(), luaZ_buffer(getBuffer()),
546546
luaZ_bufflen(getBuffer()));
547547
if (isreserved(ts)) /* reserved word? */
548548
return ts->getExtra() - 1 + FIRST_RESERVED;

src/compiler/parseutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
191191
funcstate.setProto(luaF_newproto(L));
192192
cl->setProto(funcstate.getProto());
193193
luaC_objbarrier(L, cl, cl->getProto());
194-
funcstate.getProto()->setSource(luaS_new(L, name)); /* create and anchor TString */
194+
funcstate.getProto()->setSource(TString::create(L, name)); /* create and anchor TString */
195195
luaC_objbarrier(L, funcstate.getProto(), funcstate.getProto()->getSource());
196196
lexstate.setBuffer(buff);
197197
lexstate.setDyndata(dyd);

src/core/lapi.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
489489
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
490490
TString *ts;
491491
lua_lock(L);
492-
ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
492+
ts = (len == 0) ? TString::create(L, "") : TString::create(L, s, len);
493493
setsvalue2s(L, L->getTop().p, ts);
494494
api_incr_top(L);
495495
luaC_checkGC(L);
@@ -504,7 +504,7 @@ LUA_API const char *lua_pushexternalstring (lua_State *L,
504504
lua_lock(L);
505505
api_check(L, len <= MAX_SIZE, "string too large");
506506
api_check(L, s[len] == '\0', "string not ending with zero");
507-
ts = luaS_newextlstr (L, s, len, falloc, ud);
507+
ts = TString::createExternal(L, s, len, falloc, ud);
508508
setsvalue2s(L, L->getTop().p, ts);
509509
api_incr_top(L);
510510
luaC_checkGC(L);
@@ -519,7 +519,7 @@ LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
519519
setnilvalue(s2v(L->getTop().p));
520520
else {
521521
TString *ts;
522-
ts = luaS_new(L, s);
522+
ts = TString::create(L, s);
523523
setsvalue2s(L, L->getTop().p, ts);
524524
s = getstr(ts); /* internal copy's address */
525525
}
@@ -613,7 +613,7 @@ LUA_API int lua_pushthread (lua_State *L) {
613613

614614
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
615615
lu_byte tag;
616-
TString *str = luaS_new(L, k);
616+
TString *str = TString::create(L, k);
617617
tag = luaV_fastget(t, str, s2v(L->getTop().p), luaH_getstr);
618618
if (!tagisempty(tag))
619619
api_incr_top(L);
@@ -794,7 +794,7 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
794794
*/
795795
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
796796
int hres;
797-
TString *str = luaS_new(L, k);
797+
TString *str = TString::create(L, k);
798798
api_checkpop(L, 1);
799799
hres = luaV_fastset(t, str, s2v(L->getTop().p - 1), luaH_psetstr);
800800
if (hres == HOK) {
@@ -1225,7 +1225,7 @@ LUA_API void lua_concat (lua_State *L, int n) {
12251225
luaC_checkGC(L);
12261226
}
12271227
else { /* nothing to concatenate */
1228-
setsvalue2s(L, L->getTop().p, luaS_newlstr(L, "", 0)); /* push empty string */
1228+
setsvalue2s(L, L->getTop().p, TString::create(L, "", 0)); /* push empty string */
12291229
api_incr_top(L);
12301230
}
12311231
lua_unlock(L);

src/core/ldebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ l_noret lua_State::errorMsg() {
895895
}
896896
if (ttisnil(s2v(getTop().p - 1))) { /* error object is nil? */
897897
/* change it to a proper message */
898-
setsvalue2s(this, getTop().p - 1, luaS_newliteral(this, "<no error object>"));
898+
setsvalue2s(this, getTop().p - 1, TString::create(this, "<no error object>", 17));
899899
}
900900
doThrow(LUA_ERRRUN);
901901
}

src/core/ldo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ TStatus lua_State::rawRunProtected(Pfunc f, void *ud) {
297297
/* raise a stack error while running the message handler */
298298
// Convert to lua_State method
299299
l_noret lua_State::errorError() {
300-
TString *msg = luaS_newliteral(this, "error in error handling");
300+
TString *msg = TString::create(this, "error in error handling", 23);
301301
setsvalue2s(this, getTop().p, msg);
302302
getStackSubsystem().push(); /* assume EXTRA_STACK */
303303
doThrow(LUA_ERRERR);
@@ -826,7 +826,7 @@ CallInfo* lua_State::findPCall() {
826826
static int resume_error (lua_State *L, const char *msg, int narg) {
827827
api_checkpop(L, narg);
828828
L->getStackSubsystem().popN(narg); /* remove args from the stack */
829-
setsvalue2s(L, L->getTop().p, luaS_new(L, msg)); /* push error message */
829+
setsvalue2s(L, L->getTop().p, TString::create(L, msg)); /* push error message */
830830
api_incr_top(L);
831831
lua_unlock(L);
832832
return LUA_ERRRUN;

src/core/lstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static void f_luaopen (lua_State *L, void *ud) {
214214
UNUSED(ud);
215215
stack_init(L, L); /* init stack */
216216
init_registry(L, g);
217-
luaS_init(L);
217+
TString::init(L);
218218
luaT_init(L);
219219
luaX_init(L);
220220
g->setGCStp(0); /* allow gc */

src/core/ltm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void luaT_init (lua_State *L) {
4848
};
4949
int i;
5050
for (i=0; i<static_cast<int>(TMS::TM_N); i++) {
51-
G(L)->setTMName(i, luaS_new(L, luaT_eventname[i]));
51+
G(L)->setTMName(i, TString::create(L, luaT_eventname[i]));
5252
obj2gco(G(L)->getTMName(i))->fix(L); /* never collect these names */
5353
}
5454
}
@@ -93,7 +93,7 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) {
9393
Table *mt;
9494
if ((ttistable(o) && (mt = hvalue(o)->getMetatable()) != nullptr) ||
9595
(ttisfulluserdata(o) && (mt = uvalue(o)->getMetatable()) != nullptr)) {
96-
const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name"));
96+
const TValue *name = luaH_Hgetshortstr(mt, TString::create(L, "__name"));
9797
if (ttisstring(name)) /* is '__name' a string? */
9898
return getstr(tsvalue(name)); /* use it as type name */
9999
}

src/memory/gc/gc_collector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void GCCollector::atomic(lua_State* L) {
7373
/* clear values from resurrected weak tables */
7474
GCWeak::clearbyvalues(g, g->getWeak(), origweak);
7575
GCWeak::clearbyvalues(g, g->getAllWeak(), origall);
76-
luaS_clearcache(g);
76+
TString::clearCache(g);
7777
g->setCurrentWhite(cast_byte(otherwhite(g))); /* flip current white */
7878
lua_assert(g->getGray() == nullptr);
7979
}

src/memory/gc/gc_core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ l_mem GCCore::objsize(GCObject* o) {
5959
}
6060
case LUA_VLNGSTR: {
6161
TString* ts = gco2ts(o);
62-
res = luaS_sizelngstr(ts->getLnglen(), ts->getShrlen());
62+
res = TString::calculateLongStringSize(ts->getLnglen(), ts->getShrlen());
6363
break;
6464
}
6565
case LUA_VUPVAL: {

src/memory/gc/gc_finalizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
void GCFinalizer::checkSizes(lua_State* L, global_State* g) {
4747
if (!g->getGCEmergency()) {
4848
if (g->getStringTable()->getNumElements() < g->getStringTable()->getSize() / 4)
49-
luaS_resize(L, g->getStringTable()->getSize() / 2);
49+
TString::resize(L, g->getStringTable()->getSize() / 2);
5050
}
5151
}
5252

0 commit comments

Comments
 (0)