aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-13 16:40:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-05-13 16:40:28 -0300
commit5a5a834975d135054e803b90711fcb9deb6511b8 (patch)
tree9b028d3e480b677192a6da06271f38599cb33569 /lapi.c
parent45c430eac04b070a996437b79e40656942a4c7d4 (diff)
downloadlua-5a5a834975d135054e803b90711fcb9deb6511b8.tar.gz
lua-5a5a834975d135054e803b90711fcb9deb6511b8.tar.bz2
lua-5a5a834975d135054e803b90711fcb9deb6511b8.zip
new API function 'lua_rotate'
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/lapi.c b/lapi.c
index 843e5606..0fdb6bed 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.208 2014/05/01 18:18:06 roberto Exp roberto $ 2** $Id: lapi.c,v 2.209 2014/05/01 18:21:32 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -181,26 +181,31 @@ LUA_API void lua_settop (lua_State *L, int idx) {
181} 181}
182 182
183 183
184LUA_API void lua_remove (lua_State *L, int idx) { 184/*
185 StkId p; 185** Reverse the stack segment from 'from' to 'to'
186 lua_lock(L); 186** (auxiliar to 'lua_rotate')
187 p = index2addr(L, idx); 187*/
188 api_checkstackindex(L, idx, p); 188static void reverse (lua_State *L, StkId from, StkId to) {
189 while (++p < L->top) setobjs2s(L, p-1, p); 189 for (; from < to; from++, to--) {
190 L->top--; 190 TValue temp;
191 lua_unlock(L); 191 setobj(L, &temp, from);
192 setobj(L, from, to);
193 setobj(L, to, &temp);
194 }
192} 195}
193 196
194 197
195LUA_API void lua_insert (lua_State *L, int idx) { 198LUA_API void lua_rotate (lua_State *L, int idx, int n) {
196 StkId p; 199 StkId p, t, m;
197 StkId q;
198 lua_lock(L); 200 lua_lock(L);
201 t = L->top - 1;
199 p = index2addr(L, idx); 202 p = index2addr(L, idx);
203 m = (n >= 0 ? t - n : p - n - 1);
200 api_checkstackindex(L, idx, p); 204 api_checkstackindex(L, idx, p);
201 for (q = L->top; q > p; q--) /* use L->top as a temporary */ 205 api_check(L, p <= m + 1 && m <= t, "invalid 'n'");
202 setobjs2s(L, q, q - 1); 206 reverse(L, p, m);
203 setobjs2s(L, p, L->top); 207 reverse(L, m + 1, t);
208 reverse(L, p, t);
204 lua_unlock(L); 209 lua_unlock(L);
205} 210}
206 211