MCPcopy Create free account
hub / github.com/DFHack/dfhack / tinsert

Function tinsert

depends/lua/src/ltablib.c:79–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

77
78
79static int tinsert (lua_State *L) {
80 lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
81 lua_Integer pos; /* where to insert new element */
82 switch (lua_gettop(L)) {
83 case 2: { /* called with only 2 arguments */
84 pos = e; /* insert new element at the end */
85 break;
86 }
87 case 3: {
88 lua_Integer i;
89 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
90 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
91 for (i = e; i > pos; i--) { /* move up elements */
92 lua_geti(L, 1, i - 1);
93 lua_seti(L, 1, i); /* t[i] = t[i - 1] */
94 }
95 break;
96 }
97 default: {
98 return luaL_error(L, "wrong number of arguments to 'insert'");
99 }
100 }
101 lua_seti(L, 1, pos); /* t[pos] = v */
102 return 0;
103}
104
105
106static int tremove (lua_State *L) {

Callers

nothing calls this directly

Calls 5

lua_gettopFunction · 0.85
luaL_checkintegerFunction · 0.85
lua_getiFunction · 0.85
lua_setiFunction · 0.85
luaL_errorFunction · 0.85

Tested by

no test coverage detected