2
0

opcode.c 31 KB

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