opcode.c 24 KB

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