/src/testdir/build/lua-master/source/ldo.c
Line | Count | Source |
1 | | /* |
2 | | ** $Id: ldo.c $ |
3 | | ** Stack and Call structure of Lua |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define ldo_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <setjmp.h> |
14 | | #include <stdlib.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include "lua.h" |
18 | | |
19 | | #include "lapi.h" |
20 | | #include "ldebug.h" |
21 | | #include "ldo.h" |
22 | | #include "lfunc.h" |
23 | | #include "lgc.h" |
24 | | #include "lmem.h" |
25 | | #include "lobject.h" |
26 | | #include "lopcodes.h" |
27 | | #include "lparser.h" |
28 | | #include "lstate.h" |
29 | | #include "lstring.h" |
30 | | #include "ltable.h" |
31 | | #include "ltm.h" |
32 | | #include "lundump.h" |
33 | | #include "lvm.h" |
34 | | #include "lzio.h" |
35 | | |
36 | | |
37 | | |
38 | 0 | #define errorstatus(s) ((s) > LUA_YIELD) |
39 | | |
40 | | |
41 | | /* |
42 | | ** these macros allow user-specific actions when a thread is |
43 | | ** resumed/yielded. |
44 | | */ |
45 | | #if !defined(luai_userstateresume) |
46 | 0 | #define luai_userstateresume(L,n) ((void)L) |
47 | | #endif |
48 | | |
49 | | #if !defined(luai_userstateyield) |
50 | 0 | #define luai_userstateyield(L,n) ((void)L) |
51 | | #endif |
52 | | |
53 | | |
54 | | /* |
55 | | ** {====================================================== |
56 | | ** Error-recovery functions |
57 | | ** ======================================================= |
58 | | */ |
59 | | |
60 | | /* chained list of long jump buffers */ |
61 | | typedef struct lua_longjmp { |
62 | | struct lua_longjmp *previous; |
63 | | jmp_buf b; |
64 | | volatile TStatus status; /* error code */ |
65 | | } lua_longjmp; |
66 | | |
67 | | |
68 | | /* |
69 | | ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By |
70 | | ** default, Lua handles errors with exceptions when compiling as |
71 | | ** C++ code, with _longjmp/_setjmp when available (POSIX), and with |
72 | | ** longjmp/setjmp otherwise. |
73 | | */ |
74 | | #if !defined(LUAI_THROW) /* { */ |
75 | | |
76 | | #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) /* { */ |
77 | | |
78 | | /* C++ exceptions */ |
79 | | #define LUAI_THROW(L,c) throw(c) |
80 | | |
81 | | static void LUAI_TRY (lua_State *L, lua_longjmp *c, Pfunc f, void *ud) { |
82 | | try { |
83 | | f(L, ud); /* call function protected */ |
84 | | } |
85 | | catch (lua_longjmp *c1) { /* Lua error */ |
86 | | if (c1 != c) /* not the correct level? */ |
87 | | throw; /* rethrow to upper level */ |
88 | | } |
89 | | catch (...) { /* non-Lua exception */ |
90 | | c->status = -1; /* create some error code */ |
91 | | } |
92 | | } |
93 | | |
94 | | |
95 | | #elif defined(LUA_USE_POSIX) /* }{ */ |
96 | | |
97 | | /* in POSIX, use _longjmp/_setjmp (more efficient) */ |
98 | 454 | #define LUAI_THROW(L,c) _longjmp((c)->b, 1) |
99 | 3.76k | #define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud)) |
100 | | |
101 | | #else /* }{ */ |
102 | | |
103 | | /* ISO C handling with long jumps */ |
104 | | #define LUAI_THROW(L,c) longjmp((c)->b, 1) |
105 | | #define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud)) |
106 | | |
107 | | #endif /* } */ |
108 | | |
109 | | #endif /* } */ |
110 | | |
111 | | |
112 | 454 | void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { |
113 | 454 | if (errcode == LUA_ERRMEM) { /* memory error? */ |
114 | 0 | setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ |
115 | 0 | } |
116 | 454 | else { |
117 | 454 | lua_assert(errorstatus(errcode)); /* must be a real error */ |
118 | 454 | lua_assert(!ttisnil(s2v(L->top.p - 1))); /* with a non-nil object */ |
119 | 908 | setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ |
120 | 454 | } |
121 | 454 | L->top.p = oldtop + 1; /* top goes back to old top plus error object */ |
122 | 454 | } |
123 | | |
124 | | |
125 | 454 | l_noret luaD_throw (lua_State *L, TStatus errcode) { |
126 | 454 | if (L->errorJmp) { /* thread has an error handler? */ |
127 | 454 | L->errorJmp->status = errcode; /* set status */ |
128 | 454 | LUAI_THROW(L, L->errorJmp); /* jump to it */ |
129 | 454 | } |
130 | 0 | else { /* thread has no error handler */ |
131 | 0 | global_State *g = G(L); |
132 | 0 | lua_State *mainth = mainthread(g); |
133 | 0 | errcode = luaE_resetthread(L, errcode); /* close all upvalues */ |
134 | 0 | L->status = errcode; |
135 | 0 | if (mainth->errorJmp) { /* main thread has a handler? */ |
136 | 0 | setobjs2s(L, mainth->top.p++, L->top.p - 1); /* copy error obj. */ |
137 | 0 | luaD_throw(mainth, errcode); /* re-throw in main thread */ |
138 | 0 | } |
139 | 0 | else { /* no handler at all; abort */ |
140 | 0 | if (g->panic) { /* panic function? */ |
141 | 0 | lua_unlock(L); |
142 | 0 | g->panic(L); /* call panic function (last chance to jump out) */ |
143 | 0 | } |
144 | 0 | abort(); |
145 | 0 | } |
146 | 0 | } |
147 | 454 | } |
148 | | |
149 | | |
150 | 0 | l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) { |
151 | 0 | if (L->errorJmp) { |
152 | | /* unroll error entries up to the first level */ |
153 | 0 | while (L->errorJmp->previous != NULL) |
154 | 0 | L->errorJmp = L->errorJmp->previous; |
155 | 0 | } |
156 | 0 | luaD_throw(L, errcode); |
157 | 0 | } |
158 | | |
159 | | |
160 | 3.76k | TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { |
161 | 3.76k | l_uint32 oldnCcalls = L->nCcalls; |
162 | 3.76k | lua_longjmp lj; |
163 | 3.76k | lj.status = LUA_OK; |
164 | 3.76k | lj.previous = L->errorJmp; /* chain new error handler */ |
165 | 3.76k | L->errorJmp = &lj; |
166 | 3.76k | LUAI_TRY(L, &lj, f, ud); /* call 'f' catching errors */ |
167 | 3.76k | L->errorJmp = lj.previous; /* restore old error handler */ |
168 | 3.76k | L->nCcalls = oldnCcalls; |
169 | 3.76k | return lj.status; |
170 | 3.76k | } |
171 | | |
172 | | /* }====================================================== */ |
173 | | |
174 | | |
175 | | /* |
176 | | ** {================================================================== |
177 | | ** Stack reallocation |
178 | | ** =================================================================== |
179 | | */ |
180 | | |
181 | | /* some stack space for error handling */ |
182 | 3 | #define STACKERRSPACE 200 |
183 | | |
184 | | |
185 | | /* |
186 | | ** LUAI_MAXSTACK limits the size of the Lua stack. |
187 | | ** It must fit into INT_MAX/2. |
188 | | */ |
189 | | |
190 | | #if !defined(LUAI_MAXSTACK) |
191 | | #if 1000000 < (INT_MAX / 2) |
192 | | #define LUAI_MAXSTACK 1000000 |
193 | | #else |
194 | | #define LUAI_MAXSTACK (INT_MAX / 2u) |
195 | | #endif |
196 | | #endif |
197 | | |
198 | | |
199 | | /* maximum stack size that respects size_t */ |
200 | | #define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) |
201 | | |
202 | | /* |
203 | | ** Minimum between LUAI_MAXSTACK and MAXSTACK_BYSIZET |
204 | | ** (Maximum size for the stack must respect size_t.) |
205 | | */ |
206 | 6.11k | #define MAXSTACK cast_int(LUAI_MAXSTACK < MAXSTACK_BYSIZET \ |
207 | 5.87k | ? LUAI_MAXSTACK : MAXSTACK_BYSIZET) |
208 | | |
209 | | |
210 | | /* stack size with extra space for error handling */ |
211 | 3 | #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) |
212 | | |
213 | | |
214 | | /* raise a stack error while running the message handler */ |
215 | 0 | l_noret luaD_errerr (lua_State *L) { |
216 | 0 | TString *msg = luaS_newliteral(L, "error in error handling"); |
217 | 0 | setsvalue2s(L, L->top.p, msg); |
218 | 0 | L->top.p++; /* assume EXTRA_STACK */ |
219 | 0 | luaD_throw(L, LUA_ERRERR); |
220 | 0 | } |
221 | | |
222 | | |
223 | | /* |
224 | | ** In ISO C, any pointer use after the pointer has been deallocated is |
225 | | ** undefined behavior. So, before a stack reallocation, all pointers |
226 | | ** should be changed to offsets, and after the reallocation they should |
227 | | ** be changed back to pointers. As during the reallocation the pointers |
228 | | ** are invalid, the reallocation cannot run emergency collections. |
229 | | ** Alternatively, we can use the old address after the deallocation. |
230 | | ** That is not strict ISO C, but seems to work fine everywhere. |
231 | | ** The following macro chooses how strict is the code. |
232 | | */ |
233 | | #if !defined(LUAI_STRICT_ADDRESS) |
234 | | #define LUAI_STRICT_ADDRESS 1 |
235 | | #endif |
236 | | |
237 | | #if LUAI_STRICT_ADDRESS |
238 | | /* |
239 | | ** Change all pointers to the stack into offsets. |
240 | | */ |
241 | 132 | static void relstack (lua_State *L) { |
242 | 132 | CallInfo *ci; |
243 | 132 | UpVal *up; |
244 | 132 | L->top.offset = savestack(L, L->top.p); |
245 | 132 | L->tbclist.offset = savestack(L, L->tbclist.p); |
246 | 132 | for (up = L->openupval; up != NULL; up = up->u.open.next) |
247 | 0 | up->v.offset = savestack(L, uplevel(up)); |
248 | 2.07M | for (ci = L->ci; ci != NULL; ci = ci->previous) { |
249 | 2.07M | ci->top.offset = savestack(L, ci->top.p); |
250 | 2.07M | ci->func.offset = savestack(L, ci->func.p); |
251 | 2.07M | } |
252 | 132 | } |
253 | | |
254 | | |
255 | | /* |
256 | | ** Change back all offsets into pointers. |
257 | | */ |
258 | 132 | static void correctstack (lua_State *L, StkId oldstack) { |
259 | 132 | CallInfo *ci; |
260 | 132 | UpVal *up; |
261 | 132 | UNUSED(oldstack); |
262 | 132 | L->top.p = restorestack(L, L->top.offset); |
263 | 132 | L->tbclist.p = restorestack(L, L->tbclist.offset); |
264 | 132 | for (up = L->openupval; up != NULL; up = up->u.open.next) |
265 | 0 | up->v.p = s2v(restorestack(L, up->v.offset)); |
266 | 2.07M | for (ci = L->ci; ci != NULL; ci = ci->previous) { |
267 | 2.07M | ci->top.p = restorestack(L, ci->top.offset); |
268 | 2.07M | ci->func.p = restorestack(L, ci->func.offset); |
269 | 2.07M | if (isLua(ci)) |
270 | 2.07M | ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ |
271 | 2.07M | } |
272 | 132 | } |
273 | | |
274 | | #else |
275 | | /* |
276 | | ** Assume that it is fine to use an address after its deallocation, |
277 | | ** as long as we do not dereference it. |
278 | | */ |
279 | | |
280 | | static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */ |
281 | | |
282 | | |
283 | | /* |
284 | | ** Correct pointers into 'oldstack' to point into 'L->stack'. |
285 | | */ |
286 | | static void correctstack (lua_State *L, StkId oldstack) { |
287 | | CallInfo *ci; |
288 | | UpVal *up; |
289 | | StkId newstack = L->stack.p; |
290 | | if (oldstack == newstack) |
291 | | return; |
292 | | L->top.p = L->top.p - oldstack + newstack; |
293 | | L->tbclist.p = L->tbclist.p - oldstack + newstack; |
294 | | for (up = L->openupval; up != NULL; up = up->u.open.next) |
295 | | up->v.p = s2v(uplevel(up) - oldstack + newstack); |
296 | | for (ci = L->ci; ci != NULL; ci = ci->previous) { |
297 | | ci->top.p = ci->top.p - oldstack + newstack; |
298 | | ci->func.p = ci->func.p - oldstack + newstack; |
299 | | if (isLua(ci)) |
300 | | ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ |
301 | | } |
302 | | } |
303 | | #endif |
304 | | |
305 | | |
306 | | /* |
307 | | ** Reallocate the stack to a new size, correcting all pointers into it. |
308 | | ** In case of allocation error, raise an error or return false according |
309 | | ** to 'raiseerror'. |
310 | | */ |
311 | 132 | int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { |
312 | 132 | int oldsize = stacksize(L); |
313 | 132 | int i; |
314 | 132 | StkId newstack; |
315 | 132 | StkId oldstack = L->stack.p; |
316 | 132 | lu_byte oldgcstop = G(L)->gcstopem; |
317 | 132 | lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE); |
318 | 132 | relstack(L); /* change pointers to offsets */ |
319 | 132 | G(L)->gcstopem = 1; /* stop emergency collection */ |
320 | 132 | newstack = luaM_reallocvector(L, oldstack, oldsize + EXTRA_STACK, |
321 | 132 | newsize + EXTRA_STACK, StackValue); |
322 | 132 | G(L)->gcstopem = oldgcstop; /* restore emergency collection */ |
323 | 132 | if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ |
324 | 0 | correctstack(L, oldstack); /* change offsets back to pointers */ |
325 | 0 | if (raiseerror) |
326 | 0 | luaM_error(L); |
327 | 0 | else return 0; /* do not raise an error */ |
328 | 0 | } |
329 | 132 | L->stack.p = newstack; |
330 | 132 | correctstack(L, oldstack); /* change offsets back to pointers */ |
331 | 132 | L->stack_last.p = L->stack.p + newsize; |
332 | 3.00M | for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++) |
333 | 3.00M | setnilvalue(s2v(newstack + i)); /* erase new segment */ |
334 | 132 | return 1; |
335 | 132 | } |
336 | | |
337 | | |
338 | | /* |
339 | | ** Try to grow the stack by at least 'n' elements. When 'raiseerror' |
340 | | ** is true, raises any error; otherwise, return 0 in case of errors. |
341 | | */ |
342 | 115 | int luaD_growstack (lua_State *L, int n, int raiseerror) { |
343 | 115 | int size = stacksize(L); |
344 | 115 | if (l_unlikely(size > MAXSTACK)) { |
345 | | /* if stack is larger than maximum, thread is already using the |
346 | | extra space reserved for errors, that is, thread is handling |
347 | | a stack error; cannot grow further than that. */ |
348 | 0 | lua_assert(stacksize(L) == ERRORSTACKSIZE); |
349 | 0 | if (raiseerror) |
350 | 0 | luaD_errerr(L); /* stack error inside message handler */ |
351 | 0 | return 0; /* if not 'raiseerror', just signal it */ |
352 | 0 | } |
353 | 115 | else if (n < MAXSTACK) { /* avoids arithmetic overflows */ |
354 | 115 | int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */ |
355 | 115 | int needed = cast_int(L->top.p - L->stack.p) + n; |
356 | 115 | if (newsize > MAXSTACK) /* cannot cross the limit */ |
357 | 8 | newsize = MAXSTACK; |
358 | 115 | if (newsize < needed) /* but must respect what was asked for */ |
359 | 3 | newsize = needed; |
360 | 115 | if (l_likely(newsize <= MAXSTACK)) |
361 | 112 | return luaD_reallocstack(L, newsize, raiseerror); |
362 | 115 | } |
363 | | /* else stack overflow */ |
364 | | /* add extra size to be able to handle the error message */ |
365 | 3 | luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); |
366 | 3 | if (raiseerror) |
367 | 3 | luaG_runerror(L, "stack overflow"); |
368 | 0 | return 0; |
369 | 3 | } |
370 | | |
371 | | |
372 | | /* |
373 | | ** Compute how much of the stack is being used, by computing the |
374 | | ** maximum top of all call frames in the stack and the current top. |
375 | | */ |
376 | 1.94k | static int stackinuse (lua_State *L) { |
377 | 1.94k | CallInfo *ci; |
378 | 1.94k | int res; |
379 | 1.94k | StkId lim = L->top.p; |
380 | 427k | for (ci = L->ci; ci != NULL; ci = ci->previous) { |
381 | 425k | if (lim < ci->top.p) lim = ci->top.p; |
382 | 425k | } |
383 | 1.94k | lua_assert(lim <= L->stack_last.p + EXTRA_STACK); |
384 | 1.94k | res = cast_int(lim - L->stack.p) + 1; /* part of stack in use */ |
385 | 1.94k | if (res < LUA_MINSTACK) |
386 | 0 | res = LUA_MINSTACK; /* ensure a minimum size */ |
387 | 1.94k | return res; |
388 | 1.94k | } |
389 | | |
390 | | |
391 | | /* |
392 | | ** If stack size is more than 3 times the current use, reduce that size |
393 | | ** to twice the current use. (So, the final stack size is at most 2/3 the |
394 | | ** previous size, and half of its entries are empty.) |
395 | | ** As a particular case, if stack was handling a stack overflow and now |
396 | | ** it is not, 'max' (limited by MAXSTACK) will be smaller than |
397 | | ** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack |
398 | | ** will be reduced to a "regular" size. |
399 | | */ |
400 | 1.94k | void luaD_shrinkstack (lua_State *L) { |
401 | 1.94k | int inuse = stackinuse(L); |
402 | 1.94k | int max = (inuse > MAXSTACK / 3) ? MAXSTACK : inuse * 3; |
403 | | /* if thread is currently not handling a stack overflow and its |
404 | | size is larger than maximum "reasonable" size, shrink it */ |
405 | 1.94k | if (inuse <= MAXSTACK && stacksize(L) > max) { |
406 | 17 | int nsize = (inuse > MAXSTACK / 2) ? MAXSTACK : inuse * 2; |
407 | 17 | luaD_reallocstack(L, nsize, 0); /* ok if that fails */ |
408 | 17 | } |
409 | 1.93k | else /* don't change stack */ |
410 | 1.93k | condmovestack(L,(void)0,(void)0); /* (change only for debugging) */ |
411 | 1.94k | luaE_shrinkCI(L); /* shrink CI list */ |
412 | 1.94k | } |
413 | | |
414 | | |
415 | 2.12k | void luaD_inctop (lua_State *L) { |
416 | 2.12k | L->top.p++; |
417 | 2.12k | luaD_checkstack(L, 1); |
418 | 2.12k | } |
419 | | |
420 | | /* }================================================================== */ |
421 | | |
422 | | |
423 | | /* |
424 | | ** Call a hook for the given event. Make sure there is a hook to be |
425 | | ** called. (Both 'L->hook' and 'L->hookmask', which trigger this |
426 | | ** function, can be changed asynchronously by signals.) |
427 | | */ |
428 | | void luaD_hook (lua_State *L, int event, int line, |
429 | 0 | int ftransfer, int ntransfer) { |
430 | 0 | lua_Hook hook = L->hook; |
431 | 0 | if (hook && L->allowhook) { /* make sure there is a hook */ |
432 | 0 | CallInfo *ci = L->ci; |
433 | 0 | ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */ |
434 | 0 | ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */ |
435 | 0 | lua_Debug ar; |
436 | 0 | ar.event = event; |
437 | 0 | ar.currentline = line; |
438 | 0 | ar.i_ci = ci; |
439 | 0 | L->transferinfo.ftransfer = ftransfer; |
440 | 0 | L->transferinfo.ntransfer = ntransfer; |
441 | 0 | if (isLua(ci) && L->top.p < ci->top.p) |
442 | 0 | L->top.p = ci->top.p; /* protect entire activation register */ |
443 | 0 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
444 | 0 | if (ci->top.p < L->top.p + LUA_MINSTACK) |
445 | 0 | ci->top.p = L->top.p + LUA_MINSTACK; |
446 | 0 | L->allowhook = 0; /* cannot call hooks inside a hook */ |
447 | 0 | ci->callstatus |= CIST_HOOKED; |
448 | 0 | lua_unlock(L); |
449 | 0 | (*hook)(L, &ar); |
450 | 0 | lua_lock(L); |
451 | 0 | lua_assert(!L->allowhook); |
452 | 0 | L->allowhook = 1; |
453 | 0 | ci->top.p = restorestack(L, ci_top); |
454 | 0 | L->top.p = restorestack(L, top); |
455 | 0 | ci->callstatus &= ~CIST_HOOKED; |
456 | 0 | } |
457 | 0 | } |
458 | | |
459 | | |
460 | | /* |
461 | | ** Executes a call hook for Lua functions. This function is called |
462 | | ** whenever 'hookmask' is not zero, so it checks whether call hooks are |
463 | | ** active. |
464 | | */ |
465 | 0 | void luaD_hookcall (lua_State *L, CallInfo *ci) { |
466 | 0 | L->oldpc = 0; /* set 'oldpc' for new function */ |
467 | 0 | if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ |
468 | 0 | int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL |
469 | 0 | : LUA_HOOKCALL; |
470 | 0 | Proto *p = ci_func(ci)->p; |
471 | 0 | ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ |
472 | 0 | luaD_hook(L, event, -1, 1, p->numparams); |
473 | 0 | ci->u.l.savedpc--; /* correct 'pc' */ |
474 | 0 | } |
475 | 0 | } |
476 | | |
477 | | |
478 | | /* |
479 | | ** Executes a return hook for Lua and C functions and sets/corrects |
480 | | ** 'oldpc'. (Note that this correction is needed by the line hook, so it |
481 | | ** is done even when return hooks are off.) |
482 | | */ |
483 | 0 | static void rethook (lua_State *L, CallInfo *ci, int nres) { |
484 | 0 | if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ |
485 | 0 | StkId firstres = L->top.p - nres; /* index of first result */ |
486 | 0 | int delta = 0; /* correction for vararg functions */ |
487 | 0 | int ftransfer; |
488 | 0 | if (isLua(ci)) { |
489 | 0 | Proto *p = ci_func(ci)->p; |
490 | 0 | if (p->flag & PF_ISVARARG) |
491 | 0 | delta = ci->u.l.nextraargs + p->numparams + 1; |
492 | 0 | } |
493 | 0 | ci->func.p += delta; /* if vararg, back to virtual 'func' */ |
494 | 0 | ftransfer = cast_int(firstres - ci->func.p); |
495 | 0 | luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ |
496 | 0 | ci->func.p -= delta; |
497 | 0 | } |
498 | 0 | if (isLua(ci = ci->previous)) |
499 | 0 | L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ |
500 | 0 | } |
501 | | |
502 | | |
503 | | /* |
504 | | ** Check whether 'func' has a '__call' metafield. If so, put it in the |
505 | | ** stack, below original 'func', so that 'luaD_precall' can call it. |
506 | | ** Raise an error if there is no '__call' metafield. |
507 | | ** Bits CIST_CCMT in status count how many _call metamethods were |
508 | | ** invoked and how many corresponding extra arguments were pushed. |
509 | | ** (This count will be saved in the 'callstatus' of the call). |
510 | | ** Raise an error if this counter overflows. |
511 | | */ |
512 | 13 | static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) { |
513 | 13 | const TValue *tm; |
514 | 13 | StkId p; |
515 | 13 | tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); |
516 | 13 | if (l_unlikely(ttisnil(tm))) /* no metamethod? */ |
517 | 13 | luaG_callerror(L, s2v(func)); |
518 | 0 | for (p = L->top.p; p > func; p--) /* open space for metamethod */ |
519 | 0 | setobjs2s(L, p, p-1); |
520 | 0 | L->top.p++; /* stack space pre-allocated by the caller */ |
521 | 0 | setobj2s(L, func, tm); /* metamethod is the new function to be called */ |
522 | 0 | if ((status & MAX_CCMT) == MAX_CCMT) /* is counter full? */ |
523 | 0 | luaG_runerror(L, "'__call' chain too long"); |
524 | 0 | return status + (1u << CIST_CCMT); /* increment counter */ |
525 | 0 | } |
526 | | |
527 | | |
528 | | /* Generic case for 'moveresult' */ |
529 | | l_sinline void genmoveresults (lua_State *L, StkId res, int nres, |
530 | 71 | int wanted) { |
531 | 71 | StkId firstresult = L->top.p - nres; /* index of first result */ |
532 | 71 | int i; |
533 | 71 | if (nres > wanted) /* extra results? */ |
534 | 0 | nres = wanted; /* don't need them */ |
535 | 142 | for (i = 0; i < nres; i++) /* move all results to correct place */ |
536 | 71 | setobjs2s(L, res + i, firstresult + i); |
537 | 71 | for (; i < wanted; i++) /* complete wanted number of results */ |
538 | 71 | setnilvalue(s2v(res + i)); |
539 | 71 | L->top.p = res + wanted; /* top points after the last result */ |
540 | 71 | } |
541 | | |
542 | | |
543 | | /* |
544 | | ** Given 'nres' results at 'firstResult', move 'fwanted-1' of them |
545 | | ** to 'res'. Handle most typical cases (zero results for commands, |
546 | | ** one result for expressions, multiple results for tail calls/single |
547 | | ** parameters) separated. The flag CIST_TBC in 'fwanted', if set, |
548 | | ** forces the switch to go to the default case. |
549 | | */ |
550 | | l_sinline void moveresults (lua_State *L, StkId res, int nres, |
551 | 296 | l_uint32 fwanted) { |
552 | 296 | switch (fwanted) { /* handle typical cases separately */ |
553 | 144 | case 0 + 1: /* no values needed */ |
554 | 144 | L->top.p = res; |
555 | 144 | return; |
556 | 81 | case 1 + 1: /* one value needed */ |
557 | 81 | if (nres == 0) /* no results? */ |
558 | 81 | setnilvalue(s2v(res)); /* adjust with nil */ |
559 | 81 | else /* at least one result */ |
560 | 81 | setobjs2s(L, res, L->top.p - nres); /* move it to proper place */ |
561 | 81 | L->top.p = res + 1; |
562 | 81 | return; |
563 | 0 | case LUA_MULTRET + 1: |
564 | 0 | genmoveresults(L, res, nres, nres); /* we want all results */ |
565 | 0 | break; |
566 | 71 | default: { /* two/more results and/or to-be-closed variables */ |
567 | 71 | int wanted = get_nresults(fwanted); |
568 | 71 | if (fwanted & CIST_TBC) { /* to-be-closed variables? */ |
569 | 71 | L->ci->u2.nres = nres; |
570 | 71 | L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ |
571 | 71 | res = luaF_close(L, res, CLOSEKTOP, 1); |
572 | 71 | L->ci->callstatus &= ~CIST_CLSRET; |
573 | 71 | if (L->hookmask) { /* if needed, call hook after '__close's */ |
574 | 0 | ptrdiff_t savedres = savestack(L, res); |
575 | 0 | rethook(L, L->ci, nres); |
576 | 0 | res = restorestack(L, savedres); /* hook can move stack */ |
577 | 0 | } |
578 | 71 | if (wanted == LUA_MULTRET) |
579 | 0 | wanted = nres; /* we want all results */ |
580 | 71 | } |
581 | 71 | genmoveresults(L, res, nres, wanted); |
582 | 71 | break; |
583 | 81 | } |
584 | 296 | } |
585 | 296 | } |
586 | | |
587 | | |
588 | | /* |
589 | | ** Finishes a function call: calls hook if necessary, moves current |
590 | | ** number of results to proper place, and returns to previous call |
591 | | ** info. If function has to close variables, hook must be called after |
592 | | ** that. |
593 | | */ |
594 | 296 | void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { |
595 | 296 | l_uint32 fwanted = ci->callstatus & (CIST_TBC | CIST_NRESULTS); |
596 | 296 | if (l_unlikely(L->hookmask) && !(fwanted & CIST_TBC)) |
597 | 0 | rethook(L, ci, nres); |
598 | | /* move results to proper place */ |
599 | 296 | moveresults(L, ci->func.p, nres, fwanted); |
600 | | /* function cannot be in any of these cases when returning */ |
601 | 296 | lua_assert(!(ci->callstatus & |
602 | 296 | (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_CLSRET))); |
603 | 296 | L->ci = ci->previous; /* back to caller (after closing variables) */ |
604 | 296 | } |
605 | | |
606 | | |
607 | | |
608 | 467k | #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) |
609 | | |
610 | | |
611 | | /* |
612 | | ** Allocate and initialize CallInfo structure. At this point, the |
613 | | ** only valid fields in the call status are number of results, |
614 | | ** CIST_C (if it's a C function), and number of extra arguments. |
615 | | ** (All these bit-fields fit in 16-bit values.) |
616 | | */ |
617 | | l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status, |
618 | 467k | StkId top) { |
619 | 467k | CallInfo *ci = L->ci = next_ci(L); /* new frame */ |
620 | 467k | ci->func.p = func; |
621 | 467k | lua_assert((status & ~(CIST_NRESULTS | CIST_C | MAX_CCMT)) == 0); |
622 | 467k | ci->callstatus = status; |
623 | 467k | ci->top.p = top; |
624 | 467k | return ci; |
625 | 467k | } |
626 | | |
627 | | |
628 | | /* |
629 | | ** precall for C functions |
630 | | */ |
631 | | l_sinline int precallC (lua_State *L, StkId func, unsigned status, |
632 | 294 | lua_CFunction f) { |
633 | 294 | int n; /* number of returns */ |
634 | 294 | CallInfo *ci; |
635 | 294 | checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ |
636 | 294 | L->ci = ci = prepCallInfo(L, func, status | CIST_C, |
637 | 294 | L->top.p + LUA_MINSTACK); |
638 | 294 | lua_assert(ci->top.p <= L->stack_last.p); |
639 | 294 | if (l_unlikely(L->hookmask & LUA_MASKCALL)) { |
640 | 0 | int narg = cast_int(L->top.p - func) - 1; |
641 | 0 | luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); |
642 | 0 | } |
643 | 294 | lua_unlock(L); |
644 | 294 | n = (*f)(L); /* do the actual call */ |
645 | 294 | lua_lock(L); |
646 | 294 | api_checknelems(L, n); |
647 | 294 | luaD_poscall(L, ci, n); |
648 | 294 | return n; |
649 | 294 | } |
650 | | |
651 | | |
652 | | /* |
653 | | ** Prepare a function for a tail call, building its call info on top |
654 | | ** of the current call info. 'narg1' is the number of arguments plus 1 |
655 | | ** (so that it includes the function itself). Return the number of |
656 | | ** results, if it was a C function, or -1 for a Lua function. |
657 | | */ |
658 | | int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, |
659 | 466k | int narg1, int delta) { |
660 | 466k | unsigned status = LUA_MULTRET + 1; |
661 | 466k | retry: |
662 | 466k | switch (ttypetag(s2v(func))) { |
663 | 0 | case LUA_VCCL: /* C closure */ |
664 | 0 | return precallC(L, func, status, clCvalue(s2v(func))->f); |
665 | 0 | case LUA_VLCF: /* light C function */ |
666 | 0 | return precallC(L, func, status, fvalue(s2v(func))); |
667 | 466k | case LUA_VLCL: { /* Lua function */ |
668 | 933k | Proto *p = clLvalue(s2v(func))->p; |
669 | 933k | int fsize = p->maxstacksize; /* frame size */ |
670 | 933k | int nfixparams = p->numparams; |
671 | 933k | int i; |
672 | 933k | checkstackp(L, fsize - delta, func); |
673 | 933k | ci->func.p -= delta; /* restore 'func' (if vararg) */ |
674 | 1.39M | for (i = 0; i < narg1; i++) /* move down function and arguments */ |
675 | 933k | setobjs2s(L, ci->func.p + i, func + i); |
676 | 466k | func = ci->func.p; /* moved-down function */ |
677 | 2.53M | for (; narg1 <= nfixparams; narg1++) |
678 | 2.06M | setnilvalue(s2v(func + narg1)); /* complete missing arguments */ |
679 | 466k | ci->top.p = func + 1 + fsize; /* top for new function */ |
680 | 466k | lua_assert(ci->top.p <= L->stack_last.p); |
681 | 466k | ci->u.l.savedpc = p->code; /* starting point */ |
682 | 466k | ci->callstatus |= CIST_TAIL; |
683 | 466k | L->top.p = func + narg1; /* set top */ |
684 | 466k | return -1; |
685 | 466k | } |
686 | 0 | default: { /* not a function */ |
687 | 0 | checkstackp(L, 1, func); /* space for metamethod */ |
688 | 0 | status = tryfuncTM(L, func, status); /* try '__call' metamethod */ |
689 | 0 | narg1++; |
690 | 0 | goto retry; /* try again */ |
691 | 466k | } |
692 | 466k | } |
693 | 466k | } |
694 | | |
695 | | |
696 | | /* |
697 | | ** Prepares the call to a function (C or Lua). For C functions, also do |
698 | | ** the call. The function to be called is at '*func'. The arguments |
699 | | ** are on the stack, right after the function. Returns the CallInfo |
700 | | ** to be executed, if it was a Lua function. Otherwise (a C function) |
701 | | ** returns NULL, with all the results on the stack, starting at the |
702 | | ** original function position. |
703 | | */ |
704 | 467k | CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { |
705 | 467k | unsigned status = cast_uint(nresults + 1); |
706 | 467k | lua_assert(status <= MAXRESULTS + 1); |
707 | 467k | retry: |
708 | 467k | switch (ttypetag(s2v(func))) { |
709 | 0 | case LUA_VCCL: /* C closure */ |
710 | 0 | precallC(L, func, status, clCvalue(s2v(func))->f); |
711 | 0 | return NULL; |
712 | 294 | case LUA_VLCF: /* light C function */ |
713 | 294 | precallC(L, func, status, fvalue(s2v(func))); |
714 | 294 | return NULL; |
715 | 466k | case LUA_VLCL: { /* Lua function */ |
716 | 466k | CallInfo *ci; |
717 | 933k | Proto *p = clLvalue(s2v(func))->p; |
718 | 933k | int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */ |
719 | 933k | int nfixparams = p->numparams; |
720 | 933k | int fsize = p->maxstacksize; /* frame size */ |
721 | 933k | checkstackp(L, fsize, func); |
722 | 933k | L->ci = ci = prepCallInfo(L, func, status, func + 1 + fsize); |
723 | 933k | ci->u.l.savedpc = p->code; /* starting point */ |
724 | 1.00M | for (; narg < nfixparams; narg++) |
725 | 533k | setnilvalue(s2v(L->top.p++)); /* complete missing arguments */ |
726 | 933k | lua_assert(ci->top.p <= L->stack_last.p); |
727 | 933k | return ci; |
728 | 933k | } |
729 | 13 | default: { /* not a function */ |
730 | 13 | checkstackp(L, 1, func); /* space for metamethod */ |
731 | 13 | status = tryfuncTM(L, func, status); /* try '__call' metamethod */ |
732 | 13 | goto retry; /* try again with metamethod */ |
733 | 933k | } |
734 | 467k | } |
735 | 467k | } |
736 | | |
737 | | |
738 | | /* |
739 | | ** Call a function (C or Lua) through C. 'inc' can be 1 (increment |
740 | | ** number of recursive invocations in the C stack) or nyci (the same |
741 | | ** plus increment number of non-yieldable calls). |
742 | | ** This function can be called with some use of EXTRA_STACK, so it should |
743 | | ** check the stack before doing anything else. 'luaD_precall' already |
744 | | ** does that. |
745 | | */ |
746 | 448 | l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) { |
747 | 448 | CallInfo *ci; |
748 | 448 | L->nCcalls += inc; |
749 | 448 | if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { |
750 | 0 | checkstackp(L, 0, func); /* free any use of EXTRA_STACK */ |
751 | 0 | luaE_checkcstack(L); |
752 | 0 | } |
753 | 448 | if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ |
754 | 154 | ci->callstatus |= CIST_FRESH; /* mark that it is a "fresh" execute */ |
755 | 154 | luaV_execute(L, ci); /* call it */ |
756 | 154 | } |
757 | 448 | L->nCcalls -= inc; |
758 | 448 | } |
759 | | |
760 | | |
761 | | /* |
762 | | ** External interface for 'ccall' |
763 | | */ |
764 | 0 | void luaD_call (lua_State *L, StkId func, int nResults) { |
765 | 0 | ccall(L, func, nResults, 1); |
766 | 0 | } |
767 | | |
768 | | |
769 | | /* |
770 | | ** Similar to 'luaD_call', but does not allow yields during the call. |
771 | | */ |
772 | 448 | void luaD_callnoyield (lua_State *L, StkId func, int nResults) { |
773 | 448 | ccall(L, func, nResults, nyci); |
774 | 448 | } |
775 | | |
776 | | |
777 | | /* |
778 | | ** Finish the job of 'lua_pcallk' after it was interrupted by an yield. |
779 | | ** (The caller, 'finishCcall', does the final call to 'adjustresults'.) |
780 | | ** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'. |
781 | | ** If a '__close' method yields here, eventually control will be back |
782 | | ** to 'finishCcall' (when that '__close' method finally returns) and |
783 | | ** 'finishpcallk' will run again and close any still pending '__close' |
784 | | ** methods. Similarly, if a '__close' method errs, 'precover' calls |
785 | | ** 'unroll' which calls ''finishCcall' and we are back here again, to |
786 | | ** close any pending '__close' methods. |
787 | | ** Note that, up to the call to 'luaF_close', the corresponding |
788 | | ** 'CallInfo' is not modified, so that this repeated run works like the |
789 | | ** first one (except that it has at least one less '__close' to do). In |
790 | | ** particular, field CIST_RECST preserves the error status across these |
791 | | ** multiple runs, changing only if there is a new error. |
792 | | */ |
793 | 0 | static TStatus finishpcallk (lua_State *L, CallInfo *ci) { |
794 | 0 | TStatus status = getcistrecst(ci); /* get original status */ |
795 | 0 | if (l_likely(status == LUA_OK)) /* no error? */ |
796 | 0 | status = LUA_YIELD; /* was interrupted by an yield */ |
797 | 0 | else { /* error */ |
798 | 0 | StkId func = restorestack(L, ci->u2.funcidx); |
799 | 0 | L->allowhook = getoah(ci); /* restore 'allowhook' */ |
800 | 0 | func = luaF_close(L, func, status, 1); /* can yield or raise an error */ |
801 | 0 | luaD_seterrorobj(L, status, func); |
802 | 0 | luaD_shrinkstack(L); /* restore stack size in case of overflow */ |
803 | 0 | setcistrecst(ci, LUA_OK); /* clear original status */ |
804 | 0 | } |
805 | 0 | ci->callstatus &= ~CIST_YPCALL; |
806 | 0 | L->errfunc = ci->u.c.old_errfunc; |
807 | | /* if it is here, there were errors or yields; unlike 'lua_pcallk', |
808 | | do not change status */ |
809 | 0 | return status; |
810 | 0 | } |
811 | | |
812 | | |
813 | | /* |
814 | | ** Completes the execution of a C function interrupted by an yield. |
815 | | ** The interruption must have happened while the function was either |
816 | | ** closing its tbc variables in 'moveresults' or executing |
817 | | ** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes |
818 | | ** 'luaD_poscall'. In the second case, the call to 'finishpcallk' |
819 | | ** finishes the interrupted execution of 'lua_pcallk'. After that, it |
820 | | ** calls the continuation of the interrupted function and finally it |
821 | | ** completes the job of the 'luaD_call' that called the function. In |
822 | | ** the call to 'adjustresults', we do not know the number of results |
823 | | ** of the function called by 'lua_callk'/'lua_pcallk', so we are |
824 | | ** conservative and use LUA_MULTRET (always adjust). |
825 | | */ |
826 | 0 | static void finishCcall (lua_State *L, CallInfo *ci) { |
827 | 0 | int n; /* actual number of results from C function */ |
828 | 0 | if (ci->callstatus & CIST_CLSRET) { /* was closing TBC variable? */ |
829 | 0 | lua_assert(ci->callstatus & CIST_TBC); |
830 | 0 | n = ci->u2.nres; /* just redo 'luaD_poscall' */ |
831 | | /* don't need to reset CIST_CLSRET, as it will be set again anyway */ |
832 | 0 | } |
833 | 0 | else { |
834 | 0 | TStatus status = LUA_YIELD; /* default if there were no errors */ |
835 | 0 | lua_KFunction kf = ci->u.c.k; /* continuation function */ |
836 | | /* must have a continuation and must be able to call it */ |
837 | 0 | lua_assert(kf != NULL && yieldable(L)); |
838 | 0 | if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ |
839 | 0 | status = finishpcallk(L, ci); /* finish it */ |
840 | 0 | adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ |
841 | 0 | lua_unlock(L); |
842 | 0 | n = (*kf)(L, APIstatus(status), ci->u.c.ctx); /* call continuation */ |
843 | 0 | lua_lock(L); |
844 | 0 | api_checknelems(L, n); |
845 | 0 | } |
846 | 0 | luaD_poscall(L, ci, n); /* finish 'luaD_call' */ |
847 | 0 | } |
848 | | |
849 | | |
850 | | /* |
851 | | ** Executes "full continuation" (everything in the stack) of a |
852 | | ** previously interrupted coroutine until the stack is empty (or another |
853 | | ** interruption long-jumps out of the loop). |
854 | | */ |
855 | 0 | static void unroll (lua_State *L, void *ud) { |
856 | 0 | CallInfo *ci; |
857 | 0 | UNUSED(ud); |
858 | 0 | while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ |
859 | 0 | if (!isLua(ci)) /* C function? */ |
860 | 0 | finishCcall(L, ci); /* complete its execution */ |
861 | 0 | else { /* Lua function */ |
862 | 0 | luaV_finishOp(L); /* finish interrupted instruction */ |
863 | 0 | luaV_execute(L, ci); /* execute down to higher C 'boundary' */ |
864 | 0 | } |
865 | 0 | } |
866 | 0 | } |
867 | | |
868 | | |
869 | | /* |
870 | | ** Try to find a suspended protected call (a "recover point") for the |
871 | | ** given thread. |
872 | | */ |
873 | 0 | static CallInfo *findpcall (lua_State *L) { |
874 | 0 | CallInfo *ci; |
875 | 0 | for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ |
876 | 0 | if (ci->callstatus & CIST_YPCALL) |
877 | 0 | return ci; |
878 | 0 | } |
879 | 0 | return NULL; /* no pending pcall */ |
880 | 0 | } |
881 | | |
882 | | |
883 | | /* |
884 | | ** Signal an error in the call to 'lua_resume', not in the execution |
885 | | ** of the coroutine itself. (Such errors should not be handled by any |
886 | | ** coroutine error handler and should not kill the coroutine.) |
887 | | */ |
888 | 0 | static int resume_error (lua_State *L, const char *msg, int narg) { |
889 | 0 | api_checkpop(L, narg); |
890 | 0 | L->top.p -= narg; /* remove args from the stack */ |
891 | 0 | setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */ |
892 | 0 | api_incr_top(L); |
893 | 0 | lua_unlock(L); |
894 | 0 | return LUA_ERRRUN; |
895 | 0 | } |
896 | | |
897 | | |
898 | | /* |
899 | | ** Do the work for 'lua_resume' in protected mode. Most of the work |
900 | | ** depends on the status of the coroutine: initial state, suspended |
901 | | ** inside a hook, or regularly suspended (optionally with a continuation |
902 | | ** function), plus erroneous cases: non-suspended coroutine or dead |
903 | | ** coroutine. |
904 | | */ |
905 | 0 | static void resume (lua_State *L, void *ud) { |
906 | 0 | int n = *(cast(int*, ud)); /* number of arguments */ |
907 | 0 | StkId firstArg = L->top.p - n; /* first argument */ |
908 | 0 | CallInfo *ci = L->ci; |
909 | 0 | if (L->status == LUA_OK) /* starting a coroutine? */ |
910 | 0 | ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ |
911 | 0 | else { /* resuming from previous yield */ |
912 | 0 | lua_assert(L->status == LUA_YIELD); |
913 | 0 | L->status = LUA_OK; /* mark that it is running (again) */ |
914 | 0 | if (isLua(ci)) { /* yielded inside a hook? */ |
915 | | /* undo increment made by 'luaG_traceexec': instruction was not |
916 | | executed yet */ |
917 | 0 | lua_assert(ci->callstatus & CIST_HOOKYIELD); |
918 | 0 | ci->u.l.savedpc--; |
919 | 0 | L->top.p = firstArg; /* discard arguments */ |
920 | 0 | luaV_execute(L, ci); /* just continue running Lua code */ |
921 | 0 | } |
922 | 0 | else { /* 'common' yield */ |
923 | 0 | if (ci->u.c.k != NULL) { /* does it have a continuation function? */ |
924 | 0 | lua_unlock(L); |
925 | 0 | n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ |
926 | 0 | lua_lock(L); |
927 | 0 | api_checknelems(L, n); |
928 | 0 | } |
929 | 0 | luaD_poscall(L, ci, n); /* finish 'luaD_call' */ |
930 | 0 | } |
931 | 0 | unroll(L, NULL); /* run continuation */ |
932 | 0 | } |
933 | 0 | } |
934 | | |
935 | | |
936 | | /* |
937 | | ** Unrolls a coroutine in protected mode while there are recoverable |
938 | | ** errors, that is, errors inside a protected call. (Any error |
939 | | ** interrupts 'unroll', and this loop protects it again so it can |
940 | | ** continue.) Stops with a normal end (status == LUA_OK), an yield |
941 | | ** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't |
942 | | ** find a recover point). |
943 | | */ |
944 | 0 | static TStatus precover (lua_State *L, TStatus status) { |
945 | 0 | CallInfo *ci; |
946 | 0 | while (errorstatus(status) && (ci = findpcall(L)) != NULL) { |
947 | 0 | L->ci = ci; /* go down to recovery functions */ |
948 | 0 | setcistrecst(ci, status); /* status to finish 'pcall' */ |
949 | 0 | status = luaD_rawrunprotected(L, unroll, NULL); |
950 | 0 | } |
951 | 0 | return status; |
952 | 0 | } |
953 | | |
954 | | |
955 | | LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, |
956 | 0 | int *nresults) { |
957 | 0 | TStatus status; |
958 | 0 | lua_lock(L); |
959 | 0 | if (L->status == LUA_OK) { /* may be starting a coroutine */ |
960 | 0 | if (L->ci != &L->base_ci) /* not in base level? */ |
961 | 0 | return resume_error(L, "cannot resume non-suspended coroutine", nargs); |
962 | 0 | else if (L->top.p - (L->ci->func.p + 1) == nargs) /* no function? */ |
963 | 0 | return resume_error(L, "cannot resume dead coroutine", nargs); |
964 | 0 | } |
965 | 0 | else if (L->status != LUA_YIELD) /* ended with errors? */ |
966 | 0 | return resume_error(L, "cannot resume dead coroutine", nargs); |
967 | 0 | L->nCcalls = (from) ? getCcalls(from) : 0; |
968 | 0 | if (getCcalls(L) >= LUAI_MAXCCALLS) |
969 | 0 | return resume_error(L, "C stack overflow", nargs); |
970 | 0 | L->nCcalls++; |
971 | 0 | luai_userstateresume(L, nargs); |
972 | 0 | api_checkpop(L, (L->status == LUA_OK) ? nargs + 1 : nargs); |
973 | 0 | status = luaD_rawrunprotected(L, resume, &nargs); |
974 | | /* continue running after recoverable errors */ |
975 | 0 | status = precover(L, status); |
976 | 0 | if (l_likely(!errorstatus(status))) |
977 | 0 | lua_assert(status == L->status); /* normal end or yield */ |
978 | 0 | else { /* unrecoverable error */ |
979 | 0 | L->status = status; /* mark thread as 'dead' */ |
980 | 0 | luaD_seterrorobj(L, status, L->top.p); /* push error message */ |
981 | 0 | L->ci->top.p = L->top.p; |
982 | 0 | } |
983 | 0 | *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield |
984 | 0 | : cast_int(L->top.p - (L->ci->func.p + 1)); |
985 | 0 | lua_unlock(L); |
986 | 0 | return APIstatus(status); |
987 | 0 | } |
988 | | |
989 | | |
990 | 0 | LUA_API int lua_isyieldable (lua_State *L) { |
991 | 0 | return yieldable(L); |
992 | 0 | } |
993 | | |
994 | | |
995 | | LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, |
996 | 0 | lua_KFunction k) { |
997 | 0 | CallInfo *ci; |
998 | 0 | luai_userstateyield(L, nresults); |
999 | 0 | lua_lock(L); |
1000 | 0 | ci = L->ci; |
1001 | 0 | api_checkpop(L, nresults); |
1002 | 0 | if (l_unlikely(!yieldable(L))) { |
1003 | 0 | if (L != mainthread(G(L))) |
1004 | 0 | luaG_runerror(L, "attempt to yield across a C-call boundary"); |
1005 | 0 | else |
1006 | 0 | luaG_runerror(L, "attempt to yield from outside a coroutine"); |
1007 | 0 | } |
1008 | 0 | L->status = LUA_YIELD; |
1009 | 0 | ci->u2.nyield = nresults; /* save number of results */ |
1010 | 0 | if (isLua(ci)) { /* inside a hook? */ |
1011 | 0 | lua_assert(!isLuacode(ci)); |
1012 | 0 | api_check(L, nresults == 0, "hooks cannot yield values"); |
1013 | 0 | api_check(L, k == NULL, "hooks cannot continue after yielding"); |
1014 | 0 | } |
1015 | 0 | else { |
1016 | 0 | if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ |
1017 | 0 | ci->u.c.ctx = ctx; /* save context */ |
1018 | 0 | luaD_throw(L, LUA_YIELD); |
1019 | 0 | } |
1020 | 0 | lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ |
1021 | 0 | lua_unlock(L); |
1022 | 0 | return 0; /* return to 'luaD_hook' */ |
1023 | 0 | } |
1024 | | |
1025 | | |
1026 | | /* |
1027 | | ** Auxiliary structure to call 'luaF_close' in protected mode. |
1028 | | */ |
1029 | | struct CloseP { |
1030 | | StkId level; |
1031 | | TStatus status; |
1032 | | }; |
1033 | | |
1034 | | |
1035 | | /* |
1036 | | ** Auxiliary function to call 'luaF_close' in protected mode. |
1037 | | */ |
1038 | 910 | static void closepaux (lua_State *L, void *ud) { |
1039 | 910 | struct CloseP *pcl = cast(struct CloseP *, ud); |
1040 | 910 | luaF_close(L, pcl->level, pcl->status, 0); |
1041 | 910 | } |
1042 | | |
1043 | | |
1044 | | /* |
1045 | | ** Calls 'luaF_close' in protected mode. Return the original status |
1046 | | ** or, in case of errors, the new status. |
1047 | | */ |
1048 | 910 | TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) { |
1049 | 910 | CallInfo *old_ci = L->ci; |
1050 | 910 | lu_byte old_allowhooks = L->allowhook; |
1051 | 910 | for (;;) { /* keep closing upvalues until no more errors */ |
1052 | 910 | struct CloseP pcl; |
1053 | 910 | pcl.level = restorestack(L, level); pcl.status = status; |
1054 | 910 | status = luaD_rawrunprotected(L, &closepaux, &pcl); |
1055 | 910 | if (l_likely(status == LUA_OK)) /* no more errors? */ |
1056 | 910 | return pcl.status; |
1057 | 0 | else { /* an error occurred; restore saved state and repeat */ |
1058 | 0 | L->ci = old_ci; |
1059 | 0 | L->allowhook = old_allowhooks; |
1060 | 0 | } |
1061 | 910 | } |
1062 | 910 | } |
1063 | | |
1064 | | |
1065 | | /* |
1066 | | ** Call the C function 'func' in protected mode, restoring basic |
1067 | | ** thread information ('allowhook', etc.) and in particular |
1068 | | ** its stack level in case of errors. |
1069 | | */ |
1070 | | TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, |
1071 | 681 | ptrdiff_t ef) { |
1072 | 681 | TStatus status; |
1073 | 681 | CallInfo *old_ci = L->ci; |
1074 | 681 | lu_byte old_allowhooks = L->allowhook; |
1075 | 681 | ptrdiff_t old_errfunc = L->errfunc; |
1076 | 681 | L->errfunc = ef; |
1077 | 681 | status = luaD_rawrunprotected(L, func, u); |
1078 | 681 | if (l_unlikely(status != LUA_OK)) { /* an error occurred? */ |
1079 | 454 | L->ci = old_ci; |
1080 | 454 | L->allowhook = old_allowhooks; |
1081 | 454 | status = luaD_closeprotected(L, old_top, status); |
1082 | 454 | luaD_seterrorobj(L, status, restorestack(L, old_top)); |
1083 | 454 | luaD_shrinkstack(L); /* restore stack size in case of overflow */ |
1084 | 454 | } |
1085 | 681 | L->errfunc = old_errfunc; |
1086 | 681 | return status; |
1087 | 681 | } |
1088 | | |
1089 | | |
1090 | | |
1091 | | /* |
1092 | | ** Execute a protected parser. |
1093 | | */ |
1094 | | struct SParser { /* data to 'f_parser' */ |
1095 | | ZIO *z; |
1096 | | Mbuffer buff; /* dynamic structure used by the scanner */ |
1097 | | Dyndata dyd; /* dynamic structures used by the parser */ |
1098 | | const char *mode; |
1099 | | const char *name; |
1100 | | }; |
1101 | | |
1102 | | |
1103 | 456 | static void checkmode (lua_State *L, const char *mode, const char *x) { |
1104 | 456 | if (strchr(mode, x[0]) == NULL) { |
1105 | 0 | luaO_pushfstring(L, |
1106 | 0 | "attempt to load a %s chunk (mode is '%s')", x, mode); |
1107 | 0 | luaD_throw(L, LUA_ERRSYNTAX); |
1108 | 0 | } |
1109 | 456 | } |
1110 | | |
1111 | | |
1112 | 456 | static void f_parser (lua_State *L, void *ud) { |
1113 | 456 | LClosure *cl; |
1114 | 456 | struct SParser *p = cast(struct SParser *, ud); |
1115 | 456 | const char *mode = p->mode ? p->mode : "bt"; |
1116 | 456 | int c = zgetc(p->z); /* read first character */ |
1117 | 456 | if (c == LUA_SIGNATURE[0]) { |
1118 | 0 | int fixed = 0; |
1119 | 0 | if (strchr(mode, 'B') != NULL) |
1120 | 0 | fixed = 1; |
1121 | 0 | else |
1122 | 0 | checkmode(L, mode, "binary"); |
1123 | 0 | cl = luaU_undump(L, p->z, p->name, fixed); |
1124 | 0 | } |
1125 | 456 | else { |
1126 | 456 | checkmode(L, mode, "text"); |
1127 | 456 | cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); |
1128 | 456 | } |
1129 | 456 | lua_assert(cl->nupvalues == cl->p->sizeupvalues); |
1130 | 456 | luaF_initupvals(L, cl); |
1131 | 154 | } |
1132 | | |
1133 | | |
1134 | | TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name, |
1135 | 456 | const char *mode) { |
1136 | 456 | struct SParser p; |
1137 | 456 | TStatus status; |
1138 | 456 | incnny(L); /* cannot yield during parsing */ |
1139 | 456 | p.z = z; p.name = name; p.mode = mode; |
1140 | 456 | p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; |
1141 | 456 | p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; |
1142 | 456 | p.dyd.label.arr = NULL; p.dyd.label.size = 0; |
1143 | 456 | luaZ_initbuffer(L, &p.buff); |
1144 | 456 | status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc); |
1145 | 456 | luaZ_freebuffer(L, &p.buff); |
1146 | 456 | luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size)); |
1147 | 456 | luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size)); |
1148 | 456 | luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size)); |
1149 | 456 | decnny(L); |
1150 | 456 | return status; |
1151 | 456 | } |
1152 | | |
1153 | | |