MCPcopy Create free account
hub / github.com/F-Stack/f-stack / luaV_concat

Function luaV_concat

freebsd/contrib/openzfs/module/lua/lvm.c:294–333  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

292
293
294void luaV_concat (lua_State *L, int total) {
295 lua_assert(total >= 2);
296 do {
297 StkId top = L->top;
298 int n = 2; /* number of elements handled in this pass (at least 2) */
299 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
300 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
301 luaG_concaterror(L, top-2, top-1);
302 }
303 else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
304 (void)tostring(L, top - 2); /* result is first operand */
305 else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
306 setobjs2s(L, top - 2, top - 1); /* result is second op. */
307 }
308 else {
309 /* at least two non-empty string values; get as many as possible */
310 size_t tl = tsvalue(top-1)->len;
311 char *buffer;
312 int i;
313 /* collect total length */
314 for (i = 1; i < total && tostring(L, top-i-1); i++) {
315 size_t l = tsvalue(top-i-1)->len;
316 if (l >= (MAX_SIZET/sizeof(char)) - tl)
317 luaG_runerror(L, "string length overflow");
318 tl += l;
319 }
320 buffer = luaZ_openspace(L, &G(L)->buff, tl);
321 tl = 0;
322 n = i;
323 do { /* concat all strings */
324 size_t l = tsvalue(top-i)->len;
325 memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
326 tl += l;
327 } while (--i > 0);
328 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
329 }
330 total -= n-1; /* got 'n' strings to create 1 new */
331 L->top -= n-1; /* popped 'n' strings and pushed one */
332 } while (total > 1); /* repeat until only 1 result left */
333}
334
335
336void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {

Callers 4

luaV_finishOpFunction · 0.70
luaV_executeFunction · 0.70
luaO_pushvfstringFunction · 0.70
lua_concatFunction · 0.70

Calls 6

call_binTMFunction · 0.70
luaG_concaterrorFunction · 0.70
luaG_runerrorFunction · 0.70
luaZ_openspaceFunction · 0.70
luaS_newlstrFunction · 0.70
memcpyFunction · 0.50

Tested by

no test coverage detected