ldo.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. ** $Id: ldo.c,v 1.137 2001/07/12 19:34:03 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_PRIVATE
  11. #include "lua.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 "lundump.h"
  23. #include "lvm.h"
  24. #include "lzio.h"
  25. /* space to handle stack overflow errors */
  26. #define EXTRA_STACK (2*LUA_MINSTACK)
  27. static void restore_stack_limit (lua_State *L) {
  28. StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1;
  29. if (L->top < limit)
  30. L->stack_last = limit;
  31. }
  32. void luaD_init (lua_State *L, int stacksize) {
  33. stacksize += EXTRA_STACK;
  34. L->stack = luaM_newvector(L, stacksize, TObject);
  35. L->stacksize = stacksize;
  36. L->basefunc.base = L->top = L->stack;
  37. restore_stack_limit(L);
  38. }
  39. void luaD_stackerror (lua_State *L) {
  40. if (L->stack_last == L->stack+L->stacksize-1) {
  41. /* overflow while handling overflow */
  42. luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
  43. }
  44. else {
  45. L->stack_last += EXTRA_STACK; /* to be used by error message */
  46. lua_assert(L->stack_last == L->stack+L->stacksize-1);
  47. luaD_error(L, l_s("stack overflow"));
  48. }
  49. }
  50. /*
  51. ** adjust top to new value; assume that new top is valid
  52. */
  53. void luaD_adjusttop (lua_State *L, StkId newtop) {
  54. while (L->top < newtop)
  55. setnilvalue(L->top++);
  56. L->top = newtop; /* `newtop' could be lower than `top' */
  57. }
  58. /*
  59. ** Open a hole inside the stack at `pos'
  60. */
  61. static void luaD_openstack (lua_State *L, StkId pos) {
  62. int i = L->top-pos;
  63. while (i--) setobj(pos+i+1, pos+i);
  64. incr_top;
  65. }
  66. static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
  67. StkId old_top = L->top;
  68. luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
  69. L->allowhooks = 0; /* cannot call hooks inside a hook */
  70. lua_unlock(L);
  71. (*hook)(L, ar);
  72. lua_lock(L);
  73. lua_assert(L->allowhooks == 0);
  74. L->allowhooks = 1;
  75. L->top = old_top;
  76. }
  77. void luaD_lineHook (lua_State *L, int line, lua_Hook linehook) {
  78. if (L->allowhooks) {
  79. lua_Debug ar;
  80. ar.event = l_s("line");
  81. ar._ci = L->ci;
  82. ar.currentline = line;
  83. dohook(L, &ar, linehook);
  84. }
  85. }
  86. static void luaD_callHook (lua_State *L, lua_Hook callhook,
  87. const l_char *event) {
  88. if (L->allowhooks) {
  89. lua_Debug ar;
  90. ar.event = event;
  91. ar._ci = L->ci;
  92. L->ci->pc = NULL; /* function is not active */
  93. dohook(L, &ar, callhook);
  94. }
  95. }
  96. static StkId callCclosure (lua_State *L, const struct Closure *cl) {
  97. int nup = cl->nupvalues; /* number of upvalues */
  98. int n;
  99. luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
  100. for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
  101. setobj(L->top++, &cl->upvalue[n]);
  102. lua_unlock(L);
  103. n = (*cl->f.c)(L); /* do the actual call */
  104. lua_lock(L);
  105. return L->top - n; /* return index of first result */
  106. }
  107. /*
  108. ** Call a function (C or Lua). The function to be called is at *func.
  109. ** The arguments are on the stack, right after the function.
  110. ** When returns, all the results are on the stack, starting at the original
  111. ** function position.
  112. */
  113. void luaD_call (lua_State *L, StkId func) {
  114. lua_Hook callhook;
  115. StkId firstResult;
  116. CallInfo ci;
  117. if (ttype(func) != LUA_TFUNCTION) {
  118. /* `func' is not a function; check the `function' tag method */
  119. Closure *tm = luaT_gettmbyObj(G(L), func, TM_FUNCTION);
  120. if (tm == NULL)
  121. luaG_typeerror(L, func, l_s("call"));
  122. luaD_openstack(L, func);
  123. setclvalue(func, tm); /* tag method is the new function to be called */
  124. }
  125. ci.prev = L->ci; /* chain new callinfo */
  126. L->ci = &ci;
  127. ci.base = func+1;
  128. callhook = L->callhook;
  129. if (callhook)
  130. luaD_callHook(L, callhook, l_s("call"));
  131. firstResult = (clvalue(func)->isC ? callCclosure(L, clvalue(func)) :
  132. luaV_execute(L, clvalue(func), func+1));
  133. if (callhook) /* same hook that was active at entry */
  134. luaD_callHook(L, callhook, l_s("return"));
  135. L->ci = ci.prev; /* unchain callinfo */
  136. /* move results to `func' (to erase parameters and function) */
  137. while (firstResult < L->top)
  138. setobj(func++, firstResult++);
  139. L->top = func;
  140. luaC_checkGC(L);
  141. }
  142. /*
  143. ** Execute a protected call.
  144. */
  145. struct CallS { /* data to `f_call' */
  146. StkId func;
  147. int nresults;
  148. };
  149. static void f_call (lua_State *L, void *ud) {
  150. struct CallS *c = (struct CallS *)ud;
  151. luaD_call(L, c->func);
  152. if (c->nresults != LUA_MULTRET)
  153. luaD_adjusttop(L, c->func + c->nresults);
  154. }
  155. LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
  156. StkId func;
  157. struct CallS c;
  158. int status;
  159. lua_lock(L);
  160. func = L->top - (nargs+1); /* function to be called */
  161. c.func = func; c.nresults = nresults;
  162. status = luaD_runprotected(L, f_call, &c);
  163. if (status != 0) /* an error occurred? */
  164. L->top = func; /* remove parameters from the stack */
  165. lua_unlock(L);
  166. return status;
  167. }
  168. /*
  169. ** Execute a protected parser.
  170. */
  171. struct SParser { /* data to `f_parser' */
  172. ZIO *z;
  173. int bin;
  174. };
  175. static void f_parser (lua_State *L, void *ud) {
  176. struct SParser *p = (struct SParser *)ud;
  177. Proto *tf = p->bin ? luaU_undump(L, p->z) : luaY_parser(L, p->z);
  178. luaV_Lclosure(L, tf, 0);
  179. }
  180. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  181. struct SParser p;
  182. lu_mem old_blocks;
  183. int status;
  184. lua_lock(L);
  185. p.z = z; p.bin = bin;
  186. /* before parsing, give a (good) chance to GC */
  187. if (G(L)->nblocks/8 >= G(L)->GCthreshold/10)
  188. luaC_collectgarbage(L);
  189. old_blocks = G(L)->nblocks;
  190. status = luaD_runprotected(L, f_parser, &p);
  191. if (status == 0) {
  192. /* add new memory to threshold (as it probably will stay) */
  193. lua_assert(G(L)->nblocks >= old_blocks);
  194. G(L)->GCthreshold += (G(L)->nblocks - old_blocks);
  195. }
  196. else if (status == LUA_ERRRUN) /* an error occurred: correct error code */
  197. status = LUA_ERRSYNTAX;
  198. lua_unlock(L);
  199. return status;
  200. }
  201. LUA_API int lua_loadfile (lua_State *L, const l_char *filename) {
  202. ZIO z;
  203. int status;
  204. int bin; /* flag for file mode */
  205. int nlevel; /* level on the stack of filename */
  206. FILE *f = (filename == NULL) ? stdin : fopen(filename, l_s("r"));
  207. if (f == NULL) return LUA_ERRFILE; /* unable to open file */
  208. bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
  209. if (bin && f != stdin) {
  210. fclose(f);
  211. f = fopen(filename, l_s("rb")); /* reopen in binary mode */
  212. if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
  213. }
  214. lua_pushliteral(L, l_s("@"));
  215. lua_pushstring(L, (filename == NULL) ? l_s("(stdin)") : filename);
  216. lua_concat(L, 2);
  217. nlevel = lua_gettop(L);
  218. filename = lua_tostring(L, -1); /* filename = `@'..filename */
  219. luaZ_Fopen(&z, f, filename);
  220. status = protectedparser(L, &z, bin);
  221. lua_remove(L, nlevel); /* remove filename */
  222. if (f != stdin)
  223. fclose(f);
  224. return status;
  225. }
  226. LUA_API int lua_loadbuffer (lua_State *L, const l_char *buff, size_t size,
  227. const l_char *name) {
  228. ZIO z;
  229. int status;
  230. if (!name) name = l_s("?");
  231. luaZ_mopen(&z, buff, size, name);
  232. status = protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
  233. return status;
  234. }
  235. /*
  236. ** {======================================================
  237. ** Error-recover functions (based on long jumps)
  238. ** =======================================================
  239. */
  240. /* chain list of long jump buffers */
  241. struct lua_longjmp {
  242. jmp_buf b;
  243. struct lua_longjmp *previous;
  244. volatile int status; /* error code */
  245. };
  246. static void message (lua_State *L, const l_char *s) {
  247. StkId top = L->top;
  248. luaV_getglobal(L, luaS_newliteral(L, l_s(LUA_ERRORMESSAGE)), top);
  249. if (ttype(top) == LUA_TFUNCTION) {
  250. incr_top;
  251. setsvalue(top+1, luaS_new(L, s));
  252. incr_top;
  253. luaD_call(L, top);
  254. L->top = top;
  255. }
  256. }
  257. /*
  258. ** Reports an error, and jumps up to the available recovery label
  259. */
  260. void luaD_error (lua_State *L, const l_char *s) {
  261. if (s) message(L, s);
  262. luaD_breakrun(L, LUA_ERRRUN);
  263. }
  264. void luaD_breakrun (lua_State *L, int errcode) {
  265. if (L->errorJmp) {
  266. L->errorJmp->status = errcode;
  267. longjmp(L->errorJmp->b, 1);
  268. }
  269. else {
  270. if (errcode != LUA_ERRMEM)
  271. message(L, l_s("unable to recover; exiting\n"));
  272. exit(EXIT_FAILURE);
  273. }
  274. }
  275. int luaD_runprotected (lua_State *L, void (*f)(lua_State *, void *), void *ud) {
  276. CallInfo *oldci = L->ci;
  277. StkId oldtop = L->top;
  278. struct lua_longjmp lj;
  279. int allowhooks = L->allowhooks;
  280. lj.status = 0;
  281. lj.previous = L->errorJmp; /* chain new error handler */
  282. L->errorJmp = &lj;
  283. if (setjmp(lj.b) == 0)
  284. (*f)(L, ud);
  285. else { /* an error occurred: restore the state */
  286. L->allowhooks = allowhooks;
  287. L->ci = oldci;
  288. L->top = oldtop;
  289. restore_stack_limit(L);
  290. }
  291. L->errorJmp = lj.previous; /* restore old error handler */
  292. return lj.status;
  293. }
  294. /* }====================================================== */