opcode.c 26 KB

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