diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-11-09 17:38:00 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-11-09 17:38:00 -0200 |
commit | 7133e20c94171c79481219e6a0268c694f9734c3 (patch) | |
tree | 96a859378bc1b079adbd871069aeb5cb8a5bc888 /lbaselib.c | |
parent | 888d39ea752dc7b28007954049de082ba3aa6cbc (diff) | |
download | lua-7133e20c94171c79481219e6a0268c694f9734c3.tar.gz lua-7133e20c94171c79481219e6a0268c694f9734c3.tar.bz2 lua-7133e20c94171c79481219e6a0268c694f9734c3.zip |
make treatment of 'pcall' and 'xpcall' more similar
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 25 |
1 files changed, 11 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.266 2011/09/30 12:43:54 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.267 2011/11/09 19:28:27 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 | */ |
@@ -391,7 +391,7 @@ static int luaB_select (lua_State *L) { | |||
391 | } | 391 | } |
392 | 392 | ||
393 | 393 | ||
394 | static int finishpcall (lua_State *L, int status, int isx) { | 394 | static int finishpcall (lua_State *L, int status) { |
395 | if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ | 395 | if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ |
396 | lua_settop(L, 0); /* create space for return values */ | 396 | lua_settop(L, 0); /* create space for return values */ |
397 | lua_pushboolean(L, 0); | 397 | lua_pushboolean(L, 0); |
@@ -399,27 +399,24 @@ static int finishpcall (lua_State *L, int status, int isx) { | |||
399 | return 2; /* return false, msg */ | 399 | return 2; /* return false, msg */ |
400 | } | 400 | } |
401 | lua_pushboolean(L, status); /* first result (status) */ | 401 | lua_pushboolean(L, status); /* first result (status) */ |
402 | if (isx) /* came from xpcall? */ | 402 | lua_replace(L, 1); /* put first result in first slot */ |
403 | lua_replace(L, 1); /* put first result in place of error function */ | ||
404 | else /* came from pcall */ | ||
405 | lua_insert(L, 1); /* insert first result before the others */ | ||
406 | return lua_gettop(L); | 403 | return lua_gettop(L); |
407 | } | 404 | } |
408 | 405 | ||
409 | 406 | ||
410 | static int pcallcont (lua_State *L) { | 407 | static int pcallcont (lua_State *L) { |
411 | int isx = 0; /* =0 to avoid warnings */ | 408 | int status = lua_getctx(L, NULL); |
412 | int status = lua_getctx(L, &isx); | 409 | return finishpcall(L, (status == LUA_YIELD)); |
413 | lua_assert(status != LUA_OK); | ||
414 | return finishpcall(L, (status == LUA_YIELD), isx); | ||
415 | } | 410 | } |
416 | 411 | ||
417 | 412 | ||
418 | static int luaB_pcall (lua_State *L) { | 413 | static int luaB_pcall (lua_State *L) { |
419 | int status; | 414 | int status; |
420 | luaL_checkany(L, 1); | 415 | luaL_checkany(L, 1); |
421 | status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, pcallcont); | 416 | lua_pushnil(L); |
422 | return finishpcall(L, (status == LUA_OK), 0); | 417 | lua_insert(L, 1); /* create space for status result */ |
418 | status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, pcallcont); | ||
419 | return finishpcall(L, (status == LUA_OK)); | ||
423 | } | 420 | } |
424 | 421 | ||
425 | 422 | ||
@@ -430,8 +427,8 @@ static int luaB_xpcall (lua_State *L) { | |||
430 | lua_pushvalue(L, 1); /* exchange function... */ | 427 | lua_pushvalue(L, 1); /* exchange function... */ |
431 | lua_copy(L, 2, 1); /* ...and error handler */ | 428 | lua_copy(L, 2, 1); /* ...and error handler */ |
432 | lua_replace(L, 2); | 429 | lua_replace(L, 2); |
433 | status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont); | 430 | status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 0, pcallcont); |
434 | return finishpcall(L, (status == LUA_OK), 1); | 431 | return finishpcall(L, (status == LUA_OK)); |
435 | } | 432 | } |
436 | 433 | ||
437 | 434 | ||