Skip to content

Commit 1b7bc9f

Browse files
committed
Simplify llDumpList2String() implementation, remove hard limits
1 parent 379394a commit 1b7bc9f

2 files changed

Lines changed: 15 additions & 18 deletions

File tree

VM/src/lll.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "ltable.h"
88
#include "lapi.h"
99
#include "llsl.h"
10+
#include "lstrbuf.h"
1011
#include "mono_strings.h"
1112
#include "lgcgraph.h"
1213

@@ -392,22 +393,19 @@ static int ll_dumplist2string(lua_State *L)
392393
return 1;
393394
}
394395

395-
lua_checkstack(L, 4);
396+
const char *sep = nullptr;
397+
size_t sep_size = 0;
398+
sep = lua_tolstring(L, 2, &sep_size);
399+
400+
lua_checkstack(L, 5);
396401

397-
// Assume at least 1 character per item + separator length for our initial capacity.
398-
// This is essentially a minimum but gets us in the right ballpark without counting everything
399-
// StringBuilder will allocate more as needed.
400-
// Set the max capacity to 64k (the max script size) + a little slop.
401-
static const int max_size = 65 * 1024;
402-
bool first = true;
403-
lua_pushstring(L, "");
402+
auto *strbuf = luaYB_push(L);
404403
for(int i=0; i<len; ++i)
405404
{
406-
if (first)
407-
lua_pushstring(L, "");
408-
else
409-
lua_pushvalue(L, 2);
410-
first = false;
405+
if (i != 0)
406+
{
407+
luaYB_appendmem(L, strbuf, sep, sep_size);
408+
}
411409

412410
// Unlike (string)list_val, this doesn't keep negative zero.
413411
lua_pushcfunction(L, lsl_cast_list_elem_poszero, "lsl_cast_list_elem_poszero");
@@ -418,13 +416,10 @@ static int ll_dumplist2string(lua_State *L)
418416
if (lua_type(L, -1) == LUA_TNIL)
419417
luaL_errorL(L, "non-LSL value in list");
420418

421-
lua_concat(L, 3);
422-
if (lua_strlen(L, 3) > max_size)
423-
{
424-
luaD_throw(L, LUA_ERRMEM);
425-
}
419+
luaYB_addvalue(L, strbuf);
426420
}
427421

422+
luaYB_pushresult(L, strbuf);
428423
return 1;
429424
}
430425

tests/conformance/list_operations.lsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ default
8181
checkTruth("huge indices2", (string)llList2List(NUMBERS, 201, 200) == "0123456789");
8282
checkTruth("huge indices3", (string)llList2List(NUMBERS, 1, 200) == "123456789");
8383
checkTruth("llDumpList2String", llDumpList2String(l, "|") == "1|1.100000|foo|00000000-0000-0000-0000-000000000000|<1.000000, 2.000000, 3.000000>|<1.000000, 2.000000, 3.000000, 4.000000>");
84+
checkTruth("llDumpList2String", llDumpList2String([], "|") == "");
85+
checkTruth("llDumpList2String", llDumpList2String([1], "|") == "1");
8486

8587
list nums = [1, 2, 3, 4, 5, 6];
8688
checkTruth("deletelist1", (string)llDeleteSubList(nums, 1, 2) == "1456");

0 commit comments

Comments
 (0)