lvm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. ** $Id: lvm.c,v 2.33 2005/03/16 16:59:21 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lvm_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "lobject.h"
  18. #include "lopcodes.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. #include "lvm.h"
  24. /* limit for table tag-method chains (to avoid loops) */
  25. #define MAXTAGLOOP 100
  26. const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
  27. lua_Number num;
  28. if (ttisnumber(obj)) return obj;
  29. if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  30. setnvalue(n, num);
  31. return n;
  32. }
  33. else
  34. return NULL;
  35. }
  36. int luaV_tostring (lua_State *L, StkId obj) {
  37. if (!ttisnumber(obj))
  38. return 0;
  39. else {
  40. char s[LUAI_MAXNUMBER2STR];
  41. lua_number2str(s, nvalue(obj));
  42. setsvalue2s(L, obj, luaS_new(L, s));
  43. return 1;
  44. }
  45. }
  46. static void traceexec (lua_State *L, const Instruction *pc) {
  47. lu_byte mask = L->hookmask;
  48. CallInfo *ci = L->ci;
  49. const Instruction *oldpc = ci->savedpc;
  50. ci->savedpc = pc;
  51. if (mask > LUA_MASKLINE) { /* instruction-hook set? */
  52. if (L->hookcount == 0) {
  53. resethookcount(L);
  54. luaD_callhook(L, LUA_HOOKCOUNT, -1);
  55. return;
  56. }
  57. }
  58. if (mask & LUA_MASKLINE) {
  59. Proto *p = ci_func(ci)->l.p;
  60. int npc = pcRel(pc, p);
  61. int newline = getline(p, npc);
  62. /* call linehook when enter a new function, when jump back (loop),
  63. or when enter a new line */
  64. if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
  65. luaD_callhook(L, LUA_HOOKLINE, newline);
  66. }
  67. }
  68. static void prepTMcall (lua_State *L, const TValue *f,
  69. const TValue *p1, const TValue *p2) {
  70. setobj2s(L, L->top, f); /* push function */
  71. setobj2s(L, L->top+1, p1); /* 1st argument */
  72. setobj2s(L, L->top+2, p2); /* 2nd argument */
  73. }
  74. static void callTMres (lua_State *L, StkId res) {
  75. ptrdiff_t result = savestack(L, res);
  76. luaD_checkstack(L, 3);
  77. L->top += 3;
  78. luaD_call(L, L->top - 3, 1);
  79. res = restorestack(L, result);
  80. L->top--;
  81. setobjs2s(L, res, L->top);
  82. }
  83. static void callTM (lua_State *L) {
  84. luaD_checkstack(L, 4);
  85. L->top += 4;
  86. luaD_call(L, L->top - 4, 0);
  87. }
  88. StkId luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val,
  89. const Instruction *pc) {
  90. int loop;
  91. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  92. const TValue *tm;
  93. if (ttistable(t)) { /* `t' is a table? */
  94. Table *h = hvalue(t);
  95. const TValue *res = luaH_get(h, key); /* do a primitive get */
  96. if (!ttisnil(res) || /* result is no nil? */
  97. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  98. setobj2s(L, val, res);
  99. return L->base;
  100. }
  101. /* else will try the tag method */
  102. }
  103. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) {
  104. L->ci->savedpc = pc;
  105. luaG_typeerror(L, t, "index");
  106. }
  107. if (ttisfunction(tm)) {
  108. L->ci->savedpc = pc;
  109. prepTMcall(L, tm, t, key);
  110. callTMres(L, val);
  111. return L->base;
  112. }
  113. t = tm; /* else repeat with `tm' */
  114. }
  115. L->ci->savedpc = pc;
  116. luaG_runerror(L, "loop in gettable");
  117. return NULL; /* to avoid warnings */
  118. }
  119. StkId luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val,
  120. const Instruction *pc) {
  121. int loop;
  122. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  123. const TValue *tm;
  124. if (ttistable(t)) { /* `t' is a table? */
  125. Table *h = hvalue(t);
  126. TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
  127. if (!ttisnil(oldval) || /* result is no nil? */
  128. (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
  129. setobj2t(L, oldval, val);
  130. luaC_barriert(L, h, val);
  131. return L->base;
  132. }
  133. /* else will try the tag method */
  134. }
  135. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) {
  136. L->ci->savedpc = pc;
  137. luaG_typeerror(L, t, "index");
  138. }
  139. if (ttisfunction(tm)) {
  140. L->ci->savedpc = pc;
  141. prepTMcall(L, tm, t, key);
  142. setobj2s(L, L->top+3, val); /* 3th argument */
  143. callTM(L);
  144. return L->base;
  145. }
  146. t = tm; /* else repeat with `tm' */
  147. }
  148. L->ci->savedpc = pc;
  149. luaG_runerror(L, "loop in settable");
  150. return NULL; /* to avoid warnings */
  151. }
  152. static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
  153. StkId res, TMS event) {
  154. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  155. if (ttisnil(tm))
  156. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  157. if (!ttisfunction(tm)) return 0;
  158. prepTMcall(L, tm, p1, p2);
  159. callTMres(L, res);
  160. return 1;
  161. }
  162. static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
  163. TMS event) {
  164. const TValue *tm1 = fasttm(L, mt1, event);
  165. const TValue *tm2;
  166. if (tm1 == NULL) return NULL; /* no metamethod */
  167. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  168. tm2 = fasttm(L, mt2, event);
  169. if (tm2 == NULL) return NULL; /* no metamethod */
  170. if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
  171. return tm1;
  172. return NULL;
  173. }
  174. static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
  175. TMS event) {
  176. const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
  177. const TValue *tm2;
  178. if (ttisnil(tm1)) return -1; /* no metamethod? */
  179. tm2 = luaT_gettmbyobj(L, p2, event);
  180. if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
  181. return -1;
  182. prepTMcall(L, tm1, p1, p2);
  183. callTMres(L, L->top);
  184. return !l_isfalse(L->top);
  185. }
  186. static int l_strcmp (const TString *ls, const TString *rs) {
  187. const char *l = getstr(ls);
  188. size_t ll = ls->tsv.len;
  189. const char *r = getstr(rs);
  190. size_t lr = rs->tsv.len;
  191. for (;;) {
  192. int temp = strcoll(l, r);
  193. if (temp != 0) return temp;
  194. else { /* strings are equal up to a `\0' */
  195. size_t len = strlen(l); /* index of first `\0' in both strings */
  196. if (len == lr) /* r is finished? */
  197. return (len == ll) ? 0 : 1;
  198. else if (len == ll) /* l is finished? */
  199. return -1; /* l is smaller than r (because r is not finished) */
  200. /* both strings longer than `len'; go on comparing (after the `\0') */
  201. len++;
  202. l += len; ll -= len; r += len; lr -= len;
  203. }
  204. }
  205. }
  206. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  207. int res;
  208. if (ttype(l) != ttype(r))
  209. return luaG_ordererror(L, l, r);
  210. else if (ttisnumber(l))
  211. return luai_numlt(nvalue(l), nvalue(r));
  212. else if (ttisstring(l))
  213. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  214. else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  215. return res;
  216. return luaG_ordererror(L, l, r);
  217. }
  218. static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
  219. int res;
  220. if (ttype(l) != ttype(r))
  221. return luaG_ordererror(L, l, r);
  222. else if (ttisnumber(l))
  223. return luai_numle(nvalue(l), nvalue(r));
  224. else if (ttisstring(l))
  225. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  226. else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
  227. return res;
  228. else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
  229. return !res;
  230. return luaG_ordererror(L, l, r);
  231. }
  232. int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
  233. const TValue *tm;
  234. lua_assert(ttype(t1) == ttype(t2));
  235. switch (ttype(t1)) {
  236. case LUA_TNIL: return 1;
  237. case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
  238. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  239. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  240. case LUA_TUSERDATA: {
  241. if (uvalue(t1) == uvalue(t2)) return 1;
  242. tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
  243. TM_EQ);
  244. break; /* will try TM */
  245. }
  246. case LUA_TTABLE: {
  247. if (hvalue(t1) == hvalue(t2)) return 1;
  248. tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
  249. break; /* will try TM */
  250. }
  251. default: return gcvalue(t1) == gcvalue(t2);
  252. }
  253. if (tm == NULL) return 0; /* no TM? */
  254. prepTMcall(L, tm, t1, t2);
  255. callTMres(L, L->top); /* call TM */
  256. return !l_isfalse(L->top);
  257. }
  258. void luaV_concat (lua_State *L, int total, int last) {
  259. do {
  260. StkId top = L->base + last + 1;
  261. int n = 2; /* number of elements handled in this pass (at least 2) */
  262. if (!tostring(L, top-2) || !tostring(L, top-1)) {
  263. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  264. luaG_concaterror(L, top-2, top-1);
  265. } else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */
  266. /* at least two string values; get as many as possible */
  267. size_t tl = tsvalue(top-1)->len;
  268. char *buffer;
  269. int i;
  270. /* collect total length */
  271. for (n = 1; n < total && tostring(L, top-n-1); n++) {
  272. size_t l = tsvalue(top-n-1)->len;
  273. if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
  274. tl += l;
  275. }
  276. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  277. tl = 0;
  278. for (i=n; i>0; i--) { /* concat all strings */
  279. size_t l = tsvalue(top-i)->len;
  280. memcpy(buffer+tl, svalue(top-i), l);
  281. tl += l;
  282. }
  283. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  284. }
  285. total -= n-1; /* got `n' strings to create 1 new */
  286. last -= n-1;
  287. } while (total > 1); /* repeat until only 1 result left */
  288. }
  289. static StkId Arith (lua_State *L, StkId ra, const TValue *rb,
  290. const TValue *rc, TMS op, const Instruction *pc) {
  291. TValue tempb, tempc;
  292. const TValue *b, *c;
  293. L->ci->savedpc = pc;
  294. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  295. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  296. lua_Number nb = nvalue(b), nc = nvalue(c);
  297. switch (op) {
  298. case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
  299. case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
  300. case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
  301. case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
  302. case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
  303. case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
  304. default: lua_assert(0); break;
  305. }
  306. }
  307. else if (!call_binTM(L, rb, rc, ra, op))
  308. luaG_aritherror(L, rb, rc);
  309. return L->base;
  310. }
  311. /*
  312. ** some macros for common tasks in `luaV_execute'
  313. */
  314. #define runtime_check(L, c) { if (!(c)) break; }
  315. #define RA(i) (base+GETARG_A(i))
  316. /* to be used after possible stack reallocation */
  317. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  318. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  319. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  320. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  321. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  322. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  323. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  324. #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
  325. StkId luaV_execute (lua_State *L, int nexeccalls) {
  326. LClosure *cl;
  327. TValue *k;
  328. StkId base;
  329. const Instruction *pc;
  330. callentry: /* entry point when calling new functions */
  331. if (L->hookmask & LUA_MASKCALL)
  332. luaD_callhook(L, LUA_HOOKCALL, -1);
  333. retentry: /* entry point when returning to old functions */
  334. pc = L->ci->savedpc;
  335. cl = &clvalue(L->ci->func)->l;
  336. base = L->base;
  337. k = cl->p->k;
  338. /* main loop of interpreter */
  339. for (;;) {
  340. const Instruction i = *pc++;
  341. StkId ra;
  342. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  343. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  344. traceexec(L, pc); /***/
  345. if (L->status == LUA_YIELD) { /* did hook yield? */
  346. L->ci->savedpc = pc - 1;
  347. return NULL;
  348. }
  349. base = L->base;
  350. }
  351. /* warning!! several calls may realloc the stack and invalidate `ra' */
  352. ra = RA(i);
  353. lua_assert(base == L->ci->base && base == L->base);
  354. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  355. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  356. switch (GET_OPCODE(i)) {
  357. case OP_MOVE: {
  358. setobjs2s(L, ra, RB(i));
  359. continue;
  360. }
  361. case OP_LOADK: {
  362. setobj2s(L, ra, KBx(i));
  363. continue;
  364. }
  365. case OP_LOADBOOL: {
  366. setbvalue(ra, GETARG_B(i));
  367. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  368. continue;
  369. }
  370. case OP_LOADNIL: {
  371. TValue *rb = RB(i);
  372. do {
  373. setnilvalue(rb--);
  374. } while (rb >= ra);
  375. continue;
  376. }
  377. case OP_GETUPVAL: {
  378. int b = GETARG_B(i);
  379. setobj2s(L, ra, cl->upvals[b]->v);
  380. continue;
  381. }
  382. case OP_GETGLOBAL: {
  383. TValue g;
  384. TValue *rb = KBx(i);
  385. sethvalue(L, &g, cl->env);
  386. lua_assert(ttisstring(rb));
  387. base = luaV_gettable(L, &g, rb, ra, pc); /***/
  388. continue;
  389. }
  390. case OP_GETTABLE: {
  391. base = luaV_gettable(L, RB(i), RKC(i), ra, pc); /***/
  392. continue;
  393. }
  394. case OP_SETGLOBAL: {
  395. TValue g;
  396. sethvalue(L, &g, cl->env);
  397. lua_assert(ttisstring(KBx(i)));
  398. base = luaV_settable(L, &g, KBx(i), ra, pc); /***/
  399. continue;
  400. }
  401. case OP_SETUPVAL: {
  402. UpVal *uv = cl->upvals[GETARG_B(i)];
  403. setobj(L, uv->v, ra);
  404. luaC_barrier(L, uv, ra);
  405. continue;
  406. }
  407. case OP_SETTABLE: {
  408. base = luaV_settable(L, ra, RKB(i), RKC(i), pc); /***/
  409. continue;
  410. }
  411. case OP_NEWTABLE: {
  412. int b = GETARG_B(i);
  413. int c = GETARG_C(i);
  414. sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
  415. L->ci->savedpc = pc;
  416. luaC_checkGC(L); /***/
  417. base = L->base;
  418. continue;
  419. }
  420. case OP_SELF: {
  421. StkId rb = RB(i);
  422. setobjs2s(L, ra+1, rb);
  423. base = luaV_gettable(L, rb, RKC(i), ra, pc); /***/
  424. continue;
  425. }
  426. case OP_ADD: {
  427. TValue *rb = RKB(i);
  428. TValue *rc = RKC(i);
  429. if (ttisnumber(rb) && ttisnumber(rc)) {
  430. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  431. setnvalue(ra, luai_numadd(nb, nc));
  432. }
  433. else
  434. base = Arith(L, ra, rb, rc, TM_ADD, pc); /***/
  435. continue;
  436. }
  437. case OP_SUB: {
  438. TValue *rb = RKB(i);
  439. TValue *rc = RKC(i);
  440. if (ttisnumber(rb) && ttisnumber(rc)) {
  441. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  442. setnvalue(ra, luai_numsub(nb, nc));
  443. }
  444. else
  445. base = Arith(L, ra, rb, rc, TM_SUB, pc); /***/
  446. continue;
  447. }
  448. case OP_MUL: {
  449. TValue *rb = RKB(i);
  450. TValue *rc = RKC(i);
  451. if (ttisnumber(rb) && ttisnumber(rc)) {
  452. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  453. setnvalue(ra, luai_nummul(nb, nc));
  454. }
  455. else
  456. base = Arith(L, ra, rb, rc, TM_MUL, pc); /***/
  457. continue;
  458. }
  459. case OP_DIV: {
  460. TValue *rb = RKB(i);
  461. TValue *rc = RKC(i);
  462. if (ttisnumber(rb) && ttisnumber(rc)) {
  463. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  464. setnvalue(ra, luai_numdiv(nb, nc));
  465. }
  466. else
  467. base = Arith(L, ra, rb, rc, TM_DIV, pc); /***/
  468. continue;
  469. }
  470. case OP_MOD: {
  471. TValue *rb = RKB(i);
  472. TValue *rc = RKC(i);
  473. if (ttisnumber(rb) && ttisnumber(rc)) {
  474. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  475. setnvalue(ra, luai_nummod(nb, nc));
  476. }
  477. else
  478. base = Arith(L, ra, rb, rc, TM_MOD, pc); /***/
  479. continue;
  480. }
  481. case OP_POW: {
  482. TValue *rb = RKB(i);
  483. TValue *rc = RKC(i);
  484. if (ttisnumber(rb) && ttisnumber(rc)) {
  485. lua_Number nb = nvalue(rb), nc = nvalue(rc);
  486. setnvalue(ra, luai_numpow(nb, nc));
  487. }
  488. else
  489. base = Arith(L, ra, rb, rc, TM_POW, pc); /***/
  490. continue;
  491. }
  492. case OP_UNM: {
  493. const TValue *rb = RB(i);
  494. TValue temp;
  495. if (tonumber(rb, &temp)) {
  496. lua_Number nb = nvalue(rb);
  497. setnvalue(ra, luai_numunm(nb));
  498. }
  499. else {
  500. setnilvalue(&temp);
  501. L->ci->savedpc = pc;
  502. if (!call_binTM(L, RB(i), &temp, ra, TM_UNM)) /***/
  503. luaG_aritherror(L, RB(i), &temp);
  504. base = L->base;
  505. }
  506. continue;
  507. }
  508. case OP_NOT: {
  509. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  510. setbvalue(ra, res);
  511. continue;
  512. }
  513. case OP_SIZ: {
  514. const TValue *rb = RB(i);
  515. switch (ttype(rb)) {
  516. case LUA_TTABLE:
  517. setnvalue(ra, cast(lua_Number, luaH_getn(hvalue(rb))));
  518. break;
  519. case LUA_TSTRING:
  520. setnvalue(ra, cast(lua_Number, tsvalue(rb)->len));
  521. break;
  522. default: /* no metamethod?? */
  523. L->ci->savedpc = pc;
  524. luaG_typeerror(L, rb, "get the size of");
  525. }
  526. continue;
  527. }
  528. case OP_CONCAT: {
  529. int b = GETARG_B(i);
  530. int c = GETARG_C(i);
  531. L->ci->savedpc = pc;
  532. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */ /***/
  533. luaC_checkGC(L); /***/
  534. base = L->base;
  535. setobjs2s(L, RA(i), base+b);
  536. continue;
  537. }
  538. case OP_JMP: {
  539. dojump(L, pc, GETARG_sBx(i));
  540. continue;
  541. }
  542. case OP_EQ: {
  543. L->ci->savedpc = pc;
  544. if (equalobj(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  545. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  546. base = L->base;
  547. continue;
  548. }
  549. case OP_LT: {
  550. L->ci->savedpc = pc;
  551. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  552. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  553. base = L->base;
  554. continue;
  555. }
  556. case OP_LE: {
  557. L->ci->savedpc = pc;
  558. if (lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) pc++; /***/
  559. else dojump(L, pc, GETARG_sBx(*pc) + 1);
  560. base = L->base;
  561. continue;
  562. }
  563. case OP_TEST: {
  564. TValue *rb = RB(i);
  565. if (l_isfalse(rb) == GETARG_C(i)) pc++;
  566. else {
  567. setobjs2s(L, ra, rb);
  568. dojump(L, pc, GETARG_sBx(*pc) + 1);
  569. }
  570. continue;
  571. }
  572. case OP_CALL: { /***/
  573. int pcr;
  574. int b = GETARG_B(i);
  575. int nresults = GETARG_C(i) - 1;
  576. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  577. L->ci->savedpc = pc;
  578. pcr = luaD_precall(L, ra, nresults);
  579. if (pcr == PCRLUA) {
  580. nexeccalls++;
  581. goto callentry; /* restart luaV_execute over new Lua function */
  582. }
  583. else if (pcr == PCRC) {
  584. /* it was a C function (`precall' called it); adjust results */
  585. if (nresults >= 0) L->top = L->ci->top;
  586. base = L->base;
  587. continue;
  588. }
  589. else {
  590. lua_assert(pcr == PCRYIELD);
  591. return NULL;
  592. }
  593. }
  594. case OP_TAILCALL: { /***/
  595. int pcr;
  596. int b = GETARG_B(i);
  597. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  598. L->ci->savedpc = pc;
  599. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  600. pcr = luaD_precall(L, ra, LUA_MULTRET);
  601. if (pcr == PCRLUA) {
  602. /* tail call: put new frame in place of previous one */
  603. CallInfo *ci = L->ci - 1; /* previous frame */
  604. int aux;
  605. StkId func = ci->func;
  606. StkId pfunc = (ci+1)->func; /* previous function index */
  607. if (L->openupval) luaF_close(L, ci->base);
  608. base = ci->base = ci->func + ((ci+1)->base - pfunc);
  609. L->base = base;
  610. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  611. setobjs2s(L, func+aux, pfunc+aux);
  612. ci->top = L->top = func+aux; /* correct top */
  613. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  614. ci->savedpc = L->ci->savedpc;
  615. ci->tailcalls++; /* one more call lost */
  616. L->ci--; /* remove new frame */
  617. goto callentry;
  618. }
  619. else if (pcr == PCRC) {
  620. /* it was a C function (`precall' called it) */
  621. base = L->base;
  622. continue;
  623. }
  624. else {
  625. lua_assert(pcr == PCRYIELD);
  626. return NULL;
  627. }
  628. }
  629. case OP_RETURN: {
  630. CallInfo *ci = L->ci - 1; /* previous function frame */
  631. int b = GETARG_B(i);
  632. if (b != 0) L->top = ra+b-1;
  633. if (L->openupval) luaF_close(L, base);
  634. L->ci->savedpc = pc;
  635. if (--nexeccalls == 0) /* was previous function running `here'? */
  636. return ra; /* no: return */
  637. else { /* yes: continue its execution */
  638. int nresults = (ci+1)->nresults;
  639. lua_assert(isLua(ci));
  640. lua_assert(GET_OPCODE(*(ci->savedpc - 1)) == OP_CALL);
  641. luaD_poscall(L, nresults, ra);
  642. if (nresults >= 0) L->top = L->ci->top;
  643. goto retentry;
  644. }
  645. }
  646. case OP_FORLOOP: {
  647. lua_Number step = nvalue(ra+2);
  648. lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
  649. lua_Number limit = nvalue(ra+1);
  650. if (step > 0 ? luai_numle(idx, limit) : luai_numle(limit, idx)) {
  651. dojump(L, pc, GETARG_sBx(i)); /* jump back */
  652. setnvalue(ra, idx); /* update internal index... */
  653. setnvalue(ra+3, idx); /* ...and external index */
  654. }
  655. continue;
  656. }
  657. case OP_FORPREP: { /***/
  658. const TValue *init = ra;
  659. const TValue *plimit = ra+1;
  660. const TValue *pstep = ra+2;
  661. L->ci->savedpc = pc;
  662. if (!tonumber(init, ra))
  663. luaG_runerror(L, "`for' initial value must be a number");
  664. else if (!tonumber(plimit, ra+1))
  665. luaG_runerror(L, "`for' limit must be a number");
  666. else if (!tonumber(pstep, ra+2))
  667. luaG_runerror(L, "`for' step must be a number");
  668. setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
  669. dojump(L, pc, GETARG_sBx(i));
  670. continue;
  671. }
  672. case OP_TFORLOOP: {
  673. StkId cb = ra + 3; /* call base */
  674. setobjs2s(L, cb+2, ra+2);
  675. setobjs2s(L, cb+1, ra+1);
  676. setobjs2s(L, cb, ra);
  677. L->top = cb+3; /* func. + 2 args (state and index) */
  678. L->ci->savedpc = pc;
  679. luaD_call(L, cb, GETARG_C(i)); /***/
  680. L->top = L->ci->top;
  681. base = L->base;
  682. cb = RA(i) + 3; /* previous call may change the stack */
  683. if (ttisnil(cb)) /* break loop? */
  684. pc++; /* skip jump (break loop) */
  685. else {
  686. setobjs2s(L, cb-1, cb); /* save control variable */
  687. dojump(L, pc, GETARG_sBx(*pc) + 1); /* jump back */
  688. }
  689. continue;
  690. }
  691. case OP_TFORPREP: { /* for compatibility only */
  692. if (ttistable(ra)) {
  693. setobjs2s(L, ra+1, ra);
  694. setobj2s(L, ra, luaH_getstr(hvalue(gt(L)), luaS_new(L, "next")));
  695. }
  696. dojump(L, pc, GETARG_sBx(i));
  697. continue;
  698. }
  699. case OP_SETLIST: {
  700. int n = GETARG_B(i);
  701. int c = GETARG_C(i);
  702. int last;
  703. Table *h;
  704. if (n == 0) {
  705. n = L->top - ra - 1;
  706. L->top = L->ci->top;
  707. }
  708. if (c == 0) c = cast(int, *pc++);
  709. runtime_check(L, ttistable(ra));
  710. h = hvalue(ra);
  711. last = ((c-1)*LFIELDS_PER_FLUSH) + n + LUA_FIRSTINDEX - 1;
  712. if (last > h->sizearray) /* needs more space? */
  713. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  714. for (; n > 0; n--) {
  715. TValue *val = ra+n;
  716. setobj2t(L, luaH_setnum(L, h, last--), val);
  717. luaC_barriert(L, h, val);
  718. }
  719. continue;
  720. }
  721. case OP_CLOSE: {
  722. luaF_close(L, ra);
  723. continue;
  724. }
  725. case OP_CLOSURE: {
  726. Proto *p;
  727. Closure *ncl;
  728. int nup, j;
  729. p = cl->p->p[GETARG_Bx(i)];
  730. nup = p->nups;
  731. ncl = luaF_newLclosure(L, nup, cl->env);
  732. ncl->l.p = p;
  733. for (j=0; j<nup; j++, pc++) {
  734. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  735. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  736. else {
  737. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  738. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  739. }
  740. }
  741. setclvalue(L, ra, ncl);
  742. L->ci->savedpc = pc;
  743. luaC_checkGC(L); /***/
  744. base = L->base;
  745. continue;
  746. }
  747. case OP_VARARG: {
  748. int b = GETARG_B(i) - 1;
  749. int j;
  750. CallInfo *ci = L->ci;
  751. int n = ci->base - ci->func - cl->p->numparams - 1;
  752. if (b == LUA_MULTRET) {
  753. b = n;
  754. L->top = ra + n;
  755. }
  756. for (j=0; j<b && j<n; j++)
  757. setobjs2s(L, ra+j, ci->base - n + j);
  758. for (; j<b; j++)
  759. setnilvalue(ra+j);
  760. continue;
  761. }
  762. }
  763. }
  764. }