2
0

ldo.c 9.2 KB

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