Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5205 | clevermous | 1 | /* |
2 | ** $Id: ldo.c,v 2.102 2011/11/29 15:55:08 roberto Exp $ |
||
3 | ** Stack and Call structure of Lua |
||
4 | ** See Copyright Notice in lua.h |
||
5 | */ |
||
6 | |||
7 | |||
8 | #include |
||
9 | #include |
||
10 | #include |
||
11 | |||
12 | #define ldo_c |
||
13 | #define LUA_CORE |
||
14 | |||
15 | #include "lua.h" |
||
16 | |||
17 | #include "lapi.h" |
||
18 | #include "ldebug.h" |
||
19 | #include "ldo.h" |
||
20 | #include "lfunc.h" |
||
21 | #include "lgc.h" |
||
22 | #include "lmem.h" |
||
23 | #include "lobject.h" |
||
24 | #include "lopcodes.h" |
||
25 | #include "lparser.h" |
||
26 | #include "lstate.h" |
||
27 | #include "lstring.h" |
||
28 | #include "ltable.h" |
||
29 | #include "ltm.h" |
||
30 | #include "lundump.h" |
||
31 | #include "lvm.h" |
||
32 | #include "lzio.h" |
||
33 | |||
34 | |||
35 | |||
36 | |||
37 | /* |
||
38 | ** {====================================================== |
||
39 | ** Error-recovery functions |
||
40 | ** ======================================================= |
||
41 | */ |
||
42 | |||
43 | /* |
||
44 | ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By |
||
45 | ** default, Lua handles errors with exceptions when compiling as |
||
46 | ** C++ code, with _longjmp/_setjmp when asked to use them, and with |
||
47 | ** longjmp/setjmp otherwise. |
||
48 | */ |
||
49 | #if !defined(LUAI_THROW) |
||
50 | |||
51 | #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP) |
||
52 | /* C++ exceptions */ |
||
53 | #define LUAI_THROW(L,c) throw(c) |
||
54 | #define LUAI_TRY(L,c,a) \ |
||
55 | try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } |
||
56 | #define luai_jmpbuf int /* dummy variable */ |
||
57 | |||
58 | #elif defined(LUA_USE_ULONGJMP) |
||
59 | /* in Unix, try _longjmp/_setjmp (more efficient) */ |
||
60 | #define LUAI_THROW(L,c) _longjmp((c)->b, 1) |
||
61 | #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } |
||
62 | #define luai_jmpbuf jmp_buf |
||
63 | |||
64 | #else |
||
65 | /* default handling with long jumps */ |
||
66 | #define LUAI_THROW(L,c) longjmp((c)->b, 1) |
||
67 | #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } |
||
68 | #define luai_jmpbuf jmp_buf |
||
69 | |||
70 | #endif |
||
71 | |||
72 | #endif |
||
73 | |||
74 | |||
75 | |||
76 | /* chain list of long jump buffers */ |
||
77 | struct lua_longjmp { |
||
78 | struct lua_longjmp *previous; |
||
79 | luai_jmpbuf b; |
||
80 | volatile int status; /* error code */ |
||
81 | }; |
||
82 | |||
83 | |||
84 | static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { |
||
85 | switch (errcode) { |
||
86 | case LUA_ERRMEM: { /* memory error? */ |
||
87 | setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ |
||
88 | break; |
||
89 | } |
||
90 | case LUA_ERRERR: { |
||
91 | setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); |
||
92 | break; |
||
93 | } |
||
94 | default: { |
||
95 | setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ |
||
96 | break; |
||
97 | } |
||
98 | } |
||
99 | L->top = oldtop + 1; |
||
100 | } |
||
101 | |||
102 | |||
103 | l_noret luaD_throw (lua_State *L, int errcode) { |
||
104 | if (L->errorJmp) { /* thread has an error handler? */ |
||
105 | L->errorJmp->status = errcode; /* set status */ |
||
106 | LUAI_THROW(L, L->errorJmp); /* jump to it */ |
||
107 | } |
||
108 | else { /* thread has no error handler */ |
||
109 | L->status = cast_byte(errcode); /* mark it as dead */ |
||
110 | if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ |
||
111 | setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ |
||
112 | luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ |
||
113 | } |
||
114 | else { /* no handler at all; abort */ |
||
115 | if (G(L)->panic) { /* panic function? */ |
||
116 | lua_unlock(L); |
||
117 | G(L)->panic(L); /* call it (last chance to jump out) */ |
||
118 | } |
||
119 | abort(); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | |||
125 | int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { |
||
126 | unsigned short oldnCcalls = L->nCcalls; |
||
127 | struct lua_longjmp lj; |
||
128 | lj.status = LUA_OK; |
||
129 | lj.previous = L->errorJmp; /* chain new error handler */ |
||
130 | L->errorJmp = &lj; |
||
131 | LUAI_TRY(L, &lj, |
||
132 | (*f)(L, ud); |
||
133 | ); |
||
134 | L->errorJmp = lj.previous; /* restore old error handler */ |
||
135 | L->nCcalls = oldnCcalls; |
||
136 | return lj.status; |
||
137 | } |
||
138 | |||
139 | /* }====================================================== */ |
||
140 | |||
141 | |||
142 | static void correctstack (lua_State *L, TValue *oldstack) { |
||
143 | CallInfo *ci; |
||
144 | GCObject *up; |
||
145 | L->top = (L->top - oldstack) + L->stack; |
||
146 | for (up = L->openupval; up != NULL; up = up->gch.next) |
||
147 | gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; |
||
148 | for (ci = L->ci; ci != NULL; ci = ci->previous) { |
||
149 | ci->top = (ci->top - oldstack) + L->stack; |
||
150 | ci->func = (ci->func - oldstack) + L->stack; |
||
151 | if (isLua(ci)) |
||
152 | ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | |||
157 | /* some space for error handling */ |
||
158 | #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) |
||
159 | |||
160 | |||
161 | void luaD_reallocstack (lua_State *L, int newsize) { |
||
162 | TValue *oldstack = L->stack; |
||
163 | int lim = L->stacksize; |
||
164 | lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); |
||
165 | lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); |
||
166 | luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); |
||
167 | for (; lim < newsize; lim++) |
||
168 | setnilvalue(L->stack + lim); /* erase new segment */ |
||
169 | L->stacksize = newsize; |
||
170 | L->stack_last = L->stack + newsize - EXTRA_STACK; |
||
171 | correctstack(L, oldstack); |
||
172 | } |
||
173 | |||
174 | |||
175 | void luaD_growstack (lua_State *L, int n) { |
||
176 | int size = L->stacksize; |
||
177 | if (size > LUAI_MAXSTACK) /* error after extra size? */ |
||
178 | luaD_throw(L, LUA_ERRERR); |
||
179 | else { |
||
180 | int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; |
||
181 | int newsize = 2 * size; |
||
182 | if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; |
||
183 | if (newsize < needed) newsize = needed; |
||
184 | if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ |
||
185 | luaD_reallocstack(L, ERRORSTACKSIZE); |
||
186 | luaG_runerror(L, "stack overflow"); |
||
187 | } |
||
188 | else |
||
189 | luaD_reallocstack(L, newsize); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | |||
194 | static int stackinuse (lua_State *L) { |
||
195 | CallInfo *ci; |
||
196 | StkId lim = L->top; |
||
197 | for (ci = L->ci; ci != NULL; ci = ci->previous) { |
||
198 | lua_assert(ci->top <= L->stack_last); |
||
199 | if (lim < ci->top) lim = ci->top; |
||
200 | } |
||
201 | return cast_int(lim - L->stack) + 1; /* part of stack in use */ |
||
202 | } |
||
203 | |||
204 | |||
205 | void luaD_shrinkstack (lua_State *L) { |
||
206 | int inuse = stackinuse(L); |
||
207 | int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; |
||
208 | if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; |
||
209 | if (inuse > LUAI_MAXSTACK || /* handling stack overflow? */ |
||
210 | goodsize >= L->stacksize) /* would grow instead of shrink? */ |
||
211 | condmovestack(L); /* don't change stack (change only for debugging) */ |
||
212 | else |
||
213 | luaD_reallocstack(L, goodsize); /* shrink it */ |
||
214 | } |
||
215 | |||
216 | |||
217 | void luaD_hook (lua_State *L, int event, int line) { |
||
218 | lua_Hook hook = L->hook; |
||
219 | if (hook && L->allowhook) { |
||
220 | CallInfo *ci = L->ci; |
||
221 | ptrdiff_t top = savestack(L, L->top); |
||
222 | ptrdiff_t ci_top = savestack(L, ci->top); |
||
223 | lua_Debug ar; |
||
224 | ar.event = event; |
||
225 | ar.currentline = line; |
||
226 | ar.i_ci = ci; |
||
227 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
||
228 | ci->top = L->top + LUA_MINSTACK; |
||
229 | lua_assert(ci->top <= L->stack_last); |
||
230 | L->allowhook = 0; /* cannot call hooks inside a hook */ |
||
231 | ci->callstatus |= CIST_HOOKED; |
||
232 | lua_unlock(L); |
||
233 | (*hook)(L, &ar); |
||
234 | lua_lock(L); |
||
235 | lua_assert(!L->allowhook); |
||
236 | L->allowhook = 1; |
||
237 | ci->top = restorestack(L, ci_top); |
||
238 | L->top = restorestack(L, top); |
||
239 | ci->callstatus &= ~CIST_HOOKED; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | |||
244 | static void callhook (lua_State *L, CallInfo *ci) { |
||
245 | int hook = LUA_HOOKCALL; |
||
246 | ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ |
||
247 | if (isLua(ci->previous) && |
||
248 | GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { |
||
249 | ci->callstatus |= CIST_TAIL; |
||
250 | hook = LUA_HOOKTAILCALL; |
||
251 | } |
||
252 | luaD_hook(L, hook, -1); |
||
253 | ci->u.l.savedpc--; /* correct 'pc' */ |
||
254 | } |
||
255 | |||
256 | |||
257 | static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { |
||
258 | int i; |
||
259 | int nfixargs = p->numparams; |
||
260 | StkId base, fixed; |
||
261 | lua_assert(actual >= nfixargs); |
||
262 | /* move fixed parameters to final position */ |
||
263 | fixed = L->top - actual; /* first fixed argument */ |
||
264 | base = L->top; /* final position of first argument */ |
||
265 | for (i=0; i |
||
266 | setobjs2s(L, L->top++, fixed + i); |
||
267 | setnilvalue(fixed + i); |
||
268 | } |
||
269 | return base; |
||
270 | } |
||
271 | |||
272 | |||
273 | static StkId tryfuncTM (lua_State *L, StkId func) { |
||
274 | const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); |
||
275 | StkId p; |
||
276 | ptrdiff_t funcr = savestack(L, func); |
||
277 | if (!ttisfunction(tm)) |
||
278 | luaG_typeerror(L, func, "call"); |
||
279 | /* Open a hole inside the stack at `func' */ |
||
280 | for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); |
||
281 | incr_top(L); |
||
282 | func = restorestack(L, funcr); /* previous call may change stack */ |
||
283 | setobj2s(L, func, tm); /* tag method is the new function to be called */ |
||
284 | return func; |
||
285 | } |
||
286 | |||
287 | |||
288 | |||
289 | #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) |
||
290 | |||
291 | |||
292 | /* |
||
293 | ** returns true if function has been executed (C function) |
||
294 | */ |
||
295 | int luaD_precall (lua_State *L, StkId func, int nresults) { |
||
296 | lua_CFunction f; |
||
297 | CallInfo *ci; |
||
298 | int n; /* number of arguments (Lua) or returns (C) */ |
||
299 | ptrdiff_t funcr = savestack(L, func); |
||
300 | switch (ttype(func)) { |
||
301 | case LUA_TLCF: /* light C function */ |
||
302 | f = fvalue(func); |
||
303 | goto Cfunc; |
||
304 | case LUA_TCCL: { /* C closure */ |
||
305 | f = clCvalue(func)->f; |
||
306 | Cfunc: |
||
307 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
||
308 | ci = next_ci(L); /* now 'enter' new function */ |
||
309 | ci->nresults = nresults; |
||
310 | ci->func = restorestack(L, funcr); |
||
311 | ci->top = L->top + LUA_MINSTACK; |
||
312 | lua_assert(ci->top <= L->stack_last); |
||
313 | ci->callstatus = 0; |
||
314 | if (L->hookmask & LUA_MASKCALL) |
||
315 | luaD_hook(L, LUA_HOOKCALL, -1); |
||
316 | lua_unlock(L); |
||
317 | n = (*f)(L); /* do the actual call */ |
||
318 | lua_lock(L); |
||
319 | api_checknelems(L, n); |
||
320 | luaD_poscall(L, L->top - n); |
||
321 | return 1; |
||
322 | } |
||
323 | case LUA_TLCL: { /* Lua function: prepare its call */ |
||
324 | StkId base; |
||
325 | Proto *p = clLvalue(func)->p; |
||
326 | luaD_checkstack(L, p->maxstacksize); |
||
327 | func = restorestack(L, funcr); |
||
328 | n = cast_int(L->top - func) - 1; /* number of real arguments */ |
||
329 | for (; n < p->numparams; n++) |
||
330 | setnilvalue(L->top++); /* complete missing arguments */ |
||
331 | base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n); |
||
332 | ci = next_ci(L); /* now 'enter' new function */ |
||
333 | ci->nresults = nresults; |
||
334 | ci->func = func; |
||
335 | ci->u.l.base = base; |
||
336 | ci->top = base + p->maxstacksize; |
||
337 | lua_assert(ci->top <= L->stack_last); |
||
338 | ci->u.l.savedpc = p->code; /* starting point */ |
||
339 | ci->callstatus = CIST_LUA; |
||
340 | L->top = ci->top; |
||
341 | if (L->hookmask & LUA_MASKCALL) |
||
342 | callhook(L, ci); |
||
343 | return 0; |
||
344 | } |
||
345 | default: { /* not a function */ |
||
346 | func = tryfuncTM(L, func); /* retry with 'function' tag method */ |
||
347 | return luaD_precall(L, func, nresults); /* now it must be a function */ |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | |||
353 | int luaD_poscall (lua_State *L, StkId firstResult) { |
||
354 | StkId res; |
||
355 | int wanted, i; |
||
356 | CallInfo *ci = L->ci; |
||
357 | if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { |
||
358 | if (L->hookmask & LUA_MASKRET) { |
||
359 | ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ |
||
360 | luaD_hook(L, LUA_HOOKRET, -1); |
||
361 | firstResult = restorestack(L, fr); |
||
362 | } |
||
363 | L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ |
||
364 | } |
||
365 | res = ci->func; /* res == final position of 1st result */ |
||
366 | wanted = ci->nresults; |
||
367 | L->ci = ci = ci->previous; /* back to caller */ |
||
368 | /* move results to correct place */ |
||
369 | for (i = wanted; i != 0 && firstResult < L->top; i--) |
||
370 | setobjs2s(L, res++, firstResult++); |
||
371 | while (i-- > 0) |
||
372 | setnilvalue(res++); |
||
373 | L->top = res; |
||
374 | return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ |
||
375 | } |
||
376 | |||
377 | |||
378 | /* |
||
379 | ** Call a function (C or Lua). The function to be called is at *func. |
||
380 | ** The arguments are on the stack, right after the function. |
||
381 | ** When returns, all the results are on the stack, starting at the original |
||
382 | ** function position. |
||
383 | */ |
||
384 | void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { |
||
385 | if (++L->nCcalls >= LUAI_MAXCCALLS) { |
||
386 | if (L->nCcalls == LUAI_MAXCCALLS) |
||
387 | luaG_runerror(L, "C stack overflow"); |
||
388 | else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) |
||
389 | luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ |
||
390 | } |
||
391 | if (!allowyield) L->nny++; |
||
392 | if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ |
||
393 | luaV_execute(L); /* call it */ |
||
394 | if (!allowyield) L->nny--; |
||
395 | L->nCcalls--; |
||
396 | luaC_checkGC(L); |
||
397 | } |
||
398 | |||
399 | |||
400 | static void finishCcall (lua_State *L) { |
||
401 | CallInfo *ci = L->ci; |
||
402 | int n; |
||
403 | lua_assert(ci->u.c.k != NULL); /* must have a continuation */ |
||
404 | lua_assert(L->nny == 0); |
||
405 | /* finish 'luaD_call' */ |
||
406 | L->nCcalls--; |
||
407 | /* finish 'lua_callk' */ |
||
408 | adjustresults(L, ci->nresults); |
||
409 | /* call continuation function */ |
||
410 | if (!(ci->callstatus & CIST_STAT)) /* no call status? */ |
||
411 | ci->u.c.status = LUA_YIELD; /* 'default' status */ |
||
412 | lua_assert(ci->u.c.status != LUA_OK); |
||
413 | ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED; |
||
414 | lua_unlock(L); |
||
415 | n = (*ci->u.c.k)(L); |
||
416 | lua_lock(L); |
||
417 | api_checknelems(L, n); |
||
418 | /* finish 'luaD_precall' */ |
||
419 | luaD_poscall(L, L->top - n); |
||
420 | } |
||
421 | |||
422 | |||
423 | static void unroll (lua_State *L, void *ud) { |
||
424 | UNUSED(ud); |
||
425 | for (;;) { |
||
426 | if (L->ci == &L->base_ci) /* stack is empty? */ |
||
427 | return; /* coroutine finished normally */ |
||
428 | if (!isLua(L->ci)) /* C function? */ |
||
429 | finishCcall(L); |
||
430 | else { /* Lua function */ |
||
431 | luaV_finishOp(L); /* finish interrupted instruction */ |
||
432 | luaV_execute(L); /* execute down to higher C 'boundary' */ |
||
433 | } |
||
434 | } |
||
435 | } |
||
436 | |||
437 | |||
438 | /* |
||
439 | ** check whether thread has a suspended protected call |
||
440 | */ |
||
441 | static CallInfo *findpcall (lua_State *L) { |
||
442 | CallInfo *ci; |
||
443 | for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ |
||
444 | if (ci->callstatus & CIST_YPCALL) |
||
445 | return ci; |
||
446 | } |
||
447 | return NULL; /* no pending pcall */ |
||
448 | } |
||
449 | |||
450 | |||
451 | static int recover (lua_State *L, int status) { |
||
452 | StkId oldtop; |
||
453 | CallInfo *ci = findpcall(L); |
||
454 | if (ci == NULL) return 0; /* no recovery point */ |
||
455 | /* "finish" luaD_pcall */ |
||
456 | oldtop = restorestack(L, ci->u.c.extra); |
||
457 | luaF_close(L, oldtop); |
||
458 | seterrorobj(L, status, oldtop); |
||
459 | L->ci = ci; |
||
460 | L->allowhook = ci->u.c.old_allowhook; |
||
461 | L->nny = 0; /* should be zero to be yieldable */ |
||
462 | luaD_shrinkstack(L); |
||
463 | L->errfunc = ci->u.c.old_errfunc; |
||
464 | ci->callstatus |= CIST_STAT; /* call has error status */ |
||
465 | ci->u.c.status = status; /* (here it is) */ |
||
466 | return 1; /* continue running the coroutine */ |
||
467 | } |
||
468 | |||
469 | |||
470 | /* |
||
471 | ** signal an error in the call to 'resume', not in the execution of the |
||
472 | ** coroutine itself. (Such errors should not be handled by any coroutine |
||
473 | ** error handler and should not kill the coroutine.) |
||
474 | */ |
||
475 | static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { |
||
476 | L->top = firstArg; /* remove args from the stack */ |
||
477 | setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ |
||
478 | incr_top(L); |
||
479 | luaD_throw(L, -1); /* jump back to 'lua_resume' */ |
||
480 | } |
||
481 | |||
482 | |||
483 | /* |
||
484 | ** do the work for 'lua_resume' in protected mode |
||
485 | */ |
||
486 | static void resume (lua_State *L, void *ud) { |
||
487 | StkId firstArg = cast(StkId, ud); |
||
488 | CallInfo *ci = L->ci; |
||
489 | if (L->nCcalls >= LUAI_MAXCCALLS) |
||
490 | resume_error(L, "C stack overflow", firstArg); |
||
491 | if (L->status == LUA_OK) { /* may be starting a coroutine */ |
||
492 | if (ci != &L->base_ci) /* not in base level? */ |
||
493 | resume_error(L, "cannot resume non-suspended coroutine", firstArg); |
||
494 | /* coroutine is in base level; start running it */ |
||
495 | if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ |
||
496 | luaV_execute(L); /* call it */ |
||
497 | } |
||
498 | else if (L->status != LUA_YIELD) |
||
499 | resume_error(L, "cannot resume dead coroutine", firstArg); |
||
500 | else { /* resuming from previous yield */ |
||
501 | L->status = LUA_OK; |
||
502 | if (isLua(ci)) /* yielded inside a hook? */ |
||
503 | luaV_execute(L); /* just continue running Lua code */ |
||
504 | else { /* 'common' yield */ |
||
505 | ci->func = restorestack(L, ci->u.c.extra); |
||
506 | if (ci->u.c.k != NULL) { /* does it have a continuation? */ |
||
507 | int n; |
||
508 | ci->u.c.status = LUA_YIELD; /* 'default' status */ |
||
509 | ci->callstatus |= CIST_YIELDED; |
||
510 | lua_unlock(L); |
||
511 | n = (*ci->u.c.k)(L); /* call continuation */ |
||
512 | lua_lock(L); |
||
513 | api_checknelems(L, n); |
||
514 | firstArg = L->top - n; /* yield results come from continuation */ |
||
515 | } |
||
516 | L->nCcalls--; /* finish 'luaD_call' */ |
||
517 | luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ |
||
518 | } |
||
519 | unroll(L, NULL); |
||
520 | } |
||
521 | } |
||
522 | |||
523 | |||
524 | LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { |
||
525 | int status; |
||
526 | lua_lock(L); |
||
527 | luai_userstateresume(L, nargs); |
||
528 | L->nCcalls = (from) ? from->nCcalls + 1 : 1; |
||
529 | L->nny = 0; /* allow yields */ |
||
530 | api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); |
||
531 | status = luaD_rawrunprotected(L, resume, L->top - nargs); |
||
532 | if (status == -1) /* error calling 'lua_resume'? */ |
||
533 | status = LUA_ERRRUN; |
||
534 | else { /* yield or regular error */ |
||
535 | while (status != LUA_OK && status != LUA_YIELD) { /* error? */ |
||
536 | if (recover(L, status)) /* recover point? */ |
||
537 | status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ |
||
538 | else { /* unrecoverable error */ |
||
539 | L->status = cast_byte(status); /* mark thread as `dead' */ |
||
540 | seterrorobj(L, status, L->top); |
||
541 | L->ci->top = L->top; |
||
542 | break; |
||
543 | } |
||
544 | } |
||
545 | lua_assert(status == L->status); |
||
546 | } |
||
547 | L->nny = 1; /* do not allow yields */ |
||
548 | L->nCcalls--; |
||
549 | lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); |
||
550 | lua_unlock(L); |
||
551 | return status; |
||
552 | } |
||
553 | |||
554 | |||
555 | LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { |
||
556 | CallInfo *ci = L->ci; |
||
557 | luai_userstateyield(L, nresults); |
||
558 | lua_lock(L); |
||
559 | api_checknelems(L, nresults); |
||
560 | if (L->nny > 0) { |
||
561 | if (L != G(L)->mainthread) |
||
562 | luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); |
||
563 | else |
||
564 | luaG_runerror(L, "attempt to yield from outside a coroutine"); |
||
565 | } |
||
566 | L->status = LUA_YIELD; |
||
567 | if (isLua(ci)) { /* inside a hook? */ |
||
568 | api_check(L, k == NULL, "hooks cannot continue after yielding"); |
||
569 | } |
||
570 | else { |
||
571 | if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ |
||
572 | ci->u.c.ctx = ctx; /* save context */ |
||
573 | ci->u.c.extra = savestack(L, ci->func); /* save current 'func' */ |
||
574 | ci->func = L->top - nresults - 1; /* protect stack below results */ |
||
575 | luaD_throw(L, LUA_YIELD); |
||
576 | } |
||
577 | lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ |
||
578 | lua_unlock(L); |
||
579 | return 0; /* return to 'luaD_hook' */ |
||
580 | } |
||
581 | |||
582 | |||
583 | int luaD_pcall (lua_State *L, Pfunc func, void *u, |
||
584 | ptrdiff_t old_top, ptrdiff_t ef) { |
||
585 | int status; |
||
586 | CallInfo *old_ci = L->ci; |
||
587 | lu_byte old_allowhooks = L->allowhook; |
||
588 | unsigned short old_nny = L->nny; |
||
589 | ptrdiff_t old_errfunc = L->errfunc; |
||
590 | L->errfunc = ef; |
||
591 | status = luaD_rawrunprotected(L, func, u); |
||
592 | if (status != LUA_OK) { /* an error occurred? */ |
||
593 | StkId oldtop = restorestack(L, old_top); |
||
594 | luaF_close(L, oldtop); /* close possible pending closures */ |
||
595 | seterrorobj(L, status, oldtop); |
||
596 | L->ci = old_ci; |
||
597 | L->allowhook = old_allowhooks; |
||
598 | L->nny = old_nny; |
||
599 | luaD_shrinkstack(L); |
||
600 | } |
||
601 | L->errfunc = old_errfunc; |
||
602 | return status; |
||
603 | } |
||
604 | |||
605 | |||
606 | |||
607 | /* |
||
608 | ** Execute a protected parser. |
||
609 | */ |
||
610 | struct SParser { /* data to `f_parser' */ |
||
611 | ZIO *z; |
||
612 | Mbuffer buff; /* dynamic structure used by the scanner */ |
||
613 | Dyndata dyd; /* dynamic structures used by the parser */ |
||
614 | const char *mode; |
||
615 | const char *name; |
||
616 | }; |
||
617 | |||
618 | |||
619 | static void checkmode (lua_State *L, const char *mode, const char *x) { |
||
620 | if (mode && strchr(mode, x[0]) == NULL) { |
||
621 | luaO_pushfstring(L, |
||
622 | "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode); |
||
623 | luaD_throw(L, LUA_ERRSYNTAX); |
||
624 | } |
||
625 | } |
||
626 | |||
627 | |||
628 | static void f_parser (lua_State *L, void *ud) { |
||
629 | int i; |
||
630 | Proto *tf; |
||
631 | Closure *cl; |
||
632 | struct SParser *p = cast(struct SParser *, ud); |
||
633 | int c = zgetc(p->z); /* read first character */ |
||
634 | if (c == LUA_SIGNATURE[0]) { |
||
635 | checkmode(L, p->mode, "binary"); |
||
636 | tf = luaU_undump(L, p->z, &p->buff, p->name); |
||
637 | } |
||
638 | else { |
||
639 | checkmode(L, p->mode, "text"); |
||
640 | tf = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); |
||
641 | } |
||
642 | setptvalue2s(L, L->top, tf); |
||
643 | incr_top(L); |
||
644 | cl = luaF_newLclosure(L, tf); |
||
645 | setclLvalue(L, L->top - 1, cl); |
||
646 | for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */ |
||
647 | cl->l.upvals[i] = luaF_newupval(L); |
||
648 | } |
||
649 | |||
650 | |||
651 | int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, |
||
652 | const char *mode) { |
||
653 | struct SParser p; |
||
654 | int status; |
||
655 | L->nny++; /* cannot yield during parsing */ |
||
656 | p.z = z; p.name = name; p.mode = mode; |
||
657 | p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; |
||
658 | p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; |
||
659 | p.dyd.label.arr = NULL; p.dyd.label.size = 0; |
||
660 | luaZ_initbuffer(L, &p.buff); |
||
661 | status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); |
||
662 | luaZ_freebuffer(L, &p.buff); |
||
663 | luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); |
||
664 | luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); |
||
665 | luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); |
||
666 | L->nny--; |
||
667 | return status; |
||
668 | }>>=>>=>=>>=>>>=> |
||
669 |