ldo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. ** $Id: ldo.c,v 1.26 1998/06/15 21:34:14 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. static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base)
  132. {
  133. TObject *pbase;
  134. int nup = cl->nelems; /* number of upvalues */
  135. luaD_checkstack(nup);
  136. pbase = L->stack.stack+base; /* care: previous call may change this */
  137. /* open space for upvalues as extra arguments */
  138. luaO_memup(pbase+nup, pbase, (L->stack.top-pbase)*sizeof(TObject));
  139. /* copy upvalues into stack */
  140. memcpy(pbase, cl->consts+1, nup*sizeof(TObject));
  141. L->stack.top += nup;
  142. return callC(f, base);
  143. }
  144. void luaD_callTM (TObject *f, int nParams, int nResults)
  145. {
  146. luaD_openstack(nParams);
  147. *(L->stack.top-nParams-1) = *f;
  148. luaD_call((L->stack.top-L->stack.stack)-nParams, nResults);
  149. }
  150. /*
  151. ** Call a function (C or Lua). The parameters must be on the L->stack.stack,
  152. ** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1.
  153. ** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top).
  154. ** The number of results is nResults, unless nResults=MULT_RET.
  155. */
  156. void luaD_call (StkId base, int nResults)
  157. {
  158. StkId firstResult;
  159. TObject *func = L->stack.stack+base-1;
  160. int i;
  161. switch (ttype(func)) {
  162. case LUA_T_CPROTO:
  163. ttype(func) = LUA_T_CMARK;
  164. firstResult = callC(fvalue(func), base);
  165. break;
  166. case LUA_T_PROTO:
  167. ttype(func) = LUA_T_PMARK;
  168. firstResult = luaV_execute(NULL, tfvalue(func), base);
  169. break;
  170. case LUA_T_CLOSURE: {
  171. Closure *c = clvalue(func);
  172. TObject *proto = &(c->consts[0]);
  173. ttype(func) = LUA_T_CLMARK;
  174. firstResult = (ttype(proto) == LUA_T_CPROTO) ?
  175. callCclosure(c, fvalue(proto), base) :
  176. luaV_execute(c, tfvalue(proto), base);
  177. break;
  178. }
  179. default: { /* func is not a function */
  180. /* Check the tag method for invalid functions */
  181. TObject *im = luaT_getimbyObj(func, IM_FUNCTION);
  182. if (ttype(im) == LUA_T_NIL)
  183. lua_error("call expression not a function");
  184. luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults);
  185. return;
  186. }
  187. }
  188. /* adjust the number of results */
  189. if (nResults != MULT_RET)
  190. luaD_adjusttop(firstResult+nResults);
  191. /* move results to base-1 (to erase parameters and function) */
  192. base--;
  193. nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */
  194. for (i=0; i<nResults; i++)
  195. *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i);
  196. L->stack.top -= firstResult-base;
  197. }
  198. /*
  199. ** Traverse all objects on L->stack.stack
  200. */
  201. void luaD_travstack (int (*fn)(TObject *))
  202. {
  203. StkId i;
  204. for (i = (L->stack.top-1)-L->stack.stack; i>=0; i--)
  205. fn(L->stack.stack+i);
  206. }
  207. static void message (char *s)
  208. {
  209. TObject im = L->errorim;
  210. if (ttype(&im) != LUA_T_NIL) {
  211. lua_pushstring(s);
  212. luaD_callTM(&im, 1, 0);
  213. }
  214. }
  215. /*
  216. ** Reports an error, and jumps up to the available recover label
  217. */
  218. void lua_error (char *s)
  219. {
  220. if (s) message(s);
  221. if (L->errorJmp)
  222. longjmp(*((jmp_buf *)L->errorJmp), 1);
  223. else {
  224. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  225. exit(1);
  226. }
  227. }
  228. /*
  229. ** Call the function at L->Cstack.base, and incorporate results on
  230. ** the Lua2C structure.
  231. */
  232. static void do_callinc (int nResults)
  233. {
  234. StkId base = L->Cstack.base;
  235. luaD_call(base+1, nResults);
  236. L->Cstack.lua2C = base; /* position of the luaM_new results */
  237. L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */
  238. L->Cstack.base = base + L->Cstack.num; /* incorporate results on L->stack.stack */
  239. }
  240. /*
  241. ** Execute a protected call. Assumes that function is at L->Cstack.base and
  242. ** parameters are on top of it. Leave nResults on the stack.
  243. */
  244. int luaD_protectedrun (int nResults)
  245. {
  246. jmp_buf myErrorJmp;
  247. int status;
  248. volatile struct C_Lua_Stack oldCLS = L->Cstack;
  249. jmp_buf *volatile oldErr = L->errorJmp;
  250. L->errorJmp = &myErrorJmp;
  251. if (setjmp(myErrorJmp) == 0) {
  252. do_callinc(nResults);
  253. status = 0;
  254. }
  255. else { /* an error occurred: restore L->Cstack and L->stack.top */
  256. L->Cstack = oldCLS;
  257. L->stack.top = L->stack.stack+L->Cstack.base;
  258. status = 1;
  259. }
  260. L->errorJmp = oldErr;
  261. return status;
  262. }
  263. /*
  264. ** returns 0 = chunk loaded; 1 = error; 2 = no more chunks to load
  265. */
  266. static int protectedparser (ZIO *z, int bin)
  267. {
  268. volatile int status;
  269. TProtoFunc *volatile tf;
  270. jmp_buf myErrorJmp;
  271. jmp_buf *volatile oldErr = L->errorJmp;
  272. L->errorJmp = &myErrorJmp;
  273. if (setjmp(myErrorJmp) == 0) {
  274. tf = bin ? luaU_undump1(z) : luaY_parser(z);
  275. status = 0;
  276. }
  277. else {
  278. tf = NULL;
  279. status = 1;
  280. }
  281. L->errorJmp = oldErr;
  282. if (status) return 1; /* error code */
  283. if (tf == NULL) return 2; /* 'natural' end */
  284. luaD_adjusttop(L->Cstack.base+1); /* one slot for the pseudo-function */
  285. L->stack.stack[L->Cstack.base].ttype = LUA_T_PROTO;
  286. L->stack.stack[L->Cstack.base].value.tf = tf;
  287. luaV_closure(0);
  288. return 0;
  289. }
  290. static int do_main (ZIO *z, int bin)
  291. {
  292. int status;
  293. do {
  294. long old_blocks = (luaC_checkGC(), L->nblocks);
  295. status = protectedparser(z, bin);
  296. if (status == 1) return 1; /* error */
  297. else if (status == 2) return 0; /* 'natural' end */
  298. else {
  299. unsigned long newelems2 = 2*(L->nblocks-old_blocks);
  300. L->GCthreshold += newelems2;
  301. status = luaD_protectedrun(MULT_RET);
  302. L->GCthreshold -= newelems2;
  303. }
  304. } while (bin && status == 0);
  305. return status;
  306. }
  307. void luaD_gcIM (TObject *o)
  308. {
  309. TObject *im = luaT_getimbyObj(o, IM_GC);
  310. if (ttype(im) != LUA_T_NIL) {
  311. *L->stack.top = *o;
  312. incr_top;
  313. luaD_callTM(im, 1, 0);
  314. }
  315. }
  316. int lua_dofile (char *filename)
  317. {
  318. ZIO z;
  319. int status;
  320. int c;
  321. int bin;
  322. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  323. if (f == NULL)
  324. return 2;
  325. if (filename == NULL)
  326. filename = "(stdin)";
  327. c = fgetc(f);
  328. ungetc(c, f);
  329. bin = (c == ID_CHUNK);
  330. if (bin)
  331. f = freopen(filename, "rb", f); /* set binary mode */
  332. luaZ_Fopen(&z, f, filename);
  333. status = do_main(&z, bin);
  334. if (f != stdin)
  335. fclose(f);
  336. return status;
  337. }
  338. #define SIZE_PREF 20 /* size of string prefix to appear in error messages */
  339. #define SSIZE_PREF "20"
  340. static void build_name (char *str, char *name) {
  341. if (str == NULL || *str == ID_CHUNK)
  342. strcpy(name, "(buffer)");
  343. else {
  344. char *temp;
  345. sprintf(name, "(dostring) >> \"%." SSIZE_PREF "s\"", str);
  346. temp = strchr(name, '\n');
  347. if (temp) { /* end string after first line */
  348. *temp = '"';
  349. *(temp+1) = 0;
  350. }
  351. }
  352. }
  353. int lua_dostring (char *str) {
  354. return lua_dobuffer(str, strlen(str), NULL);
  355. }
  356. int lua_dobuffer (char *buff, int size, char *name) {
  357. char newname[SIZE_PREF+25];
  358. ZIO z;
  359. int status;
  360. if (name==NULL) {
  361. build_name(buff, newname);
  362. name = newname;
  363. }
  364. luaZ_mopen(&z, buff, size, name);
  365. status = do_main(&z, buff[0]==ID_CHUNK);
  366. return status;
  367. }