opcode.c 26 KB

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