opcode.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 4.10 1997/06/16 18:42:32 roberto Exp roberto $";
  6. #include <setjmp.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "luadebug.h"
  11. #include "luamem.h"
  12. #include "opcode.h"
  13. #include "hash.h"
  14. #include "inout.h"
  15. #include "table.h"
  16. #include "lua.h"
  17. #include "fallback.h"
  18. #include "auxlib.h"
  19. #include "lex.h"
  20. #define tonumber(o) ((ttype(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
  21. #define tostring(o) ((ttype(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
  22. #define STACK_SIZE 128
  23. #ifndef STACK_LIMIT
  24. #define STACK_LIMIT 6000
  25. #endif
  26. typedef int StkId; /* index to stack elements */
  27. static TObject initial_stack;
  28. static TObject *stackLimit = &initial_stack+1;
  29. static TObject *stack = &initial_stack;
  30. static TObject *top = &initial_stack;
  31. /* macros to convert from lua_Object to (TObject *) and back */
  32. #define Address(lo) ((lo)+stack-1)
  33. #define Ref(st) ((st)-stack+1)
  34. /* macro to increment stack top. There must be always an empty slot in
  35. * the stack
  36. */
  37. #define incr_top if (++top >= stackLimit) growstack()
  38. struct C_Lua_Stack {
  39. StkId base; /* when Lua calls C or C calls Lua, points to */
  40. /* the first slot after the last parameter. */
  41. StkId lua2C; /* points to first element of "array" lua2C */
  42. int num; /* size of "array" lua2C */
  43. };
  44. static struct C_Lua_Stack CLS_current = {0, 0, 0};
  45. static jmp_buf *errorJmp = NULL; /* current error recover point */
  46. /* Hooks */
  47. lua_LHFunction lua_linehook = NULL;
  48. lua_CHFunction lua_callhook = NULL;
  49. static StkId lua_execute (Byte *pc, StkId base);
  50. static void do_call (StkId base, int nResults);
  51. TObject *luaI_Address (lua_Object o)
  52. {
  53. return Address(o);
  54. }
  55. /*
  56. ** Init stack
  57. */
  58. static void lua_initstack (void)
  59. {
  60. Long maxstack = STACK_SIZE;
  61. stack = newvector(maxstack, TObject);
  62. stackLimit = stack+maxstack;
  63. top = stack;
  64. *(top++) = initial_stack;
  65. }
  66. /*
  67. ** Check stack overflow and, if necessary, realloc vector
  68. */
  69. #define lua_checkstack(nt) if ((nt) >= stackLimit) growstack()
  70. static void growstack (void)
  71. {
  72. if (stack == &initial_stack)
  73. lua_initstack();
  74. else
  75. {
  76. static int limit = STACK_LIMIT;
  77. StkId t = top-stack;
  78. Long stacksize = stackLimit - stack;
  79. stacksize = growvector(&stack, stacksize, TObject, stackEM, limit+100);
  80. stackLimit = stack+stacksize;
  81. top = stack + t;
  82. if (stacksize >= limit)
  83. {
  84. limit = stacksize;
  85. lua_error(stackEM);
  86. }
  87. }
  88. }
  89. /*
  90. ** Concatenate two given strings. Return the new string pointer.
  91. */
  92. static char *lua_strconc (char *l, char *r)
  93. {
  94. size_t nl = strlen(l);
  95. char *buffer = luaI_buffer(nl+strlen(r)+1);
  96. strcpy(buffer, l);
  97. strcpy(buffer+nl, r);
  98. return buffer;
  99. }
  100. /*
  101. ** Convert, if possible, to a number object.
  102. ** Return 0 if success, not 0 if error.
  103. */
  104. static int lua_tonumber (TObject *obj)
  105. {
  106. float t;
  107. char c;
  108. if (ttype(obj) != LUA_T_STRING)
  109. return 1;
  110. else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
  111. {
  112. nvalue(obj) = t;
  113. ttype(obj) = LUA_T_NUMBER;
  114. return 0;
  115. }
  116. else
  117. return 2;
  118. }
  119. /*
  120. ** Convert, if possible, to a string ttype
  121. ** Return 0 in success or not 0 on error.
  122. */
  123. static int lua_tostring (TObject *obj)
  124. {
  125. if (ttype(obj) != LUA_T_NUMBER)
  126. return 1;
  127. else {
  128. char s[60];
  129. real f = nvalue(obj);
  130. int i;
  131. if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f)
  132. sprintf (s, "%d", i);
  133. else
  134. sprintf (s, "%g", nvalue(obj));
  135. tsvalue(obj) = lua_createstring(s);
  136. ttype(obj) = LUA_T_STRING;
  137. return 0;
  138. }
  139. }
  140. /*
  141. ** Adjust stack. Set top to the given value, pushing NILs if needed.
  142. */
  143. static void adjust_top (StkId newtop)
  144. {
  145. if (newtop <= top-stack) /* int arith, since newtop may be out of stack */
  146. top = stack+newtop;
  147. else {
  148. TObject *nt;
  149. lua_checkstack(stack+newtop);
  150. nt = stack+newtop; /* warning: previous call may change stack */
  151. while (top < nt) ttype(top++) = LUA_T_NIL;
  152. }
  153. }
  154. #define adjustC(nParams) adjust_top(CLS_current.base+nParams)
  155. static void checkCparams (int nParams)
  156. {
  157. if (top-stack < CLS_current.base+nParams)
  158. lua_error("API error - wrong number of arguments in C2lua stack");
  159. }
  160. /*
  161. ** Open a hole below "nelems" from the top.
  162. */
  163. static void open_stack (int nelems)
  164. {
  165. int i;
  166. for (i=0; i<nelems; i++)
  167. *(top-i) = *(top-i-1);
  168. incr_top;
  169. }
  170. static lua_Object put_luaObject (TObject *o)
  171. {
  172. open_stack((top-stack)-CLS_current.base);
  173. stack[CLS_current.base++] = *o;
  174. return CLS_current.base; /* this is +1 real position (see Ref) */
  175. }
  176. static lua_Object put_luaObjectonTop (void)
  177. {
  178. open_stack((top-stack)-CLS_current.base);
  179. stack[CLS_current.base++] = *(--top);
  180. return CLS_current.base; /* this is +1 real position (see Ref) */
  181. }
  182. lua_Object lua_pop (void)
  183. {
  184. checkCparams(1);
  185. return put_luaObjectonTop();
  186. }
  187. /*
  188. ** call Line hook
  189. */
  190. static void lineHook (int line)
  191. {
  192. struct C_Lua_Stack oldCLS = CLS_current;
  193. StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
  194. CLS_current.num = 0;
  195. (*lua_linehook)(line);
  196. top = stack+old_top;
  197. CLS_current = oldCLS;
  198. }
  199. /*
  200. ** Call hook
  201. ** The function being called is in [stack+base-1]
  202. */
  203. static void callHook (StkId base, lua_Type type, int isreturn)
  204. {
  205. struct C_Lua_Stack oldCLS = CLS_current;
  206. StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
  207. CLS_current.num = 0;
  208. if (isreturn)
  209. (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
  210. else
  211. {
  212. TObject *f = stack+base-1;
  213. if (type == LUA_T_MARK)
  214. (*lua_callhook)(Ref(f), f->value.tf->fileName, f->value.tf->lineDefined);
  215. else
  216. (*lua_callhook)(Ref(f), "(C)", -1);
  217. }
  218. top = stack+old_top;
  219. CLS_current = oldCLS;
  220. }
  221. /*
  222. ** Call a C function. CLS_current.base will point to the top of the stack,
  223. ** and CLS_current.num is the number of parameters. Returns an index
  224. ** to the first result from C.
  225. */
  226. static StkId callC (lua_CFunction func, StkId base)
  227. {
  228. struct C_Lua_Stack oldCLS = CLS_current;
  229. StkId firstResult;
  230. CLS_current.num = (top-stack) - base;
  231. /* incorporate parameters on the stack */
  232. CLS_current.lua2C = base;
  233. CLS_current.base = base+CLS_current.num; /* == top-stack */
  234. if (lua_callhook)
  235. callHook(base, LUA_T_CMARK, 0);
  236. (*func)();
  237. if (lua_callhook) /* func may have changed lua_callhook */
  238. callHook(base, LUA_T_CMARK, 1);
  239. firstResult = CLS_current.base;
  240. CLS_current = oldCLS;
  241. return firstResult;
  242. }
  243. static void callIM (TObject *f, int nParams, int nResults)
  244. {
  245. open_stack(nParams);
  246. *(top-nParams-1) = *f;
  247. do_call((top-stack)-nParams, nResults);
  248. }
  249. /*
  250. ** Call a function (C or Lua). The parameters must be on the stack,
  251. ** between [stack+base,top). The function to be called is at stack+base-1.
  252. ** When returns, the results are on the stack, between [stack+base-1,top).
  253. ** The number of results is nResults, unless nResults=MULT_RET.
  254. */
  255. static void do_call (StkId base, int nResults)
  256. {
  257. StkId firstResult;
  258. TObject *func = stack+base-1;
  259. int i;
  260. if (ttype(func) == LUA_T_CFUNCTION) {
  261. ttype(func) = LUA_T_CMARK;
  262. firstResult = callC(fvalue(func), base);
  263. }
  264. else if (ttype(func) == LUA_T_FUNCTION) {
  265. ttype(func) = LUA_T_MARK;
  266. firstResult = lua_execute(func->value.tf->code, base);
  267. }
  268. else { /* func is not a function */
  269. /* Check the fallback for invalid functions */
  270. TObject *im = luaI_getimbyObj(func, IM_FUNCTION);
  271. if (ttype(im) == LUA_T_NIL)
  272. lua_error("call expression not a function");
  273. open_stack((top-stack)-(base-1));
  274. stack[base-1] = *im;
  275. do_call(base, nResults);
  276. return;
  277. }
  278. /* adjust the number of results */
  279. if (nResults != MULT_RET)
  280. adjust_top(firstResult+nResults);
  281. /* move results to base-1 (to erase parameters and function) */
  282. base--;
  283. nResults = top - (stack+firstResult); /* actual number of results */
  284. for (i=0; i<nResults; i++)
  285. *(stack+base+i) = *(stack+firstResult+i);
  286. top -= firstResult-base;
  287. }
  288. /*
  289. ** Function to index a table. Receives the table at top-2 and the index
  290. ** at top-1.
  291. */
  292. static void pushsubscript (void)
  293. {
  294. TObject *im;
  295. if (ttype(top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
  296. im = luaI_getimbyObj(top-2, IM_GETTABLE);
  297. else { /* object is a table... */
  298. int tg = (top-2)->value.a->htag;
  299. im = luaI_getim(tg, IM_GETTABLE);
  300. if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
  301. TObject *h = lua_hashget(avalue(top-2), top-1);
  302. if (h != NULL && ttype(h) != LUA_T_NIL) {
  303. --top;
  304. *(top-1) = *h;
  305. }
  306. else if (ttype(im=luaI_getim(tg, IM_INDEX)) != LUA_T_NIL)
  307. callIM(im, 2, 1);
  308. else {
  309. --top;
  310. ttype(top-1) = LUA_T_NIL;
  311. }
  312. return;
  313. }
  314. /* else it has a "gettable" method, go through to next command */
  315. }
  316. /* object is not a table, or it has a "gettable" method */
  317. if (ttype(im) != LUA_T_NIL)
  318. callIM(im, 2, 1);
  319. else
  320. lua_error("indexed expression not a table");
  321. }
  322. lua_Object lua_rawgettable (void)
  323. {
  324. checkCparams(2);
  325. if (ttype(top-2) != LUA_T_ARRAY)
  326. lua_error("indexed expression not a table in raw gettable");
  327. else {
  328. TObject *h = lua_hashget(avalue(top-2), top-1);
  329. --top;
  330. if (h != NULL)
  331. *(top-1) = *h;
  332. else
  333. ttype(top-1) = LUA_T_NIL;
  334. }
  335. return put_luaObjectonTop();
  336. }
  337. /*
  338. ** Function to store indexed based on values at the top
  339. ** mode = 0: raw store (without internal methods)
  340. ** mode = 1: normal store (with internal methods)
  341. ** mode = 2: "deep stack" store (with internal methods)
  342. */
  343. static void storesubscript (TObject *t, int mode)
  344. {
  345. TObject *im = (mode == 0) ? NULL : luaI_getimbyObj(t, IM_SETTABLE);
  346. if (ttype(t) == LUA_T_ARRAY && (im == NULL || ttype(im) == LUA_T_NIL)) {
  347. TObject *h = lua_hashdefine(avalue(t), t+1);
  348. *h = *(top-1);
  349. top -= (mode == 2) ? 1 : 3;
  350. }
  351. else { /* object is not a table, and/or has a specific "settable" method */
  352. if (im && ttype(im) != LUA_T_NIL) {
  353. if (mode == 2) {
  354. lua_checkstack(top+2);
  355. *(top+1) = *(top-1);
  356. *(top) = *(t+1);
  357. *(top-1) = *t;
  358. top += 2;
  359. }
  360. callIM(im, 3, 0);
  361. }
  362. else
  363. lua_error("indexed expression not a table");
  364. }
  365. }
  366. static void getglobal (Word n)
  367. {
  368. TObject *value = &lua_table[n].object;
  369. TObject *im = luaI_getimbyObj(value, IM_GETGLOBAL);
  370. if (ttype(im) == LUA_T_NIL) { /* default behavior */
  371. *top = *value;
  372. incr_top;
  373. }
  374. else {
  375. ttype(top) = LUA_T_STRING;
  376. tsvalue(top) = lua_table[n].varname;
  377. incr_top;
  378. *top = *value;
  379. incr_top;
  380. callIM(im, 2, 1);
  381. }
  382. }
  383. /*
  384. ** Traverse all objects on stack
  385. */
  386. void lua_travstack (int (*fn)(TObject *))
  387. {
  388. StkId i;
  389. for (i = (top-1)-stack; i>=0; i--)
  390. fn (stack+i);
  391. }
  392. /*
  393. ** Error messages and debug functions
  394. */
  395. static void lua_message (char *s)
  396. {
  397. TObject *im = luaI_geterrorim();
  398. if (ttype(im) != LUA_T_NIL) {
  399. lua_pushstring(s);
  400. callIM(im, 1, 0);
  401. }
  402. }
  403. /*
  404. ** Reports an error, and jumps up to the available recover label
  405. */
  406. void lua_error (char *s)
  407. {
  408. if (s) lua_message(s);
  409. if (errorJmp)
  410. longjmp(*errorJmp, 1);
  411. else
  412. {
  413. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  414. exit(1);
  415. }
  416. }
  417. lua_Function lua_stackedfunction (int level)
  418. {
  419. StkId i;
  420. for (i = (top-1)-stack; i>=0; i--)
  421. if (stack[i].ttype == LUA_T_MARK || stack[i].ttype == LUA_T_CMARK)
  422. if (level-- == 0)
  423. return Ref(stack+i);
  424. return LUA_NOOBJECT;
  425. }
  426. int lua_currentline (lua_Function func)
  427. {
  428. TObject *f = Address(func);
  429. return (f+1 < top && (f+1)->ttype == LUA_T_LINE) ? (f+1)->value.i : -1;
  430. }
  431. lua_Object lua_getlocal (lua_Function func, int local_number, char **name)
  432. {
  433. TObject *f = luaI_Address(func);
  434. *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  435. if (*name)
  436. {
  437. /* if "*name", there must be a LUA_T_LINE */
  438. /* therefore, f+2 points to function base */
  439. return Ref((f+2)+(local_number-1));
  440. }
  441. else
  442. return LUA_NOOBJECT;
  443. }
  444. int lua_setlocal (lua_Function func, int local_number)
  445. {
  446. TObject *f = Address(func);
  447. char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
  448. checkCparams(1);
  449. --top;
  450. if (name)
  451. {
  452. /* if "name", there must be a LUA_T_LINE */
  453. /* therefore, f+2 points to function base */
  454. *((f+2)+(local_number-1)) = *top;
  455. return 1;
  456. }
  457. else
  458. return 0;
  459. }
  460. /*
  461. ** Call the function at CLS_current.base, and incorporate results on
  462. ** the Lua2C structure.
  463. */
  464. static void do_callinc (int nResults)
  465. {
  466. StkId base = CLS_current.base;
  467. do_call(base+1, nResults);
  468. CLS_current.lua2C = base; /* position of the new results */
  469. CLS_current.num = (top-stack) - base; /* number of results */
  470. CLS_current.base = base + CLS_current.num; /* incorporate results on stack */
  471. }
  472. static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
  473. {
  474. StkId base = (top-stack)-nParams;
  475. open_stack(nParams);
  476. stack[base].ttype = LUA_T_CFUNCTION;
  477. stack[base].value.f = f;
  478. do_call(base+1, nResults);
  479. }
  480. /*
  481. ** Execute a protected call. Assumes that function is at CLS_current.base and
  482. ** parameters are on top of it. Leave nResults on the stack.
  483. */
  484. static int do_protectedrun (int nResults)
  485. {
  486. jmp_buf myErrorJmp;
  487. int status;
  488. struct C_Lua_Stack oldCLS = CLS_current;
  489. jmp_buf *oldErr = errorJmp;
  490. errorJmp = &myErrorJmp;
  491. if (setjmp(myErrorJmp) == 0) {
  492. do_callinc(nResults);
  493. status = 0;
  494. }
  495. else { /* an error occurred: restore CLS_current and top */
  496. CLS_current = oldCLS;
  497. top = stack+CLS_current.base;
  498. status = 1;
  499. }
  500. errorJmp = oldErr;
  501. return status;
  502. }
  503. int luaI_dorun (TFunc *tf)
  504. {
  505. int status;
  506. adjustC(1); /* one slot for the pseudo-function */
  507. stack[CLS_current.base].ttype = LUA_T_FUNCTION;
  508. stack[CLS_current.base].value.tf = tf;
  509. status = do_protectedrun(MULT_RET);
  510. return status;
  511. }
  512. int lua_domain (void)
  513. {
  514. TFunc tf;
  515. int status;
  516. jmp_buf myErrorJmp;
  517. jmp_buf *oldErr = errorJmp;
  518. errorJmp = &myErrorJmp;
  519. luaI_initTFunc(&tf);
  520. tf.fileName = lua_parsedfile;
  521. if (setjmp(myErrorJmp) == 0) {
  522. lua_parse(&tf);
  523. status = 0;
  524. }
  525. else {
  526. adjustC(0); /* erase extra slot */
  527. status = 1;
  528. }
  529. if (status == 0)
  530. status = luaI_dorun(&tf);
  531. errorJmp = oldErr;
  532. luaI_free(tf.code);
  533. return status;
  534. }
  535. /*
  536. ** Execute the given lua function. Return 0 on success or 1 on error.
  537. */
  538. int lua_callfunction (lua_Object function)
  539. {
  540. if (function == LUA_NOOBJECT)
  541. return 1;
  542. else
  543. {
  544. open_stack((top-stack)-CLS_current.base);
  545. stack[CLS_current.base] = *Address(function);
  546. return do_protectedrun (MULT_RET);
  547. }
  548. }
  549. /*
  550. ** API: set a function as a fallback
  551. */
  552. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  553. {
  554. lua_pushstring(name);
  555. lua_pushcfunction(fallback);
  556. do_unprotectedrun(luaI_setfallback, 2, 1);
  557. return put_luaObjectonTop();
  558. }
  559. lua_Object lua_gettagmethod (int tag, char *event)
  560. {
  561. lua_pushnumber(tag);
  562. lua_pushstring(event);
  563. do_unprotectedrun(luaI_gettagmethod, 2, 1);
  564. return put_luaObjectonTop();
  565. }
  566. lua_Object lua_settagmethod (int tag, char *event, lua_CFunction method)
  567. {
  568. lua_pushnumber(tag);
  569. lua_pushstring(event);
  570. if (method)
  571. lua_pushcfunction (method);
  572. else
  573. lua_pushnil();
  574. do_unprotectedrun(luaI_settagmethod, 3, 1);
  575. return put_luaObjectonTop();
  576. }
  577. lua_Object lua_seterrormethod (lua_CFunction method)
  578. {
  579. lua_pushcfunction(method);
  580. do_unprotectedrun(luaI_seterrormethod, 1, 1);
  581. return put_luaObjectonTop();
  582. }
  583. /*
  584. ** API: receives on the stack the table and the index.
  585. ** returns the value.
  586. */
  587. lua_Object lua_gettable (void)
  588. {
  589. checkCparams(2);
  590. pushsubscript();
  591. return put_luaObjectonTop();
  592. }
  593. #define MAX_C_BLOCKS 10
  594. static int numCblocks = 0;
  595. static struct C_Lua_Stack Cblocks[MAX_C_BLOCKS];
  596. /*
  597. ** API: starts a new block
  598. */
  599. void lua_beginblock (void)
  600. {
  601. if (numCblocks >= MAX_C_BLOCKS)
  602. lua_error("`lua_beginblock': too many nested blocks");
  603. Cblocks[numCblocks] = CLS_current;
  604. numCblocks++;
  605. }
  606. /*
  607. ** API: ends a block
  608. */
  609. void lua_endblock (void)
  610. {
  611. --numCblocks;
  612. CLS_current = Cblocks[numCblocks];
  613. adjustC(0);
  614. }
  615. void lua_settag (int tag)
  616. {
  617. checkCparams(1);
  618. luaI_settag(tag, --top);
  619. }
  620. /*
  621. ** API: receives on the stack the table, the index, and the new value.
  622. */
  623. void lua_settable (void)
  624. {
  625. checkCparams(3);
  626. storesubscript(top-3, 1);
  627. }
  628. void lua_rawsettable (void)
  629. {
  630. checkCparams(3);
  631. storesubscript(top-3, 0);
  632. }
  633. /*
  634. ** API: creates a new table
  635. */
  636. lua_Object lua_createtable (void)
  637. {
  638. TObject o;
  639. avalue(&o) = lua_createarray(0);
  640. ttype(&o) = LUA_T_ARRAY;
  641. return put_luaObject(&o);
  642. }
  643. /*
  644. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  645. ** 'number' must be 1 to get the first parameter.
  646. */
  647. lua_Object lua_lua2C (int number)
  648. {
  649. if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
  650. /* Ref(stack+(CLS_current.lua2C+number-1)) ==
  651. stack+(CLS_current.lua2C+number-1)-stack+1 == */
  652. return CLS_current.lua2C+number;
  653. }
  654. int lua_isnil (lua_Object o)
  655. {
  656. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_NIL);
  657. }
  658. int lua_istable (lua_Object o)
  659. {
  660. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_ARRAY);
  661. }
  662. int lua_isuserdata (lua_Object o)
  663. {
  664. return (o!= LUA_NOOBJECT) && (ttype(Address(o)) == LUA_T_USERDATA);
  665. }
  666. int lua_iscfunction (lua_Object o)
  667. {
  668. int t = lua_tag(o);
  669. return (t == LUA_T_CMARK) || (t == LUA_T_CFUNCTION);
  670. }
  671. int lua_isnumber (lua_Object o)
  672. {
  673. return (o!= LUA_NOOBJECT) && (tonumber(Address(o)) == 0);
  674. }
  675. int lua_isstring (lua_Object o)
  676. {
  677. int t = lua_tag(o);
  678. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  679. }
  680. int lua_isfunction (lua_Object o)
  681. {
  682. int t = lua_tag(o);
  683. return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION) ||
  684. (t == LUA_T_MARK) || (t == LUA_T_CMARK);
  685. }
  686. /*
  687. ** Given an object handle, return its number value. On error, return 0.0.
  688. */
  689. real lua_getnumber (lua_Object object)
  690. {
  691. if (object == LUA_NOOBJECT) return 0.0;
  692. if (tonumber (Address(object))) return 0.0;
  693. else return (nvalue(Address(object)));
  694. }
  695. /*
  696. ** Given an object handle, return its string pointer. On error, return NULL.
  697. */
  698. char *lua_getstring (lua_Object object)
  699. {
  700. if (object == LUA_NOOBJECT || tostring (Address(object)))
  701. return NULL;
  702. else return (svalue(Address(object)));
  703. }
  704. void *lua_getuserdata (lua_Object object)
  705. {
  706. if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
  707. return NULL;
  708. else return tsvalue(Address(object))->u.v;
  709. }
  710. /*
  711. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  712. */
  713. lua_CFunction lua_getcfunction (lua_Object object)
  714. {
  715. if (object == LUA_NOOBJECT || ((ttype(Address(object)) != LUA_T_CFUNCTION) &&
  716. (ttype(Address(object)) != LUA_T_CMARK)))
  717. return NULL;
  718. else return (fvalue(Address(object)));
  719. }
  720. lua_Object lua_getref (int ref)
  721. {
  722. TObject *o = luaI_getref(ref);
  723. if (o == NULL)
  724. return LUA_NOOBJECT;
  725. return put_luaObject(o);
  726. }
  727. int lua_ref (int lock)
  728. {
  729. checkCparams(1);
  730. return luaI_ref(--top, lock);
  731. }
  732. /*
  733. ** Get a global object.
  734. */
  735. lua_Object lua_getglobal (char *name)
  736. {
  737. getglobal(luaI_findsymbolbyname(name));
  738. return put_luaObjectonTop();
  739. }
  740. lua_Object lua_rawgetglobal (char *name)
  741. {
  742. return put_luaObject(&lua_table[luaI_findsymbolbyname(name)].object);
  743. }
  744. /*
  745. ** Store top of the stack at a global variable array field.
  746. */
  747. static void setglobal (Word n)
  748. {
  749. TObject *oldvalue = &lua_table[n].object;
  750. TObject *im = luaI_getimbyObj(oldvalue, IM_SETGLOBAL);
  751. if (ttype(im) == LUA_T_NIL) /* default behavior */
  752. s_object(n) = *(--top);
  753. else {
  754. TObject newvalue = *(top-1);
  755. ttype(top-1) = LUA_T_STRING;
  756. tsvalue(top-1) = lua_table[n].varname;
  757. *top = *oldvalue;
  758. incr_top;
  759. *top = newvalue;
  760. incr_top;
  761. callIM(im, 3, 0);
  762. }
  763. }
  764. void lua_setglobal (char *name)
  765. {
  766. checkCparams(1);
  767. setglobal(luaI_findsymbolbyname(name));
  768. }
  769. void lua_rawsetglobal (char *name)
  770. {
  771. Word n = luaI_findsymbolbyname(name);
  772. checkCparams(1);
  773. s_object(n) = *(--top);
  774. }
  775. /*
  776. ** Push a nil object
  777. */
  778. void lua_pushnil (void)
  779. {
  780. ttype(top) = LUA_T_NIL;
  781. incr_top;
  782. }
  783. /*
  784. ** Push an object (ttype=number) to stack.
  785. */
  786. void lua_pushnumber (real n)
  787. {
  788. ttype(top) = LUA_T_NUMBER; nvalue(top) = n;
  789. incr_top;
  790. }
  791. /*
  792. ** Push an object (ttype=string) to stack.
  793. */
  794. void lua_pushstring (char *s)
  795. {
  796. if (s == NULL)
  797. ttype(top) = LUA_T_NIL;
  798. else
  799. {
  800. tsvalue(top) = lua_createstring(s);
  801. ttype(top) = LUA_T_STRING;
  802. }
  803. incr_top;
  804. }
  805. /*
  806. ** Push an object (ttype=cfunction) to stack.
  807. */
  808. void lua_pushcfunction (lua_CFunction fn)
  809. {
  810. ttype(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  811. incr_top;
  812. }
  813. void lua_pushusertag (void *u, int tag)
  814. {
  815. if (tag < 0 && tag != LUA_ANYTAG)
  816. luaI_realtag(tag); /* error if tag is not valid */
  817. tsvalue(top) = luaI_createudata(u, tag);
  818. ttype(top) = LUA_T_USERDATA;
  819. incr_top;
  820. }
  821. /*
  822. ** Push an object on the stack.
  823. */
  824. void luaI_pushobject (TObject *o)
  825. {
  826. *top = *o;
  827. incr_top;
  828. }
  829. /*
  830. ** Push a lua_Object on stack.
  831. */
  832. void lua_pushobject (lua_Object o)
  833. {
  834. if (o == LUA_NOOBJECT)
  835. lua_error("API error - attempt to push a NOOBJECT");
  836. *top = *Address(o);
  837. if (ttype(top) == LUA_T_MARK) ttype(top) = LUA_T_FUNCTION;
  838. else if (ttype(top) == LUA_T_CMARK) ttype(top) = LUA_T_CFUNCTION;
  839. incr_top;
  840. }
  841. int lua_tag (lua_Object lo)
  842. {
  843. if (lo == LUA_NOOBJECT) return LUA_T_NIL;
  844. else {
  845. TObject *o = Address(lo);
  846. lua_Type t = ttype(o);
  847. if (t == LUA_T_USERDATA)
  848. return o->value.ts->tag;
  849. else if (t == LUA_T_ARRAY)
  850. return o->value.a->htag;
  851. else return t;
  852. }
  853. }
  854. void luaI_gcIM (TObject *o)
  855. {
  856. TObject *im = luaI_getimbyObj(o, IM_GC);
  857. if (ttype(im) != LUA_T_NIL) {
  858. *top = *o;
  859. incr_top;
  860. callIM(im, 1, 0);
  861. }
  862. }
  863. static void call_binTM (IMS event, char *msg)
  864. {
  865. TObject *im = luaI_getimbyObj(top-2, event); /* try first operand */
  866. if (ttype(im) == LUA_T_NIL) {
  867. im = luaI_getimbyObj(top-1, event); /* try second operand */
  868. if (ttype(im) == LUA_T_NIL) {
  869. im = luaI_getim(0, event); /* try a 'global' i.m. */
  870. if (ttype(im) == LUA_T_NIL)
  871. lua_error(msg);
  872. }
  873. }
  874. lua_pushstring(luaI_eventname[event]);
  875. callIM(im, 3, 1);
  876. }
  877. static void call_arith (IMS event)
  878. {
  879. call_binTM(event, "unexpected type at arithmetic operation");
  880. }
  881. static void comparison (lua_Type ttype_less, lua_Type ttype_equal,
  882. lua_Type ttype_great, IMS op)
  883. {
  884. TObject *l = top-2;
  885. TObject *r = top-1;
  886. int result;
  887. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  888. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  889. else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
  890. result = strcmp(svalue(l), svalue(r));
  891. else {
  892. call_binTM(op, "unexpected type at comparison");
  893. return;
  894. }
  895. top--;
  896. nvalue(top-1) = 1;
  897. ttype(top-1) = (result < 0) ? ttype_less :
  898. (result == 0) ? ttype_equal : ttype_great;
  899. }
  900. static void adjust_varargs (StkId first_extra_arg)
  901. {
  902. TObject arg;
  903. TObject *firstelem = stack+first_extra_arg;
  904. int nvararg = top-firstelem;
  905. int i;
  906. if (nvararg < 0) nvararg = 0;
  907. avalue(&arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
  908. ttype(&arg) = LUA_T_ARRAY;
  909. for (i=0; i<nvararg; i++) {
  910. TObject index;
  911. ttype(&index) = LUA_T_NUMBER;
  912. nvalue(&index) = i+1;
  913. *(lua_hashdefine(avalue(&arg), &index)) = *(firstelem+i);
  914. }
  915. /* store counter in field "n" */ {
  916. TObject index, extra;
  917. ttype(&index) = LUA_T_STRING;
  918. tsvalue(&index) = lua_createstring("n");
  919. ttype(&extra) = LUA_T_NUMBER;
  920. nvalue(&extra) = nvararg;
  921. *(lua_hashdefine(avalue(&arg), &index)) = extra;
  922. }
  923. adjust_top(first_extra_arg);
  924. *top = arg; incr_top;
  925. }
  926. /*
  927. ** Execute the given opcode, until a RET. Parameters are between
  928. ** [stack+base,top). Returns n such that the the results are between
  929. ** [stack+n,top).
  930. */
  931. static StkId lua_execute (Byte *pc, StkId base)
  932. {
  933. if (lua_callhook)
  934. callHook (base, LUA_T_MARK, 0);
  935. while (1)
  936. {
  937. OpCode opcode;
  938. switch (opcode = (OpCode)*pc++)
  939. {
  940. case PUSHNIL: ttype(top) = LUA_T_NIL; incr_top; break;
  941. case PUSH0: case PUSH1: case PUSH2:
  942. ttype(top) = LUA_T_NUMBER;
  943. nvalue(top) = opcode-PUSH0;
  944. incr_top;
  945. break;
  946. case PUSHBYTE:
  947. ttype(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  948. case PUSHWORD:
  949. {
  950. Word w;
  951. get_word(w,pc);
  952. ttype(top) = LUA_T_NUMBER; nvalue(top) = w;
  953. incr_top;
  954. }
  955. break;
  956. case PUSHFLOAT:
  957. {
  958. real num;
  959. get_float(num,pc);
  960. ttype(top) = LUA_T_NUMBER; nvalue(top) = num;
  961. incr_top;
  962. }
  963. break;
  964. case PUSHSTRING:
  965. {
  966. Word w;
  967. get_word(w,pc);
  968. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  969. incr_top;
  970. }
  971. break;
  972. case PUSHFUNCTION:
  973. {
  974. TFunc *f;
  975. get_code(f,pc);
  976. luaI_insertfunction(f); /* may take part in GC */
  977. top->ttype = LUA_T_FUNCTION;
  978. top->value.tf = f;
  979. incr_top;
  980. }
  981. break;
  982. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  983. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  984. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  985. case PUSHLOCAL9:
  986. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  987. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  988. case PUSHGLOBAL:
  989. {
  990. Word w;
  991. get_word(w,pc);
  992. getglobal(w);
  993. }
  994. break;
  995. case PUSHINDEXED:
  996. pushsubscript();
  997. break;
  998. case PUSHSELF:
  999. {
  1000. TObject receiver = *(top-1);
  1001. Word w;
  1002. get_word(w,pc);
  1003. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1004. incr_top;
  1005. pushsubscript();
  1006. *top = receiver;
  1007. incr_top;
  1008. break;
  1009. }
  1010. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  1011. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  1012. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  1013. case STORELOCAL9:
  1014. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  1015. break;
  1016. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  1017. case STOREGLOBAL:
  1018. {
  1019. Word w;
  1020. get_word(w,pc);
  1021. setglobal(w);
  1022. }
  1023. break;
  1024. case STOREINDEXED0:
  1025. storesubscript(top-3, 1);
  1026. break;
  1027. case STOREINDEXED: {
  1028. int n = *pc++;
  1029. storesubscript(top-3-n, 2);
  1030. break;
  1031. }
  1032. case STORELIST0:
  1033. case STORELIST:
  1034. {
  1035. int m, n;
  1036. TObject *arr;
  1037. if (opcode == STORELIST0) m = 0;
  1038. else m = *(pc++) * FIELDS_PER_FLUSH;
  1039. n = *(pc++);
  1040. arr = top-n-1;
  1041. while (n)
  1042. {
  1043. ttype(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  1044. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1045. top--;
  1046. n--;
  1047. }
  1048. }
  1049. break;
  1050. case STORERECORD: /* opcode obsolete: supersed by STOREMAP */
  1051. {
  1052. int n = *(pc++);
  1053. TObject *arr = top-n-1;
  1054. while (n)
  1055. {
  1056. Word w;
  1057. get_word(w,pc);
  1058. ttype(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  1059. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  1060. top--;
  1061. n--;
  1062. }
  1063. }
  1064. break;
  1065. case STOREMAP: {
  1066. int n = *(pc++);
  1067. TObject *arr = top-(2*n)-1;
  1068. while (n--) {
  1069. *(lua_hashdefine (avalue(arr), top-2)) = *(top-1);
  1070. top-=2;
  1071. }
  1072. }
  1073. break;
  1074. case ADJUST0:
  1075. adjust_top(base);
  1076. break;
  1077. case ADJUST:
  1078. adjust_top(base + *(pc++));
  1079. break;
  1080. case VARARGS:
  1081. adjust_varargs(base + *(pc++));
  1082. break;
  1083. case CREATEARRAY:
  1084. {
  1085. Word size;
  1086. get_word(size,pc);
  1087. avalue(top) = lua_createarray(size);
  1088. ttype(top) = LUA_T_ARRAY;
  1089. incr_top;
  1090. }
  1091. break;
  1092. case EQOP:
  1093. {
  1094. int res = lua_equalObj(top-2, top-1);
  1095. --top;
  1096. ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  1097. nvalue(top-1) = 1;
  1098. }
  1099. break;
  1100. case LTOP:
  1101. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  1102. break;
  1103. case LEOP:
  1104. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
  1105. break;
  1106. case GTOP:
  1107. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
  1108. break;
  1109. case GEOP:
  1110. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
  1111. break;
  1112. case ADDOP:
  1113. {
  1114. TObject *l = top-2;
  1115. TObject *r = top-1;
  1116. if (tonumber(r) || tonumber(l))
  1117. call_arith(IM_ADD);
  1118. else
  1119. {
  1120. nvalue(l) += nvalue(r);
  1121. --top;
  1122. }
  1123. }
  1124. break;
  1125. case SUBOP:
  1126. {
  1127. TObject *l = top-2;
  1128. TObject *r = top-1;
  1129. if (tonumber(r) || tonumber(l))
  1130. call_arith(IM_SUB);
  1131. else
  1132. {
  1133. nvalue(l) -= nvalue(r);
  1134. --top;
  1135. }
  1136. }
  1137. break;
  1138. case MULTOP:
  1139. {
  1140. TObject *l = top-2;
  1141. TObject *r = top-1;
  1142. if (tonumber(r) || tonumber(l))
  1143. call_arith(IM_MUL);
  1144. else
  1145. {
  1146. nvalue(l) *= nvalue(r);
  1147. --top;
  1148. }
  1149. }
  1150. break;
  1151. case DIVOP:
  1152. {
  1153. TObject *l = top-2;
  1154. TObject *r = top-1;
  1155. if (tonumber(r) || tonumber(l))
  1156. call_arith(IM_DIV);
  1157. else
  1158. {
  1159. nvalue(l) /= nvalue(r);
  1160. --top;
  1161. }
  1162. }
  1163. break;
  1164. case POWOP:
  1165. call_arith(IM_POW);
  1166. break;
  1167. case CONCOP: {
  1168. TObject *l = top-2;
  1169. TObject *r = top-1;
  1170. if (tostring(l) || tostring(r))
  1171. call_binTM(IM_CONCAT, "unexpected type for concatenation");
  1172. else {
  1173. tsvalue(l) = lua_createstring(lua_strconc(svalue(l),svalue(r)));
  1174. --top;
  1175. }
  1176. }
  1177. break;
  1178. case MINUSOP:
  1179. if (tonumber(top-1))
  1180. {
  1181. ttype(top) = LUA_T_NIL;
  1182. incr_top;
  1183. call_arith(IM_UNM);
  1184. }
  1185. else
  1186. nvalue(top-1) = - nvalue(top-1);
  1187. break;
  1188. case NOTOP:
  1189. ttype(top-1) = (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1190. nvalue(top-1) = 1;
  1191. break;
  1192. case ONTJMP:
  1193. {
  1194. Word w;
  1195. get_word(w,pc);
  1196. if (ttype(top-1) != LUA_T_NIL) pc += w;
  1197. }
  1198. break;
  1199. case ONFJMP:
  1200. {
  1201. Word w;
  1202. get_word(w,pc);
  1203. if (ttype(top-1) == LUA_T_NIL) pc += w;
  1204. }
  1205. break;
  1206. case JMP:
  1207. {
  1208. Word w;
  1209. get_word(w,pc);
  1210. pc += w;
  1211. }
  1212. break;
  1213. case UPJMP:
  1214. {
  1215. Word w;
  1216. get_word(w,pc);
  1217. pc -= w;
  1218. }
  1219. break;
  1220. case IFFJMP:
  1221. {
  1222. Word w;
  1223. get_word(w,pc);
  1224. top--;
  1225. if (ttype(top) == LUA_T_NIL) pc += w;
  1226. }
  1227. break;
  1228. case IFFUPJMP:
  1229. {
  1230. Word w;
  1231. get_word(w,pc);
  1232. top--;
  1233. if (ttype(top) == LUA_T_NIL) pc -= w;
  1234. }
  1235. break;
  1236. case POP: --top; break;
  1237. case CALLFUNC:
  1238. {
  1239. int nParams = *(pc++);
  1240. int nResults = *(pc++);
  1241. StkId newBase = (top-stack)-nParams;
  1242. do_call(newBase, nResults);
  1243. }
  1244. break;
  1245. case RETCODE0:
  1246. case RETCODE:
  1247. if (lua_callhook)
  1248. callHook (base, LUA_T_MARK, 1);
  1249. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1250. case SETLINE:
  1251. {
  1252. Word line;
  1253. get_word(line,pc);
  1254. if ((stack+base-1)->ttype != LUA_T_LINE)
  1255. {
  1256. /* open space for LINE value */
  1257. open_stack((top-stack)-base);
  1258. base++;
  1259. (stack+base-1)->ttype = LUA_T_LINE;
  1260. }
  1261. (stack+base-1)->value.i = line;
  1262. if (lua_linehook)
  1263. lineHook (line);
  1264. break;
  1265. }
  1266. default:
  1267. lua_error ("internal error - opcode doesn't match");
  1268. }
  1269. }
  1270. }