opcode.c 24 KB

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