opcode.c 32 KB

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