Browse Source

api functions to manipulate upvalues do not need to check their
arguments (the caller must check them before calling)

Roberto Ierusalimschy 15 years ago
parent
commit
b7d5f18d71
3 changed files with 24 additions and 35 deletions
  1. 12 19
      lapi.c
  2. 10 14
      ldblib.c
  3. 2 2
      lua.h

+ 12 - 19
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.94 2009/10/23 19:12:19 roberto Exp roberto $
+** $Id: lapi.c,v 2.95 2009/11/05 16:48:31 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -1092,43 +1092,36 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
   Closure *f;
   Proto *p;
   StkId fi = index2addr(L, fidx);
-  if (!ttisfunction(fi)) return NULL;  /* not a function? */
+  api_check(L, ttisfunction(fi), "function expected");
   f = clvalue(fi);
-  if (f->c.isC) return NULL;  /* not a Lua function? */
+  api_check(L, !f->c.isC, "Lua function expected");
   p = f->l.p;
-  if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
-  else {
-    if (pf) *pf = f;
-    return &f->l.upvals[n - 1];  /* get its upvalue pointer */
-  }
+  api_check(L, (1 <= n && n <= p->sizeupvalues), "invalid upvalue index");
+  if (pf) *pf = f;
+  return &f->l.upvals[n - 1];  /* get its upvalue pointer */
 }
 
 
 LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n) {
   Closure *f;
   StkId fi = index2addr(L, fidx);
-  if (!ttisfunction(fi)) return NULL;
+  api_check(L, ttisfunction(fi), "function expected");
   f = clvalue(fi);
   if (f->c.isC) {
-    if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
-    else return &f->c.upvalue[n - 1];
-  }
-  else {
-    UpVal **uv = getupvalref(L, fidx, n, NULL);
-    return (uv == NULL) ? NULL : *uv;
+    api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
+    return &f->c.upvalue[n - 1];
   }
+  else return *getupvalref(L, fidx, n, NULL);
 }
 
 
-LUA_API int (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
-                                           int fidx2, int n2) {
+LUA_API void (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
+                                            int fidx2, int n2) {
   Closure *f1;
   UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
   UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
-  if (up1 == NULL || up2 == NULL) return 0;
   *up1 = *up2;
   luaC_objbarrier(L, f1, *up2);
-  return 1;
 }
 
 

+ 10 - 14
ldblib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldblib.c,v 1.112 2009/09/09 20:32:19 roberto Exp roberto $
+** $Id: ldblib.c,v 1.113 2009/11/05 16:48:31 roberto Exp roberto $
 ** Interface from Lua to its debug API
 ** See Copyright Notice in lua.h
 */
@@ -199,23 +199,10 @@ static int db_setupvalue (lua_State *L) {
 }
 
 
-static int db_upvaladdr (lua_State *L) {
-  void *addr;
-  int n = luaL_checkint(L, 2);
-  luaL_checktype(L, 1, LUA_TFUNCTION);
-  addr = lua_upvaladdr(L, 1, n);
-  if (addr == NULL) lua_pushnil(L);
-  else lua_pushlightuserdata(L, addr);
-  return 1;
-}
-
-
 static int checkupval (lua_State *L, int argf, int argnup) {
   lua_Debug ar;
   int nup = luaL_checkint(L, argnup);
   luaL_checktype(L, argf, LUA_TFUNCTION);
-  luaL_argcheck(L, !lua_iscfunction(L, argf), argf,
-                   "cannot join upvalues of a C function");
   lua_pushvalue(L, argf);
   lua_getinfo(L, ">u", &ar);
   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) {
 }
 
 
+static int db_upvaladdr (lua_State *L) {
+  int n = checkupval(L, 1, 2);
+  lua_pushlightuserdata(L, lua_upvaladdr(L, 1, n));
+  return 1;
+}
+
+
 static int db_joinupval (lua_State *L) {
   int n1 = checkupval(L, 1, 2);
   int n2 = checkupval(L, 3, 4);
+  luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
+  luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
   lua_upvaljoin(L, 1, n1, 3, n2);
   return 0;
 }

+ 2 - 2
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.246 2009/10/11 20:02:19 roberto Exp roberto $
+** $Id: lua.h,v 1.247 2009/11/05 16:48:31 roberto Exp roberto $
 ** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** 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);
 LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n);
 
 LUA_API void *(lua_upvaladdr) (lua_State *L, int fidx, int n);
-LUA_API int (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
+LUA_API void  (lua_upvaljoin) (lua_State *L, int fidx1, int n1,
                                            int fidx2, int n2);
 
 LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count);