aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-04-04 11:45:26 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-04-04 11:45:26 -0300
commit5ca1075b714e825006e8ba4f8e7ea5544879bb41 (patch)
treead085afa58f29e8e96071ae88c1e6e1dd73e0750
parent38425e069243fe6d991f2e99b4bba192af3563c7 (diff)
downloadlua-5ca1075b714e825006e8ba4f8e7ea5544879bb41.tar.gz
lua-5ca1075b714e825006e8ba4f8e7ea5544879bb41.tar.bz2
lua-5ca1075b714e825006e8ba4f8e7ea5544879bb41.zip
Added field 'srclen' to structure 'lua_Debug'
This new field gets the length of 'source' in the same structure. Unlike the other strings in that structure, 'source' can be relatively large, and Lua already has its length readily available.
-rw-r--r--ldblib.c3
-rw-r--r--ldebug.c14
-rw-r--r--llimits.h4
-rw-r--r--lobject.c27
-rw-r--r--lobject.h2
-rw-r--r--lua.h1
6 files changed, 31 insertions, 20 deletions
diff --git a/ldblib.c b/ldblib.c
index ada35250..d045a82e 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -167,7 +167,8 @@ static int db_getinfo (lua_State *L) {
167 return luaL_argerror(L, arg+2, "invalid option"); 167 return luaL_argerror(L, arg+2, "invalid option");
168 lua_newtable(L); /* table to collect results */ 168 lua_newtable(L); /* table to collect results */
169 if (strchr(options, 'S')) { 169 if (strchr(options, 'S')) {
170 settabss(L, "source", ar.source); 170 lua_pushlstring(L, ar.source, ar.srclen);
171 lua_setfield(L, -2, "source");
171 settabss(L, "short_src", ar.short_src); 172 settabss(L, "short_src", ar.short_src);
172 settabsi(L, "linedefined", ar.linedefined); 173 settabsi(L, "linedefined", ar.linedefined);
173 settabsi(L, "lastlinedefined", ar.lastlinedefined); 174 settabsi(L, "lastlinedefined", ar.lastlinedefined);
diff --git a/ldebug.c b/ldebug.c
index bd471e0c..6cd4e071 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -262,18 +262,26 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
262static void funcinfo (lua_Debug *ar, Closure *cl) { 262static void funcinfo (lua_Debug *ar, Closure *cl) {
263 if (noLuaClosure(cl)) { 263 if (noLuaClosure(cl)) {
264 ar->source = "=[C]"; 264 ar->source = "=[C]";
265 ar->srclen = LL("=[C]");
265 ar->linedefined = -1; 266 ar->linedefined = -1;
266 ar->lastlinedefined = -1; 267 ar->lastlinedefined = -1;
267 ar->what = "C"; 268 ar->what = "C";
268 } 269 }
269 else { 270 else {
270 const Proto *p = cl->l.p; 271 const Proto *p = cl->l.p;
271 ar->source = p->source ? getstr(p->source) : "=?"; 272 if (p->source) {
273 ar->source = getstr(p->source);
274 ar->srclen = tsslen(p->source);
275 }
276 else {
277 ar->source = "=?";
278 ar->srclen = LL("=?");
279 }
272 ar->linedefined = p->linedefined; 280 ar->linedefined = p->linedefined;
273 ar->lastlinedefined = p->lastlinedefined; 281 ar->lastlinedefined = p->lastlinedefined;
274 ar->what = (ar->linedefined == 0) ? "main" : "Lua"; 282 ar->what = (ar->linedefined == 0) ? "main" : "Lua";
275 } 283 }
276 luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); 284 luaO_chunkid(ar->short_src, ar->source, ar->srclen);
277} 285}
278 286
279 287
@@ -750,7 +758,7 @@ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
750 int line) { 758 int line) {
751 char buff[LUA_IDSIZE]; 759 char buff[LUA_IDSIZE];
752 if (src) 760 if (src)
753 luaO_chunkid(buff, getstr(src), LUA_IDSIZE); 761 luaO_chunkid(buff, getstr(src), tsslen(src));
754 else { /* no source available; use "?" instead */ 762 else { /* no source available; use "?" instead */
755 buff[0] = '?'; buff[1] = '\0'; 763 buff[0] = '?'; buff[1] = '\0';
756 } 764 }
diff --git a/llimits.h b/llimits.h
index 3df873db..cc983972 100644
--- a/llimits.h
+++ b/llimits.h
@@ -65,6 +65,10 @@ typedef signed char ls_byte;
65#define ispow2(x) (((x) & ((x) - 1)) == 0) 65#define ispow2(x) (((x) & ((x) - 1)) == 0)
66 66
67 67
68/* number of chars of a literal string without the ending \0 */
69#define LL(x) (sizeof(x)/sizeof(char) - 1)
70
71
68/* 72/*
69** conversion of pointer to unsigned integer: 73** conversion of pointer to unsigned integer:
70** this is for hashing only; there is no problem if the integer 74** this is for hashing only; there is no problem if the integer
diff --git a/lobject.c b/lobject.c
index 5d340de6..67c37124 100644
--- a/lobject.c
+++ b/lobject.c
@@ -473,45 +473,42 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
473} 473}
474 474
475 475
476/* number of chars of a literal string without the ending \0 */
477#define LL(x) (sizeof(x)/sizeof(char) - 1)
478
479#define RETS "..." 476#define RETS "..."
480#define PRE "[string \"" 477#define PRE "[string \""
481#define POS "\"]" 478#define POS "\"]"
482 479
483#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) 480#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
484 481
485void luaO_chunkid (char *out, const char *source, size_t bufflen) { 482void luaO_chunkid (char *out, const char *source, size_t srclen) {
486 size_t l = strlen(source); 483 size_t bufflen = LUA_IDSIZE; /* free space in buffer */
487 if (*source == '=') { /* 'literal' source */ 484 if (*source == '=') { /* 'literal' source */
488 if (l <= bufflen) /* small enough? */ 485 if (srclen <= bufflen) /* small enough? */
489 memcpy(out, source + 1, l * sizeof(char)); 486 memcpy(out, source + 1, srclen * sizeof(char));
490 else { /* truncate it */ 487 else { /* truncate it */
491 addstr(out, source + 1, bufflen - 1); 488 addstr(out, source + 1, bufflen - 1);
492 *out = '\0'; 489 *out = '\0';
493 } 490 }
494 } 491 }
495 else if (*source == '@') { /* file name */ 492 else if (*source == '@') { /* file name */
496 if (l <= bufflen) /* small enough? */ 493 if (srclen <= bufflen) /* small enough? */
497 memcpy(out, source + 1, l * sizeof(char)); 494 memcpy(out, source + 1, srclen * sizeof(char));
498 else { /* add '...' before rest of name */ 495 else { /* add '...' before rest of name */
499 addstr(out, RETS, LL(RETS)); 496 addstr(out, RETS, LL(RETS));
500 bufflen -= LL(RETS); 497 bufflen -= LL(RETS);
501 memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); 498 memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char));
502 } 499 }
503 } 500 }
504 else { /* string; format as [string "source"] */ 501 else { /* string; format as [string "source"] */
505 const char *nl = strchr(source, '\n'); /* find first new line (if any) */ 502 const char *nl = strchr(source, '\n'); /* find first new line (if any) */
506 addstr(out, PRE, LL(PRE)); /* add prefix */ 503 addstr(out, PRE, LL(PRE)); /* add prefix */
507 bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ 504 bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
508 if (l < bufflen && nl == NULL) { /* small one-line source? */ 505 if (srclen < bufflen && nl == NULL) { /* small one-line source? */
509 addstr(out, source, l); /* keep it */ 506 addstr(out, source, srclen); /* keep it */
510 } 507 }
511 else { 508 else {
512 if (nl != NULL) l = nl - source; /* stop at first newline */ 509 if (nl != NULL) srclen = nl - source; /* stop at first newline */
513 if (l > bufflen) l = bufflen; 510 if (srclen > bufflen) srclen = bufflen;
514 addstr(out, source, l); 511 addstr(out, source, srclen);
515 addstr(out, RETS, LL(RETS)); 512 addstr(out, RETS, LL(RETS));
516 } 513 }
517 memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); 514 memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
diff --git a/lobject.h b/lobject.h
index 53e67932..403b6047 100644
--- a/lobject.h
+++ b/lobject.h
@@ -747,7 +747,7 @@ LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
747LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 747LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
748 va_list argp); 748 va_list argp);
749LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); 749LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
750LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); 750LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
751 751
752 752
753#endif 753#endif
diff --git a/lua.h b/lua.h
index 7c4a13bc..ec31c781 100644
--- a/lua.h
+++ b/lua.h
@@ -469,6 +469,7 @@ struct lua_Debug {
469 const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ 469 const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */
470 const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ 470 const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */
471 const char *source; /* (S) */ 471 const char *source; /* (S) */
472 size_t srclen; /* (S) */
472 int currentline; /* (l) */ 473 int currentline; /* (l) */
473 int linedefined; /* (S) */ 474 int linedefined; /* (S) */
474 int lastlinedefined; /* (S) */ 475 int lastlinedefined; /* (S) */