lvm.c 24 KB

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