opcode.c 25 KB

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