opcode.c 31 KB

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