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