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 /lapi.c | |
| parent | fffbaede758abe3b460621962113c352dbd7c605 (diff) | |
| download | lua-70a63fa5adfb2480ce385502f1d0306000217690.tar.gz lua-70a63fa5adfb2480ce385502f1d0306000217690.tar.bz2 lua-70a63fa5adfb2480ce385502f1d0306000217690.zip | |
first implementation of yieldable 'pcall'
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 33 |
1 files changed, 25 insertions, 8 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.72 2009/03/23 14:26:12 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.73 2009/03/30 18:39:20 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -762,9 +762,9 @@ LUA_API int lua_setfenv (lua_State *L, int idx) { | |||
| 762 | 762 | ||
| 763 | 763 | ||
| 764 | LUA_API int lua_getctx (lua_State *L, int *ctx) { | 764 | LUA_API int lua_getctx (lua_State *L, int *ctx) { |
| 765 | if (L->ci->callstatus & CIST_CTX) { /* call has ctx? */ | 765 | if (L->ci->callstatus & CIST_YIELDED) { |
| 766 | *ctx = L->ci->u.c.ctx; | 766 | if (ctx) *ctx = L->ci->u.c.ctx; |
| 767 | return LUA_YIELD; | 767 | return L->ci->u.c.status; |
| 768 | } | 768 | } |
| 769 | else return LUA_OK; | 769 | else return LUA_OK; |
| 770 | } | 770 | } |
| @@ -782,7 +782,6 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, | |||
| 782 | if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ | 782 | if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ |
| 783 | L->ci->u.c.k = k; /* save continuation */ | 783 | L->ci->u.c.k = k; /* save continuation */ |
| 784 | L->ci->u.c.ctx = ctx; /* save context */ | 784 | L->ci->u.c.ctx = ctx; /* save context */ |
| 785 | L->ci->callstatus |= CIST_CTX; /* mark that call has context */ | ||
| 786 | luaD_call(L, func, nresults, 1); /* do the call */ | 785 | luaD_call(L, func, nresults, 1); /* do the call */ |
| 787 | } | 786 | } |
| 788 | else /* no continuation or no yieldable */ | 787 | else /* no continuation or no yieldable */ |
| @@ -809,7 +808,8 @@ static void f_call (lua_State *L, void *ud) { | |||
| 809 | 808 | ||
| 810 | 809 | ||
| 811 | 810 | ||
| 812 | LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { | 811 | LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, |
| 812 | int ctx, lua_CFunction k) { | ||
| 813 | struct CallS c; | 813 | struct CallS c; |
| 814 | int status; | 814 | int status; |
| 815 | ptrdiff_t func; | 815 | ptrdiff_t func; |
| @@ -824,8 +824,25 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { | |||
| 824 | func = savestack(L, o); | 824 | func = savestack(L, o); |
| 825 | } | 825 | } |
| 826 | c.func = L->top - (nargs+1); /* function to be called */ | 826 | c.func = L->top - (nargs+1); /* function to be called */ |
| 827 | c.nresults = nresults; | 827 | if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ |
| 828 | status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); | 828 | c.nresults = nresults; /* do a 'conventional' protected call */ |
| 829 | status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); | ||
| 830 | } | ||
| 831 | else { /* prepare continuation (call is already protected by 'resume') */ | ||
| 832 | L->ci->u.c.k = k; /* save continuation */ | ||
| 833 | L->ci->u.c.ctx = ctx; /* save context */ | ||
| 834 | /* save information for error recovery */ | ||
| 835 | L->ci->u.c.oldtop = savestack(L, c.func); | ||
| 836 | L->ci->u.c.old_allowhook = L->allowhook; | ||
| 837 | L->ci->u.c.old_errfunc = L->errfunc; | ||
| 838 | L->errfunc = func; | ||
| 839 | /* mark that function may do error recovery */ | ||
| 840 | L->ci->callstatus |= CIST_YPCALL; | ||
| 841 | luaD_call(L, c.func, nresults, 1); /* do the call */ | ||
| 842 | L->ci->callstatus &= ~CIST_YPCALL; | ||
| 843 | L->errfunc = L->ci->u.c.old_errfunc; | ||
| 844 | status = LUA_OK; /* if it is here, there were no errors */ | ||
| 845 | } | ||
| 829 | adjustresults(L, nresults); | 846 | adjustresults(L, nresults); |
| 830 | lua_unlock(L); | 847 | lua_unlock(L); |
| 831 | return status; | 848 | return status; |
