ldo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. ** $Id: ldo.c,v 1.171 2002/04/16 17:08:28 roberto Exp roberto $
  3. ** Stack and Call structure of Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <setjmp.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lopcodes.h"
  18. #include "lparser.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. #include "lundump.h"
  24. #include "lvm.h"
  25. #include "lzio.h"
  26. /* chain list of long jump buffers */
  27. struct lua_longjmp {
  28. struct lua_longjmp *previous;
  29. CallInfo *ci; /* index of call info of active function that set protection */
  30. StkId top; /* top stack when protection was set */
  31. jmp_buf b;
  32. int allowhooks; /* `allowhook' state when protection was set */
  33. volatile int status; /* error code */
  34. TObject err; /* function to be called in case of errors */
  35. };
  36. static void correctstack (lua_State *L, TObject *oldstack) {
  37. struct lua_longjmp *lj;
  38. CallInfo *ci;
  39. UpVal *up;
  40. L->top = (L->top - oldstack) + L->stack;
  41. for (lj = L->errorJmp; lj != NULL; lj = lj->previous)
  42. lj->top = (lj->top - oldstack) + L->stack;
  43. for (up = L->openupval; up != NULL; up = up->next)
  44. up->v = (up->v - oldstack) + L->stack;
  45. for (ci = L->base_ci; ci <= L->ci; ci++) {
  46. ci->base = (ci->base - oldstack) + L->stack;
  47. ci->top = (ci->top - oldstack) + L->stack;
  48. if (ci->pc) { /* entry is of an active Lua function? */
  49. if (ci->pc != (ci-1)->pc)
  50. *ci->pb = (*ci->pb - oldstack) + L->stack;
  51. }
  52. }
  53. }
  54. void luaD_reallocstack (lua_State *L, int newsize) {
  55. TObject *oldstack = L->stack;
  56. luaM_reallocvector(L, L->stack, L->stacksize, newsize, TObject);
  57. L->stacksize = newsize;
  58. L->stack_last = L->stack+(newsize-EXTRA_STACK)-1;
  59. correctstack(L, oldstack);
  60. }
  61. static void correctCI (lua_State *L, CallInfo *oldci) {
  62. struct lua_longjmp *lj;
  63. for (lj = L->errorJmp; lj != NULL; lj = lj->previous) {
  64. lj->ci = (lj->ci - oldci) + L->base_ci;
  65. }
  66. }
  67. void luaD_reallocCI (lua_State *L, int newsize) {
  68. CallInfo *oldci = L->base_ci;
  69. luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
  70. L->size_ci = newsize;
  71. L->ci = (L->ci - oldci) + L->base_ci;
  72. L->end_ci = L->base_ci + L->size_ci;
  73. correctCI(L, oldci);
  74. }
  75. static void restore_stack_limit (lua_State *L) {
  76. if (L->size_ci > LUA_MAXCALLS) { /* there was an overflow? */
  77. int inuse = (L->ci - L->base_ci);
  78. if (inuse + 1 < LUA_MAXCALLS) /* can `undo' overflow? */
  79. luaD_reallocCI(L, LUA_MAXCALLS);
  80. }
  81. }
  82. void luaD_growstack (lua_State *L, int n) {
  83. if (n <= L->stacksize) /* double size is enough? */
  84. luaD_reallocstack(L, 2*L->stacksize);
  85. else
  86. luaD_reallocstack(L, L->stacksize + n + EXTRA_STACK);
  87. }
  88. static void luaD_growCI (lua_State *L) {
  89. L->ci--;
  90. if (L->size_ci > LUA_MAXCALLS) /* overflow while handling overflow? */
  91. luaD_error(L, NULL, LUA_ERRERR); /* break run without error message */
  92. else {
  93. luaD_reallocCI(L, 2*L->size_ci);
  94. if (L->size_ci > LUA_MAXCALLS)
  95. luaD_runerror(L, "stack overflow");
  96. }
  97. L->ci++;
  98. }
  99. /*
  100. ** Open a hole inside the stack at `pos'
  101. */
  102. static void luaD_openstack (lua_State *L, StkId pos) {
  103. StkId p;
  104. for (p = L->top; p > pos; p--) setobj(p, p-1);
  105. incr_top(L);
  106. }
  107. static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
  108. ptrdiff_t top = savestack(L, L->top);
  109. ptrdiff_t ci_top = savestack(L, L->ci->top);
  110. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  111. L->ci->top = L->top + LUA_MINSTACK;
  112. L->allowhooks = 0; /* cannot call hooks inside a hook */
  113. lua_unlock(L);
  114. (*hook)(L, ar);
  115. lua_lock(L);
  116. lua_assert(L->allowhooks == 0);
  117. L->allowhooks = 1;
  118. L->ci->top = restorestack(L, ci_top);
  119. L->top = restorestack(L, top);
  120. }
  121. void luaD_lineHook (lua_State *L, int line) {
  122. if (L->allowhooks) {
  123. lua_Debug ar;
  124. ar.event = "line";
  125. ar.i_ci = L->ci - L->base_ci;
  126. ar.currentline = line;
  127. dohook(L, &ar, L->linehook);
  128. }
  129. }
  130. static void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event) {
  131. if (L->allowhooks) {
  132. lua_Debug ar;
  133. ar.event = event;
  134. ar.i_ci = L->ci - L->base_ci;
  135. L->ci->pc = NULL; /* function is not active */
  136. L->ci->top = L->ci->base; /* `top' may not have a valid value yet */
  137. dohook(L, &ar, callhook);
  138. }
  139. }
  140. static void adjust_varargs (lua_State *L, int nfixargs) {
  141. int i;
  142. Table *htab;
  143. TObject n, nname;
  144. int actual = L->top - L->ci->base; /* actual number of arguments */
  145. if (actual < nfixargs) {
  146. luaD_checkstack(L, nfixargs - actual);
  147. for (; actual < nfixargs; ++actual)
  148. setnilvalue(L->top++);
  149. }
  150. actual -= nfixargs; /* number of extra arguments */
  151. htab = luaH_new(L, 0, 0); /* create `arg' table */
  152. for (i=0; i<actual; i++) /* put extra arguments into `arg' table */
  153. luaH_setnum(L, htab, i+1, L->top - actual + i);
  154. /* store counter in field `n' */
  155. setnvalue(&n, actual);
  156. setsvalue(&nname, luaS_newliteral(L, "n"));
  157. luaH_set(L, htab, &nname, &n);
  158. L->top -= actual; /* remove extra elements from the stack */
  159. sethvalue(L->top, htab);
  160. incr_top(L);
  161. }
  162. static StkId tryfuncTM (lua_State *L, StkId func) {
  163. const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL);
  164. if (ttype(tm) != LUA_TFUNCTION) {
  165. L->ci--; /* undo increment (no function here) */
  166. luaG_typeerror(L, func, "call");
  167. }
  168. luaD_openstack(L, func);
  169. func = L->ci->base - 1; /* previous call may change stack */
  170. setobj(func, tm); /* tag method is the new function to be called */
  171. return func;
  172. }
  173. StkId luaD_precall (lua_State *L, StkId func) {
  174. CallInfo *ci;
  175. LClosure *cl;
  176. if (++L->ci == L->end_ci) luaD_growCI(L);
  177. ci = L->ci;
  178. ci->base = func+1;
  179. ci->pc = NULL;
  180. if (ttype(func) != LUA_TFUNCTION) /* `func' is not a function? */
  181. func = tryfuncTM(L, func); /* check the `function' tag method */
  182. cl = &clvalue(func)->l;
  183. if (L->callhook) {
  184. luaD_callHook(L, L->callhook, "call");
  185. ci = L->ci; /* previous call may realocate `ci' */
  186. }
  187. if (!cl->isC) { /* Lua function? prepare its call */
  188. Proto *p = cl->p;
  189. ci->savedpc = p->code; /* starting point */
  190. if (p->is_vararg) /* varargs? */
  191. adjust_varargs(L, p->numparams);
  192. luaD_checkstack(L, p->maxstacksize);
  193. ci->top = ci->base + p->maxstacksize;
  194. while (L->top < ci->top)
  195. setnilvalue(L->top++);
  196. L->top = ci->top;
  197. return NULL;
  198. }
  199. else { /* if is a C function, call it */
  200. int n;
  201. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  202. ci->top = L->top + LUA_MINSTACK;
  203. lua_unlock(L);
  204. #if LUA_COMPATUPVALUES
  205. lua_pushupvalues(L);
  206. #endif
  207. n = (*clvalue(ci->base-1)->c.f)(L); /* do the actual call */
  208. lua_lock(L);
  209. return L->top - n;
  210. }
  211. }
  212. void luaD_poscall (lua_State *L, int wanted, StkId firstResult) {
  213. StkId res;
  214. if (L->callhook) {
  215. ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
  216. luaD_callHook(L, L->callhook, "return");
  217. firstResult = restorestack(L, fr);
  218. }
  219. res = L->ci->base - 1; /* res == final position of 1st result */
  220. L->ci--;
  221. /* move results to correct place */
  222. while (wanted != 0 && firstResult < L->top) {
  223. setobj(res++, firstResult++);
  224. wanted--;
  225. }
  226. while (wanted-- > 0)
  227. setnilvalue(res++);
  228. L->top = res;
  229. luaC_checkGC(L);
  230. }
  231. /*
  232. ** Call a function (C or Lua). The function to be called is at *func.
  233. ** The arguments are on the stack, right after the function.
  234. ** When returns, all the results are on the stack, starting at the original
  235. ** function position.
  236. */
  237. void luaD_call (lua_State *L, StkId func, int nResults) {
  238. StkId firstResult = luaD_precall(L, func);
  239. if (firstResult == NULL) { /* is a Lua function? */
  240. firstResult = luaV_execute(L); /* call it */
  241. if (firstResult == NULL) {
  242. luaD_poscall(L, 0, L->top);
  243. luaD_runerror(L, "attempt to `yield' across tag-method/C-call boundary");
  244. }
  245. }
  246. luaD_poscall(L, nResults, firstResult);
  247. }
  248. LUA_API void lua_cobegin (lua_State *L, int nargs) {
  249. lua_lock(L);
  250. luaD_precall(L, L->top - (nargs+1));
  251. lua_unlock(L);
  252. }
  253. static void move_results (lua_State *L, TObject *from, TObject *to) {
  254. while (from < to) {
  255. setobj(L->top, from);
  256. from++;
  257. incr_top(L);
  258. }
  259. }
  260. static void resume (lua_State *L, void *numres) {
  261. StkId firstResult;
  262. CallInfo *ci = L->ci;
  263. if (ci->savedpc != ci_func(ci)->l.p->code) { /* not first time? */
  264. /* finish interupted execution of `OP_CALL' */
  265. int nresults;
  266. lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL);
  267. nresults = GETARG_C(*((ci-1)->savedpc - 1)) - 1;
  268. luaD_poscall(L, nresults, L->top); /* complete it */
  269. if (nresults >= 0) L->top = L->ci->top;
  270. }
  271. firstResult = luaV_execute(L);
  272. if (firstResult == NULL) /* yield? */
  273. *(int *)numres = L->ci->yield_results;
  274. else { /* return */
  275. *(int *)numres = L->top - firstResult;
  276. luaD_poscall(L, LUA_MULTRET, firstResult); /* finalize this coroutine */
  277. }
  278. }
  279. LUA_API int lua_resume (lua_State *L, lua_State *co) {
  280. CallInfo *ci;
  281. int numres;
  282. TObject o;
  283. int status;
  284. lua_lock(L);
  285. ci = co->ci;
  286. if (ci == co->base_ci) /* no activation record? ?? */
  287. luaD_runerror(L, "thread is dead - cannot be resumed");
  288. if (co->errorJmp != NULL) /* ?? */
  289. luaD_runerror(L, "thread is active - cannot be resumed");
  290. setsvalue(&o, luaS_newliteral(L, "_ERRORMESSAGE"));
  291. luaV_gettable(L, gt(L), &o, &o);
  292. status = luaD_runprotected(co, resume, &o, &numres);
  293. if (status == 0)
  294. move_results(L, co->top - numres, co->top);
  295. lua_unlock(L);
  296. return status;
  297. }
  298. LUA_API int lua_yield (lua_State *L, int nresults) {
  299. CallInfo *ci;
  300. lua_lock(L);
  301. ci = L->ci;
  302. if (ci_func(ci-1)->c.isC)
  303. luaD_runerror(L, "cannot `yield' a C function");
  304. ci->yield_results = nresults;
  305. lua_unlock(L);
  306. return -1;
  307. }
  308. /*
  309. ** Execute a protected call.
  310. */
  311. struct CallS { /* data to `f_call' */
  312. StkId func;
  313. int nresults;
  314. };
  315. static void f_call (lua_State *L, void *ud) {
  316. struct CallS *c = cast(struct CallS *, ud);
  317. luaD_call(L, c->func, c->nresults);
  318. }
  319. int luaD_pcall (lua_State *L, int nargs, int nresults, const TObject *err) {
  320. struct CallS c;
  321. int status;
  322. c.func = L->top - (nargs+1); /* function to be called */
  323. c.nresults = nresults;
  324. status = luaD_runprotected(L, &f_call, err, &c);
  325. if (status != 0) { /* an error occurred? */
  326. L->top -= nargs+1; /* remove parameters and func from the stack */
  327. luaF_close(L, L->top); /* close eventual pending closures */
  328. }
  329. return status;
  330. }
  331. /*
  332. ** Execute a protected parser.
  333. */
  334. struct SParser { /* data to `f_parser' */
  335. ZIO *z;
  336. int bin;
  337. };
  338. static void f_parser (lua_State *L, void *ud) {
  339. struct SParser *p = cast(struct SParser *, ud);
  340. Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
  341. Closure *cl = luaF_newLclosure(L, 0);
  342. cl->l.p = tf;
  343. setclvalue(L->top, cl);
  344. incr_top(L);
  345. }
  346. int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {
  347. struct SParser p;
  348. TObject o;
  349. lu_mem old_blocks;
  350. int status;
  351. lua_lock(L);
  352. p.z = z; p.bin = bin;
  353. /* before parsing, give a (good) chance to GC */
  354. if (G(L)->nblocks/8 >= G(L)->GCthreshold/10)
  355. luaC_collectgarbage(L);
  356. old_blocks = G(L)->nblocks;
  357. setsvalue(&o, luaS_newliteral(L, "_ERRORMESSAGE"));
  358. luaV_gettable(L, gt(L), &o, &o);
  359. status = luaD_runprotected(L, f_parser, &o, &p);
  360. if (status == 0) {
  361. /* add new memory to threshold (as it probably will stay) */
  362. lua_assert(G(L)->nblocks >= old_blocks);
  363. G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
  364. }
  365. else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
  366. status = LUA_ERRSYNTAX;
  367. lua_unlock(L);
  368. return status;
  369. }
  370. /*
  371. ** {======================================================
  372. ** Error-recovery functions (based on long jumps)
  373. ** =======================================================
  374. */
  375. static void message (lua_State *L, const char *msg) {
  376. TObject *m = &L->errorJmp->err;
  377. if (ttype(m) == LUA_TFUNCTION) {
  378. setobj(L->top, m);
  379. incr_top(L);
  380. setsvalue(L->top, luaS_new(L, msg));
  381. incr_top(L);
  382. luaD_call(L, L->top - 2, 0);
  383. }
  384. }
  385. /*
  386. ** Reports an error, and jumps up to the available recovery label
  387. */
  388. void luaD_error (lua_State *L, const char *s, int errcode) {
  389. if (L->errorJmp) {
  390. L->errorJmp->status = errcode;
  391. if (s) message(L, s);
  392. longjmp(L->errorJmp->b, 1);
  393. }
  394. else {
  395. G(L)->panic(L);
  396. exit(EXIT_FAILURE);
  397. }
  398. }
  399. void luaD_runerror (lua_State *L, const char *s) {
  400. luaD_error(L, s, LUA_ERRRUN);
  401. }
  402. int luaD_runprotected (lua_State *L, Pfunc f, const TObject *err, void *ud) {
  403. struct lua_longjmp lj;
  404. lj.ci = L->ci;
  405. lj.top = L->top;
  406. lj.allowhooks = L->allowhooks;
  407. lj.status = 0;
  408. lj.err = *err;
  409. lj.previous = L->errorJmp; /* chain new error handler */
  410. L->errorJmp = &lj;
  411. if (setjmp(lj.b) == 0)
  412. (*f)(L, ud);
  413. else { /* an error occurred: restore the state */
  414. L->ci = lj.ci;
  415. L->top = lj.top;
  416. L->allowhooks = lj.allowhooks;
  417. restore_stack_limit(L);
  418. }
  419. L->errorJmp = lj.previous; /* restore old error handler */
  420. return lj.status;
  421. }
  422. /* }====================================================== */