ldo.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. ** $Id: ldo.c,v 1.80 2000/06/26 19:28: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 "lua.h"
  12. #include "lauxlib.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lgc.h"
  16. #include "lmem.h"
  17. #include "lobject.h"
  18. #include "lparser.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.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, lua_Hook linehook) {
  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. (*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->nupvalues; /* 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->upvalue[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. CallInfo ci;
  161. ci.func = clvalue(func);
  162. ci.line = 0;
  163. ttype(func) = TAG_LMARK;
  164. infovalue(func) = &ci;
  165. if (callhook)
  166. luaD_callHook(L, func, callhook, "call");
  167. firstResult = luaV_execute(L, ci.func, func+1);
  168. break;
  169. }
  170. case TAG_CCLOSURE: {
  171. ttype(func) = TAG_CMARK;
  172. if (callhook)
  173. luaD_callHook(L, func, callhook, "call");
  174. firstResult = callCclosure(L, clvalue(func), func+1);
  175. break;
  176. }
  177. default: { /* `func' is not a function; check the `function' tag method */
  178. const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
  179. if (ttype(im) == TAG_NIL)
  180. luaG_callerror(L, func);
  181. luaD_openstack(L, func);
  182. *func = *im; /* tag method is the new function to be called */
  183. goto retry; /* retry the call */
  184. }
  185. }
  186. if (callhook) /* same hook that was active at entry */
  187. luaD_callHook(L, func, callhook, "return");
  188. /* adjust the number of results */
  189. if (nResults == MULT_RET)
  190. nResults = L->top - firstResult;
  191. else
  192. luaD_adjusttop(L, firstResult, nResults);
  193. /* move results to `func' (to erase parameters and function) */
  194. while (nResults) {
  195. *func++ = *(L->top - nResults);
  196. nResults--;
  197. }
  198. L->top = func;
  199. }
  200. static void message (lua_State *L, const char *s) {
  201. const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
  202. if (*luaO_typename(em) == 'f') {
  203. *L->top = *em;
  204. incr_top;
  205. lua_pushstring(L, s);
  206. luaD_call(L, L->top-2, 0);
  207. }
  208. }
  209. /*
  210. ** Reports an error, and jumps up to the available recovery label
  211. */
  212. void lua_error (lua_State *L, const char *s) {
  213. if (s) message(L, s);
  214. if (L->errorJmp)
  215. longjmp(L->errorJmp->b, 1);
  216. else {
  217. message(L, "unable to recover; exiting\n");
  218. exit(1);
  219. }
  220. }
  221. /*
  222. ** Execute a protected call. Assumes that function is at Cstack.base and
  223. ** parameters are on top of it.
  224. */
  225. int luaD_protectedrun (lua_State *L) {
  226. struct lua_longjmp myErrorJmp;
  227. StkId base = L->Cstack.base;
  228. int numCblocks = L->numCblocks;
  229. int status;
  230. struct lua_longjmp *volatile oldErr = L->errorJmp;
  231. L->errorJmp = &myErrorJmp;
  232. if (setjmp(myErrorJmp.b) == 0) {
  233. luaD_call(L, base, MULT_RET);
  234. L->Cstack.lua2C = base; /* position of the new results */
  235. L->Cstack.num = L->top - base;
  236. L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
  237. status = 0;
  238. }
  239. else { /* an error occurred: restore the stack */
  240. L->Cstack.num = 0; /* no results */
  241. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  242. L->numCblocks = numCblocks;
  243. restore_stack_limit(L);
  244. status = 1;
  245. }
  246. L->errorJmp = oldErr;
  247. return status;
  248. }
  249. /*
  250. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  251. */
  252. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  253. struct lua_longjmp myErrorJmp;
  254. StkId base = L->Cstack.base;
  255. int numCblocks = L->numCblocks;
  256. int status;
  257. Proto *volatile tf;
  258. struct lua_longjmp *volatile oldErr = L->errorJmp;
  259. L->errorJmp = &myErrorJmp;
  260. L->top = base; /* clear C2Lua */
  261. if (setjmp(myErrorJmp.b) == 0) {
  262. tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
  263. status = 0;
  264. }
  265. else { /* an error occurred: restore Cstack and top */
  266. L->Cstack.num = 0; /* no results */
  267. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  268. L->numCblocks = numCblocks;
  269. tf = NULL;
  270. status = 1;
  271. }
  272. L->errorJmp = oldErr;
  273. if (status) return 1; /* error code */
  274. if (tf == NULL) return 2; /* `natural' end */
  275. luaV_Lclosure(L, tf, 0);
  276. return 0;
  277. }
  278. static int do_main (lua_State *L, ZIO *z, int bin) {
  279. int status;
  280. int debug = L->debug; /* save debug status */
  281. do {
  282. unsigned long old_blocks;
  283. luaC_checkGC(L);
  284. old_blocks = L->nblocks;
  285. status = protectedparser(L, z, bin);
  286. if (status == 1) return 1; /* error */
  287. else if (status == 2) return 0; /* `natural' end */
  288. else {
  289. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  290. L->GCthreshold += newelems2;
  291. status = luaD_protectedrun(L);
  292. L->GCthreshold -= newelems2;
  293. }
  294. } while (bin && status == 0);
  295. L->debug = debug; /* restore debug status */
  296. return status;
  297. }
  298. int lua_dofile (lua_State *L, const char *filename) {
  299. ZIO z;
  300. int status;
  301. int bin; /* flag for file mode */
  302. int c; /* look ahead char */
  303. char source[MAXFILENAME];
  304. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  305. if (f == NULL) return 2; /* unable to open file */
  306. luaL_filesource(source, filename, sizeof(source));
  307. c = fgetc(f);
  308. ungetc(c, f);
  309. bin = (c == ID_CHUNK);
  310. if (bin && f != stdin) {
  311. f = freopen(filename, "rb", f); /* set binary mode */
  312. if (f == NULL) return 2; /* unable to reopen file */
  313. }
  314. luaZ_Fopen(&z, f, source);
  315. status = do_main(L, &z, bin);
  316. if (f != stdin)
  317. fclose(f);
  318. return status;
  319. }
  320. int lua_dostring (lua_State *L, const char *str) {
  321. return lua_dobuffer(L, str, strlen(str), str);
  322. }
  323. int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  324. const char *name) {
  325. ZIO z;
  326. if (!name) name = "?";
  327. luaZ_mopen(&z, buff, size, name);
  328. return do_main(L, &z, buff[0]==ID_CHUNK);
  329. }