aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c44
-rw-r--r--lauxlib.c6
-rw-r--r--lbaselib.c5
-rw-r--r--loadlib.c5
-rw-r--r--lstate.c5
-rw-r--r--ltests.c6
-rw-r--r--lua.h3
7 files changed, 45 insertions, 29 deletions
diff --git a/lapi.c b/lapi.c
index 08a42516..8426dea0 100644
--- a/lapi.c
+++ b/lapi.c
@@ -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
188LUA_API void lua_replace (lua_State *L, int idx) { 188static 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
206LUA_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
218LUA_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
214LUA_API void lua_pushvalue (lua_State *L, int idx) { 228LUA_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));
diff --git a/lauxlib.c b/lauxlib.c
index ed82e8cd..2d33adf6 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.191 2009/09/18 18:58:45 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.192 2009/09/28 12:36:40 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -75,8 +75,8 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
75 lua_getinfo(L, "f", ar); /* push function */ 75 lua_getinfo(L, "f", ar); /* push function */
76 lua_pushvalue(L, LUA_GLOBALSINDEX); /* push global table */ 76 lua_pushvalue(L, LUA_GLOBALSINDEX); /* push global table */
77 if (findfield(L, top + 1, 2)) { 77 if (findfield(L, top + 1, 2)) {
78 lua_replace(L, top + 1); /* move name to proper place */ 78 lua_copy(L, -1, top + 1); /* move name to proper place */
79 lua_pop(L, 1); /* remove other pushed value */ 79 lua_pop(L, 2); /* remove pushed values */
80 return 1; 80 return 1;
81 } 81 }
82 else { 82 else {
diff --git a/lbaselib.c b/lbaselib.c
index edd72dcd..aa6fe1dc 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.217 2009/07/15 17:35:20 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.218 2009/08/04 18:20:18 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -432,8 +432,7 @@ static int luaB_xpcall (lua_State *L) {
432 int n = lua_gettop(L); 432 int n = lua_gettop(L);
433 luaL_argcheck(L, n >= 2, 2, "value expected"); 433 luaL_argcheck(L, n >= 2, 2, "value expected");
434 lua_pushvalue(L, 1); /* exchange function... */ 434 lua_pushvalue(L, 1); /* exchange function... */
435 lua_pushvalue(L, 2); /* ...and error handler */ 435 lua_copy(L, 2, 1); /* ...and error handler */
436 lua_replace(L, 1);
437 lua_replace(L, 2); 436 lua_replace(L, 2);
438 status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont); 437 status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont);
439 luaL_checkstack(L, 1, NULL); 438 luaL_checkstack(L, 1, NULL);
diff --git a/loadlib.c b/loadlib.c
index d21c654f..3e807c2c 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.64 2009/07/15 17:49:48 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.65 2009/09/07 14:24:12 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -655,8 +655,7 @@ LUALIB_API int luaopen_package (lua_State *L) {
655 lua_setfield(L, -2, "__gc"); 655 lua_setfield(L, -2, "__gc");
656 /* create `package' table */ 656 /* create `package' table */
657 luaL_register(L, LUA_LOADLIBNAME, pk_funcs); 657 luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
658 lua_pushvalue(L, -1); 658 lua_copy(L, -1, LUA_ENVIRONINDEX);
659 lua_replace(L, LUA_ENVIRONINDEX);
660 /* create `loaders' table */ 659 /* create `loaders' table */
661 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); 660 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
662 /* fill it with pre-defined loaders */ 661 /* fill it with pre-defined loaders */
diff --git a/lstate.c b/lstate.c
index bc56079f..b571c828 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.60 2009/09/28 13:50:19 roberto Exp roberto $ 2** $Id: lstate.c,v 2.61 2009/09/30 20:49:47 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -100,8 +100,7 @@ static int cpcall (lua_State *L) {
100 lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1); 100 lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1);
101 lua_remove(L, 1); /* remove f from stack */ 101 lua_remove(L, 1); /* remove f from stack */
102 /* restore original environment for 'cpcall' */ 102 /* restore original environment for 'cpcall' */
103 lua_pushvalue(L, LUA_GLOBALSINDEX); 103 lua_copy(L, LUA_GLOBALSINDEX, LUA_ENVIRONINDEX);
104 lua_replace(L, LUA_ENVIRONINDEX);
105 return f(L); 104 return f(L);
106} 105}
107 106
diff --git a/ltests.c b/ltests.c
index 46730667..40a7f965 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.73 2009/09/28 16:32:50 roberto Exp roberto $ 2** $Id: ltests.c,v 2.74 2009/09/30 20:49:25 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*/
@@ -971,6 +971,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
971 else if EQ("replace") { 971 else if EQ("replace") {
972 lua_replace(L1, getindex); 972 lua_replace(L1, getindex);
973 } 973 }
974 else if EQ("copy") {
975 int f = getindex;
976 lua_copy(L1, f, getindex);
977 }
974 else if EQ("gettable") { 978 else if EQ("gettable") {
975 lua_gettable(L1, getindex); 979 lua_gettable(L1, getindex);
976 } 980 }
diff --git a/lua.h b/lua.h
index e440e30c..478374e3 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.243 2009/09/17 18:04:21 roberto Exp roberto $ 2** $Id: lua.h,v 1.244 2009/09/21 12:09:52 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension 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
@@ -135,6 +135,7 @@ LUA_API void (lua_pushvalue) (lua_State *L, int idx);
135LUA_API void (lua_remove) (lua_State *L, int idx); 135LUA_API void (lua_remove) (lua_State *L, int idx);
136LUA_API void (lua_insert) (lua_State *L, int idx); 136LUA_API void (lua_insert) (lua_State *L, int idx);
137LUA_API void (lua_replace) (lua_State *L, int idx); 137LUA_API void (lua_replace) (lua_State *L, int idx);
138LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
138LUA_API int (lua_checkstack) (lua_State *L, int sz); 139LUA_API int (lua_checkstack) (lua_State *L, int sz);
139 140
140LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); 141LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);