diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-04-08 15:04:33 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-04-08 15:04:33 -0300 |
commit | 70a63fa5adfb2480ce385502f1d0306000217690 (patch) | |
tree | bcf7b3e8ac2c1c995a5bbc83678d1be7e6eef62c /lbaselib.c | |
parent | fffbaede758abe3b460621962113c352dbd7c605 (diff) | |
download | lua-70a63fa5adfb2480ce385502f1d0306000217690.tar.gz lua-70a63fa5adfb2480ce385502f1d0306000217690.tar.bz2 lua-70a63fa5adfb2480ce385502f1d0306000217690.zip |
first implementation of yieldable 'pcall'
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 19 |
1 files changed, 16 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.213 2009/03/16 16:30:50 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.214 2009/03/23 14:26:12 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 | */ |
@@ -402,10 +402,23 @@ static int luaB_select (lua_State *L) { | |||
402 | } | 402 | } |
403 | 403 | ||
404 | 404 | ||
405 | static int pcallcont (lua_State *L) { | ||
406 | int errfunc; /* call has an error function in bottom of the stack */ | ||
407 | int status = lua_getctx(L, &errfunc); | ||
408 | lua_assert(status != LUA_OK); | ||
409 | lua_pushboolean(L, (status == LUA_YIELD)); | ||
410 | if (errfunc) /* came from xpcall? */ | ||
411 | lua_replace(L, 1); /* put result in place of error function */ | ||
412 | else /* came from pcall */ | ||
413 | lua_insert(L, 1); /* open space for result */ | ||
414 | return lua_gettop(L); | ||
415 | } | ||
416 | |||
417 | |||
405 | static int luaB_pcall (lua_State *L) { | 418 | static int luaB_pcall (lua_State *L) { |
406 | int status; | 419 | int status; |
407 | luaL_checkany(L, 1); | 420 | luaL_checkany(L, 1); |
408 | status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); | 421 | status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, pcallcont); |
409 | lua_pushboolean(L, (status == LUA_OK)); | 422 | lua_pushboolean(L, (status == LUA_OK)); |
410 | lua_insert(L, 1); | 423 | lua_insert(L, 1); |
411 | return lua_gettop(L); /* return status + all results */ | 424 | return lua_gettop(L); /* return status + all results */ |
@@ -420,7 +433,7 @@ static int luaB_xpcall (lua_State *L) { | |||
420 | lua_pushvalue(L, 2); /* ...and error handler */ | 433 | lua_pushvalue(L, 2); /* ...and error handler */ |
421 | lua_replace(L, 1); | 434 | lua_replace(L, 1); |
422 | lua_replace(L, 2); | 435 | lua_replace(L, 2); |
423 | status = lua_pcall(L, n - 2, LUA_MULTRET, 1); | 436 | status = lua_pcallk(L, n - 2, LUA_MULTRET, 1, 1, pcallcont); |
424 | lua_pushboolean(L, (status == LUA_OK)); | 437 | lua_pushboolean(L, (status == LUA_OK)); |
425 | lua_replace(L, 1); | 438 | lua_replace(L, 1); |
426 | return lua_gettop(L); /* return status + all results */ | 439 | return lua_gettop(L); /* return status + all results */ |