ldo.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. ** $Id: ldo.c,v 1.72 2000/03/30 20:55:50 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_Debug 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++) = TAG_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_Debug 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. static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
  102. const char *event) {
  103. if (L->allowhooks) {
  104. lua_Debug 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. static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
  118. int nup = cl->nelems; /* number of upvalues */
  119. int numarg = L->top-base;
  120. struct C_Lua_Stack oldCLS = L->Cstack;
  121. StkId firstResult;
  122. if (nup > 0) {
  123. int n = numarg;
  124. luaD_checkstack(L, nup);
  125. /* open space for upvalues as extra arguments */
  126. while (n--) *(base+nup+n) = *(base+n);
  127. L->top += nup;
  128. numarg += nup;
  129. /* copy upvalues into stack */
  130. while (nup--) *(base+nup) = cl->consts[nup];
  131. }
  132. L->Cstack.num = numarg;
  133. L->Cstack.lua2C = base;
  134. L->Cstack.base = L->top;
  135. (*cl->f.c)(L); /* do the actual call */
  136. firstResult = L->Cstack.base;
  137. L->Cstack = oldCLS;
  138. return firstResult;
  139. }
  140. void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
  141. StkId base = L->top - nParams;
  142. luaD_openstack(L, base);
  143. *base = *f;
  144. luaD_call(L, base, nResults);
  145. }
  146. /*
  147. ** Call a function (C or Lua). The function to be called is at *func.
  148. ** The arguments are on the stack, right after the function.
  149. ** When returns, the results are on the stack, starting at the original
  150. ** function position.
  151. ** The number of results is nResults, unless nResults=MULT_RET.
  152. */
  153. void luaD_call (lua_State *L, StkId func, int nResults) {
  154. StkId firstResult;
  155. lua_Hook callhook = L->callhook;
  156. retry: /* for `function' tag method */
  157. switch (ttype(func)) {
  158. case TAG_LCLOSURE: {
  159. ttype(func) = TAG_LMARK;
  160. if (callhook)
  161. luaD_callHook(L, func, callhook, "call");
  162. firstResult = luaV_execute(L, clvalue(func), func+1);
  163. break;
  164. }
  165. case TAG_CCLOSURE: {
  166. ttype(func) = TAG_CMARK;
  167. if (callhook)
  168. luaD_callHook(L, func, callhook, "call");
  169. firstResult = callCclosure(L, clvalue(func), func+1);
  170. break;
  171. }
  172. default: { /* `func' is not a function; check the `function' tag method */
  173. const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
  174. if (ttype(im) == TAG_NIL)
  175. luaG_callerror(L, func);
  176. luaD_openstack(L, func);
  177. *func = *im; /* tag method is the new function to be called */
  178. goto retry; /* retry the call */
  179. }
  180. }
  181. if (callhook) /* same hook that was active at entry */
  182. luaD_callHook(L, func, callhook, "return");
  183. /* adjust the number of results */
  184. if (nResults == MULT_RET)
  185. nResults = L->top - firstResult;
  186. else
  187. luaD_adjusttop(L, firstResult, nResults);
  188. /* move results to `func' (to erase parameters and function) */
  189. while (nResults) {
  190. *func++ = *(L->top - nResults);
  191. nResults--;
  192. }
  193. L->top = func;
  194. }
  195. static void message (lua_State *L, const char *s) {
  196. const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
  197. if (*luaO_typename(em) == 'f') {
  198. *L->top = *em;
  199. incr_top;
  200. lua_pushstring(L, s);
  201. luaD_call(L, L->top-2, 0);
  202. }
  203. }
  204. /*
  205. ** Reports an error, and jumps up to the available recovery label
  206. */
  207. void lua_error (lua_State *L, const char *s) {
  208. if (s) message(L, s);
  209. if (L->errorJmp)
  210. longjmp(L->errorJmp->b, 1);
  211. else {
  212. message(L, "unable to recover; exiting\n");
  213. exit(1);
  214. }
  215. }
  216. /*
  217. ** Execute a protected call. Assumes that function is at Cstack.base and
  218. ** parameters are on top of it.
  219. */
  220. int luaD_protectedrun (lua_State *L) {
  221. struct lua_longjmp myErrorJmp;
  222. StkId base = L->Cstack.base;
  223. int numCblocks = L->numCblocks;
  224. int status;
  225. struct lua_longjmp *volatile oldErr = L->errorJmp;
  226. L->errorJmp = &myErrorJmp;
  227. if (setjmp(myErrorJmp.b) == 0) {
  228. luaD_call(L, base, MULT_RET);
  229. L->Cstack.lua2C = base; /* position of the new results */
  230. L->Cstack.num = L->top - base;
  231. L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
  232. status = 0;
  233. }
  234. else { /* an error occurred: restore the stack */
  235. L->Cstack.num = 0; /* no results */
  236. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  237. L->numCblocks = numCblocks;
  238. restore_stack_limit(L);
  239. status = 1;
  240. }
  241. L->errorJmp = oldErr;
  242. return status;
  243. }
  244. /*
  245. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  246. */
  247. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  248. struct lua_longjmp myErrorJmp;
  249. StkId base = L->Cstack.base;
  250. int numCblocks = L->numCblocks;
  251. int status;
  252. Proto *volatile tf;
  253. struct lua_longjmp *volatile oldErr = L->errorJmp;
  254. L->errorJmp = &myErrorJmp;
  255. L->top = base; /* clear C2Lua */
  256. if (setjmp(myErrorJmp.b) == 0) {
  257. tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
  258. status = 0;
  259. }
  260. else { /* an error occurred: restore Cstack and top */
  261. L->Cstack.num = 0; /* no results */
  262. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  263. L->numCblocks = numCblocks;
  264. tf = NULL;
  265. status = 1;
  266. }
  267. L->errorJmp = oldErr;
  268. if (status) return 1; /* error code */
  269. if (tf == NULL) return 2; /* `natural' end */
  270. luaV_Lclosure(L, tf, 0);
  271. return 0;
  272. }
  273. static int do_main (lua_State *L, ZIO *z, int bin) {
  274. int status;
  275. int debug = L->debug; /* save debug status */
  276. do {
  277. long old_blocks = (luaC_checkGC(L), L->nblocks);
  278. status = protectedparser(L, z, bin);
  279. if (status == 1) return 1; /* error */
  280. else if (status == 2) return 0; /* `natural' end */
  281. else {
  282. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  283. L->GCthreshold += newelems2;
  284. status = luaD_protectedrun(L);
  285. L->GCthreshold -= newelems2;
  286. }
  287. } while (bin && status == 0);
  288. L->debug = debug; /* restore debug status */
  289. return status;
  290. }
  291. #define MAXFILENAME 260 /* maximum part of a file name kept */
  292. int lua_dofile (lua_State *L, const char *filename) {
  293. ZIO z;
  294. int status;
  295. int bin; /* flag for file mode */
  296. int c; /* look ahead char */
  297. char source[MAXFILENAME];
  298. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  299. if (f == NULL) return 2; /* unable to open file */
  300. luaL_filesource(source, filename, sizeof(source));
  301. c = fgetc(f);
  302. ungetc(c, f);
  303. bin = (c == ID_CHUNK);
  304. if (bin && f != stdin) {
  305. f = freopen(filename, "rb", f); /* set binary mode */
  306. if (f == NULL) return 2; /* unable to reopen file */
  307. }
  308. luaZ_Fopen(&z, f, source);
  309. status = do_main(L, &z, bin);
  310. if (f != stdin)
  311. fclose(f);
  312. return status;
  313. }
  314. int lua_dostring (lua_State *L, const char *str) {
  315. return lua_dobuffer(L, str, strlen(str), str);
  316. }
  317. int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) {
  318. ZIO z;
  319. if (!name) name = "?";
  320. luaZ_mopen(&z, buff, size, name);
  321. return do_main(L, &z, buff[0]==ID_CHUNK);
  322. }