diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 37 |
1 files changed, 21 insertions, 16 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.265 2011/07/27 12:14:06 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.266 2011/09/30 12:43:54 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,27 +391,35 @@ static int luaB_select (lua_State *L) { | |||
391 | } | 391 | } |
392 | 392 | ||
393 | 393 | ||
394 | static int pcallcont (lua_State *L) { | 394 | static int finishpcall (lua_State *L, int status, int isx) { |
395 | int errfunc = 0; /* =0 to avoid warnings */ | 395 | if (!lua_checkstack(L, 1)) { /* no space for extra boolean? */ |
396 | int status = lua_getctx(L, &errfunc); | 396 | lua_settop(L, 0); /* create space for return values */ |
397 | lua_assert(status != LUA_OK); | 397 | lua_pushboolean(L, 0); |
398 | lua_pushboolean(L, (status == LUA_YIELD)); /* first result (status) */ | 398 | lua_pushstring(L, "stack overflow"); |
399 | if (errfunc) /* came from xpcall? */ | 399 | return 2; /* return false, msg */ |
400 | } | ||
401 | lua_pushboolean(L, status); /* first result (status) */ | ||
402 | if (isx) /* came from xpcall? */ | ||
400 | lua_replace(L, 1); /* put first result in place of error function */ | 403 | lua_replace(L, 1); /* put first result in place of error function */ |
401 | else /* came from pcall */ | 404 | else /* came from pcall */ |
402 | lua_insert(L, 1); /* open space for first result */ | 405 | lua_insert(L, 1); /* insert first result before the others */ |
403 | return lua_gettop(L); | 406 | return lua_gettop(L); |
404 | } | 407 | } |
405 | 408 | ||
406 | 409 | ||
410 | static int pcallcont (lua_State *L) { | ||
411 | int isx = 0; /* =0 to avoid warnings */ | ||
412 | int status = lua_getctx(L, &isx); | ||
413 | lua_assert(status != LUA_OK); | ||
414 | return finishpcall(L, (status == LUA_YIELD), isx); | ||
415 | } | ||
416 | |||
417 | |||
407 | static int luaB_pcall (lua_State *L) { | 418 | static int luaB_pcall (lua_State *L) { |
408 | int status; | 419 | int status; |
409 | luaL_checkany(L, 1); | 420 | luaL_checkany(L, 1); |
410 | status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, pcallcont); | 421 | status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, pcallcont); |
411 | luaL_checkstack(L, 1, NULL); | 422 | return finishpcall(L, (status == LUA_OK), 0); |
412 | lua_pushboolean(L, (status == LUA_OK)); | ||
413 | lua_insert(L, 1); | ||
414 | return lua_gettop(L); /* return status + all results */ | ||
415 | } | 423 | } |
416 | 424 | ||
417 | 425 | ||
@@ -423,10 +431,7 @@ static int luaB_xpcall (lua_State *L) { | |||
423 | lua_copy(L, 2, 1); /* ...and error handler */ | 431 | lua_copy(L, 2, 1); /* ...and error handler */ |
424 | lua_replace(L, 2); | 432 | lua_replace(L, 2); |
425 | status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont); | 433 | status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont); |
426 | luaL_checkstack(L, 1, NULL); | 434 | return finishpcall(L, (status == LUA_OK), 1); |
427 | lua_pushboolean(L, (status == LUA_OK)); | ||
428 | lua_replace(L, 1); | ||
429 | return lua_gettop(L); /* return status + all results */ | ||
430 | } | 435 | } |
431 | 436 | ||
432 | 437 | ||