lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. ** $Id: lvm.c,v 1.234 2002/06/03 14:08:43 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lobject.h"
  15. #include "lopcodes.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #include "lvm.h"
  21. /* function to convert a lua_Number to a string */
  22. #ifndef lua_number2str
  23. #include <stdio.h>
  24. #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  25. #endif
  26. /* limit for table tag-method chains (to avoid loops) */
  27. #define MAXTAGLOOP 10000
  28. static void luaV_checkGC (lua_State *L, StkId top) {
  29. if (G(L)->nblocks >= G(L)->GCthreshold) {
  30. L->top = top; /* limit for active registers */
  31. luaC_collectgarbage(L);
  32. L->top = L->ci->top; /* restore old top position */
  33. }
  34. }
  35. const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
  36. lua_Number num;
  37. if (ttype(obj) == LUA_TNUMBER) return obj;
  38. if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &num)) {
  39. setnvalue(n, num);
  40. return n;
  41. }
  42. else
  43. return NULL;
  44. }
  45. int luaV_tostring (lua_State *L, TObject *obj) {
  46. if (ttype(obj) != LUA_TNUMBER)
  47. return 0;
  48. else {
  49. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  50. lua_number2str(s, nvalue(obj));
  51. setsvalue(obj, luaS_new(L, s));
  52. return 1;
  53. }
  54. }
  55. static void traceexec (lua_State *L) {
  56. CallInfo *ci = L->ci;
  57. Proto *p = ci_func(ci)->l.p;
  58. int newline = getline(p, pcRel(*ci->pc, p));
  59. if (pcRel(*ci->pc, p) == 0) /* tracing may be starting now? */
  60. ci->savedpc = *ci->pc; /* initialize `savedpc' */
  61. /* calls linehook when enters a new line or jumps back (loop) */
  62. if (*ci->pc <= ci->savedpc || newline != getline(p, pcRel(ci->savedpc, p))) {
  63. luaD_lineHook(L, newline);
  64. ci = L->ci; /* previous call may reallocate `ci' */
  65. }
  66. ci->savedpc = *ci->pc;
  67. }
  68. static void callTMres (lua_State *L, const TObject *f,
  69. const TObject *p1, const TObject *p2, TObject *result ) {
  70. ptrdiff_t res = savestack(L, result);
  71. setobj(L->top, f); /* push function */
  72. setobj(L->top+1, p1); /* 1st argument */
  73. setobj(L->top+2, p2); /* 2nd argument */
  74. luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
  75. L->top += 3;
  76. luaD_call(L, L->top - 3, 1);
  77. result = restorestack(L, res); /* previous call may change stack */
  78. setobj(result, --L->top); /* get result */
  79. }
  80. static void callTM (lua_State *L, const TObject *f,
  81. const TObject *p1, const TObject *p2, const TObject *p3) {
  82. setobj(L->top, f); /* push function */
  83. setobj(L->top+1, p1); /* 1st argument */
  84. setobj(L->top+2, p2); /* 2nd argument */
  85. setobj(L->top+3, p3); /* 3th argument */
  86. luaD_checkstack(L, 4); /* cannot check before (could invalidate p1...p3) */
  87. L->top += 4;
  88. luaD_call(L, L->top - 4, 0);
  89. }
  90. /*
  91. ** Function to index a table.
  92. ** Receives the table at `t' and the key at `key'.
  93. ** leaves the result at `res'.
  94. */
  95. void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) {
  96. const TObject *tm;
  97. int loop = 0;
  98. init:
  99. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  100. Table *h = hvalue(t);
  101. Table *et = h->metatable;
  102. if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */
  103. const TObject *v = luaH_get(h, key); /* do a primitive get */
  104. if (ttype(v) != LUA_TNIL || /* result is no nil ... */
  105. (tm = fasttm(L, et, TM_INDEX)) == NULL) { /* ... or no index TM? */
  106. setobj(res, v); /* default get */
  107. return;
  108. }
  109. }
  110. /* else will try the tag method */
  111. } else { /* not a table; try a `gettable' tag method */
  112. if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) {
  113. luaG_typeerror(L, t, "index");
  114. return; /* to avoid warnings */
  115. }
  116. }
  117. if (ttype(tm) == LUA_TFUNCTION)
  118. callTMres(L, tm, t, key, res);
  119. else {
  120. if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in gettable");
  121. t = tm;
  122. goto init; /* return luaV_gettable(L, tm, key, res); */
  123. }
  124. }
  125. /*
  126. ** Receives table at `t', key at `key' and value at `val'.
  127. */
  128. void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
  129. const TObject *tm;
  130. int loop = 0;
  131. init:
  132. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  133. Table *h = hvalue(t);
  134. Table *et = h->metatable;
  135. if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no settable TM? */
  136. TObject *oldval = luaH_set(L, h, key); /* do a primitive set */
  137. if (ttype(oldval) != LUA_TNIL || /* result is no nil ... */
  138. (tm = fasttm(L, et, TM_NEWINDEX)) == NULL) { /* ... or no TM? */
  139. setobj(oldval, val);
  140. return;
  141. }
  142. }
  143. /* else will try the tag method */
  144. } else { /* `t' is not a table; try a `settable' tag method */
  145. if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL) {
  146. luaG_typeerror(L, t, "index");
  147. return; /* to avoid warnings */
  148. }
  149. }
  150. if (ttype(tm) == LUA_TFUNCTION)
  151. callTM(L, tm, t, key, val);
  152. else {
  153. if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in settable");
  154. t = tm;
  155. goto init; /* luaV_settable(L, tm, key, val); */
  156. }
  157. }
  158. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  159. TObject *res, TMS event) {
  160. const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  161. if (ttype(tm) == LUA_TNIL)
  162. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  163. if (ttype(tm) != LUA_TFUNCTION) return 0;
  164. callTMres(L, tm, p1, p2, res);
  165. return 1;
  166. }
  167. static void call_arith (lua_State *L, StkId p1, const TObject *p2,
  168. StkId res, TMS event) {
  169. if (!call_binTM(L, p1, p2, res, event))
  170. luaG_aritherror(L, p1, p2);
  171. }
  172. static int luaV_strcmp (const TString *ls, const TString *rs) {
  173. const char *l = getstr(ls);
  174. size_t ll = ls->tsv.len;
  175. const char *r = getstr(rs);
  176. size_t lr = rs->tsv.len;
  177. for (;;) {
  178. int temp = strcoll(l, r);
  179. if (temp < 0) return CMP_LT;
  180. else if (temp > 0) return CMP_GT;
  181. else { /* strings are equal up to a `\0' */
  182. size_t len = strlen(l); /* index of first `\0' in both strings */
  183. if (len == lr) /* r is finished? */
  184. return (len == ll) ? CMP_EQ : CMP_GT; /* l is eq. or gt. than r */
  185. else if (len == ll) /* l is finished? */
  186. return CMP_LT; /* l is smaller than r (because r is not finished) */
  187. /* both strings longer than `len'; go on comparing (after the `\0') */
  188. len++;
  189. l += len; ll -= len; r += len; lr -= len;
  190. }
  191. }
  192. }
  193. int luaV_cmp (lua_State *L, const TObject *l, const TObject *r, int cond) {
  194. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER) {
  195. lua_Number n1 = nvalue(l);
  196. lua_Number n2 = nvalue(r);
  197. if (n1 < n2) return (cond & CMP_LT);
  198. else if (n1 > n2) return (cond & CMP_GT);
  199. else if (n1 == n2) return (cond & CMP_EQ);
  200. else return (cond & CMP_N);
  201. }
  202. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  203. return luaV_strcmp(tsvalue(l), tsvalue(r)) & cond;
  204. else { /* try TM */
  205. if (cond & CMP_EQ ? cond & CMP_LT : cond & CMP_GT) { /* `<=' or `>' ? */
  206. const TObject *temp = l; l = r; r = temp; /* exchange terms */
  207. }
  208. if (!call_binTM(L, l, r, L->top, TM_LT))
  209. luaG_ordererror(L, l, r);
  210. return (cond & CMP_EQ) ? l_isfalse(L->top) : !l_isfalse(L->top);
  211. }
  212. }
  213. void luaV_concat (lua_State *L, int total, int last) {
  214. do {
  215. StkId top = L->ci->base + last + 1;
  216. int n = 2; /* number of elements handled in this pass (at least 2) */
  217. if (!tostring(L, top-2) || !tostring(L, top-1)) {
  218. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  219. luaG_concaterror(L, top-2, top-1);
  220. } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
  221. /* at least two string values; get as many as possible */
  222. lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
  223. cast(lu_mem, tsvalue(top-2)->tsv.len);
  224. char *buffer;
  225. int i;
  226. while (n < total && tostring(L, top-n-1)) { /* collect total length */
  227. tl += tsvalue(top-n-1)->tsv.len;
  228. n++;
  229. }
  230. if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
  231. buffer = luaO_openspace(L, tl, char);
  232. tl = 0;
  233. for (i=n; i>0; i--) { /* concat all strings */
  234. size_t l = tsvalue(top-i)->tsv.len;
  235. memcpy(buffer+tl, svalue(top-i), l);
  236. tl += l;
  237. }
  238. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  239. }
  240. total -= n-1; /* got `n' strings to create 1 new */
  241. last -= n-1;
  242. } while (total > 1); /* repeat until only 1 result left */
  243. }
  244. static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
  245. const TObject *b = rb;
  246. const TObject *c = rc;
  247. TObject tempb, tempc;
  248. if (tonumber(b, &tempb) && tonumber(c, &tempc)) {
  249. TObject f, o;
  250. setsvalue(&o, luaS_newliteral(L, "pow"));
  251. luaV_gettable(L, gt(L), &o, &f);
  252. if (ttype(&f) != LUA_TFUNCTION)
  253. luaG_runerror(L, "`pow' (for `^' operator) is not a function");
  254. callTMres(L, &f, b, c, ra);
  255. }
  256. else
  257. call_arith(L, rb, rc, ra, TM_POW);
  258. }
  259. /*
  260. ** some macros for common tasks in `luaV_execute'
  261. */
  262. #define runtime_check(L, c) { if (!(c)) return 0; }
  263. #define RA(i) (base+GETARG_A(i))
  264. #define RB(i) (base+GETARG_B(i))
  265. #define RC(i) (base+GETARG_C(i))
  266. #define RKC(i) ((GETARG_C(i) < MAXSTACK) ? \
  267. base+GETARG_C(i) : \
  268. k+GETARG_C(i)-MAXSTACK)
  269. #define KBx(i) (k+GETARG_Bx(i))
  270. #define Arith(op, optm) { \
  271. const TObject *b = RB(i); const TObject *c = RKC(i); \
  272. TObject tempb, tempc; \
  273. if (tonumber(b, &tempb) && tonumber(c, &tempc)) { \
  274. setnvalue(ra, nvalue(b) op nvalue(c)); \
  275. } else \
  276. call_arith(L, RB(i), RKC(i), ra, optm); \
  277. }
  278. #define dojump(pc, i) ((pc) += (i))
  279. StkId luaV_execute (lua_State *L) {
  280. StkId base;
  281. LClosure *cl;
  282. TObject *k;
  283. const Instruction *pc;
  284. callentry: /* entry point when calling new functions */
  285. L->ci->pc = &pc;
  286. L->ci->pb = &base;
  287. pc = L->ci->savedpc;
  288. retentry: /* entry point when returning to old functions */
  289. base = L->ci->base;
  290. cl = &clvalue(base - 1)->l;
  291. k = cl->p->k;
  292. /* main loop of interpreter */
  293. for (;;) {
  294. const Instruction i = *pc++;
  295. StkId ra;
  296. if (L->linehook)
  297. traceexec(L);
  298. ra = RA(i);
  299. lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
  300. lua_assert(L->top == L->ci->top ||
  301. GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  302. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  303. switch (GET_OPCODE(i)) {
  304. case OP_MOVE: {
  305. setobj(ra, RB(i));
  306. break;
  307. }
  308. case OP_LOADK: {
  309. setobj(ra, KBx(i));
  310. break;
  311. }
  312. case OP_LOADBOOL: {
  313. setbvalue(ra, GETARG_B(i));
  314. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  315. break;
  316. }
  317. case OP_LOADNIL: {
  318. TObject *rb = RB(i);
  319. do {
  320. setnilvalue(rb--);
  321. } while (rb >= ra);
  322. break;
  323. }
  324. case OP_GETUPVAL: {
  325. int b = GETARG_B(i);
  326. setobj(ra, cl->upvals[b]->v);
  327. break;
  328. }
  329. case OP_GETGLOBAL: {
  330. lua_assert(ttype(KBx(i)) == LUA_TSTRING);
  331. luaV_gettable(L, gt(L), KBx(i), ra);
  332. break;
  333. }
  334. case OP_GETTABLE: {
  335. luaV_gettable(L, RB(i), RKC(i), ra);
  336. break;
  337. }
  338. case OP_SETGLOBAL: {
  339. lua_assert(ttype(KBx(i)) == LUA_TSTRING);
  340. luaV_settable(L, gt(L), KBx(i), ra);
  341. break;
  342. }
  343. case OP_SETUPVAL: {
  344. int b = GETARG_B(i);
  345. setobj(cl->upvals[b]->v, ra);
  346. break;
  347. }
  348. case OP_SETTABLE: {
  349. luaV_settable(L, RB(i), RKC(i), ra);
  350. break;
  351. }
  352. case OP_NEWTABLE: {
  353. int b = GETARG_B(i);
  354. if (b > 0) b = twoto(b-1);
  355. sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
  356. luaV_checkGC(L, ra+1);
  357. break;
  358. }
  359. case OP_SELF: {
  360. StkId rb = RB(i);
  361. setobj(ra+1, rb);
  362. luaV_gettable(L, rb, RKC(i), ra);
  363. break;
  364. }
  365. case OP_ADD: {
  366. Arith( + , TM_ADD);
  367. break;
  368. }
  369. case OP_SUB: {
  370. Arith( - , TM_SUB);
  371. break;
  372. }
  373. case OP_MUL: {
  374. Arith( * , TM_MUL);
  375. break;
  376. }
  377. case OP_DIV: {
  378. Arith( / , TM_DIV);
  379. break;
  380. }
  381. case OP_POW: {
  382. powOp(L, ra, RB(i), RKC(i));
  383. break;
  384. }
  385. case OP_UNM: {
  386. const TObject *rb = RB(i);
  387. if (tonumber(rb, ra)) {
  388. setnvalue(ra, -nvalue(rb));
  389. }
  390. else {
  391. TObject temp;
  392. setnilvalue(&temp);
  393. call_arith(L, RB(i), &temp, ra, TM_UNM);
  394. }
  395. break;
  396. }
  397. case OP_NOT: {
  398. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  399. setbvalue(ra, res);
  400. break;
  401. }
  402. case OP_CONCAT: {
  403. int b = GETARG_B(i);
  404. int c = GETARG_C(i);
  405. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */
  406. setobj(base+GETARG_A(i), base+b);
  407. luaV_checkGC(L, base+c+1);
  408. break;
  409. }
  410. case OP_JMP: {
  411. dojump(pc, GETARG_sBx(i));
  412. break;
  413. }
  414. case OP_EQ: { /* skip next instruction if test fails */
  415. if (luaO_equalObj(ra, RKC(i)) != GETARG_B(i)) pc++;
  416. else dojump(pc, GETARG_sBx(*pc) + 1);
  417. break;
  418. }
  419. case OP_CMP: {
  420. if (!(luaV_cmp(L, ra, RKC(i), GETARG_B(i)))) pc++;
  421. else dojump(pc, GETARG_sBx(*pc) + 1);
  422. break;
  423. }
  424. case OP_TEST: {
  425. StkId rc = RKC(i);
  426. if (l_isfalse(rc) == GETARG_B(i)) pc++;
  427. else {
  428. setobj(ra, rc);
  429. dojump(pc, GETARG_sBx(*pc) + 1);
  430. }
  431. break;
  432. }
  433. case OP_CALL: {
  434. StkId firstResult;
  435. int b = GETARG_B(i);
  436. int nresults;
  437. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  438. nresults = GETARG_C(i) - 1;
  439. firstResult = luaD_precall(L, ra);
  440. if (firstResult) {
  441. if (firstResult > L->top) { /* yield? */
  442. (L->ci-1)->savedpc = pc;
  443. return NULL;
  444. }
  445. /* it was a C function (`precall' called it); adjust results */
  446. luaD_poscall(L, nresults, firstResult);
  447. if (nresults >= 0) L->top = L->ci->top;
  448. }
  449. else { /* it is a Lua function: `call' it */
  450. (L->ci-1)->savedpc = pc;
  451. goto callentry;
  452. }
  453. break;
  454. }
  455. case OP_TAILCALL: {
  456. int b = GETARG_B(i);
  457. if (L->openupval) luaF_close(L, base);
  458. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  459. luaD_poscall(L, LUA_MULTRET, ra); /* move down function and args. */
  460. ra = luaD_precall(L, base-1);
  461. if (ra == NULL) goto callentry; /* it is a Lua function */
  462. else if (ra > L->top) return NULL; /* yield??? */
  463. else goto ret;
  464. }
  465. case OP_RETURN: {
  466. int b;
  467. if (L->openupval) luaF_close(L, base);
  468. b = GETARG_B(i);
  469. if (b != 0) L->top = ra+b-1;
  470. lua_assert(L->ci->pc == &pc);
  471. }
  472. ret: {
  473. CallInfo *ci;
  474. ci = L->ci - 1;
  475. if (ci->pc != &pc) /* previous function was running `here'? */
  476. return ra; /* no: return */
  477. else { /* yes: continue its execution */
  478. int nresults;
  479. lua_assert(ttype(ci->base-1) == LUA_TFUNCTION);
  480. pc = ci->savedpc;
  481. lua_assert(GET_OPCODE(*(pc-1)) == OP_CALL);
  482. nresults = GETARG_C(*(pc-1)) - 1;
  483. luaD_poscall(L, nresults, ra);
  484. if (nresults >= 0) L->top = L->ci->top;
  485. goto retentry;
  486. }
  487. break;
  488. }
  489. case OP_FORLOOP: {
  490. lua_Number step, index, limit;
  491. const TObject *plimit = ra+1;
  492. const TObject *pstep = ra+2;
  493. if (ttype(ra) != LUA_TNUMBER)
  494. luaG_runerror(L, "`for' initial value must be a number");
  495. if (!tonumber(plimit, ra+1))
  496. luaG_runerror(L, "`for' limit must be a number");
  497. if (!tonumber(pstep, ra+2))
  498. luaG_runerror(L, "`for' step must be a number");
  499. step = nvalue(pstep);
  500. index = nvalue(ra) + step; /* increment index */
  501. limit = nvalue(plimit);
  502. if (step > 0 ? index <= limit : index >= limit) {
  503. dojump(pc, GETARG_sBx(i)); /* jump back */
  504. chgnvalue(ra, index); /* update index */
  505. }
  506. break;
  507. }
  508. case OP_TFORLOOP: {
  509. setobj(ra+4, ra+2);
  510. setobj(ra+3, ra+1);
  511. setobj(ra+2, ra);
  512. L->top = ra+5;
  513. luaD_call(L, ra+2, GETARG_C(i) + 1);
  514. L->top = L->ci->top;
  515. if (ttype(ra+2) == LUA_TNIL) pc++; /* skip jump (break loop) */
  516. else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
  517. break;
  518. }
  519. case OP_TFORPREP: {
  520. if (ttype(ra) == LUA_TTABLE) {
  521. setobj(ra+1, ra);
  522. setsvalue(ra, luaS_new(L, "next"));
  523. luaV_gettable(L, gt(L), ra, ra);
  524. }
  525. dojump(pc, GETARG_sBx(i));
  526. break;
  527. }
  528. case OP_SETLIST:
  529. case OP_SETLISTO: {
  530. int bc;
  531. int n;
  532. Table *h;
  533. runtime_check(L, ttype(ra) == LUA_TTABLE);
  534. h = hvalue(ra);
  535. bc = GETARG_Bx(i);
  536. if (GET_OPCODE(i) == OP_SETLIST)
  537. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  538. else {
  539. n = L->top - ra - 1;
  540. L->top = L->ci->top;
  541. }
  542. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  543. for (; n > 0; n--)
  544. setobj(luaH_setnum(L, h, bc+n), ra+n);
  545. break;
  546. }
  547. case OP_CLOSE: {
  548. luaF_close(L, ra);
  549. break;
  550. }
  551. case OP_CLOSURE: {
  552. Proto *p;
  553. Closure *ncl;
  554. int nup, j;
  555. p = cl->p->p[GETARG_Bx(i)];
  556. nup = p->nupvalues;
  557. ncl = luaF_newLclosure(L, nup);
  558. ncl->l.p = p;
  559. for (j=0; j<nup; j++, pc++) {
  560. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  561. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  562. else {
  563. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  564. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  565. }
  566. }
  567. setclvalue(ra, ncl);
  568. luaV_checkGC(L, L->top);
  569. break;
  570. }
  571. }
  572. }
  573. }