opcode.c 26 KB

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