lvm.c 24 KB

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