aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-07-03 11:54:58 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-07-03 11:54:58 -0300
commitae809e9fd132ab867741a6a777450f9bc0d49be4 (patch)
tree402821af72bbf8ee5a5779b48ba978cb6f578416
parente96385adede47a1abf160a41565ec742d3d4e413 (diff)
downloadlua-ae809e9fd132ab867741a6a777450f9bc0d49be4.tar.gz
lua-ae809e9fd132ab867741a6a777450f9bc0d49be4.tar.bz2
lua-ae809e9fd132ab867741a6a777450f9bc0d49be4.zip
'luaV_concat' can "concat" one single value
Several of its callers needed that case and had to do the check themselves.
-rw-r--r--lapi.c8
-rw-r--r--lobject.c6
-rw-r--r--lvm.c9
3 files changed, 9 insertions, 14 deletions
diff --git a/lapi.c b/lapi.c
index 184b8dd7..bbba88a3 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1239,14 +1239,12 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
1239LUA_API void lua_concat (lua_State *L, int n) { 1239LUA_API void lua_concat (lua_State *L, int n) {
1240 lua_lock(L); 1240 lua_lock(L);
1241 api_checknelems(L, n); 1241 api_checknelems(L, n);
1242 if (n >= 2) { 1242 if (n > 0)
1243 luaV_concat(L, n); 1243 luaV_concat(L, n);
1244 } 1244 else { /* nothing to concatenate */
1245 else if (n == 0) { /* push empty string */ 1245 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */
1246 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1247 api_incr_top(L); 1246 api_incr_top(L);
1248 } 1247 }
1249 /* else n == 1; nothing to do */
1250 luaC_checkGC(L); 1248 luaC_checkGC(L);
1251 lua_unlock(L); 1249 lua_unlock(L);
1252} 1250}
diff --git a/lobject.c b/lobject.c
index 2a28ebd4..223bbd0c 100644
--- a/lobject.c
+++ b/lobject.c
@@ -402,10 +402,8 @@ static void pushstr (BuffFS *buff, const char *str, size_t l) {
402 setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); 402 setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
403 L->top++; /* may use one extra slot */ 403 L->top++; /* may use one extra slot */
404 buff->pushed++; 404 buff->pushed++;
405 if (buff->pushed > 1) { 405 luaV_concat(L, buff->pushed); /* join partial results into one */
406 luaV_concat(L, buff->pushed); /* join partial results into one */ 406 buff->pushed = 1;
407 buff->pushed = 1;
408 }
409} 407}
410 408
411 409
diff --git a/lvm.c b/lvm.c
index e7781dbf..ccbfbab5 100644
--- a/lvm.c
+++ b/lvm.c
@@ -634,7 +634,8 @@ static void copy2buff (StkId top, int n, char *buff) {
634** from 'L->top - total' up to 'L->top - 1'. 634** from 'L->top - total' up to 'L->top - 1'.
635*/ 635*/
636void luaV_concat (lua_State *L, int total) { 636void luaV_concat (lua_State *L, int total) {
637 lua_assert(total >= 2); 637 if (total == 1)
638 return; /* "all" values already concatenated */
638 do { 639 do {
639 StkId top = L->top; 640 StkId top = L->top;
640 int n = 2; /* number of elements handled in this pass (at least 2) */ 641 int n = 2; /* number of elements handled in this pass (at least 2) */
@@ -840,10 +841,8 @@ void luaV_finishOp (lua_State *L) {
840 int a = GETARG_A(inst); /* first element to concatenate */ 841 int a = GETARG_A(inst); /* first element to concatenate */
841 int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ 842 int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */
842 setobjs2s(L, top - 2, top); /* put TM result in proper position */ 843 setobjs2s(L, top - 2, top); /* put TM result in proper position */
843 if (total > 1) { /* are there elements to concat? */ 844 L->top = top - 1; /* top is one after last element (at top-2) */
844 L->top = top - 1; /* top is one after last element (at top-2) */ 845 luaV_concat(L, total); /* concat them (may yield again) */
845 luaV_concat(L, total); /* concat them (may yield again) */
846 }
847 break; 846 break;
848 } 847 }
849 default: { 848 default: {