lvm.c 18 KB

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