Skip to content

Commit 97e299c

Browse files
committed
'lua_replace' implemented as a macro using 'lua_copy'
1 parent c697aa3 commit 97e299c

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

lapi.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
** Lua API
44
** See Copyright Notice in lua.h
55
*/
@@ -44,6 +44,9 @@ const char lua_ident[] =
4444
/* test for pseudo index */
4545
#define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
4646

47+
/* test for upvalue */
48+
#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
49+
4750
/* test for valid but not pseudo index */
4851
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
4952

@@ -214,31 +217,17 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) {
214217
}
215218

216219

217-
static void moveto (lua_State *L, TValue *fr, int idx) {
218-
TValue *to = index2addr(L, idx);
220+
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
221+
TValue *fr, *to;
222+
lua_lock(L);
223+
fr = index2addr(L, fromidx);
224+
to = index2addr(L, toidx);
219225
api_checkvalidindex(to);
220226
setobj(L, to, fr);
221-
if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
227+
if (isupvalue(toidx)) /* function upvalue? */
222228
luaC_barrier(L, clCvalue(L->ci->func), fr);
223229
/* LUA_REGISTRYINDEX does not need gc barrier
224230
(collector revisits it before finishing collection) */
225-
}
226-
227-
228-
LUA_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-
237-
LUA_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);
242231
lua_unlock(L);
243232
}
244233

lua.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
** Lua - A Scripting Language
44
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
55
** See Copyright Notice at the end of this file
@@ -158,7 +158,6 @@ LUA_API int (lua_gettop) (lua_State *L);
158158
LUA_API void (lua_settop) (lua_State *L, int idx);
159159
LUA_API void (lua_pushvalue) (lua_State *L, int idx);
160160
LUA_API void (lua_rotate) (lua_State *L, int idx, int n);
161-
LUA_API void (lua_replace) (lua_State *L, int idx);
162161
LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
163162
LUA_API int (lua_checkstack) (lua_State *L, int sz);
164163

@@ -366,6 +365,8 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
366365

367366
#define lua_remove(L,idx) (lua_rotate(L, (idx), -1), lua_pop(L, 1))
368367

368+
#define lua_replace(L,idx) (lua_copy(L, -1, (idx)), lua_pop(L, 1))
369+
369370
/* }============================================================== */
370371

371372

0 commit comments

Comments
 (0)