ldo.c 9.4 KB

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