ldo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. ** $Id: ldo.c,v 1.57 1999/12/06 11:43:58 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 "ldo.h"
  13. #include "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lparser.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltm.h"
  20. #include "lua.h"
  21. #include "luadebug.h"
  22. #include "lundump.h"
  23. #include "lvm.h"
  24. #include "lzio.h"
  25. #define EXTRA_STACK 32 /* space to handle stack overflow errors */
  26. /*
  27. ** typical numer of stack slots used by a (big) function
  28. ** (this constant is used only for choosing error messages)
  29. */
  30. #define SLOTS_PER_F 20
  31. void luaD_init (lua_State *L, int stacksize) {
  32. L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
  33. L->stack_last = L->stack+(stacksize-1);
  34. L->stacksize = stacksize;
  35. L->Cstack.base = L->Cstack.lua2C = L->top = L->stack;
  36. L->Cstack.num = 0;
  37. }
  38. void luaD_checkstack (lua_State *L, int n) {
  39. if (L->stack_last-L->top <= n) { /* stack overflow? */
  40. if (L->stack_last-L->stack > (L->stacksize-1)) {
  41. /* overflow while handling overflow: do what?? */
  42. L->top -= EXTRA_STACK;
  43. lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!!");
  44. }
  45. else {
  46. L->stack_last += EXTRA_STACK; /* to be used by error message */
  47. if (lua_stackedfunction(L, L->stacksize/SLOTS_PER_F) == LUA_NOOBJECT) {
  48. /* too few funcs on stack: doesn't look like a rec. loop */
  49. lua_error(L, "Lua2C - C2Lua overflow");
  50. }
  51. else
  52. lua_error(L, "stack size overflow");
  53. }
  54. }
  55. }
  56. static void restore_stack_limit (lua_State *L) {
  57. if (L->top-L->stack < L->stacksize-1)
  58. L->stack_last = L->stack+(L->stacksize-1);
  59. }
  60. /*
  61. ** Adjust stack. Set top to base+extra, pushing NILs if needed.
  62. ** (we cannot add base+extra unless we are sure it fits in the stack;
  63. ** otherwise the result of such operation on pointers is undefined)
  64. */
  65. void luaD_adjusttop (lua_State *L, StkId base, int extra) {
  66. int diff = extra-(L->top-base);
  67. if (diff <= 0)
  68. L->top = base+extra;
  69. else {
  70. luaD_checkstack(L, diff);
  71. while (diff--)
  72. ttype(L->top++) = LUA_T_NIL;
  73. }
  74. }
  75. /*
  76. ** Open a hole inside the stack at `pos'
  77. */
  78. void luaD_openstack (lua_State *L, StkId pos) {
  79. luaO_memup(pos+1, pos, (L->top-pos)*sizeof(TObject));
  80. incr_top;
  81. }
  82. void luaD_lineHook (lua_State *L, int line) {
  83. struct C_Lua_Stack oldCLS = L->Cstack;
  84. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
  85. L->Cstack.num = 0;
  86. (*L->linehook)(L, line);
  87. L->top = old_top;
  88. L->Cstack = oldCLS;
  89. }
  90. void luaD_callHook (lua_State *L, StkId base, const TProtoFunc *tf,
  91. int isreturn) {
  92. struct C_Lua_Stack oldCLS = L->Cstack;
  93. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->top;
  94. L->Cstack.num = 0;
  95. if (isreturn)
  96. (*L->callhook)(L, LUA_NOOBJECT, "(return)", 0);
  97. else {
  98. TObject *f = base-1;
  99. if (tf)
  100. v 1.3 1997/10/16, f, tf->source->str, tf->lineDefined);
  101. else (*L->callhook)(L, f, "(C)", -1);
  102. }
  103. L->top = old_top;
  104. L->Cstack = oldCLS;
  105. }
  106. /*
  107. ** Call a C function.
  108. ** Cstack.num is the number of arguments; Cstack.lua2C points to the
  109. ** first argument. Returns an index to the first result from C.
  110. */
  111. static StkId callC (lua_State *L, lua_CFunction f, StkId base) {
  112. struct C_Lua_Stack oldCLS = L->Cstack;
  113. StkId firstResult;
  114. int numarg = L->top - base;
  115. L->Cstack.num = numarg;
  116. L->Cstack.lua2C = base;
  117. L->Cstack.base = L->top;
  118. if (L->callhook)
  119. luaD_callHook(L, base, NULL, 0);
  120. (*f)(L); /* do the actual call */
  121. if (L->callhook) /* test again: `func' may change callhook */
  122. luaD_callHook(L, base, NULL, 1);
  123. firstResult = L->Cstack.base;
  124. L->Cstack = oldCLS;
  125. return firstResult;
  126. }
  127. static StkId callCclosure (lua_State *L, const struct Closure *cl,
  128. lua_CFunction f, StkId base) {
  129. int nup = cl->nelems; /* number of upvalues */
  130. luaD_checkstack(L, nup);
  131. /* open space for upvalues as extra arguments */
  132. luaO_memup(base+nup, base, (L->top-base)*sizeof(TObject));
  133. /* copy upvalues into stack */
  134. memcpy(base, cl->consts+1, nup*sizeof(TObject));
  135. L->top += nup;
  136. return callC(L, f, base);
  137. }
  138. void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
  139. StkId base = L->top - nParams;
  140. luaD_openstack(L, base);
  141. *base = *f;
  142. luaD_call(L, base, nResults);
  143. }
  144. /*
  145. ** Call a function (C or Lua). The function to be called is at *func.
  146. ** The arguments are on the stack, right after the function.
  147. ** When returns, the results are on the stack, starting at the original
  148. ** function position.
  149. ** The number of results is nResults, unless nResults=MULT_RET.
  150. */
  151. void luaD_call (lua_State *L, StkId func, int nResults) {
  152. StkId firstResult;
  153. switch (ttype(func)) {
  154. case LUA_T_CPROTO:
  155. ttype(func) = LUA_T_CMARK;
  156. firstResult = callC(L, fvalue(func), func+1);
  157. break;
  158. case LUA_T_PROTO:
  159. ttype(func) = LUA_T_PMARK;
  160. firstResult = luaV_execute(L, NULL, tfvalue(func), func+1);
  161. break;
  162. case LUA_T_CLOSURE: {
  163. Closure *c = clvalue(func);
  164. TObject *proto = c->consts;
  165. ttype(func) = LUA_T_CLMARK;
  166. firstResult = (ttype(proto) == LUA_T_CPROTO) ?
  167. callCclosure(L, c, fvalue(proto), func+1) :
  168. luaV_execute(L, c, tfvalue(proto), func+1);
  169. break;
  170. }
  171. default: { /* `func' is not a function */
  172. /* Check the tag method for invalid functions */
  173. const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
  174. if (ttype(im) == LUA_T_NIL)
  175. lua_error(L, "call expression not a function");
  176. luaD_callTM(L, im, L->top-func, nResults);
  177. return;
  178. }
  179. }
  180. /* adjust the number of results */
  181. if (nResults == MULT_RET)
  182. nResults = L->top - firstResult;
  183. else
  184. luaD_adjusttop(L, firstResult, nResults);
  185. /* move results to func (to erase parameters and function) */
  186. luaO_memdown(func, firstResult, nResults*sizeof(TObject));
  187. L->top = func+nResults;
  188. }
  189. static void message (lua_State *L, const char *s) {
  190. const TObject *em = &(luaS_assertglobalbyname(L, "_ERRORMESSAGE")->value);
  191. if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO ||
  192. ttype(em) == LUA_T_CLOSURE) {
  193. *L->top = *em;
  194. incr_top;
  195. lua_pushstring(L, s);
  196. luaD_call(L, L->top-2, 0);
  197. }
  198. }
  199. /*
  200. ** Reports an error, and jumps up to the available recover label
  201. */
  202. void lua_error (lua_State *L, const char *s) {
  203. if (s) message(L, s);
  204. if (L->errorJmp)
  205. longjmp(L->errorJmp->b, 1);
  206. else {
  207. message(L, "exit(1). Unable to recover.\n");
  208. exit(1);
  209. }
  210. }
  211. /*
  212. ** Execute a protected call. Assumes that function is at Cstack.base and
  213. ** parameters are on top of it.
  214. */
  215. int luaD_protectedrun (lua_State *L) {
  216. struct lua_longjmp myErrorJmp;
  217. volatile StkId base = L->Cstack.base;
  218. volatile int numCblocks = L->numCblocks;
  219. volatile int status;
  220. struct lua_longjmp *volatile oldErr = L->errorJmp;
  221. L->errorJmp = &myErrorJmp;
  222. if (setjmp(myErrorJmp.b) == 0) {
  223. luaD_call(L, base, MULT_RET);
  224. L->Cstack.lua2C = base; /* position of the new results */
  225. L->Cstack.num = L->top - base;
  226. L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */
  227. status = 0;
  228. }
  229. else { /* an error occurred: restore the stack */
  230. L->Cstack.num = 0; /* no results */
  231. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  232. L->numCblocks = numCblocks;
  233. restore_stack_limit(L);
  234. status = 1;
  235. }
  236. L->errorJmp = oldErr;
  237. return status;
  238. }
  239. /*
  240. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  241. */
  242. static int protectedparser (lua_State *L, ZIO *z, int bin) {
  243. struct lua_longjmp myErrorJmp;
  244. volatile StkId base = L->Cstack.base;
  245. volatile int numCblocks = L->numCblocks;
  246. volatile int status;
  247. TProtoFunc *volatile tf;
  248. struct lua_longjmp *volatile oldErr = L->errorJmp;
  249. L->errorJmp = &myErrorJmp;
  250. L->top = base; /* erase C2Lua */
  251. if (setjmp(myErrorJmp.b) == 0) {
  252. tf = bin ? luaU_undump1(L, z) : luaY_parser(L, z);
  253. status = 0;
  254. }
  255. else { /* an error occurred: restore Cstack and top */
  256. L->Cstack.num = 0; /* no results */
  257. L->top = L->Cstack.base = L->Cstack.lua2C = base;
  258. L->numCblocks = numCblocks;
  259. tf = NULL;
  260. status = 1;
  261. }
  262. L->errorJmp = oldErr;
  263. if (status) return 1; /* error code */
  264. if (tf == NULL) return 2; /* `natural' end */
  265. L->top->ttype = LUA_T_PROTO; /* push new function on the stack */
  266. L->top->value.tf = tf;
  267. incr_top;
  268. return 0;
  269. }
  270. static int do_main (lua_State *L, ZIO *z, int bin) {
  271. int status;
  272. int debug = L->debug; /* save debug status */
  273. do {
  274. long old_blocks = (luaC_checkGC(L), L->nblocks);
  275. status = protectedparser(L, z, bin);
  276. if (status == 1) return 1; /* error */
  277. else if (status == 2) return 0; /* `natural' end */
  278. else {
  279. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  280. L->GCthreshold += newelems2;
  281. status = luaD_protectedrun(L);
  282. L->GCthreshold -= newelems2;
  283. }
  284. } while (bin && status == 0);
  285. L->debug = debug; /* restore debug status */
  286. return status;
  287. }
  288. void luaD_gcIM (lua_State *L, const TObject *o) {
  289. const TObject *im = luaT_getimbyObj(L, o, IM_GC);
  290. if (ttype(im) != LUA_T_NIL) {
  291. *L->top = *o;
  292. incr_top;
  293. luaD_callTM(L, im, 1, 0);
  294. }
  295. }
  296. #define MAXFILENAME 260 /* maximum part of a file name kept */
  297. int lua_dofile (lua_State *L, const char *filename) {
  298. ZIO z;
  299. int status;
  300. int c;
  301. int bin;
  302. char source[MAXFILENAME];
  303. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  304. if (f == NULL)
  305. return 2;
  306. c = fgetc(f);
  307. ungetc(c, f);
  308. bin = (c == ID_CHUNK);
  309. if (bin)
  310. f = freopen(filename, "rb", f); /* set binary mode */
  311. luaL_filesource(source, filename, sizeof(source));
  312. luaZ_Fopen(&z, f, source);
  313. status = do_main(L, &z, bin);
  314. if (f != stdin)
  315. fclose(f);
  316. return status;
  317. }
  318. int lua_dostring (lua_State *L, const char *str) {
  319. return lua_dobuffer(L, str, strlen(str), str);
  320. }
  321. int lua_dobuffer (lua_State *L, const char *buff, int size, const char *name) {
  322. ZIO z;
  323. if (!name) name = "?";
  324. luaZ_mopen(&z, buff, size, name);
  325. return do_main(L, &z, buff[0]==ID_CHUNK);
  326. }