MCPcopy Create free account
hub / github.com/PlutoLang/Pluto / treverse

Function treverse

src/ltablib.cpp:631–657  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

629
630template <bool make_copy>
631static int treverse (lua_State *L) {
632 luaL_checktype(L, 1, LUA_TTABLE);
633
634 if (make_copy) {
635 lua_settop(L, 1);
636 lua_newtable(L);
637 }
638 const lua_Unsigned l = lua_rawlen(L, 1);
639 for (lua_Unsigned i = 1; i <= l/2; ++i) {
640 lua_pushinteger(L, l - i + 1);
641 lua_pushinteger(L, i);
642 lua_rawget(L, 1);
643 lua_pushinteger(L, i);
644 lua_pushinteger(L, l - i + 1);
645 lua_rawget(L, 1);
646 lua_rawset(L, make_copy ? 2 : 1);
647 lua_rawset(L, make_copy ? 2 : 1);
648 }
649 if (make_copy && (l % 2 != 0)) {
650 /* for oddly sized tables, we need to manually copy the element in the middle */
651 lua_pushinteger(L, l/2+1);
652 lua_pushinteger(L, l/2+1);
653 lua_rawget(L, 1);
654 lua_rawset(L, 2);
655 }
656 return 1;
657}
658
659
660TValue *index2value (lua_State *L, int idx);

Callers

nothing calls this directly

Calls 5

luaL_checktypeFunction · 0.85
lua_settopFunction · 0.85
lua_pushintegerFunction · 0.85
lua_rawgetFunction · 0.85
lua_rawsetFunction · 0.85

Tested by

no test coverage detected