ldo.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. ** $Id: ldo.c,v 1.19 1997/12/23 12:50:49 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. #include "ldo.h"
  11. #include "lfunc.h"
  12. #include "lgc.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lparser.h"
  16. #include "lstate.h"
  17. #include "ltm.h"
  18. #include "lua.h"
  19. #include "luadebug.h"
  20. #include "lundump.h"
  21. #include "lvm.h"
  22. #include "lzio.h"
  23. #ifndef STACK_LIMIT
  24. #define STACK_LIMIT 6000
  25. #endif
  26. /*
  27. ** Error messages
  28. */
  29. static void stderrorim (void)
  30. {
  31. fprintf(stderr, "lua error: %s\n", lua_getstring(lua_getparam(1)));
  32. }
  33. #define STACK_UNIT 128
  34. void luaD_init (void)
  35. {
  36. L->stack.stack = luaM_newvector(STACK_UNIT, TObject);
  37. L->stack.top = L->stack.stack;
  38. L->stack.last = L->stack.stack+(STACK_UNIT-1);
  39. ttype(&L->errorim) = LUA_T_CPROTO;
  40. fvalue(&L->errorim) = stderrorim;
  41. }
  42. void luaD_checkstack (int n)
  43. {
  44. struct Stack *S = &L->stack;
  45. if (S->last-S->top <= n) {
  46. StkId top = S->top-S->stack;
  47. int stacksize = (S->last-S->stack)+1+STACK_UNIT+n;
  48. S->stack = luaM_reallocvector(S->stack, stacksize, TObject);
  49. S->last = S->stack+(stacksize-1);
  50. S->top = S->stack + top;
  51. if (stacksize >= STACK_LIMIT) { /* stack overflow? */
  52. if (lua_stackedfunction(100) == LUA_NOOBJECT) /* 100 funcs on stack? */
  53. lua_error("Lua2C - C2Lua overflow"); /* doesn't look like a rec. loop */
  54. else
  55. lua_error("stack size overflow");
  56. }
  57. }
  58. }
  59. /*
  60. ** Adjust stack. Set top to the given value, pushing NILs if needed.
  61. */
  62. void luaD_adjusttop (StkId newtop)
  63. {
  64. int diff = newtop-(L->stack.top-L->stack.stack);
  65. if (diff <= 0)
  66. L->stack.top += diff;
  67. else {
  68. luaD_checkstack(diff);
  69. while (diff--)
  70. ttype(L->stack.top++) = LUA_T_NIL;
  71. }
  72. }
  73. /*
  74. ** Open a hole below "nelems" from the L->stack.top.
  75. */
  76. void luaD_openstack (int nelems)
  77. {
  78. luaO_memup(L->stack.top-nelems+1, L->stack.top-nelems,
  79. nelems*sizeof(TObject));
  80. incr_top;
  81. }
  82. void luaD_lineHook (int line)
  83. {
  84. struct C_Lua_Stack oldCLS = L->Cstack;
  85. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
  86. L->Cstack.num = 0;
  87. (*lua_linehook)(line);
  88. L->stack.top = L->stack.stack+old_top;
  89. L->Cstack = oldCLS;
  90. }
  91. void luaD_callHook (StkId base, TProtoFunc *tf, int isreturn)
  92. {
  93. struct C_Lua_Stack oldCLS = L->Cstack;
  94. StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack;
  95. L->Cstack.num = 0;
  96. if (isreturn)
  97. (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
  98. else {
  99. TObject *f = L->stack.stack+base-1;
  100. if (tf)
  101. (*lua_callhook)(Ref(f), tf->fileName->str, tf->lineDefined);
  102. else
  103. (*lua_callhook)(Ref(f), "(C)", -1);
  104. }
  105. L->stack.top = L->stack.stack+old_top;
  106. L->Cstack = oldCLS;
  107. }
  108. /*
  109. ** Call a C function.
  110. ** Cstack.num is the number of arguments; Cstack.lua2C points to the
  111. ** first argument. Returns an index to the first result from C.
  112. */
  113. static StkId callC (lua_CFunction f, StkId base)
  114. {
  115. struct C_Lua_Stack *CS = &L->Cstack;
  116. struct C_Lua_Stack oldCLS = *CS;
  117. StkId firstResult;
  118. int numarg = (L->stack.top-L->stack.stack) - base;
  119. CS->num = numarg;
  120. CS->lua2C = base;
  121. CS->base = base+numarg; /* == top-stack */
  122. if (lua_callhook)
  123. luaD_callHook(base, NULL, 0);
  124. (*f)(); /* do the actual call */
  125. if (lua_callhook) /* func may have changed lua_callhook */
  126. luaD_callHook(base, NULL, 1);
  127. firstResult = CS->base;
  128. *CS = oldCLS;
  129. return firstResult;
  130. }
  131. void luaD_callTM (TObject *f, int nParams, int nResults)
  132. {
  133. luaD_openstack(nParams);
  134. *(L->stack.top-nParams-1) = *f;
  135. luaD_call((L->stack.top-L->stack.stack)-nParams, nResults);
  136. }
  137. /*
  138. ** Call a function (C or Lua). The parameters must be on the L->stack.stack,
  139. ** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
  140. ** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
  141. ** The number of results is nResults, unless nResults=MULT_RET.
  142. */
  143. void luaD_call (StkId base, int nResults)
  144. {
  145. StkId firstResult;
  146. TObject *func = L->stack.stack+base-1;
  147. int i;
  148. switch (ttype(func)) {
  149. case LUA_T_CPROTO:
  150. ttype(func) = LUA_T_CMARK;
  151. firstResult = callC(fvalue(func), base);
  152. break;
  153. case LUA_T_PROTO:
  154. ttype(func) = LUA_T_PMARK;
  155. firstResult = luaV_execute(NULL, tfvalue(func), base);
  156. break;
  157. case LUA_T_CLOSURE: {
  158. Closure *c = clvalue(func);
  159. TObject *proto = &(c->consts[0]);
  160. ttype(func) = LUA_T_CLMARK;
  161. firstResult = (ttype(proto) == LUA_T_CPROTO) ?
  162. callC(fvalue(proto), base) :
  163. luaV_execute(c, tfvalue(proto), base);
  164. break;
  165. }
  166. default: { /* func is not a function */
  167. /* Check the tag method for invalid functions */
  168. TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
  169. if (ttype(im) == LUA_T_NIL)
  170. lua_error("call expression not a function");
  171. luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults);
  172. return;
  173. }
  174. }
  175. /* adjust the number of results */
  176. if (nResults != MULT_RET)
  177. luaD_adjusttop(firstResult+nResults);
  178. /* move results to base-1 (to erase parameters and function) */
  179. base--;
  180. nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
  181. for (i=0; i<nResults; i++)
  182. *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
  183. L->stack.top -= firstResult-base;
  184. }
  185. /*
  186. ** Traverse all objects on L->stack.stack
  187. */
  188. void luaD_travstack (int (*fn)(TObject *))
  189. {
  190. StkId i;
  191. for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
  192. fn(L->stack.stack+i);
  193. }
  194. static void message (char *s)
  195. {
  196. TObject im = L->errorim;
  197. if (ttype(&im) != LUA_T_NIL) {
  198. lua_pushstring(s);
  199. luaD_callTM(&im, 1, 0);
  200. }
  201. }
  202. /*
  203. ** Reports an error, and jumps up to the available recover label
  204. */
  205. void lua_error (char *s)
  206. {
  207. if (s) message(s);
  208. if (L->errorJmp)
  209. longjmp(*((jmp_buf *)L->errorJmp), 1);
  210. else {
  211. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  212. exit(1);
  213. }
  214. }
  215. /*
  216. ** Call the function at L->Cstack.base, and incorporate results on
  217. ** the Lua2C structure.
  218. */
  219. static void do_callinc (int nResults)
  220. {
  221. StkId base = L->Cstack.base;
  222. luaD_call(base+1, nResults);
  223. L->Cstack.lua2C = base; /* position of the luaM_new results */
  224. L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
  225. L->Cstack.base = base + L->Cstack.num; /* incorporate results on L->stack.stack */
  226. }
  227. /*
  228. ** Execute a protected call. Assumes that function is at L->Cstack.base and
  229. ** parameters are on top of it. Leave nResults on the stack.
  230. */
  231. int luaD_protectedrun (int nResults)
  232. {
  233. jmp_buf myErrorJmp;
  234. int status;
  235. struct C_Lua_Stack oldCLS = L->Cstack;
  236. jmp_buf *oldErr = L->errorJmp;
  237. L->errorJmp = &myErrorJmp;
  238. if (setjmp(myErrorJmp) == 0) {
  239. do_callinc(nResults);
  240. status = 0;
  241. }
  242. else { /* an error occurred: restore L->Cstack and L->stack.top */
  243. L->Cstack = oldCLS;
  244. L->stack.top = L->stack.stack+L->Cstack.base;
  245. status = 1;
  246. }
  247. L->errorJmp = oldErr;
  248. return status;
  249. }
  250. /*
  251. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  252. */
  253. static int protectedparser (ZIO *z, int bin)
  254. {
  255. int status;
  256. TProtoFunc *tf;
  257. jmp_buf myErrorJmp;
  258. jmp_buf *oldErr = L->errorJmp;
  259. L->errorJmp = &myErrorJmp;
  260. if (setjmp(myErrorJmp) == 0) {
  261. tf = bin ? luaU_undump1(z) : luaY_parser(z);
  262. status = 0;
  263. }
  264. else {
  265. tf = NULL;
  266. status = 1;
  267. }
  268. L->errorJmp = oldErr;
  269. if (status) return 1; /* error code */
  270. if (tf == NULL) return 2; /* 'natural' end */
  271. luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
  272. L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
  273. L->stack.stack[L->Cstack.base].value.tf = tf;
  274. luaV_closure(0);
  275. return 0;
  276. }
  277. static int do_main (ZIO *z, int bin)
  278. {
  279. int status;
  280. do {
  281. long old_blocks = (luaC_checkGC(), L->nblocks);
  282. status = protectedparser(z, bin);
  283. if (status == 1) return 1; /* error */
  284. else if (status == 2) return 0; /* 'natural' end */
  285. else {
  286. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  287. L->GCthreshold += newelems2;
  288. status = luaD_protectedrun(MULT_RET);
  289. L->GCthreshold -= newelems2;
  290. }
  291. } while (bin && status == 0);
  292. return status;
  293. }
  294. void luaD_gcIM (TObject *o)
  295. {
  296. TObject *im = luaT_getimbyObj(o, IM_GC);
  297. if (ttype(im) != LUA_T_NIL) {
  298. *L->stack.top = *o;
  299. incr_top;
  300. luaD_callTM(im, 1, 0);
  301. }
  302. }
  303. int lua_dofile (char *filename)
  304. {
  305. ZIO z;
  306. int status;
  307. int c;
  308. int bin;
  309. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  310. if (f == NULL)
  311. return 2;
  312. if (filename == NULL)
  313. filename = "(stdin)";
  314. c = fgetc(f);
  315. ungetc(c, f);
  316. bin = (c == ID_CHUNK);
  317. if (bin)
  318. f = freopen(filename, "rb", f); /* set binary mode */
  319. luaZ_Fopen(&z, f, filename);
  320. status = do_main(&z, bin);
  321. if (f != stdin)
  322. fclose(f);
  323. return status;
  324. }
  325. #define SIZE_PREF 20 /* size of string prefix to appear in error messages */
  326. #define SSIZE_PREF "20"
  327. int lua_dostring (char *str)
  328. {
  329. int status;
  330. char name[SIZE_PREF+25];
  331. char *temp;
  332. ZIO z;
  333. if (str == NULL) return 1;
  334. sprintf(name, "(dostring) >> %." SSIZE_PREF "s", str);
  335. temp = strchr(name, '\n');
  336. if (temp) *temp = 0; /* end string after first line */
  337. luaZ_sopen(&z, str, name);
  338. status = do_main(&z, 0);
  339. return status;
  340. }
  341. #if 0
  342. int lua_dobuffer (char *buff, int size)
  343. {
  344. int status;
  345. ZIO z;
  346. luaZ_mopen(&z, buff, size, "(buffer)");
  347. status = do_main(&z, 1);
  348. return status;
  349. }
  350. #endif