diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-12-04 15:29:32 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-12-04 15:29:32 -0200 |
commit | 0bb8eb51517ef63c65fb986d711c3163ad0be38f (patch) | |
tree | 88c0badaddb58e14a6c4e795ab106f787f81843a /lapi.c | |
parent | 38da8c0d7d69da5eed2e98a0871b86113869b858 (diff) | |
download | lua-0bb8eb51517ef63c65fb986d711c3163ad0be38f.tar.gz lua-0bb8eb51517ef63c65fb986d711c3163ad0be38f.tar.bz2 lua-0bb8eb51517ef63c65fb986d711c3163ad0be38f.zip |
new function `lua_cpcall'
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 58 |
1 files changed, 56 insertions, 2 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.223 2002/11/25 11:16:48 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.224 2002/11/25 17:50:14 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 | */ |
@@ -649,12 +649,66 @@ LUA_API void lua_call (lua_State *L, int nargs, int nresults) { | |||
649 | } | 649 | } |
650 | 650 | ||
651 | 651 | ||
652 | |||
653 | /* | ||
654 | ** Execute a protected call. | ||
655 | */ | ||
656 | struct CallS { /* data to `f_call' */ | ||
657 | StkId func; | ||
658 | int nresults; | ||
659 | }; | ||
660 | |||
661 | |||
662 | static void f_call (lua_State *L, void *ud) { | ||
663 | struct CallS *c = cast(struct CallS *, ud); | ||
664 | luaD_call(L, c->func, c->nresults); | ||
665 | } | ||
666 | |||
667 | |||
668 | |||
652 | LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { | 669 | LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { |
670 | struct CallS c; | ||
653 | int status; | 671 | int status; |
654 | ptrdiff_t func; | 672 | ptrdiff_t func; |
655 | lua_lock(L); | 673 | lua_lock(L); |
656 | func = (errfunc == 0) ? 0 : savestack(L, luaA_index(L, errfunc)); | 674 | func = (errfunc == 0) ? 0 : savestack(L, luaA_index(L, errfunc)); |
657 | status = luaD_pcall(L, nargs, nresults, func); | 675 | c.func = L->top - (nargs+1); /* function to be called */ |
676 | c.nresults = nresults; | ||
677 | status = luaD_pcall(L, &f_call, &c, savestack(L, c.func), func); | ||
678 | lua_unlock(L); | ||
679 | return status; | ||
680 | } | ||
681 | |||
682 | |||
683 | /* | ||
684 | ** Execute a protected C call. | ||
685 | */ | ||
686 | struct CCallS { /* data to `f_Ccall' */ | ||
687 | lua_CFunction func; | ||
688 | void *ud; | ||
689 | }; | ||
690 | |||
691 | |||
692 | static void f_Ccall (lua_State *L, void *ud) { | ||
693 | struct CCallS *c = cast(struct CCallS *, ud); | ||
694 | Closure *cl; | ||
695 | cl = luaF_newCclosure(L, 0); | ||
696 | cl->c.f = c->func; | ||
697 | setclvalue(L->top, cl); /* push function */ | ||
698 | incr_top(L); | ||
699 | setpvalue(L->top, c->ud); /* push only argument */ | ||
700 | incr_top(L); | ||
701 | luaD_call(L, L->top - 2, 0); | ||
702 | } | ||
703 | |||
704 | |||
705 | LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { | ||
706 | struct CCallS c; | ||
707 | int status; | ||
708 | lua_lock(L); | ||
709 | c.func = func; | ||
710 | c.ud = ud; | ||
711 | status = luaD_pcall(L, &f_Ccall, &c, savestack(L, L->top), 0); | ||
658 | lua_unlock(L); | 712 | lua_unlock(L); |
659 | return status; | 713 | return status; |
660 | } | 714 | } |