opcode.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.67 1996/04/22 18:00:37 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_getref (int ref)
  622. {
  623. Object *o = luaI_getref(ref);
  624. if (o == NULL)
  625. return LUA_NOOBJECT;
  626. adjustC(0);
  627. luaI_pushobject(o);
  628. CBase++; /* incorporate object in the stack */
  629. return Ref(top-1);
  630. }
  631. void lua_pushref (int ref)
  632. {
  633. Object *o = luaI_getref(ref);
  634. if (o == NULL)
  635. lua_error("access to invalid (possibly garbage collected) reference");
  636. luaI_pushobject(o);
  637. }
  638. int lua_ref (int lock)
  639. {
  640. adjustC(1);
  641. return luaI_ref(--top, lock);
  642. }
  643. /*
  644. ** Get a global object.
  645. */
  646. lua_Object lua_getglobal (char *name)
  647. {
  648. adjustC(0);
  649. getglobal(luaI_findsymbolbyname(name));
  650. CBase++; /* incorporate object in the stack */
  651. return Ref(top-1);
  652. }
  653. /*
  654. ** Store top of the stack at a global variable array field.
  655. */
  656. void lua_storeglobal (char *name)
  657. {
  658. Word n = luaI_findsymbolbyname(name);
  659. adjustC(1);
  660. s_object(n) = *(--top);
  661. }
  662. /*
  663. ** Push a nil object
  664. */
  665. void lua_pushnil (void)
  666. {
  667. tag(top) = LUA_T_NIL;
  668. incr_top;
  669. }
  670. /*
  671. ** Push an object (tag=number) to stack.
  672. */
  673. void lua_pushnumber (real n)
  674. {
  675. tag(top) = LUA_T_NUMBER; nvalue(top) = n;
  676. incr_top;
  677. }
  678. /*
  679. ** Push an object (tag=string) to stack.
  680. */
  681. void lua_pushstring (char *s)
  682. {
  683. if (s == NULL)
  684. tag(top) = LUA_T_NIL;
  685. else
  686. {
  687. tsvalue(top) = lua_createstring(s);
  688. tag(top) = LUA_T_STRING;
  689. }
  690. incr_top;
  691. }
  692. /*>>>>>>>>>#undef lua_pushliteral
  693. void lua_pushliteral(char *s) { lua_pushstring(s); }*/
  694. /*
  695. ** Push an object (tag=cfunction) to stack.
  696. */
  697. void lua_pushcfunction (lua_CFunction fn)
  698. {
  699. tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  700. incr_top;
  701. }
  702. /*
  703. ** Push an object (tag=userdata) to stack.
  704. */
  705. void lua_pushusertag (void *u, int tag)
  706. {
  707. if (tag < LUA_T_USERDATA)
  708. lua_error("invalid tag in `lua_pushusertag'");
  709. tag(top) = tag; uvalue(top) = u;
  710. incr_top;
  711. }
  712. /*
  713. ** Push an object on the stack.
  714. */
  715. void luaI_pushobject (Object *o)
  716. {
  717. *top = *o;
  718. incr_top;
  719. }
  720. /*
  721. ** Push a lua_Object on stack.
  722. */
  723. void lua_pushobject (lua_Object o)
  724. {
  725. if (o == LUA_NOOBJECT)
  726. lua_error("attempt to push a NOOBJECT");
  727. luaI_pushobject(Address(o));
  728. }
  729. int lua_type (lua_Object o)
  730. {
  731. if (o == LUA_NOOBJECT)
  732. return LUA_T_NIL;
  733. else
  734. return tag(Address(o));
  735. }
  736. void luaI_gcFB (Object *o)
  737. {
  738. *top = *o;
  739. incr_top;
  740. callFB(FB_GC);
  741. }
  742. static void call_arith (char *op)
  743. {
  744. lua_pushstring(op);
  745. callFB(FB_ARITH);
  746. }
  747. static void comparison (lua_Type tag_less, lua_Type tag_equal,
  748. lua_Type tag_great, char *op)
  749. {
  750. Object *l = top-2;
  751. Object *r = top-1;
  752. int result;
  753. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  754. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  755. else if (tostring(l) || tostring(r))
  756. {
  757. lua_pushstring(op);
  758. callFB(FB_ORDER);
  759. return;
  760. }
  761. else
  762. result = strcmp(svalue(l), svalue(r));
  763. top--;
  764. nvalue(top-1) = 1;
  765. tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
  766. }
  767. /*
  768. ** Execute the given opcode, until a RET. Parameters are between
  769. ** [stack+base,top). Returns n such that the the results are between
  770. ** [stack+n,top).
  771. */
  772. static StkId lua_execute (Byte *pc, StkId base)
  773. {
  774. if (lua_callhook)
  775. callHook (base, LUA_T_MARK, 0);
  776. while (1)
  777. {
  778. OpCode opcode;
  779. switch (opcode = (OpCode)*pc++)
  780. {
  781. case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
  782. case PUSH0: case PUSH1: case PUSH2:
  783. tag(top) = LUA_T_NUMBER;
  784. nvalue(top) = opcode-PUSH0;
  785. incr_top;
  786. break;
  787. case PUSHBYTE:
  788. tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  789. case PUSHWORD:
  790. {
  791. CodeWord code;
  792. get_word(code,pc);
  793. tag(top) = LUA_T_NUMBER; nvalue(top) = code.w;
  794. incr_top;
  795. }
  796. break;
  797. case PUSHFLOAT:
  798. {
  799. CodeFloat code;
  800. get_float(code,pc);
  801. tag(top) = LUA_T_NUMBER; nvalue(top) = code.f;
  802. incr_top;
  803. }
  804. break;
  805. case PUSHSTRING:
  806. {
  807. CodeWord code;
  808. get_word(code,pc);
  809. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
  810. incr_top;
  811. }
  812. break;
  813. case PUSHFUNCTION:
  814. {
  815. CodeCode code;
  816. get_code(code,pc);
  817. luaI_insertfunction(code.tf); /* may take part in GC */
  818. top->tag = LUA_T_FUNCTION;
  819. top->value.tf = code.tf;
  820. incr_top;
  821. }
  822. break;
  823. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  824. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  825. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  826. case PUSHLOCAL9:
  827. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  828. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  829. case PUSHGLOBAL:
  830. {
  831. CodeWord code;
  832. get_word(code,pc);
  833. getglobal(code.w);
  834. }
  835. break;
  836. case PUSHINDEXED:
  837. pushsubscript();
  838. break;
  839. case PUSHSELF:
  840. {
  841. Object receiver = *(top-1);
  842. CodeWord code;
  843. get_word(code,pc);
  844. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
  845. incr_top;
  846. pushsubscript();
  847. *top = receiver;
  848. incr_top;
  849. break;
  850. }
  851. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  852. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  853. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  854. case STORELOCAL9:
  855. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  856. break;
  857. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  858. case STOREGLOBAL:
  859. {
  860. CodeWord code;
  861. get_word(code,pc);
  862. s_object(code.w) = *(--top);
  863. }
  864. break;
  865. case STOREINDEXED0:
  866. storesubscript();
  867. break;
  868. case STOREINDEXED:
  869. {
  870. int n = *pc++;
  871. if (tag(top-3-n) != LUA_T_ARRAY)
  872. {
  873. lua_checkstack(top+2);
  874. *(top+1) = *(top-1);
  875. *(top) = *(top-2-n);
  876. *(top-1) = *(top-3-n);
  877. top += 2;
  878. callFB(FB_SETTABLE);
  879. }
  880. else
  881. {
  882. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  883. *h = *(top-1);
  884. top--;
  885. }
  886. }
  887. break;
  888. case STORELIST0:
  889. case STORELIST:
  890. {
  891. int m, n;
  892. Object *arr;
  893. if (opcode == STORELIST0) m = 0;
  894. else m = *(pc++) * FIELDS_PER_FLUSH;
  895. n = *(pc++);
  896. arr = top-n-1;
  897. while (n)
  898. {
  899. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  900. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  901. top--;
  902. n--;
  903. }
  904. }
  905. break;
  906. case STORERECORD:
  907. {
  908. int n = *(pc++);
  909. Object *arr = top-n-1;
  910. while (n)
  911. {
  912. CodeWord code;
  913. get_word(code,pc);
  914. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
  915. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  916. top--;
  917. n--;
  918. }
  919. }
  920. break;
  921. case ADJUST0:
  922. adjust_top(base);
  923. break;
  924. case ADJUST:
  925. adjust_top(base + *(pc++));
  926. break;
  927. case CREATEARRAY:
  928. {
  929. CodeWord size;
  930. get_word(size,pc);
  931. avalue(top) = lua_createarray(size.w);
  932. tag(top) = LUA_T_ARRAY;
  933. incr_top;
  934. }
  935. break;
  936. case EQOP:
  937. {
  938. int res = lua_equalObj(top-2, top-1);
  939. --top;
  940. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  941. nvalue(top-1) = 1;
  942. }
  943. break;
  944. case LTOP:
  945. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
  946. break;
  947. case LEOP:
  948. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
  949. break;
  950. case GTOP:
  951. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
  952. break;
  953. case GEOP:
  954. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
  955. break;
  956. case ADDOP:
  957. {
  958. Object *l = top-2;
  959. Object *r = top-1;
  960. if (tonumber(r) || tonumber(l))
  961. call_arith("add");
  962. else
  963. {
  964. nvalue(l) += nvalue(r);
  965. --top;
  966. }
  967. }
  968. break;
  969. case SUBOP:
  970. {
  971. Object *l = top-2;
  972. Object *r = top-1;
  973. if (tonumber(r) || tonumber(l))
  974. call_arith("sub");
  975. else
  976. {
  977. nvalue(l) -= nvalue(r);
  978. --top;
  979. }
  980. }
  981. break;
  982. case MULTOP:
  983. {
  984. Object *l = top-2;
  985. Object *r = top-1;
  986. if (tonumber(r) || tonumber(l))
  987. call_arith("mul");
  988. else
  989. {
  990. nvalue(l) *= nvalue(r);
  991. --top;
  992. }
  993. }
  994. break;
  995. case DIVOP:
  996. {
  997. Object *l = top-2;
  998. Object *r = top-1;
  999. if (tonumber(r) || tonumber(l))
  1000. call_arith("div");
  1001. else
  1002. {
  1003. nvalue(l) /= nvalue(r);
  1004. --top;
  1005. }
  1006. }
  1007. break;
  1008. case POWOP:
  1009. call_arith("pow");
  1010. break;
  1011. case CONCOP:
  1012. {
  1013. Object *l = top-2;
  1014. Object *r = top-1;
  1015. if (tostring(r) || tostring(l))
  1016. callFB(FB_CONCAT);
  1017. else
  1018. {
  1019. tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  1020. --top;
  1021. }
  1022. }
  1023. break;
  1024. case MINUSOP:
  1025. if (tonumber(top-1))
  1026. {
  1027. tag(top) = LUA_T_NIL;
  1028. incr_top;
  1029. call_arith("unm");
  1030. }
  1031. else
  1032. nvalue(top-1) = - nvalue(top-1);
  1033. break;
  1034. case NOTOP:
  1035. tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1036. nvalue(top-1) = 1;
  1037. break;
  1038. case ONTJMP:
  1039. {
  1040. CodeWord code;
  1041. get_word(code,pc);
  1042. if (tag(top-1) != LUA_T_NIL) pc += code.w;
  1043. }
  1044. break;
  1045. case ONFJMP:
  1046. {
  1047. CodeWord code;
  1048. get_word(code,pc);
  1049. if (tag(top-1) == LUA_T_NIL) pc += code.w;
  1050. }
  1051. break;
  1052. case JMP:
  1053. {
  1054. CodeWord code;
  1055. get_word(code,pc);
  1056. pc += code.w;
  1057. }
  1058. break;
  1059. case UPJMP:
  1060. {
  1061. CodeWord code;
  1062. get_word(code,pc);
  1063. pc -= code.w;
  1064. }
  1065. break;
  1066. case IFFJMP:
  1067. {
  1068. CodeWord code;
  1069. get_word(code,pc);
  1070. top--;
  1071. if (tag(top) == LUA_T_NIL) pc += code.w;
  1072. }
  1073. break;
  1074. case IFFUPJMP:
  1075. {
  1076. CodeWord code;
  1077. get_word(code,pc);
  1078. top--;
  1079. if (tag(top) == LUA_T_NIL) pc -= code.w;
  1080. }
  1081. break;
  1082. case POP: --top; break;
  1083. case CALLFUNC:
  1084. {
  1085. int nParams = *(pc++);
  1086. int nResults = *(pc++);
  1087. StkId newBase = (top-stack)-nParams;
  1088. do_call(newBase, nResults);
  1089. }
  1090. break;
  1091. case RETCODE0:
  1092. case RETCODE:
  1093. if (lua_callhook)
  1094. callHook (base, LUA_T_MARK, 1);
  1095. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1096. case SETLINE:
  1097. {
  1098. CodeWord code;
  1099. get_word(code,pc);
  1100. if ((stack+base-1)->tag != LUA_T_LINE)
  1101. {
  1102. /* open space for LINE value */
  1103. open_stack((top-stack)-base);
  1104. base++;
  1105. (stack+base-1)->tag = LUA_T_LINE;
  1106. }
  1107. (stack+base-1)->value.i = code.w;
  1108. if (lua_linehook)
  1109. lineHook (code.w);
  1110. break;
  1111. }
  1112. default:
  1113. lua_error ("internal error - opcode doesn't match");
  1114. }
  1115. }
  1116. }