diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-10-05 13:44:33 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-10-05 13:44:33 -0300 |
commit | a5382b763c2faa4c47e55ee0e49889b4c47daac4 (patch) | |
tree | d5f4ea49f2343593ced46ce303783baf0cb2cd56 /lapi.c | |
parent | ba21aa8b2b29f516f1f488f996fc899c62f7105b (diff) | |
download | lua-a5382b763c2faa4c47e55ee0e49889b4c47daac4.tar.gz lua-a5382b763c2faa4c47e55ee0e49889b4c47daac4.tar.bz2 lua-a5382b763c2faa4c47e55ee0e49889b4c47daac4.zip |
new function lua_copy
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 44 |
1 files changed, 29 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.91 2009/09/21 12:09:52 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.92 2009/09/28 16:32:50 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 | */ |
@@ -185,32 +185,46 @@ LUA_API void lua_insert (lua_State *L, int idx) { | |||
185 | } | 185 | } |
186 | 186 | ||
187 | 187 | ||
188 | LUA_API void lua_replace (lua_State *L, int idx) { | 188 | static void moveto (lua_State *L, TValue *fr, int idx) { |
189 | StkId o; | 189 | TValue *to = index2addr(L, idx); |
190 | lua_lock(L); | 190 | api_checkvalidindex(L, to); |
191 | /* explicit test for incompatible code */ | ||
192 | if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL) | ||
193 | luaG_runerror(L, "no calling environment"); | ||
194 | api_checknelems(L, 1); | ||
195 | o = index2addr(L, idx); | ||
196 | api_checkvalidindex(L, o); | ||
197 | if (idx == LUA_ENVIRONINDEX) { | 191 | if (idx == LUA_ENVIRONINDEX) { |
198 | Closure *func = curr_func(L); | 192 | Closure *func = curr_func(L); |
199 | api_check(L, ttistable(L->top - 1), "table expected"); | 193 | api_check(L, ttistable(fr), "table expected"); |
200 | func->c.env = hvalue(L->top - 1); | 194 | func->c.env = hvalue(fr); |
201 | luaC_barrier(L, func, L->top - 1); | 195 | luaC_barrier(L, func, fr); |
202 | } | 196 | } |
203 | else { | 197 | else { |
204 | setobj(L, o, L->top - 1); | 198 | setobj(L, to, fr); |
205 | if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ | 199 | if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ |
206 | luaC_barrier(L, curr_func(L), L->top - 1); | 200 | luaC_barrier(L, curr_func(L), fr); |
207 | } | 201 | } |
208 | /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */ | 202 | /* LUA_GLOBALSINDEX does not need gc barrier (threads are never black) */ |
203 | } | ||
204 | |||
205 | |||
206 | LUA_API void lua_replace (lua_State *L, int idx) { | ||
207 | lua_lock(L); | ||
208 | /* explicit test for incompatible code */ | ||
209 | if (idx == LUA_ENVIRONINDEX && L->ci->previous == NULL) | ||
210 | luaG_runerror(L, "no calling environment"); | ||
211 | api_checknelems(L, 1); | ||
212 | moveto(L, L->top - 1, idx); | ||
209 | L->top--; | 213 | L->top--; |
210 | lua_unlock(L); | 214 | lua_unlock(L); |
211 | } | 215 | } |
212 | 216 | ||
213 | 217 | ||
218 | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { | ||
219 | TValue *fr; | ||
220 | lua_lock(L); | ||
221 | fr = index2addr(L, fromidx); | ||
222 | api_checkvalidindex(L, fr); | ||
223 | moveto(L, fr, toidx); | ||
224 | lua_unlock(L); | ||
225 | } | ||
226 | |||
227 | |||
214 | LUA_API void lua_pushvalue (lua_State *L, int idx) { | 228 | LUA_API void lua_pushvalue (lua_State *L, int idx) { |
215 | lua_lock(L); | 229 | lua_lock(L); |
216 | setobj2s(L, L->top, index2addr(L, idx)); | 230 | setobj2s(L, L->top, index2addr(L, idx)); |