MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / tinsert

Function tinsert

extlibs/lua/src/ltablib.c:61–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59
60
61static int tinsert (lua_State *L) {
62 lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
63 lua_Integer pos; /* where to insert new element */
64 switch (lua_gettop(L)) {
65 case 2: { /* called with only 2 arguments */
66 pos = e; /* insert new element at the end */
67 break;
68 }
69 case 3: {
70 lua_Integer i;
71 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
72 /* check whether 'pos' is in [1, e] */
73 luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
74 "position out of bounds");
75 for (i = e; i > pos; i--) { /* move up elements */
76 lua_geti(L, 1, i - 1);
77 lua_seti(L, 1, i); /* t[i] = t[i - 1] */
78 }
79 break;
80 }
81 default: {
82 return luaL_error(L, "wrong number of arguments to 'insert'");
83 }
84 }
85 lua_seti(L, 1, pos); /* t[pos] = v */
86 return 0;
87}
88
89
90static int tremove (lua_State *L) {

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected