New macro 'getlstr'

Accesses content and length of a 'TString'.
This commit is contained in:
Roberto Ierusalimschy 2023-08-30 11:26:16 -03:00
parent 96f7714237
commit f33cda8d6e
5 changed files with 37 additions and 22 deletions

View File

@ -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';
}

15
ldump.c
View File

@ -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 */
}
}

View File

@ -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))
/* }================================================================== */

View File

@ -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 = "<goto %s> 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 */

17
lvm.c
View File

@ -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);
}