ldo.c 9.4 KB

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