From f33cda8d6eb1cac5b9042429e85f1096175c7ca5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Aug 2023 11:26:16 -0300 Subject: New macro 'getlstr' Accesses content and length of a 'TString'. --- ldebug.c | 10 ++++++---- ldump.c | 15 ++++++++------- lobject.h | 11 +++++++++-- lparser.c | 6 ++++-- lvm.c | 17 ++++++++++------- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/ldebug.c b/ldebug.c index 1b789520..504459a6 100644 --- a/ldebug.c +++ b/ldebug.c @@ -264,8 +264,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { else { const Proto *p = cl->l.p; if (p->source) { - ar->source = getstr(p->source); - ar->srclen = tsslen(p->source); + ar->source = getlstr(p->source, ar->srclen); } else { ar->source = "=?"; @@ -797,8 +796,11 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line) { char buff[LUA_IDSIZE]; - if (src) - luaO_chunkid(buff, getstr(src), tsslen(src)); + if (src) { + size_t idlen; + const char *id = getlstr(src, idlen); + luaO_chunkid(buff, id, idlen); + } else { /* no source available; use "?" instead */ buff[0] = '?'; buff[1] = '\0'; } diff --git a/ldump.c b/ldump.c index bb15e45f..01169c12 100644 --- a/ldump.c +++ b/ldump.c @@ -112,24 +112,25 @@ static void dumpInteger (DumpState *D, lua_Integer x) { ** size==size-2 and means that string, which will be saved with ** the next available index. */ -static void dumpString (DumpState *D, TString *s) { - if (s == NULL) +static void dumpString (DumpState *D, TString *ts) { + if (ts == NULL) dumpSize(D, 0); else { - const TValue *idx = luaH_getstr(D->h, s); + const TValue *idx = luaH_getstr(D->h, ts); if (ttisinteger(idx)) { /* string already saved? */ dumpSize(D, 1); /* reuse a saved string */ dumpInt(D, ivalue(idx)); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ - size_t size = tsslen(s); + size_t size; + const char *s = getlstr(ts, size); dumpSize(D, size + 2); - dumpVector(D, getstr(s), size); + dumpVector(D, s, size); D->nstr++; /* one more saved string */ - setsvalue(D->L, &key, s); /* the string is the key */ + setsvalue(D->L, &key, ts); /* the string is the key */ setivalue(&value, D->nstr); /* its index is the value */ - luaH_finishset(D->L, D->h, &key, idx, &value); /* h[s] = nstr */ + luaH_finishset(D->L, D->h, &key, idx, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } } diff --git a/lobject.h b/lobject.h index 0e4924f5..9e7953e3 100644 --- a/lobject.h +++ b/lobject.h @@ -392,7 +392,7 @@ typedef struct TString { size_t lnglen; /* length for long strings */ struct TString *hnext; /* linked list for hash table */ } u; - char contents[1]; + char contents[1]; /* string body starts here */ } TString; @@ -401,15 +401,22 @@ typedef struct TString { ** Get the actual string (array of bytes) from a 'TString'. (Generic ** version and specialized versions for long and short strings.) */ -#define getstr(ts) ((ts)->contents) #define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents) #define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents) +#define getstr(ts) ((ts)->contents) /* get string length from 'TString *s' */ #define tsslen(s) \ ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen) +/* +** Get string and length */ +#define getlstr(ts, len) \ + ((ts)->shrlen != 0xFF \ + ? (cast_void(len = (ts)->shrlen), (ts)->contents) \ + : (cast_void(len = (ts)->u.lnglen), (ts)->contents)) + /* }================================================================== */ diff --git a/lparser.c b/lparser.c index a1164510..2a84637a 100644 --- a/lparser.c +++ b/lparser.c @@ -520,7 +520,8 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { ** local variable. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { - const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); + TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; + const char *varname = getstr(tsname); const char *msg = " at line %d jumps into the scope of local '%s'"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); luaK_semerror(ls, msg); /* raise the error */ @@ -1708,7 +1709,8 @@ static void localfunc (LexState *ls) { static int getlocalattribute (LexState *ls) { /* ATTRIB -> ['<' Name '>'] */ if (testnext(ls, '<')) { - const char *attr = getstr(str_checkname(ls)); + TString *ts = str_checkname(ls); + const char *attr = getstr(ts); checknext(ls, '>'); if (strcmp(attr, "const") == 0) return RDKCONST; /* read-only variable */ diff --git a/lvm.c b/lvm.c index 256d689f..0a6d6270 100644 --- a/lvm.c +++ b/lvm.c @@ -93,7 +93,9 @@ static int l_strton (const TValue *obj, TValue *result) { return 0; else { TString *st = tsvalue(obj); - return (luaO_str2num(getstr(st), result) == tsslen(st) + 1); + size_t stlen; + const char *s = getlstr(st, stlen); + return (luaO_str2num(s, result) == stlen + 1); } } @@ -377,10 +379,10 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, ** have different lengths. */ static int l_strcmp (const TString *ts1, const TString *ts2) { - const char *s1 = getstr(ts1); - size_t rl1 = tsslen(ts1); /* real length */ - const char *s2 = getstr(ts2); - size_t rl2 = tsslen(ts2); + size_t rl1; /* real length */ + const char *s1 = getlstr(ts1, rl1); + size_t rl2; + const char *s2 = getlstr(ts2, rl2); for (;;) { /* for each segment */ int temp = strcoll(s1, s2); if (temp != 0) /* not equal? */ @@ -630,8 +632,9 @@ static void copy2buff (StkId top, int n, char *buff) { size_t tl = 0; /* size already copied */ do { TString *st = tsvalue(s2v(top - n)); - size_t l = tsslen(st); /* length of string being copied */ - memcpy(buff + tl, getstr(st), l * sizeof(char)); + size_t l; /* length of string being copied */ + const char *s = getlstr(st, l); + memcpy(buff + tl, s, l * sizeof(char)); tl += l; } while (--n > 0); } -- cgit v1.2.3-55-g6feb