aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-11-05 15:26:00 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-11-05 15:26:00 -0200
commitb7d5f18d71f691df752e220f844ea613a8f6d722 (patch)
tree9f9ba3cd43799389c1564be16b7836e09fcf7317
parent5598b2bc558ba0c755068f21e786d3a6d59eb1ca (diff)
downloadlua-b7d5f18d71f691df752e220f844ea613a8f6d722.tar.gz
lua-b7d5f18d71f691df752e220f844ea613a8f6d722.tar.bz2
lua-b7d5f18d71f691df752e220f844ea613a8f6d722.zip
api functions to manipulate upvalues do not need to check their
arguments (the caller must check them before calling)
-rw-r--r--lapi.c31
-rw-r--r--ldblib.c24
-rw-r--r--lua.h4
3 files changed, 24 insertions, 35 deletions
diff --git a/lapi.c b/lapi.c
index 4d85508e..a79eb19f 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.94 2009/10/23 19:12:19 roberto Exp roberto $ 2** $Id: lapi.c,v 2.95 2009/11/05 16:48:31 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*/
@@ -1092,43 +1092,36 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
1092 Closure *f; 1092 Closure *f;
1093 Proto *p; 1093 Proto *p;
1094 StkId fi = index2addr(L, fidx); 1094 StkId fi = index2addr(L, fidx);
1095 if (!ttisfunction(fi)) return NULL; /* not a function? */ 1095 api_check(L, ttisfunction(fi), "function expected");
1096 f = clvalue(fi); 1096 f = clvalue(fi);
1097 if (f->c.isC) return NULL; /* not a Lua function? */ 1097 api_check(L, !f->c.isC, "Lua function expected");
1098 p = f->l.p; 1098 p = f->l.p;
1099 if (!(1 <= n && n <= p->sizeupvalues)) return NULL; 1099 api_check(L, (1 <= n && n <= p->sizeupvalues), "invalid upvalue index");
1100 else { 1100 if (pf) *pf = f;
1101 if (pf) *pf = f; 1101 return &f->l.upvals[n - 1]; /* get its upvalue pointer */
1102 return &f->l.upvals[n - 1]; /* get its upvalue pointer */
1103 }
1104} 1102}
1105 1103
1106 1104
1107LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n) { 1105LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n) {
1108 Closure *f; 1106 Closure *f;
1109 StkId fi = index2addr(L, fidx); 1107 StkId fi = index2addr(L, fidx);
1110 if (!ttisfunction(fi)) return NULL; 1108 api_check(L, ttisfunction(fi), "function expected");
1111 f = clvalue(fi); 1109 f = clvalue(fi);
1112 if (f->c.isC) { 1110 if (f->c.isC) {
1113 if (!(1 <= n && n <= f->c.nupvalues)) return NULL; 1111 api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
1114 else return &f->c.upvalue[n - 1]; 1112 return &f->c.upvalue[n - 1];
1115 }
1116 else {
1117 UpVal **uv = getupvalref(L, fidx, n, NULL);
1118 return (uv == NULL) ? NULL : *uv;
1119 } 1113 }
1114 else return *getupvalref(L, fidx, n, NULL);
1120} 1115}
1121 1116
1122 1117
1123LUA_API int (lua_upvaljoin) (lua_State *L, int fidx1, int n1, 1118LUA_API void (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
1124 int fidx2, int n2) { 1119 int fidx2, int n2) {
1125 Closure *f1; 1120 Closure *f1;
1126 UpVal **up1 = getupvalref(L, fidx1, n1, &f1); 1121 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1127 UpVal **up2 = getupvalref(L, fidx2, n2, NULL); 1122 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1128 if (up1 == NULL || up2 == NULL) return 0;
1129 *up1 = *up2; 1123 *up1 = *up2;
1130 luaC_objbarrier(L, f1, *up2); 1124 luaC_objbarrier(L, f1, *up2);
1131 return 1;
1132} 1125}
1133 1126
1134 1127
diff --git a/ldblib.c b/ldblib.c
index e067efac..5c1c57d0 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.112 2009/09/09 20:32:19 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.113 2009/11/05 16:48:31 roberto Exp roberto $
3** Interface from Lua to its debug API 3** Interface from Lua to its debug API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -199,23 +199,10 @@ static int db_setupvalue (lua_State *L) {
199} 199}
200 200
201 201
202static int db_upvaladdr (lua_State *L) {
203 void *addr;
204 int n = luaL_checkint(L, 2);
205 luaL_checktype(L, 1, LUA_TFUNCTION);
206 addr = lua_upvaladdr(L, 1, n);
207 if (addr == NULL) lua_pushnil(L);
208 else lua_pushlightuserdata(L, addr);
209 return 1;
210}
211
212
213static int checkupval (lua_State *L, int argf, int argnup) { 202static int checkupval (lua_State *L, int argf, int argnup) {
214 lua_Debug ar; 203 lua_Debug ar;
215 int nup = luaL_checkint(L, argnup); 204 int nup = luaL_checkint(L, argnup);
216 luaL_checktype(L, argf, LUA_TFUNCTION); 205 luaL_checktype(L, argf, LUA_TFUNCTION);
217 luaL_argcheck(L, !lua_iscfunction(L, argf), argf,
218 "cannot join upvalues of a C function");
219 lua_pushvalue(L, argf); 206 lua_pushvalue(L, argf);
220 lua_getinfo(L, ">u", &ar); 207 lua_getinfo(L, ">u", &ar);
221 luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index"); 208 luaL_argcheck(L, 1 <= nup && nup <= ar.nups, argnup, "invalid upvalue index");
@@ -223,9 +210,18 @@ static int checkupval (lua_State *L, int argf, int argnup) {
223} 210}
224 211
225 212
213static int db_upvaladdr (lua_State *L) {
214 int n = checkupval(L, 1, 2);
215 lua_pushlightuserdata(L, lua_upvaladdr(L, 1, n));
216 return 1;
217}
218
219
226static int db_joinupval (lua_State *L) { 220static int db_joinupval (lua_State *L) {
227 int n1 = checkupval(L, 1, 2); 221 int n1 = checkupval(L, 1, 2);
228 int n2 = checkupval(L, 3, 4); 222 int n2 = checkupval(L, 3, 4);
223 luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
224 luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
229 lua_upvaljoin(L, 1, n1, 3, n2); 225 lua_upvaljoin(L, 1, n1, 3, n2);
230 return 0; 226 return 0;
231} 227}
diff --git a/lua.h b/lua.h
index 2357bdaa..57127bca 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.246 2009/10/11 20:02:19 roberto Exp roberto $ 2** $Id: lua.h,v 1.247 2009/11/05 16:48:31 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
@@ -381,7 +381,7 @@ LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n);
381LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n); 381LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n);
382 382
383LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n); 383LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n);
384LUA_API int (lua_upvaljoin) (lua_State *L, int fidx1, int n1, 384LUA_API void (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
385 int fidx2, int n2); 385 int fidx2, int n2);
386 386
387LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count); 387LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count);