lvm.c 18 KB

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