aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c58
1 files changed, 56 insertions, 2 deletions
diff --git a/lapi.c b/lapi.c
index 7577ce9f..7e663f05 100644
--- a/lapi.c
+++ b/lapi.c
@@ -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*/
656struct CallS { /* data to `f_call' */
657 StkId func;
658 int nresults;
659};
660
661
662static 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
652LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { 669LUA_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*/
686struct CCallS { /* data to `f_Ccall' */
687 lua_CFunction func;
688 void *ud;
689};
690
691
692static 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
705LUA_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}