ldo.c 10 KB

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