ldo.c 13 KB

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