opcode.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.71 1996/07/24 17:55:57 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. if (c == ID_CHUNK)
  478. status = luaI_undump(f);
  479. else {
  480. if (c == '#')
  481. while ((c=fgetc(f)) != '\n') /* skip first line */;
  482. status = do_protectedmain();
  483. }
  484. lua_closefile();
  485. return status;
  486. }
  487. /*
  488. ** Generate opcode stored on string and execute global statement. Return 0 on
  489. ** success or non 0 on error.
  490. */
  491. int lua_dostring (char *str)
  492. {
  493. int status;
  494. if (str == NULL)
  495. return 1;
  496. lua_openstring(str);
  497. status = do_protectedmain();
  498. lua_closestring();
  499. return status;
  500. }
  501. /*
  502. ** API: set a function as a fallback
  503. */
  504. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  505. {
  506. adjustC(1); /* one slot for the pseudo-function */
  507. stack[CBase].tag = LUA_T_CFUNCTION;
  508. stack[CBase].value.f = luaI_setfallback;
  509. lua_pushstring(name);
  510. lua_pushcfunction(fallback);
  511. if (do_protectedrun(1) == 0)
  512. return (Ref(top-1));
  513. else
  514. return LUA_NOOBJECT;
  515. }
  516. /*
  517. ** API: receives on the stack the table and the index.
  518. ** returns the value.
  519. */
  520. lua_Object lua_getsubscript (void)
  521. {
  522. adjustC(2);
  523. pushsubscript();
  524. CBase++; /* incorporate object in the stack */
  525. return (Ref(top-1));
  526. }
  527. #define MAX_C_BLOCKS 10
  528. static int numCblocks = 0;
  529. static StkId Cblocks[MAX_C_BLOCKS];
  530. /*
  531. ** API: starts a new block
  532. */
  533. void lua_beginblock (void)
  534. {
  535. if (numCblocks >= MAX_C_BLOCKS)
  536. lua_error("`lua_beginblock': too many nested blocks");
  537. Cblocks[numCblocks] = CBase;
  538. numCblocks++;
  539. }
  540. /*
  541. ** API: ends a block
  542. */
  543. void lua_endblock (void)
  544. {
  545. --numCblocks;
  546. CBase = Cblocks[numCblocks];
  547. adjustC(0);
  548. }
  549. /*
  550. ** API: receives on the stack the table, the index, and the new value.
  551. */
  552. void lua_storesubscript (void)
  553. {
  554. adjustC(3);
  555. storesubscript();
  556. }
  557. /*
  558. ** API: creates a new table
  559. */
  560. lua_Object lua_createtable (void)
  561. {
  562. adjustC(0);
  563. avalue(top) = lua_createarray(0);
  564. tag(top) = LUA_T_ARRAY;
  565. incr_top;
  566. CBase++; /* incorporate object in the stack */
  567. return Ref(top-1);
  568. }
  569. /*
  570. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  571. ** 'number' must be 1 to get the first parameter.
  572. */
  573. lua_Object lua_getparam (int number)
  574. {
  575. if (number <= 0 || number > CnResults) return LUA_NOOBJECT;
  576. /* Ref(stack+(CBase-CnResults+number-1)) ==
  577. stack+(CBase-CnResults+number-1)-stack+1 == */
  578. return CBase-CnResults+number;
  579. }
  580. int lua_isnumber (lua_Object object)
  581. {
  582. return (object != LUA_NOOBJECT) && (tonumber(Address(object)) == 0);
  583. }
  584. int lua_isstring (lua_Object object)
  585. {
  586. int t = lua_type(object);
  587. return (t == LUA_T_STRING) || (t == LUA_T_NUMBER);
  588. }
  589. int lua_isfunction (lua_Object object)
  590. {
  591. int t = lua_type(object);
  592. return (t == LUA_T_FUNCTION) || (t == LUA_T_CFUNCTION);
  593. }
  594. /*
  595. ** Given an object handle, return its number value. On error, return 0.0.
  596. */
  597. real lua_getnumber (lua_Object object)
  598. {
  599. if (object == LUA_NOOBJECT) return 0.0;
  600. if (tonumber (Address(object))) return 0.0;
  601. else return (nvalue(Address(object)));
  602. }
  603. /*
  604. ** Given an object handle, return its string pointer. On error, return NULL.
  605. */
  606. char *lua_getstring (lua_Object object)
  607. {
  608. if (object == LUA_NOOBJECT) return NULL;
  609. if (tostring (Address(object))) return NULL;
  610. else return (svalue(Address(object)));
  611. }
  612. /*
  613. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  614. */
  615. lua_CFunction lua_getcfunction (lua_Object object)
  616. {
  617. if (object == LUA_NOOBJECT || tag(Address(object)) != LUA_T_CFUNCTION)
  618. return NULL;
  619. else return (fvalue(Address(object)));
  620. }
  621. /*
  622. ** Given an object handle, return its user data. On error, return NULL.
  623. */
  624. void *lua_getuserdata (lua_Object object)
  625. {
  626. if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)
  627. return NULL;
  628. else return (uvalue(Address(object)));
  629. }
  630. lua_Object lua_getref (int ref)
  631. {
  632. Object *o = luaI_getref(ref);
  633. if (o == NULL)
  634. return LUA_NOOBJECT;
  635. adjustC(0);
  636. luaI_pushobject(o);
  637. CBase++; /* incorporate object in the stack */
  638. return Ref(top-1);
  639. }
  640. void lua_pushref (int ref)
  641. {
  642. Object *o = luaI_getref(ref);
  643. if (o == NULL)
  644. lua_error("access to invalid (possibly garbage collected) reference");
  645. luaI_pushobject(o);
  646. }
  647. int lua_ref (int lock)
  648. {
  649. adjustC(1);
  650. return luaI_ref(--top, lock);
  651. }
  652. /*
  653. ** Get a global object.
  654. */
  655. lua_Object lua_getglobal (char *name)
  656. {
  657. adjustC(0);
  658. getglobal(luaI_findsymbolbyname(name));
  659. CBase++; /* incorporate object in the stack */
  660. return Ref(top-1);
  661. }
  662. /*
  663. ** Store top of the stack at a global variable array field.
  664. */
  665. void lua_storeglobal (char *name)
  666. {
  667. Word n = luaI_findsymbolbyname(name);
  668. adjustC(1);
  669. s_object(n) = *(--top);
  670. }
  671. /*
  672. ** Push a nil object
  673. */
  674. void lua_pushnil (void)
  675. {
  676. tag(top) = LUA_T_NIL;
  677. incr_top;
  678. }
  679. /*
  680. ** Push an object (tag=number) to stack.
  681. */
  682. void lua_pushnumber (real n)
  683. {
  684. tag(top) = LUA_T_NUMBER; nvalue(top) = n;
  685. incr_top;
  686. }
  687. /*
  688. ** Push an object (tag=string) to stack.
  689. */
  690. void lua_pushstring (char *s)
  691. {
  692. if (s == NULL)
  693. tag(top) = LUA_T_NIL;
  694. else
  695. {
  696. tsvalue(top) = lua_createstring(s);
  697. tag(top) = LUA_T_STRING;
  698. }
  699. incr_top;
  700. }
  701. /*>>>>>>>>>#undef lua_pushliteral
  702. void lua_pushliteral(char *s) { lua_pushstring(s); }*/
  703. /*
  704. ** Push an object (tag=cfunction) to stack.
  705. */
  706. void lua_pushcfunction (lua_CFunction fn)
  707. {
  708. tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  709. incr_top;
  710. }
  711. /*
  712. ** Push an object (tag=userdata) to stack.
  713. */
  714. void lua_pushusertag (void *u, int tag)
  715. {
  716. if (tag < LUA_T_USERDATA)
  717. lua_error("invalid tag in `lua_pushusertag'");
  718. tag(top) = tag; uvalue(top) = u;
  719. incr_top;
  720. }
  721. /*
  722. ** Push an object on the stack.
  723. */
  724. void luaI_pushobject (Object *o)
  725. {
  726. *top = *o;
  727. incr_top;
  728. }
  729. /*
  730. ** Push a lua_Object on stack.
  731. */
  732. void lua_pushobject (lua_Object o)
  733. {
  734. if (o == LUA_NOOBJECT)
  735. lua_error("attempt to push a NOOBJECT");
  736. luaI_pushobject(Address(o));
  737. }
  738. int lua_type (lua_Object o)
  739. {
  740. if (o == LUA_NOOBJECT)
  741. return LUA_T_NIL;
  742. else
  743. return tag(Address(o));
  744. }
  745. void luaI_gcFB (Object *o)
  746. {
  747. *top = *o;
  748. incr_top;
  749. callFB(FB_GC);
  750. }
  751. static void call_arith (char *op)
  752. {
  753. lua_pushstring(op);
  754. callFB(FB_ARITH);
  755. }
  756. static void comparison (lua_Type tag_less, lua_Type tag_equal,
  757. lua_Type tag_great, char *op)
  758. {
  759. Object *l = top-2;
  760. Object *r = top-1;
  761. int result;
  762. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  763. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  764. else if (tostring(l) || tostring(r))
  765. {
  766. lua_pushstring(op);
  767. callFB(FB_ORDER);
  768. return;
  769. }
  770. else
  771. result = strcmp(svalue(l), svalue(r));
  772. top--;
  773. nvalue(top-1) = 1;
  774. tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
  775. }
  776. void luaI_packarg (Object *firstelem, Object *arg)
  777. {
  778. int nvararg = (firstelem != NULL) ? top-firstelem : 0;
  779. int i;
  780. if (nvararg < 0) nvararg = 0;
  781. avalue(arg) = lua_createarray(nvararg+1); /* +1 for field 'n' */
  782. tag(arg) = LUA_T_ARRAY;
  783. for (i=0; i<nvararg; i++)
  784. {
  785. Object index;
  786. tag(&index) = LUA_T_NUMBER;
  787. nvalue(&index) = i+1;
  788. *(lua_hashdefine(avalue(arg), &index)) = *(firstelem+i);
  789. }
  790. /* store counter in field "n" */
  791. {
  792. Object index, extra;
  793. tag(&index) = LUA_T_STRING;
  794. tsvalue(&index) = lua_createstring("n");
  795. tag(&extra) = LUA_T_NUMBER;
  796. nvalue(&extra) = nvararg;
  797. *(lua_hashdefine(avalue(arg), &index)) = extra;
  798. }
  799. }
  800. static void adjust_varargs (StkId first_extra_arg)
  801. {
  802. Object arg;
  803. luaI_packarg(stack+first_extra_arg, &arg);
  804. adjust_top(first_extra_arg);
  805. *top = arg; incr_top;
  806. }
  807. /*
  808. ** Execute the given opcode, until a RET. Parameters are between
  809. ** [stack+base,top). Returns n such that the the results are between
  810. ** [stack+n,top).
  811. */
  812. static StkId lua_execute (Byte *pc, StkId base)
  813. {
  814. if (lua_callhook)
  815. callHook (base, LUA_T_MARK, 0);
  816. while (1)
  817. {
  818. OpCode opcode;
  819. switch (opcode = (OpCode)*pc++)
  820. {
  821. case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
  822. case PUSH0: case PUSH1: case PUSH2:
  823. tag(top) = LUA_T_NUMBER;
  824. nvalue(top) = opcode-PUSH0;
  825. incr_top;
  826. break;
  827. case PUSHBYTE:
  828. tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  829. case PUSHWORD:
  830. {
  831. Word w;
  832. get_word(w,pc);
  833. tag(top) = LUA_T_NUMBER; nvalue(top) = w;
  834. incr_top;
  835. }
  836. break;
  837. case PUSHFLOAT:
  838. {
  839. real num;
  840. get_float(num,pc);
  841. tag(top) = LUA_T_NUMBER; nvalue(top) = num;
  842. incr_top;
  843. }
  844. break;
  845. case PUSHSTRING:
  846. {
  847. Word w;
  848. get_word(w,pc);
  849. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  850. incr_top;
  851. }
  852. break;
  853. case PUSHFUNCTION:
  854. {
  855. TFunc *f;
  856. get_code(f,pc);
  857. luaI_insertfunction(f); /* may take part in GC */
  858. top->tag = LUA_T_FUNCTION;
  859. top->value.tf = f;
  860. incr_top;
  861. }
  862. break;
  863. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  864. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  865. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  866. case PUSHLOCAL9:
  867. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  868. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  869. case PUSHGLOBAL:
  870. {
  871. Word w;
  872. get_word(w,pc);
  873. getglobal(w);
  874. }
  875. break;
  876. case PUSHINDEXED:
  877. pushsubscript();
  878. break;
  879. case PUSHSELF:
  880. {
  881. Object receiver = *(top-1);
  882. Word w;
  883. get_word(w,pc);
  884. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  885. incr_top;
  886. pushsubscript();
  887. *top = receiver;
  888. incr_top;
  889. break;
  890. }
  891. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  892. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  893. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  894. case STORELOCAL9:
  895. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  896. break;
  897. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  898. case STOREGLOBAL:
  899. {
  900. Word w;
  901. get_word(w,pc);
  902. s_object(w) = *(--top);
  903. }
  904. break;
  905. case STOREINDEXED0:
  906. storesubscript();
  907. break;
  908. case STOREINDEXED:
  909. {
  910. int n = *pc++;
  911. if (tag(top-3-n) != LUA_T_ARRAY)
  912. {
  913. lua_checkstack(top+2);
  914. *(top+1) = *(top-1);
  915. *(top) = *(top-2-n);
  916. *(top-1) = *(top-3-n);
  917. top += 2;
  918. callFB(FB_SETTABLE);
  919. }
  920. else
  921. {
  922. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  923. *h = *(top-1);
  924. top--;
  925. }
  926. }
  927. break;
  928. case STORELIST0:
  929. case STORELIST:
  930. {
  931. int m, n;
  932. Object *arr;
  933. if (opcode == STORELIST0) m = 0;
  934. else m = *(pc++) * FIELDS_PER_FLUSH;
  935. n = *(pc++);
  936. arr = top-n-1;
  937. while (n)
  938. {
  939. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  940. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  941. top--;
  942. n--;
  943. }
  944. }
  945. break;
  946. case STORERECORD:
  947. {
  948. int n = *(pc++);
  949. Object *arr = top-n-1;
  950. while (n)
  951. {
  952. Word w;
  953. get_word(w,pc);
  954. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[w];
  955. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  956. top--;
  957. n--;
  958. }
  959. }
  960. break;
  961. case ADJUST0:
  962. adjust_top(base);
  963. break;
  964. case ADJUST:
  965. adjust_top(base + *(pc++));
  966. break;
  967. case VARARGS:
  968. adjust_varargs(base + *(pc++));
  969. break;
  970. case CREATEARRAY:
  971. {
  972. Word size;
  973. get_word(size,pc);
  974. avalue(top) = lua_createarray(size);
  975. tag(top) = LUA_T_ARRAY;
  976. incr_top;
  977. }
  978. break;
  979. case EQOP:
  980. {
  981. int res = lua_equalObj(top-2, top-1);
  982. --top;
  983. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  984. nvalue(top-1) = 1;
  985. }
  986. break;
  987. case LTOP:
  988. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
  989. break;
  990. case LEOP:
  991. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
  992. break;
  993. case GTOP:
  994. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
  995. break;
  996. case GEOP:
  997. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
  998. break;
  999. case ADDOP:
  1000. {
  1001. Object *l = top-2;
  1002. Object *r = top-1;
  1003. if (tonumber(r) || tonumber(l))
  1004. call_arith("add");
  1005. else
  1006. {
  1007. nvalue(l) += nvalue(r);
  1008. --top;
  1009. }
  1010. }
  1011. break;
  1012. case SUBOP:
  1013. {
  1014. Object *l = top-2;
  1015. Object *r = top-1;
  1016. if (tonumber(r) || tonumber(l))
  1017. call_arith("sub");
  1018. else
  1019. {
  1020. nvalue(l) -= nvalue(r);
  1021. --top;
  1022. }
  1023. }
  1024. break;
  1025. case MULTOP:
  1026. {
  1027. Object *l = top-2;
  1028. Object *r = top-1;
  1029. if (tonumber(r) || tonumber(l))
  1030. call_arith("mul");
  1031. else
  1032. {
  1033. nvalue(l) *= nvalue(r);
  1034. --top;
  1035. }
  1036. }
  1037. break;
  1038. case DIVOP:
  1039. {
  1040. Object *l = top-2;
  1041. Object *r = top-1;
  1042. if (tonumber(r) || tonumber(l))
  1043. call_arith("div");
  1044. else
  1045. {
  1046. nvalue(l) /= nvalue(r);
  1047. --top;
  1048. }
  1049. }
  1050. break;
  1051. case POWOP:
  1052. call_arith("pow");
  1053. break;
  1054. case CONCOP:
  1055. {
  1056. Object *l = top-2;
  1057. Object *r = top-1;
  1058. if (tostring(r) || tostring(l))
  1059. callFB(FB_CONCAT);
  1060. else
  1061. {
  1062. tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  1063. --top;
  1064. }
  1065. }
  1066. break;
  1067. case MINUSOP:
  1068. if (tonumber(top-1))
  1069. {
  1070. tag(top) = LUA_T_NIL;
  1071. incr_top;
  1072. call_arith("unm");
  1073. }
  1074. else
  1075. nvalue(top-1) = - nvalue(top-1);
  1076. break;
  1077. case NOTOP:
  1078. tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  1079. nvalue(top-1) = 1;
  1080. break;
  1081. case ONTJMP:
  1082. {
  1083. Word w;
  1084. get_word(w,pc);
  1085. if (tag(top-1) != LUA_T_NIL) pc += w;
  1086. }
  1087. break;
  1088. case ONFJMP:
  1089. {
  1090. Word w;
  1091. get_word(w,pc);
  1092. if (tag(top-1) == LUA_T_NIL) pc += w;
  1093. }
  1094. break;
  1095. case JMP:
  1096. {
  1097. Word w;
  1098. get_word(w,pc);
  1099. pc += w;
  1100. }
  1101. break;
  1102. case UPJMP:
  1103. {
  1104. Word w;
  1105. get_word(w,pc);
  1106. pc -= w;
  1107. }
  1108. break;
  1109. case IFFJMP:
  1110. {
  1111. Word w;
  1112. get_word(w,pc);
  1113. top--;
  1114. if (tag(top) == LUA_T_NIL) pc += w;
  1115. }
  1116. break;
  1117. case IFFUPJMP:
  1118. {
  1119. Word w;
  1120. get_word(w,pc);
  1121. top--;
  1122. if (tag(top) == LUA_T_NIL) pc -= w;
  1123. }
  1124. break;
  1125. case POP: --top; break;
  1126. case CALLFUNC:
  1127. {
  1128. int nParams = *(pc++);
  1129. int nResults = *(pc++);
  1130. StkId newBase = (top-stack)-nParams;
  1131. do_call(newBase, nResults);
  1132. }
  1133. break;
  1134. case RETCODE0:
  1135. case RETCODE:
  1136. if (lua_callhook)
  1137. callHook (base, LUA_T_MARK, 1);
  1138. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1139. case SETLINE:
  1140. {
  1141. Word line;
  1142. get_word(line,pc);
  1143. if ((stack+base-1)->tag != LUA_T_LINE)
  1144. {
  1145. /* open space for LINE value */
  1146. open_stack((top-stack)-base);
  1147. base++;
  1148. (stack+base-1)->tag = LUA_T_LINE;
  1149. }
  1150. (stack+base-1)->value.i = line;
  1151. if (lua_linehook)
  1152. lineHook (line);
  1153. break;
  1154. }
  1155. default:
  1156. lua_error ("internal error - opcode doesn't match");
  1157. }
  1158. }
  1159. }