| 1074 | |
| 1075 | |
| 1076 | LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, |
| 1077 | lua_KContext ctx, lua_KFunction k) { |
| 1078 | struct CallS c; |
| 1079 | TStatus status; |
| 1080 | ptrdiff_t func; |
| 1081 | lua_lock(L); |
| 1082 | api_check(L, k == NULL || !isLua(L->ci), |
| 1083 | "cannot use continuations inside hooks"); |
| 1084 | api_checkpop(L, nargs + 1); |
| 1085 | api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); |
| 1086 | checkresults(L, nargs, nresults); |
| 1087 | if (errfunc == 0) |
| 1088 | func = 0; |
| 1089 | else { |
| 1090 | StkId o = index2stack(L, errfunc); |
| 1091 | api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); |
| 1092 | func = savestack(L, o); |
| 1093 | } |
| 1094 | c.func = L->top.p - (nargs+1); /* function to be called */ |
| 1095 | if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ |
| 1096 | c.nresults = nresults; /* do a 'conventional' protected call */ |
| 1097 | status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); |
| 1098 | } |
| 1099 | else { /* prepare continuation (call is already protected by 'resume') */ |
| 1100 | CallInfo *ci = L->ci; |
| 1101 | ci->u.c.k = k; /* save continuation */ |
| 1102 | ci->u.c.ctx = ctx; /* save context */ |
| 1103 | /* save information for error recovery */ |
| 1104 | ci->u2.funcidx = cast_int(savestack(L, c.func)); |
| 1105 | ci->u.c.old_errfunc = L->errfunc; |
| 1106 | L->errfunc = func; |
| 1107 | setoah(ci, L->allowhook); /* save value of 'allowhook' */ |
| 1108 | ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ |
| 1109 | luaD_call(L, c.func, nresults); /* do the call */ |
| 1110 | ci->callstatus &= ~CIST_YPCALL; |
| 1111 | L->errfunc = ci->u.c.old_errfunc; |
| 1112 | status = LUA_OK; /* if it is here, there were no errors */ |
| 1113 | } |
| 1114 | adjustresults(L, nresults); |
| 1115 | lua_unlock(L); |
| 1116 | return APIstatus(status); |
| 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, |
no test coverage detected