ldo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. ** $Id: ldo.c,v 1.154 2002/01/11 20:27:41 roberto Exp $
  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. jmp_buf b;
  29. struct lua_longjmp *previous;
  30. CallInfo *ci; /* index of call info of active function that set protection */
  31. StkId top; /* top stack when protection was set */
  32. TObject *stack; /* active stack for this entry */
  33. int allowhooks; /* `allowhook' state when protection was set */
  34. volatile int status; /* error code */
  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 && lj->stack == oldstack; lj = lj->previous) {
  42. lj->top = (lj->top - oldstack) + L->stack;
  43. lj->stack = L->stack;
  44. }
  45. for (up = L->openupval; up != NULL; up = up->next)
  46. up->v = (up->v - oldstack) + L->stack;
  47. for (ci = L->base_ci; ci <= L->ci; ci++) {
  48. ci->base = (ci->base - oldstack) + L->stack;
  49. ci->top = (ci->top - oldstack) + L->stack;
  50. if (ci->pc) { /* entry is of an active Lua function? */
  51. if (ci->pc != (ci-1)->pc)
  52. *ci->pb = (*ci->pb - oldstack) + L->stack;
  53. }
  54. }
  55. }
  56. void luaD_reallocstack (lua_State *L, int newsize) {
  57. TObject *oldstack = L->stack;
  58. luaM_reallocvector(L, L->stack, L->stacksize, newsize, TObject);
  59. L->stacksize = newsize;
  60. L->stack_last = L->stack+(newsize-EXTRA_STACK)-1;
  61. correctstack(L, oldstack);
  62. }
  63. static void restore_stack_limit (lua_State *L) {
  64. if (L->stacksize > L->maxstacksize) { /* there was an overflow? */
  65. int inuse = (L->top - L->stack);
  66. if (inuse + MAXSTACK < L->maxstacksize) /* can `undo' overflow? */
  67. luaD_reallocstack(L, L->maxstacksize);
  68. }
  69. }
  70. void luaD_growstack (lua_State *L, int n) {
  71. if (L->stacksize > L->maxstacksize) { /* overflow while handling overflow? */
  72. luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
  73. }
  74. else {
  75. if (n <= L->stacksize && 2*L->stacksize < L->maxstacksize) /* can double? */
  76. luaD_reallocstack(L, 2*L->stacksize);
  77. else if (L->stacksize+n <= L->maxstacksize) /* no overflow? */
  78. luaD_reallocstack(L, L->maxstacksize);
  79. else {
  80. /* resize to maximum + some extra space to handle error */
  81. luaD_reallocstack(L, L->maxstacksize+4*LUA_MINSTACK);
  82. luaD_error(L, "stack overflow");
  83. }
  84. }
  85. }
  86. /*
  87. ** Open a hole inside the stack at `pos'
  88. */
  89. static void luaD_openstack (lua_State *L, StkId pos) {
  90. int i = L->top-pos;
  91. while (i--) setobj(pos+i+1, pos+i);
  92. incr_top(L);
  93. }
  94. static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
  95. L->ci->top = L->top;
  96. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  97. L->allowhooks = 0; /* cannot call hooks inside a hook */
  98. lua_unlock(L);
  99. (*hook)(L, ar);
  100. lua_lock(L);
  101. lua_assert(L->allowhooks == 0);
  102. L->allowhooks = 1;
  103. L->top = L->ci->top;
  104. }
  105. void luaD_lineHook (lua_State *L, int line, lua_Hook linehook) {
  106. if (L->allowhooks) {
  107. lua_Debug ar;
  108. ar.event = "line";
  109. ar._ci = L->ci - L->base_ci;
  110. ar.currentline = line;
  111. dohook(L, &ar, linehook);
  112. }
  113. }
  114. void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event) {
  115. if (L->allowhooks) {
  116. lua_Debug ar;
  117. ar.event = event;
  118. ar._ci = L->ci - L->base_ci;
  119. L->ci->pc = NULL; /* function is not active */
  120. dohook(L, &ar, callhook);
  121. }
  122. }
  123. static void correctCI (lua_State *L, CallInfo *oldci) {
  124. struct lua_longjmp *lj;
  125. for (lj = L->errorJmp; lj && lj->stack == L->stack; lj = lj->previous) {
  126. lj->ci = (lj->ci - oldci) + L->base_ci;
  127. }
  128. }
  129. void luaD_reallocCI (lua_State *L, int newsize) {
  130. CallInfo *oldci = L->base_ci;
  131. luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
  132. L->size_ci = newsize;
  133. if (oldci != L->ci) {
  134. L->ci = (L->ci - oldci) + L->base_ci;
  135. L->end_ci = L->base_ci + L->size_ci;
  136. correctCI(L, oldci);
  137. }
  138. }
  139. static void adjust_varargs (lua_State *L, int nfixargs) {
  140. int i;
  141. Table *htab;
  142. TObject n, 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. luaH_setnum(L, htab, i+1, L->top - actual + i);
  153. /* store counter in field `n' */
  154. setnvalue(&n, actual);
  155. setsvalue(&nname, luaS_newliteral(L, "n"));
  156. luaH_set(L, htab, &nname, &n);
  157. L->top -= actual; /* remove extra elements from the stack */
  158. sethvalue(L->top, htab);
  159. incr_top(L);
  160. }
  161. StkId luaD_precall (lua_State *L, StkId func) {
  162. CallInfo *ci;
  163. LClosure *cl;
  164. if (++L->ci == L->end_ci) luaD_reallocCI(L, 2*L->size_ci);
  165. ci = L->ci;
  166. ci->base = func+1;
  167. ci->pc = NULL;
  168. if (ttype(func) != LUA_TFUNCTION) {
  169. /* `func' is not a function; check the `function' tag method */
  170. const TObject *tm = luaT_gettmbyobj(L, func, TM_CALL);
  171. if (ttype(tm) != LUA_TFUNCTION) {
  172. L->ci--; /* undo increment (no function here) */
  173. luaG_typeerror(L, func, "call");
  174. }
  175. luaD_openstack(L, func);
  176. func = ci->base - 1; /* previous call may change stack */
  177. setobj(func, tm); /* tag method is the new function to be called */
  178. }
  179. cl = &clvalue(func)->l;
  180. if (L->callhook) {
  181. luaD_callHook(L, L->callhook, "call");
  182. ci = L->ci; /* previous call may realocate `ci' */
  183. }
  184. if (!cl->isC) { /* Lua function? prepare its call */
  185. Proto *p = cl->p;
  186. ci->savedpc = p->code; /* starting point */
  187. if (p->is_vararg) /* varargs? */
  188. adjust_varargs(L, p->numparams);
  189. if (ci->base > L->stack_last - p->maxstacksize)
  190. luaD_growstack(L, p->maxstacksize);
  191. ci->line = 0;
  192. ci->top = ci->base + p->maxstacksize;
  193. while (L->top < ci->top)
  194. setnilvalue(L->top++);
  195. L->top = ci->top;
  196. return NULL;
  197. }
  198. else { /* if is a C function, call it */
  199. int n;
  200. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  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. StkId stack = L->stack; /* next call may change stack */
  214. luaD_callHook(L, L->callhook, "return");
  215. firstResult = (firstResult - stack) + L->stack;
  216. }
  217. res = L->ci->base - 1; /* func == 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. luaD_error(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 resume_results (lua_State *L, lua_State *from, int numresults) {
  252. while (numresults) {
  253. setobj(L->top, from->top - numresults);
  254. numresults--;
  255. incr_top(L);
  256. }
  257. }
  258. LUA_API void lua_resume (lua_State *L, lua_State *co) {
  259. StkId firstResult;
  260. lua_lock(L);
  261. if (co->ci == co->base_ci) /* no activation record? ?? */
  262. luaD_error(L, "thread is dead - cannot be resumed");
  263. lua_assert(co->errorJmp == NULL);
  264. co->errorJmp = L->errorJmp;
  265. firstResult = luaV_execute(co);
  266. if (firstResult != NULL) { /* `return'? */
  267. resume_results(L, co, co->top - firstResult);
  268. luaD_poscall(co, 0, firstResult); /* ends this coroutine */
  269. }
  270. else { /* `yield' */
  271. int nresults = GETARG_C(*((co->ci-1)->savedpc - 1)) - 1;
  272. resume_results(L, co, co->ci->yield_results);
  273. luaD_poscall(co, nresults, co->top); /* complete it */
  274. if (nresults >= 0) co->top = co->ci->top;
  275. }
  276. co->errorJmp = NULL;
  277. lua_unlock(L);
  278. }
  279. LUA_API int lua_yield (lua_State *L, int nresults) {
  280. CallInfo *ci;
  281. lua_lock(L);
  282. ci = L->ci;
  283. if (ci_func(ci-1)->c.isC)
  284. luaD_error(L, "cannot `yield' a C function");
  285. ci->yield_results = nresults;
  286. lua_unlock(L);
  287. return -1;
  288. }
  289. /*
  290. ** Execute a protected call.
  291. */
  292. struct CallS { /* data to `f_call' */
  293. StkId func;
  294. int nresults;
  295. };
  296. static void f_call (lua_State *L, void *ud) {
  297. struct CallS *c = cast(struct CallS *, ud);
  298. luaD_call(L, c->func, c->nresults);
  299. }
  300. LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
  301. struct CallS c;
  302. int status;
  303. lua_lock(L);
  304. c.func = L->top - (nargs+1); /* function to be called */
  305. c.nresults = nresults;
  306. status = luaD_runprotected(L, f_call, &c);
  307. if (status != 0) { /* an error occurred? */
  308. L->top -= nargs+1; /* remove parameters and func from the stack */
  309. luaF_close(L, L->top); /* close eventual pending closures */
  310. }
  311. lua_unlock(L);
  312. return status;
  313. }
  314. /*
  315. ** Execute a protected parser.
  316. */
  317. struct SParser { /* data to `f_parser' */
  318. ZIO *z;
  319. int bin;
  320. };
  321. static void f_parser (lua_State *L, void *ud) {
  322. struct SParser *p = cast(struct SParser *, ud);
  323. Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
  324. Closure *cl = luaF_newLclosure(L, 0);
  325. cl->l.p = tf;
  326. setclvalue(L->top, cl);
  327. incr_top(L);
  328. }
  329. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  330. struct SParser p;
  331. lu_mem old_blocks;
  332. int status;
  333. lua_lock(L);
  334. p.z = z; p.bin = bin;
  335. /* before parsing, give a (good) chance to GC */
  336. if (G(L)->nblocks/8 >= G(L)->GCthreshold/10)
  337. luaC_collectgarbage(L);
  338. old_blocks = G(L)->nblocks;
  339. status = luaD_runprotected(L, f_parser, &p);
  340. if (status == 0) {
  341. /* add new memory to threshold (as it probably will stay) */
  342. lua_assert(G(L)->nblocks >= old_blocks);
  343. G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
  344. }
  345. else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
  346. status = LUA_ERRSYNTAX;
  347. lua_unlock(L);
  348. return status;
  349. }
  350. LUA_API int lua_loadfile (lua_State *L, const char *filename) {
  351. ZIO z;
  352. int status;
  353. int bin; /* flag for file mode */
  354. int nlevel; /* level on the stack of filename */
  355. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  356. if (f == NULL) return LUA_ERRFILE; /* unable to open file */
  357. bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
  358. if (bin && f != stdin) {
  359. fclose(f);
  360. f = fopen(filename, "rb"); /* reopen in binary mode */
  361. if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
  362. }
  363. lua_pushliteral(L, "@");
  364. lua_pushstring(L, (filename == NULL) ? "=stdin" : filename);
  365. lua_concat(L, 2);
  366. nlevel = lua_gettop(L);
  367. filename = lua_tostring(L, -1); /* filename = `@'..filename */
  368. luaZ_Fopen(&z, f, filename);
  369. status = protectedparser(L, &z, bin);
  370. lua_remove(L, nlevel); /* remove filename */
  371. if (f != stdin)
  372. fclose(f);
  373. return status;
  374. }
  375. LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
  376. const char *name) {
  377. ZIO z;
  378. if (!name) name = "?";
  379. luaZ_mopen(&z, buff, size, name);
  380. return protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
  381. }
  382. /*
  383. ** {======================================================
  384. ** Error-recovery functions (based on long jumps)
  385. ** =======================================================
  386. */
  387. static void message (lua_State *L, const char *s) {
  388. TObject o, m;
  389. setsvalue(&o, luaS_newliteral(L, LUA_ERRORMESSAGE));
  390. luaV_gettable(L, gt(L), &o, &m);
  391. if (ttype(&m) == LUA_TFUNCTION) {
  392. setobj(L->top, &m);
  393. incr_top(L);
  394. setsvalue(L->top, luaS_new(L, s));
  395. incr_top(L);
  396. luaD_call(L, L->top-2, 0);
  397. }
  398. }
  399. /*
  400. ** Reports an error, and jumps up to the available recovery label
  401. */
  402. void luaD_error (lua_State *L, const char *s) {
  403. if (s) message(L, s);
  404. luaD_breakrun(L, LUA_ERRRUN);
  405. }
  406. void luaD_breakrun (lua_State *L, int errcode) {
  407. if (L->errorJmp) {
  408. L->errorJmp->status = errcode;
  409. longjmp(L->errorJmp->b, 1);
  410. }
  411. else {
  412. if (errcode != LUA_ERRMEM && errcode != LUA_ERRERR)
  413. message(L, "unable to recover; exiting\n");
  414. exit(EXIT_FAILURE);
  415. }
  416. }
  417. int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
  418. struct lua_longjmp lj;
  419. lj.ci = L->ci;
  420. lj.top = L->top;
  421. lj.stack = L->stack;
  422. lj.allowhooks = L->allowhooks;
  423. lj.status = 0;
  424. lj.previous = L->errorJmp; /* chain new error handler */
  425. L->errorJmp = &lj;
  426. if (setjmp(lj.b) == 0)
  427. (*f)(L, ud);
  428. else { /* an error occurred: restore the state */
  429. L->ci = lj.ci;
  430. L->top = lj.top;
  431. L->allowhooks = lj.allowhooks;
  432. restore_stack_limit(L);
  433. }
  434. L->errorJmp = lj.previous; /* restore old error handler */
  435. return lj.status;
  436. }
  437. /* }====================================================== */