opcode.c 26 KB

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