2
0

opcode.c 27 KB

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