ldo.c 10 KB

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