ldo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. ** $Id: ldo.c,v 1.67 2000/02/08 16:34:31 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. #define LUA_REENTRANT
  11. #include "lauxlib.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lgc.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lparser.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltm.h"
  21. #include "lua.h"
  22. #include "luadebug.h"
  23. #include "lundump.h"
  24. #include "lvm.h"
  25. #include "lzio.h"
  26. #define EXTRA_STACK 32 /* space to handle stack overflow errors */
  27. /*
  28. ** typical numer of stack slots used by a (big) function
  29. ** (this constant is used only for choosing error messages)
  30. */
  31. #define SLOTS_PER_F 20
  32. void luaD_init (lua_State *L, int stacksize) {
  33. L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
  34. L->stack_last = L->stack+(stacksize-1);
  35. L->stacksize = stacksize;
  36. L->Cstack.base = L->Cstack.lua2C = L->top = L->stack;
  37. L->Cstack.num = 0;
  38. }
  39. void luaD_checkstack (lua_State *L, int n) {
  40. if (L->stack_last-L->top <= n) { /* stack overflow? */
  41. if (L->stack_last-L->stack > (L->stacksize-1)) {
  42. /* overflow while handling overflow: do what?? */
  43. L->top -= EXTRA_STACK;
  44. lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
  45. }
  46. else {
  47. lua_Dbgactreg dummy;
  48. L->stack_last += EXTRA_STACK; /* to be used by error message */
  49. if (lua_getstack(L, L->stacksize/SLOTS_PER_F, &dummy) == 0) {
  50. /* too few funcs on stack: doesn't look like a recursion loop */
  51. lua_error(L, "Lua2C - C2Lua overflow");
  52. }
  53. else
  54. lua_error(L, "stack overflow; possible recursion loop");
  55. }
  56. }
  57. }
  58. static void restore_stack_limit (lua_State *L) {
  59. if (L->top-L->stack < L->stacksize-1)
  60. L->stack_last = L->stack+(L->stacksize-1);
  61. }
  62. /*
  63. ** Adjust stack. Set top to base+extra, pushing NILs if needed.
  64. ** (we cannot add base+extra unless we are sure it fits in the stack;
  65. ** otherwise the result of such operation on pointers is undefined)
  66. */
  67. void luaD_adjusttop (lua_State *L, StkId base, int extra) {
  68. int diff = extra-(L->top-base);
  69. if (diff <= 0)
  70. L->top = base+extra;
  71. else {
  72. luaD_checkstack(L, diff);
  73. while (diff--)
  74. ttype(L->top++) = LUA_T_NIL;
  75. }
  76. }
  77. /*
  78. ** Open a hole inside the stack at `pos'
  79. */
  80. void luaD_openstack (lua_State *L, StkId pos) {
  81. int i = L->top-pos;
  82. while (i--) pos[i+1] = pos[i];
  83. incr_top;
  84. }
  85. void luaD_lineHook (lua_State *L, StkId func, int line) {
  86. if (L->allowhooks) {
  87. lua_Dbgactreg ar;
  88. struct C_Lua_Stack oldCLS = L->Cstack;
  89. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
  90. L->Cstack.num = 0;
  91. ar._func = func;
  92. ar.event = "line";
  93. ar.currentline = line;
  94. L->allowhooks = 0; /* cannot call hooks inside a hook */
  95. (*L->linehook)(L, &ar);
  96. L->allowhooks = 1;
  97. L->top = old_top;
  98. L->Cstack = oldCLS;
  99. }
  100. }
  101. void luaD_callHook (lua_State *L, StkId func, lua_Dbghook callhook,
  102. const char *event) {
  103. if (L->allowhooks) {
  104. lua_Dbgactreg ar;
  105. struct C_Lua_Stack oldCLS = L->Cstack;
  106. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
  107. L->Cstack.num = 0;
  108. ar._func = func;
  109. ar.event = event;
  110. L->allowhooks = 0; /* cannot call hooks inside a hook */
  111. callhook(L, &ar);
  112. L->allowhooks = 1;
  113. L->top = old_top;
  114. L->Cstack = oldCLS;
  115. }
  116. }
  117. /*
  118. ** Call a C function.
  119. ** Cstack.num is the number of arguments; Cstack.lua2C points to the
  120. ** first argument. Returns an index to the first result from C.
  121. */
  122. static StkId callC (lua_State *L, lua_CFunction f, StkId base) {
  123. struct C_Lua_Stack oldCLS = L->Cstack;
  124. StkId firstResult;
  125. int numarg = L->top - base;
  126. L->Cstack.num = numarg;
  127. L->Cstack.lua2C = base;
  128. L->Cstack.base = L->top;
  129. if (L->callhook)
  130. luaD_callHook(L, base-1, L->callhook, "call");
  131. (*f)(L); /* do the actual call */
  132. firstResult = L->Cstack.base;
  133. L->Cstack = oldCLS;
  134. return firstResult;
  135. }
  136. static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
  137. int nup = cl->nelems; /* number of upvalues */
  138. int n = L->top-base; /* number of arguments (to move up) */
  139. luaD_checkstack(L, nup);
  140. /* open space for upvalues as extra arguments */
  141. while (n--) *(base+nup+n) = *(base+n);
  142. L->top += nup;
  143. /* copy upvalues into stack */
  144. while (nup--) *(base+nup) = cl->consts[nup+1];
  145. return callC(L, fvalue(cl->consts), base);
  146. }
  147. void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
  148. StkId base = L->top - nParams;
  149. luaD_openstack(L, base);
  150. *base = *f;
  151. luaD_call(L, base, nResults);
  152. }
  153. /*
  154. ** Call a function (C or Lua). The function to be called is at *func.
  155. ** The arguments are on the stack, right after the function.
  156. ** When returns, the results are on the stack, starting at the original
  157. ** function position.
  158. ** The number of results is nResults, unless nResults=MULT_RET.
  159. */
  160. void luaD_call (lua_State *L, StkId func, int nResults) {
  161. StkId firstResult;
  162. lua_Dbghook callhook = L->callhook;
  163. retry: /* for `function' tag method */
  164. switch (ttype(func)) {
  165. case LUA_T_CPROTO:
  166. ttype(func) = LUA_T_CMARK;
  167. firstResult = callC(L, fvalue(func), func+1);
  168. break;
  169. case LUA_T_LPROTO:
  170. ttype(func) = LUA_T_LMARK;
  171. firstResult = luaV_execute(L, NULL, tfvalue(func), func+1);
  172. break;
  173. case LUA_T_LCLOSURE: {
  174. Closure *c = clvalue(func);
  175. ttype(func) = LUA_T_LCLMARK;
  176. firstResult = luaV_execute(L, c, tfvalue(c->consts), func+1);
  177. break;
  178. }
  179. case LUA_T_CCLOSURE: {
  180. Closure *c = clvalue(func);
  181. ttype(func) = LUA_T_CCLMARK;
  182. firstResult = callCclosure(L, c, func+1);
  183. break;
  184. }
  185. default: { /* `func' is not a function; check the `function' tag method */
  186. const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
  187. if (ttype(im) == LUA_T_NIL)
  188. luaG_callerror(L, func);
  189. luaD_openstack(L, func);
  190. *func = *im; /* tag method is the new function to be called */
  191. goto retry; /* retry the call */
  192. }
  193. }
  194. if (callhook) /* same hook that was active at entry */
  195. luaD_callHook(L, func, callhook, "return");
  196. /* adjust the number of results */
  197. if (nResults == MULT_RET)
  198. nResults = L->top - firstResult;
  199. else
  200. luaD_adjusttop(L, firstResult, nResults);
  201. /* move results to `func' (to erase parameters and function) */
  202. while (nResults) {
  203. *func++ = *(L->top - nResults);
  204. nResults--;
  205. }
  206. L->top = func;
  207. }
  208. static void message (lua_State *L, const char *s) {
  209. const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
  210. if (*luaO_typename(em) == 'f') {
  211. *L->top = *em;
  212. incr_top;
  213. lua_pushstring(L, s);
  214. luaD_call(L, L->top-2, 0);
  215. }
  216. }
  217. /*
  218. ** Reports an error, and jumps up to the available recovery label
  219. */
  220. void lua_error (lua_State *L, const char *s) {
  221. if (s) message(L, s);
  222. if (L->errorJmp)
  223. longjmp(L->errorJmp->b, 1);
  224. else {
  225. message(L, "unable to recover; exiting\n");
  226. exit(1);
  227. }
  228. }
  229. /*
  230. ** Execute a protected call. Assumes that function is at Cstack.base and
  231. ** parameters are on top of it.
  232. */
  233. int luaD_protectedrun (lua_State *L) {
  234. struct lua_longjmp myErrorJmp;
  235. StkId base = L->Cstack.base;
  236. int numCblocks = L->numCblocks;
  237. int status;
  238. struct lua_longjmp *volatile oldErr = L->errorJmp;
  239. L->errorJmp = &myErrorJmp;
  240. if (setjmp(myErrorJmp.b) == 0) {
  241. luaD_call(L, base, MULT_RET);
  242. L->Cstack.lua2C = base; /* position of the new results */
  243. L->Cstack.num = L->top - base;
  244. L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
  245. status = 0;
  246. }
  247. else { /* an error occurred: restore the stack */
  248. L->Cstack.num = 0; /* no results */
  249. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  250. L->numCblocks = numCblocks;
  251. restore_stack_limit(L);
  252. status = 1;
  253. }
  254. L->errorJmp = oldErr;
  255. return status;
  256. }
  257. /*
  258. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  259. */
  260. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  261. struct lua_longjmp myErrorJmp;
  262. StkId base = L->Cstack.base;
  263. int numCblocks = L->numCblocks;
  264. int status;
  265. TProtoFunc *volatile tf;
  266. struct lua_longjmp *volatile oldErr = L->errorJmp;
  267. L->errorJmp = &myErrorJmp;
  268. L->top = base; /* clear C2Lua */
  269. if (setjmp(myErrorJmp.b) == 0) {
  270. tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
  271. status = 0;
  272. }
  273. else { /* an error occurred: restore Cstack and top */
  274. L->Cstack.num = 0; /* no results */
  275. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  276. L->numCblocks = numCblocks;
  277. tf = NULL;
  278. status = 1;
  279. }
  280. L->errorJmp = oldErr;
  281. if (status) return 1; /* error code */
  282. if (tf == NULL) return 2; /* `natural' end */
  283. L->top->ttype = LUA_T_LPROTO; /* push new function on the stack */
  284. L->top->value.tf = tf;
  285. incr_top;
  286. return 0;
  287. }
  288. static int do_main (lua_State *L, ZIO *z, int bin) {
  289. int status;
  290. int debug = L->debug; /* save debug status */
  291. do {
  292. long old_blocks = (luaC_checkGC(L), L->nblocks);
  293. status = protectedparser(L, z, bin);
  294. if (status == 1) return 1; /* error */
  295. else if (status == 2) return 0; /* `natural' end */
  296. else {
  297. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  298. L->GCthreshold += newelems2;
  299. status = luaD_protectedrun(L);
  300. L->GCthreshold -= newelems2;
  301. }
  302. } while (bin && status == 0);
  303. L->debug = debug; /* restore debug status */
  304. return status;
  305. }
  306. void luaD_gcIM (lua_State *L, const TObject *o) {
  307. const TObject *im = luaT_getimbyObj(L, o, IM_GC);
  308. if (ttype(im) != LUA_T_NIL) {
  309. luaD_checkstack(L, 2);
  310. *(L->top++) = *im;
  311. *(L->top++) = *o;
  312. luaD_call(L, L->top-2, 0);
  313. }
  314. }
  315. #define MAXFILENAME 260 /* maximum part of a file name kept */
  316. int lua_dofile (lua_State *L, const char *filename) {
  317. ZIO z;
  318. int status;
  319. int bin; /* flag for file mode */
  320. int c; /* look ahead char */
  321. char source[MAXFILENAME];
  322. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  323. if (f == NULL) return 2; /* unable to open file */
  324. luaL_filesource(source, filename, sizeof(source));
  325. c = fgetc(f);
  326. ungetc(c, f);
  327. bin = (c == ID_CHUNK);
  328. if (bin && f != stdin) {
  329. f = freopen(filename, "rb", f); /* set binary mode */
  330. if (f == NULL) return 2; /* unable to reopen file */
  331. }
  332. luaZ_Fopen(&z, f, source);
  333. status = do_main(L, &z, bin);
  334. if (f != stdin)
  335. fclose(f);
  336. return status;
  337. }
  338. int lua_dostring (lua_State *L, const char *str) {
  339. return lua_dobuffer(L, str, strlen(str), str);
  340. }
  341. int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) {
  342. ZIO z;
  343. if (!name) name = "?";
  344. luaZ_mopen(&z, buff, size, name);
  345. return do_main(L, &z, buff[0]==ID_CHUNK);
  346. }