aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-08-30 11:26:16 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-08-30 11:26:16 -0300
commitf33cda8d6eb1cac5b9042429e85f1096175c7ca5 (patch)
treefc82b6d637628a489bb68fa8ac6f320334170ace
parent96f77142374da8a4a7d4e5e8afd559fbaf0430e8 (diff)
downloadlua-f33cda8d6eb1cac5b9042429e85f1096175c7ca5.tar.gz
lua-f33cda8d6eb1cac5b9042429e85f1096175c7ca5.tar.bz2
lua-f33cda8d6eb1cac5b9042429e85f1096175c7ca5.zip
New macro 'getlstr'
Accesses content and length of a 'TString'.
Diffstat (limited to '')
-rw-r--r--ldebug.c10
-rw-r--r--ldump.c15
-rw-r--r--lobject.h11
-rw-r--r--lparser.c6
-rw-r--r--lvm.c17
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) {
264 else { 264 else {
265 const Proto *p = cl->l.p; 265 const Proto *p = cl->l.p;
266 if (p->source) { 266 if (p->source) {
267 ar->source = getstr(p->source); 267 ar->source = getlstr(p->source, ar->srclen);
268 ar->srclen = tsslen(p->source);
269 } 268 }
270 else { 269 else {
271 ar->source = "=?"; 270 ar->source = "=?";
@@ -797,8 +796,11 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
797const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, 796const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
798 int line) { 797 int line) {
799 char buff[LUA_IDSIZE]; 798 char buff[LUA_IDSIZE];
800 if (src) 799 if (src) {
801 luaO_chunkid(buff, getstr(src), tsslen(src)); 800 size_t idlen;
801 const char *id = getlstr(src, idlen);
802 luaO_chunkid(buff, id, idlen);
803 }
802 else { /* no source available; use "?" instead */ 804 else { /* no source available; use "?" instead */
803 buff[0] = '?'; buff[1] = '\0'; 805 buff[0] = '?'; buff[1] = '\0';
804 } 806 }
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) {
112** size==size-2 and means that string, which will be saved with 112** size==size-2 and means that string, which will be saved with
113** the next available index. 113** the next available index.
114*/ 114*/
115static void dumpString (DumpState *D, TString *s) { 115static void dumpString (DumpState *D, TString *ts) {
116 if (s == NULL) 116 if (ts == NULL)
117 dumpSize(D, 0); 117 dumpSize(D, 0);
118 else { 118 else {
119 const TValue *idx = luaH_getstr(D->h, s); 119 const TValue *idx = luaH_getstr(D->h, ts);
120 if (ttisinteger(idx)) { /* string already saved? */ 120 if (ttisinteger(idx)) { /* string already saved? */
121 dumpSize(D, 1); /* reuse a saved string */ 121 dumpSize(D, 1); /* reuse a saved string */
122 dumpInt(D, ivalue(idx)); /* index of saved string */ 122 dumpInt(D, ivalue(idx)); /* index of saved string */
123 } 123 }
124 else { /* must write and save the string */ 124 else { /* must write and save the string */
125 TValue key, value; /* to save the string in the hash */ 125 TValue key, value; /* to save the string in the hash */
126 size_t size = tsslen(s); 126 size_t size;
127 const char *s = getlstr(ts, size);
127 dumpSize(D, size + 2); 128 dumpSize(D, size + 2);
128 dumpVector(D, getstr(s), size); 129 dumpVector(D, s, size);
129 D->nstr++; /* one more saved string */ 130 D->nstr++; /* one more saved string */
130 setsvalue(D->L, &key, s); /* the string is the key */ 131 setsvalue(D->L, &key, ts); /* the string is the key */
131 setivalue(&value, D->nstr); /* its index is the value */ 132 setivalue(&value, D->nstr); /* its index is the value */
132 luaH_finishset(D->L, D->h, &key, idx, &value); /* h[s] = nstr */ 133 luaH_finishset(D->L, D->h, &key, idx, &value); /* h[ts] = nstr */
133 /* integer value does not need barrier */ 134 /* integer value does not need barrier */
134 } 135 }
135 } 136 }
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 {
392 size_t lnglen; /* length for long strings */ 392 size_t lnglen; /* length for long strings */
393 struct TString *hnext; /* linked list for hash table */ 393 struct TString *hnext; /* linked list for hash table */
394 } u; 394 } u;
395 char contents[1]; 395 char contents[1]; /* string body starts here */
396} TString; 396} TString;
397 397
398 398
@@ -401,15 +401,22 @@ typedef struct TString {
401** Get the actual string (array of bytes) from a 'TString'. (Generic 401** Get the actual string (array of bytes) from a 'TString'. (Generic
402** version and specialized versions for long and short strings.) 402** version and specialized versions for long and short strings.)
403*/ 403*/
404#define getstr(ts) ((ts)->contents)
405#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents) 404#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents)
406#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents) 405#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents)
406#define getstr(ts) ((ts)->contents)
407 407
408 408
409/* get string length from 'TString *s' */ 409/* get string length from 'TString *s' */
410#define tsslen(s) \ 410#define tsslen(s) \
411 ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen) 411 ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen)
412 412
413/*
414** Get string and length */
415#define getlstr(ts, len) \
416 ((ts)->shrlen != 0xFF \
417 ? (cast_void(len = (ts)->shrlen), (ts)->contents) \
418 : (cast_void(len = (ts)->u.lnglen), (ts)->contents))
419
413/* }================================================================== */ 420/* }================================================================== */
414 421
415 422
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) {
520** local variable. 520** local variable.
521*/ 521*/
522static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { 522static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) {
523 const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); 523 TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name;
524 const char *varname = getstr(tsname);
524 const char *msg = "<goto %s> at line %d jumps into the scope of local '%s'"; 525 const char *msg = "<goto %s> at line %d jumps into the scope of local '%s'";
525 msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); 526 msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname);
526 luaK_semerror(ls, msg); /* raise the error */ 527 luaK_semerror(ls, msg); /* raise the error */
@@ -1708,7 +1709,8 @@ static void localfunc (LexState *ls) {
1708static int getlocalattribute (LexState *ls) { 1709static int getlocalattribute (LexState *ls) {
1709 /* ATTRIB -> ['<' Name '>'] */ 1710 /* ATTRIB -> ['<' Name '>'] */
1710 if (testnext(ls, '<')) { 1711 if (testnext(ls, '<')) {
1711 const char *attr = getstr(str_checkname(ls)); 1712 TString *ts = str_checkname(ls);
1713 const char *attr = getstr(ts);
1712 checknext(ls, '>'); 1714 checknext(ls, '>');
1713 if (strcmp(attr, "const") == 0) 1715 if (strcmp(attr, "const") == 0)
1714 return RDKCONST; /* read-only variable */ 1716 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) {
93 return 0; 93 return 0;
94 else { 94 else {
95 TString *st = tsvalue(obj); 95 TString *st = tsvalue(obj);
96 return (luaO_str2num(getstr(st), result) == tsslen(st) + 1); 96 size_t stlen;
97 const char *s = getlstr(st, stlen);
98 return (luaO_str2num(s, result) == stlen + 1);
97 } 99 }
98} 100}
99 101
@@ -377,10 +379,10 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
377** have different lengths. 379** have different lengths.
378*/ 380*/
379static int l_strcmp (const TString *ts1, const TString *ts2) { 381static int l_strcmp (const TString *ts1, const TString *ts2) {
380 const char *s1 = getstr(ts1); 382 size_t rl1; /* real length */
381 size_t rl1 = tsslen(ts1); /* real length */ 383 const char *s1 = getlstr(ts1, rl1);
382 const char *s2 = getstr(ts2); 384 size_t rl2;
383 size_t rl2 = tsslen(ts2); 385 const char *s2 = getlstr(ts2, rl2);
384 for (;;) { /* for each segment */ 386 for (;;) { /* for each segment */
385 int temp = strcoll(s1, s2); 387 int temp = strcoll(s1, s2);
386 if (temp != 0) /* not equal? */ 388 if (temp != 0) /* not equal? */
@@ -630,8 +632,9 @@ static void copy2buff (StkId top, int n, char *buff) {
630 size_t tl = 0; /* size already copied */ 632 size_t tl = 0; /* size already copied */
631 do { 633 do {
632 TString *st = tsvalue(s2v(top - n)); 634 TString *st = tsvalue(s2v(top - n));
633 size_t l = tsslen(st); /* length of string being copied */ 635 size_t l; /* length of string being copied */
634 memcpy(buff + tl, getstr(st), l * sizeof(char)); 636 const char *s = getlstr(st, l);
637 memcpy(buff + tl, s, l * sizeof(char));
635 tl += l; 638 tl += l;
636 } while (--n > 0); 639 } while (--n > 0);
637} 640}