2
0

opcode.c 32 KB

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