Przeglądaj źródła

make treatment of 'pcall' and 'xpcall' more similar

Roberto Ierusalimschy 13 lat temu
rodzic
commit
7133e20c94
1 zmienionych plików z 11 dodań i 14 usunięć
  1. 11 14
      lbaselib.c

+ 11 - 14
lbaselib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbaselib.c,v 1.266 2011/09/30 12:43:54 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.267 2011/11/09 19:28:27 roberto Exp roberto $
 ** Basic library
 ** See Copyright Notice in lua.h
 */
@@ -391,7 +391,7 @@ static int luaB_select (lua_State *L) {
 }
 
 
-static int finishpcall (lua_State *L, int status, int isx) {
+static int finishpcall (lua_State *L, int status) {
   if (!lua_checkstack(L, 1)) {  /* no space for extra boolean? */
     lua_settop(L, 0);  /* create space for return values */
     lua_pushboolean(L, 0);
@@ -399,27 +399,24 @@ static int finishpcall (lua_State *L, int status, int isx) {
     return 2;  /* return false, msg */
   }
   lua_pushboolean(L, status);  /* first result (status) */
-  if (isx)  /* came from xpcall? */
-    lua_replace(L, 1);  /* put first result in place of error function */
-  else  /* came from pcall */
-    lua_insert(L, 1);  /* insert first result before the others */
+  lua_replace(L, 1);  /* put first result in first slot */
   return lua_gettop(L);
 }
 
 
 static int pcallcont (lua_State *L) {
-  int isx = 0;  /* =0 to avoid warnings */
-  int status = lua_getctx(L, &isx);
-  lua_assert(status != LUA_OK);
-  return finishpcall(L, (status == LUA_YIELD), isx);
+  int status = lua_getctx(L, NULL);
+  return finishpcall(L, (status == LUA_YIELD));
 }
 
 
 static int luaB_pcall (lua_State *L) {
   int status;
   luaL_checkany(L, 1);
-  status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, pcallcont);
-  return finishpcall(L, (status == LUA_OK), 0);
+  lua_pushnil(L);
+  lua_insert(L, 1);  /* create space for status result */
+  status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont);
+  return finishpcall(L, (status == LUA_OK));
 }
 
 
@@ -430,8 +427,8 @@ static int luaB_xpcall (lua_State *L) {
   lua_pushvalue(L, 1);  /* exchange function... */
   lua_copy(L, 2, 1);  /* ...and error handler */
   lua_replace(L, 2);
-  status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont);
-  return finishpcall(L, (status == LUA_OK), 1);
+  status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont);
+  return finishpcall(L, (status == LUA_OK));
 }