opcode.c 24 KB

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