diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-13 16:40:28 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-13 16:40:28 -0300 |
| commit | 5a5a834975d135054e803b90711fcb9deb6511b8 (patch) | |
| tree | 9b028d3e480b677192a6da06271f38599cb33569 | |
| parent | 45c430eac04b070a996437b79e40656942a4c7d4 (diff) | |
| download | lua-5a5a834975d135054e803b90711fcb9deb6511b8.tar.gz lua-5a5a834975d135054e803b90711fcb9deb6511b8.tar.bz2 lua-5a5a834975d135054e803b90711fcb9deb6511b8.zip | |
new API function 'lua_rotate'
| -rw-r--r-- | lapi.c | 35 | ||||
| -rw-r--r-- | liolib.c | 6 | ||||
| -rw-r--r-- | ltests.c | 6 | ||||
| -rw-r--r-- | lua.h | 15 |
4 files changed, 36 insertions, 26 deletions
| @@ -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 | ||
| 184 | LUA_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); | 188 | static 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 | ||
| 195 | LUA_API void lua_insert (lua_State *L, int idx) { | 198 | LUA_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 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.121 2014/04/15 16:46:45 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.122 2014/05/11 14:46:19 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -319,14 +319,12 @@ static int io_readline (lua_State *L); | |||
| 319 | 319 | ||
| 320 | 320 | ||
| 321 | static void aux_lines (lua_State *L, int toclose) { | 321 | static void aux_lines (lua_State *L, int toclose) { |
| 322 | int i; | ||
| 323 | int n = lua_gettop(L) - 1; /* number of arguments to read */ | 322 | int n = lua_gettop(L) - 1; /* number of arguments to read */ |
| 324 | /* ensure that arguments will fit here and into 'io_readline' stack */ | 323 | /* ensure that arguments will fit here and into 'io_readline' stack */ |
| 325 | luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); | 324 | luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); |
| 326 | lua_pushvalue(L, 1); /* file handle */ | ||
| 327 | lua_pushinteger(L, n); /* number of arguments to read */ | 325 | lua_pushinteger(L, n); /* number of arguments to read */ |
| 328 | lua_pushboolean(L, toclose); /* close/not close file when finished */ | 326 | lua_pushboolean(L, toclose); /* close/not close file when finished */ |
| 329 | for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */ | 327 | lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ |
| 330 | lua_pushcclosure(L, io_readline, 3 + n); | 328 | lua_pushcclosure(L, io_readline, 3 + n); |
| 331 | } | 329 | } |
| 332 | 330 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 2.168 2014/04/14 18:42:44 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 2.169 2014/05/08 19:08:46 roberto Exp roberto $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -1215,6 +1215,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
| 1215 | } | 1215 | } |
| 1216 | return n; | 1216 | return n; |
| 1217 | } | 1217 | } |
| 1218 | else if EQ("rotate") { | ||
| 1219 | int i = getindex; | ||
| 1220 | lua_rotate(L1, i, getnum); | ||
| 1221 | } | ||
| 1218 | else if EQ("setfield") { | 1222 | else if EQ("setfield") { |
| 1219 | int t = getindex; | 1223 | int t = getindex; |
| 1220 | lua_setfield(L1, t, getstring); | 1224 | lua_setfield(L1, t, getstring); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.h,v 1.304 2014/05/01 18:21:32 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.305 2014/05/08 13:52:20 roberto Exp roberto $ |
| 3 | ** Lua - A Scripting Language | 3 | ** Lua - A Scripting Language |
| 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
| 5 | ** See Copyright Notice at the end of this file | 5 | ** See Copyright Notice at the end of this file |
| @@ -145,8 +145,7 @@ LUA_API int (lua_absindex) (lua_State *L, int idx); | |||
| 145 | LUA_API int (lua_gettop) (lua_State *L); | 145 | LUA_API int (lua_gettop) (lua_State *L); |
| 146 | LUA_API void (lua_settop) (lua_State *L, int idx); | 146 | LUA_API void (lua_settop) (lua_State *L, int idx); |
| 147 | LUA_API void (lua_pushvalue) (lua_State *L, int idx); | 147 | LUA_API void (lua_pushvalue) (lua_State *L, int idx); |
| 148 | LUA_API void (lua_remove) (lua_State *L, int idx); | 148 | LUA_API void (lua_rotate) (lua_State *L, int idx, int n); |
| 149 | LUA_API void (lua_insert) (lua_State *L, int idx); | ||
| 150 | LUA_API void (lua_replace) (lua_State *L, int idx); | 149 | LUA_API void (lua_replace) (lua_State *L, int idx); |
| 151 | LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); | 150 | LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); |
| 152 | LUA_API int (lua_checkstack) (lua_State *L, int sz); | 151 | LUA_API int (lua_checkstack) (lua_State *L, int sz); |
| @@ -326,9 +325,9 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); | |||
| 326 | ** =============================================================== | 325 | ** =============================================================== |
| 327 | */ | 326 | */ |
| 328 | 327 | ||
| 329 | #define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) | 328 | #define lua_tonumber(L,i) lua_tonumberx(L,(i),NULL) |
| 330 | #define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) | 329 | #define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL) |
| 331 | #define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL) | 330 | #define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) |
| 332 | 331 | ||
| 333 | #define lua_pop(L,n) lua_settop(L, -(n)-1) | 332 | #define lua_pop(L,n) lua_settop(L, -(n)-1) |
| 334 | 333 | ||
| @@ -356,6 +355,10 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); | |||
| 356 | #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) | 355 | #define lua_tostring(L,i) lua_tolstring(L, (i), NULL) |
| 357 | 356 | ||
| 358 | 357 | ||
| 358 | #define lua_insert(L,idx) lua_rotate(L, (idx), 1) | ||
| 359 | |||
| 360 | #define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) | ||
| 361 | |||
| 359 | 362 | ||
| 360 | /* | 363 | /* |
| 361 | ** {====================================================================== | 364 | ** {====================================================================== |
