MCPcopy Create free account
hub / github.com/Overload-Technologies/Overload / luaV_concat

Function luaV_concat

Dependencies/lua/src/lvm.c:643–684  ·  view source on GitHub ↗

** Main operation for concatenation: concat 'total' values in the stack, ** from 'L->top.p - total' up to 'L->top.p - 1'. */

Source from the content-addressed store, hash-verified

641** from 'L->top.p - total' up to 'L->top.p - 1'.
642*/
643void luaV_concat (lua_State *L, int total) {
644 if (total == 1)
645 return; /* "all" values already concatenated */
646 do {
647 StkId top = L->top.p;
648 int n = 2; /* number of elements handled in this pass (at least 2) */
649 if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
650 !tostring(L, s2v(top - 1)))
651 luaT_tryconcatTM(L); /* may invalidate 'top' */
652 else if (isemptystr(s2v(top - 1))) /* second operand is empty? */
653 cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
654 else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
655 setobjs2s(L, top - 2, top - 1); /* result is second op. */
656 }
657 else {
658 /* at least two non-empty string values; get as many as possible */
659 size_t tl = tsslen(tsvalue(s2v(top - 1)));
660 TString *ts;
661 /* collect total length and number of strings */
662 for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
663 size_t l = tsslen(tsvalue(s2v(top - n - 1)));
664 if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) {
665 L->top.p = top - total; /* pop strings to avoid wasting stack */
666 luaG_runerror(L, "string length overflow");
667 }
668 tl += l;
669 }
670 if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
671 char buff[LUAI_MAXSHORTLEN];
672 copy2buff(top, n, buff); /* copy strings to buffer */
673 ts = luaS_newlstr(L, buff, tl);
674 }
675 else { /* long string; copy strings directly to final result */
676 ts = luaS_createlngstrobj(L, tl);
677 copy2buff(top, n, getlngstr(ts));
678 }
679 setsvalue2s(L, top - n, ts); /* create result */
680 }
681 total -= n - 1; /* got 'n' strings to create one new */
682 L->top.p -= n - 1; /* popped 'n' strings and pushed one */
683 } while (total > 1); /* repeat until only 1 result left */
684}
685
686
687/*

Callers 4

luaV_finishOpFunction · 0.85
luaV_executeFunction · 0.85
pushstrFunction · 0.85
lua_concatFunction · 0.85

Calls 5

luaT_tryconcatTMFunction · 0.85
luaG_runerrorFunction · 0.85
copy2buffFunction · 0.85
luaS_newlstrFunction · 0.85
luaS_createlngstrobjFunction · 0.85

Tested by

no test coverage detected