2
0

ldo.c 10 KB

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