aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-22 15:07:47 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-22 15:07:47 -0300
commit97e299c3958dfd865cda7cce06cf1ea825bbaffb (patch)
treefb79b7d5ea20c351a0aec7a224307e0b6f06bb19
parentc697aa30bccc80af3b005cfdbe62686edd730a14 (diff)
downloadlua-97e299c3958dfd865cda7cce06cf1ea825bbaffb.tar.gz
lua-97e299c3958dfd865cda7cce06cf1ea825bbaffb.tar.bz2
lua-97e299c3958dfd865cda7cce06cf1ea825bbaffb.zip
'lua_replace' implemented as a macro using 'lua_copy'
-rw-r--r--lapi.c31
-rw-r--r--lua.h5
2 files changed, 13 insertions, 23 deletions
diff --git a/lapi.c b/lapi.c
index fc17b918..680630dd 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.229 2014/07/19 15:09:37 roberto Exp roberto $ 2** $Id: lapi.c,v 2.230 2014/07/21 16:02:57 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*/
@@ -44,6 +44,9 @@ const char lua_ident[] =
44/* test for pseudo index */ 44/* test for pseudo index */
45#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) 45#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
46 46
47/* test for upvalue */
48#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
49
47/* test for valid but not pseudo index */ 50/* test for valid but not pseudo index */
48#define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) 51#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
49 52
@@ -214,31 +217,17 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) {
214} 217}
215 218
216 219
217static void moveto (lua_State *L, TValue *fr, int idx) { 220LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
218 TValue *to = index2addr(L, idx); 221 TValue *fr, *to;
222 lua_lock(L);
223 fr = index2addr(L, fromidx);
224 to = index2addr(L, toidx);
219 api_checkvalidindex(to); 225 api_checkvalidindex(to);
220 setobj(L, to, fr); 226 setobj(L, to, fr);
221 if (idx < LUA_REGISTRYINDEX) /* function upvalue? */ 227 if (isupvalue(toidx)) /* function upvalue? */
222 luaC_barrier(L, clCvalue(L->ci->func), fr); 228 luaC_barrier(L, clCvalue(L->ci->func), fr);
223 /* LUA_REGISTRYINDEX does not need gc barrier 229 /* LUA_REGISTRYINDEX does not need gc barrier
224 (collector revisits it before finishing collection) */ 230 (collector revisits it before finishing collection) */
225}
226
227
228LUA_API void lua_replace (lua_State *L, int idx) {
229 lua_lock(L);
230 api_checknelems(L, 1);
231 moveto(L, L->top - 1, idx);
232 L->top--;
233 lua_unlock(L);
234}
235
236
237LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
238 TValue *fr;
239 lua_lock(L);
240 fr = index2addr(L, fromidx);
241 moveto(L, fr, toidx);
242 lua_unlock(L); 231 lua_unlock(L);
243} 232}
244 233
diff --git a/lua.h b/lua.h
index f79da4bc..cdfc88ce 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.308 2014/06/26 17:25:11 roberto Exp roberto $ 2** $Id: lua.h,v 1.309 2014/07/17 13:53:37 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
@@ -158,7 +158,6 @@ LUA_API int (lua_gettop) (lua_State *L);
158LUA_API void (lua_settop) (lua_State *L, int idx); 158LUA_API void (lua_settop) (lua_State *L, int idx);
159LUA_API void (lua_pushvalue) (lua_State *L, int idx); 159LUA_API void (lua_pushvalue) (lua_State *L, int idx);
160LUA_API void (lua_rotate) (lua_State *L, int idx, int n); 160LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
161LUA_API void (lua_replace) (lua_State *L, int idx);
162LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx); 161LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
163LUA_API int (lua_checkstack) (lua_State *L, int sz); 162LUA_API int (lua_checkstack) (lua_State *L, int sz);
164 163
@@ -366,6 +365,8 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
366 365
367#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1)) 366#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
368 367
368#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1))
369
369/* }============================================================== */ 370/* }============================================================== */
370 371
371 372