ldo.c 9.7 KB

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