ldo.c 11 KB

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