opcode.c 25 KB

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