| 59 | |
| 60 | |
| 61 | static 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 | |
| 90 | static int tremove (lua_State *L) { |
nothing calls this directly
no test coverage detected