lapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. ** $Id: lapi.c,v 1.160 2001/11/16 16:29:51 roberto Exp $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lua.h"
  8. #include "lapi.h"
  9. #include "ldo.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. #include "ltable.h"
  17. #include "ltm.h"
  18. #include "lvm.h"
  19. const char lua_ident[] =
  20. "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  21. "$Authors: " LUA_AUTHORS " $\n"
  22. "$URL: www.lua.org $\n";
  23. #ifndef api_check
  24. #define api_check(L, o) /* nothing */
  25. #endif
  26. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->ci->base))
  27. #define api_incr_top(L) incr_top
  28. static TObject *negindex (lua_State *L, int index) {
  29. if (index > LUA_REGISTRYINDEX) {
  30. api_check(L, index != 0 && -index <= L->top - L->ci->base);
  31. return L->top+index;
  32. }
  33. else switch (index) { /* pseudo-indices */
  34. case LUA_REGISTRYINDEX: return &G(L)->registry;
  35. case LUA_GLOBALSINDEX: return &L->gt;
  36. default: {
  37. TObject *func = (L->ci->base - 1);
  38. index = LUA_GLOBALSINDEX - index;
  39. api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues);
  40. return &clvalue(func)->c.upvalue[index-1];
  41. }
  42. }
  43. }
  44. TObject *luaA_index (lua_State *L, int index) {
  45. if (index > 0) {
  46. api_check(L, index <= L->top - L->ci->base);
  47. return L->ci->base+index-1;
  48. }
  49. else
  50. return negindex(L, index);
  51. }
  52. static TObject *luaA_indexAcceptable (lua_State *L, int index) {
  53. if (index > 0) {
  54. TObject *o = L->ci->base+(index-1);
  55. api_check(L, index <= L->stack_last - L->ci->base);
  56. if (o >= L->top) return NULL;
  57. else return o;
  58. }
  59. else
  60. return negindex(L, index);
  61. }
  62. void luaA_pushobject (lua_State *L, const TObject *o) {
  63. setobj(L->top, o);
  64. incr_top;
  65. }
  66. LUA_API int lua_stackspace (lua_State *L) {
  67. return (L->stack_last - L->top);
  68. }
  69. /*
  70. ** basic stack manipulation
  71. */
  72. LUA_API int lua_gettop (lua_State *L) {
  73. return (L->top - L->ci->base);
  74. }
  75. LUA_API void lua_settop (lua_State *L, int index) {
  76. lua_lock(L);
  77. if (index >= 0) {
  78. api_check(L, index <= L->stack_last - L->ci->base);
  79. luaD_adjusttop(L, L->ci->base+index);
  80. }
  81. else {
  82. api_check(L, -(index+1) <= (L->top - L->ci->base));
  83. L->top += index+1; /* `subtract' index (index is negative) */
  84. }
  85. lua_unlock(L);
  86. }
  87. LUA_API void lua_remove (lua_State *L, int index) {
  88. StkId p;
  89. lua_lock(L);
  90. p = luaA_index(L, index);
  91. while (++p < L->top) setobj(p-1, p);
  92. L->top--;
  93. lua_unlock(L);
  94. }
  95. LUA_API void lua_insert (lua_State *L, int index) {
  96. StkId p;
  97. StkId q;
  98. lua_lock(L);
  99. p = luaA_index(L, index);
  100. for (q = L->top; q>p; q--) setobj(q, q-1);
  101. setobj(p, L->top);
  102. lua_unlock(L);
  103. }
  104. LUA_API void lua_pushvalue (lua_State *L, int index) {
  105. lua_lock(L);
  106. setobj(L->top, luaA_index(L, index));
  107. api_incr_top(L);
  108. lua_unlock(L);
  109. }
  110. /*
  111. ** access functions (stack -> C)
  112. */
  113. LUA_API int lua_rawtag (lua_State *L, int index) {
  114. StkId o = luaA_indexAcceptable(L, index);
  115. return (o == NULL) ? LUA_TNONE : ttype(o);
  116. }
  117. LUA_API const char *lua_type (lua_State *L, int index) {
  118. StkId o;
  119. const char *type;
  120. lua_lock(L);
  121. o = luaA_indexAcceptable(L, index);
  122. type = (o == NULL) ? "no value" : luaT_typename(G(L), o);
  123. lua_unlock(L);
  124. return type;
  125. }
  126. LUA_API int lua_iscfunction (lua_State *L, int index) {
  127. StkId o = luaA_indexAcceptable(L, index);
  128. return (o == NULL) ? 0 : iscfunction(o);
  129. }
  130. LUA_API int lua_isnumber (lua_State *L, int index) {
  131. TObject n;
  132. TObject *o = luaA_indexAcceptable(L, index);
  133. return (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n)));
  134. }
  135. LUA_API int lua_isstring (lua_State *L, int index) {
  136. int t = lua_rawtag(L, index);
  137. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  138. }
  139. LUA_API int lua_tag (lua_State *L, int index) {
  140. StkId o;
  141. int i;
  142. lua_lock(L); /* other thread could be changing the tag */
  143. o = luaA_indexAcceptable(L, index);
  144. i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
  145. lua_unlock(L);
  146. return i;
  147. }
  148. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  149. StkId o1 = luaA_indexAcceptable(L, index1);
  150. StkId o2 = luaA_indexAcceptable(L, index2);
  151. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  152. : luaO_equalObj(o1, o2);
  153. }
  154. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  155. StkId o1, o2;
  156. int i;
  157. lua_lock(L); /* may call tag method */
  158. o1 = luaA_indexAcceptable(L, index1);
  159. o2 = luaA_indexAcceptable(L, index2);
  160. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  161. : luaV_lessthan(L, o1, o2);
  162. lua_unlock(L);
  163. return i;
  164. }
  165. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  166. TObject n;
  167. const TObject *o = luaA_indexAcceptable(L, index);
  168. if (o != NULL &&
  169. (ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL))
  170. return nvalue(o);
  171. else
  172. return 0;
  173. }
  174. LUA_API const char *lua_tostring (lua_State *L, int index) {
  175. StkId o = luaA_indexAcceptable(L, index);
  176. if (o == NULL)
  177. return NULL;
  178. else if (ttype(o) == LUA_TSTRING)
  179. return svalue(o);
  180. else {
  181. const char *s;
  182. lua_lock(L); /* `luaV_tostring' may create a new string */
  183. s = (luaV_tostring(L, o) == 0) ? svalue(o) : NULL;
  184. lua_unlock(L);
  185. return s;
  186. }
  187. }
  188. LUA_API size_t lua_strlen (lua_State *L, int index) {
  189. StkId o = luaA_indexAcceptable(L, index);
  190. if (o == NULL)
  191. return 0;
  192. else if (ttype(o) == LUA_TSTRING)
  193. return tsvalue(o)->tsv.len;
  194. else {
  195. size_t l;
  196. lua_lock(L); /* `luaV_tostring' may create a new string */
  197. l = (luaV_tostring(L, o) == 0) ? tsvalue(o)->tsv.len : 0;
  198. lua_unlock(L);
  199. return l;
  200. }
  201. }
  202. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  203. StkId o = luaA_indexAcceptable(L, index);
  204. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
  205. }
  206. LUA_API void *lua_touserdata (lua_State *L, int index) {
  207. StkId o = luaA_indexAcceptable(L, index);
  208. return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : uvalue(o)->uv.value;
  209. }
  210. LUA_API const void *lua_topointer (lua_State *L, int index) {
  211. StkId o = luaA_indexAcceptable(L, index);
  212. if (o == NULL) return NULL;
  213. else {
  214. switch (ttype(o)) {
  215. case LUA_TTABLE: return hvalue(o);
  216. case LUA_TFUNCTION: return clvalue(o);
  217. default: return NULL;
  218. }
  219. }
  220. }
  221. /*
  222. ** push functions (C -> stack)
  223. */
  224. LUA_API void lua_pushnil (lua_State *L) {
  225. lua_lock(L);
  226. setnilvalue(L->top);
  227. api_incr_top(L);
  228. lua_unlock(L);
  229. }
  230. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  231. lua_lock(L);
  232. setnvalue(L->top, n);
  233. api_incr_top(L);
  234. lua_unlock(L);
  235. }
  236. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  237. lua_lock(L);
  238. setsvalue(L->top, luaS_newlstr(L, s, len));
  239. api_incr_top(L);
  240. lua_unlock(L);
  241. }
  242. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  243. if (s == NULL)
  244. lua_pushnil(L);
  245. else
  246. lua_pushlstring(L, s, strlen(s));
  247. }
  248. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  249. Closure *cl;
  250. lua_lock(L);
  251. api_checknelems(L, n);
  252. cl = luaF_newCclosure(L, n);
  253. cl->c.f = fn;
  254. L->top -= n;
  255. while (n--)
  256. setobj(&cl->c.upvalue[n], L->top+n);
  257. setclvalue(L->top, cl);
  258. incr_top;
  259. lua_unlock(L);
  260. }
  261. /*
  262. ** get functions (Lua -> stack)
  263. */
  264. LUA_API void lua_getglobal (lua_State *L, const char *name) {
  265. lua_lock(L);
  266. luaV_getglobal(L, luaS_new(L, name), L->top);
  267. api_incr_top(L);
  268. lua_unlock(L);
  269. }
  270. LUA_API void lua_gettable (lua_State *L, int index) {
  271. StkId t;
  272. lua_lock(L);
  273. t = luaA_index(L, index);
  274. luaV_gettable(L, t, L->top-1, L->top-1);
  275. lua_unlock(L);
  276. }
  277. LUA_API void lua_rawget (lua_State *L, int index) {
  278. StkId t;
  279. lua_lock(L);
  280. t = luaA_index(L, index);
  281. api_check(L, ttype(t) == LUA_TTABLE);
  282. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  283. lua_unlock(L);
  284. }
  285. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  286. StkId o;
  287. lua_lock(L);
  288. o = luaA_index(L, index);
  289. api_check(L, ttype(o) == LUA_TTABLE);
  290. setobj(L->top, luaH_getnum(hvalue(o), n));
  291. api_incr_top(L);
  292. lua_unlock(L);
  293. }
  294. LUA_API void lua_newtable (lua_State *L) {
  295. lua_lock(L);
  296. sethvalue(L->top, luaH_new(L, 0, 0));
  297. api_incr_top(L);
  298. lua_unlock(L);
  299. }
  300. /*
  301. ** set functions (stack -> Lua)
  302. */
  303. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  304. lua_lock(L);
  305. api_checknelems(L, 1);
  306. luaV_setglobal(L, luaS_new(L, name), L->top - 1);
  307. L->top--; /* remove element from the top */
  308. lua_unlock(L);
  309. }
  310. LUA_API void lua_settable (lua_State *L, int index) {
  311. StkId t;
  312. lua_lock(L);
  313. api_checknelems(L, 2);
  314. t = luaA_index(L, index);
  315. luaV_settable(L, t, L->top - 2, L->top - 1);
  316. L->top -= 2; /* pop index and value */
  317. lua_unlock(L);
  318. }
  319. LUA_API void lua_rawset (lua_State *L, int index) {
  320. StkId t;
  321. lua_lock(L);
  322. api_checknelems(L, 2);
  323. t = luaA_index(L, index);
  324. api_check(L, ttype(t) == LUA_TTABLE);
  325. luaH_set(L, hvalue(t), L->top-2, L->top-1);
  326. L->top -= 2;
  327. lua_unlock(L);
  328. }
  329. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  330. StkId o;
  331. lua_lock(L);
  332. api_checknelems(L, 1);
  333. o = luaA_index(L, index);
  334. api_check(L, ttype(o) == LUA_TTABLE);
  335. luaH_setnum(L, hvalue(o), n, L->top-1);
  336. L->top--;
  337. lua_unlock(L);
  338. }
  339. LUA_API void lua_setglobals (lua_State *L) {
  340. StkId newtable;
  341. lua_lock(L);
  342. api_checknelems(L, 1);
  343. newtable = --L->top;
  344. api_check(L, ttype(newtable) == LUA_TTABLE);
  345. setobj(&L->gt, newtable);
  346. lua_unlock(L);
  347. }
  348. /*
  349. ** `do' functions (run Lua code)
  350. */
  351. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  352. StkId func;
  353. lua_lock(L);
  354. api_checknelems(L, nargs+1);
  355. func = L->top - (nargs+1);
  356. luaD_call(L, func);
  357. if (nresults != LUA_MULTRET)
  358. luaD_adjusttop(L, func + nresults);
  359. lua_unlock(L);
  360. }
  361. LUA_API int lua_dofile (lua_State *L, const char *filename) {
  362. int status;
  363. status = lua_loadfile(L, filename);
  364. if (status == 0) /* parse OK? */
  365. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  366. return status;
  367. }
  368. LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  369. const char *name) {
  370. int status;
  371. status = lua_loadbuffer(L, buff, size, name);
  372. if (status == 0) /* parse OK? */
  373. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  374. return status;
  375. }
  376. LUA_API int lua_dostring (lua_State *L, const char *str) {
  377. return lua_dobuffer(L, str, strlen(str), str);
  378. }
  379. /*
  380. ** Garbage-collection functions
  381. */
  382. /* GC values are expressed in Kbytes: #bytes/2^10 */
  383. #define GCscale(x) (cast(int, (x)>>10))
  384. #define GCunscale(x) (cast(lu_mem, (x)<<10))
  385. LUA_API int lua_getgcthreshold (lua_State *L) {
  386. int threshold;
  387. lua_lock(L);
  388. threshold = GCscale(G(L)->GCthreshold);
  389. lua_unlock(L);
  390. return threshold;
  391. }
  392. LUA_API int lua_getgccount (lua_State *L) {
  393. int count;
  394. lua_lock(L);
  395. count = GCscale(G(L)->nblocks);
  396. lua_unlock(L);
  397. return count;
  398. }
  399. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  400. lua_lock(L);
  401. if (newthreshold > GCscale(ULONG_MAX))
  402. G(L)->GCthreshold = ULONG_MAX;
  403. else
  404. G(L)->GCthreshold = GCunscale(newthreshold);
  405. luaC_checkGC(L);
  406. lua_unlock(L);
  407. }
  408. /*
  409. ** miscellaneous functions
  410. */
  411. LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
  412. int tag;
  413. lua_lock(L);
  414. if (basictype != LUA_TNONE &&
  415. basictype != LUA_TTABLE &&
  416. basictype != LUA_TUSERDATA)
  417. luaO_verror(L, "invalid basic type (%d) for new type", basictype);
  418. tag = luaT_newtag(L, name, basictype);
  419. if (tag == LUA_TNONE)
  420. luaO_verror(L, "type name '%.30s' already exists", name);
  421. lua_unlock(L);
  422. return tag;
  423. }
  424. LUA_API int lua_name2tag (lua_State *L, const char *name) {
  425. int tag;
  426. const TObject *v;
  427. lua_lock(L);
  428. v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
  429. if (ttype(v) == LUA_TNIL)
  430. tag = LUA_TNONE;
  431. else {
  432. lua_assert(ttype(v) == LUA_TNUMBER);
  433. tag = cast(int, nvalue(v));
  434. }
  435. lua_unlock(L);
  436. return tag;
  437. }
  438. LUA_API const char *lua_tag2name (lua_State *L, int tag) {
  439. const char *s;
  440. lua_lock(L);
  441. s = (tag == LUA_TNONE) ? "no value" : typenamebytag(G(L), tag);
  442. lua_unlock(L);
  443. return s;
  444. }
  445. LUA_API void lua_settag (lua_State *L, int tag) {
  446. int basictype;
  447. lua_lock(L);
  448. api_checknelems(L, 1);
  449. if (tag < 0 || tag >= G(L)->ntag)
  450. luaO_verror(L, "%d is not a valid tag", tag);
  451. basictype = G(L)->TMtable[tag].basictype;
  452. if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
  453. luaO_verror(L, "tag %d can only be used for type '%.20s'", tag,
  454. typenamebytag(G(L), basictype));
  455. switch (ttype(L->top-1)) {
  456. case LUA_TTABLE:
  457. hvalue(L->top-1)->htag = tag;
  458. break;
  459. case LUA_TUSERDATA:
  460. uvalue(L->top-1)->uv.tag = tag;
  461. break;
  462. default:
  463. luaO_verror(L, "cannot change the tag of a %.20s",
  464. luaT_typename(G(L), L->top-1));
  465. }
  466. lua_unlock(L);
  467. }
  468. LUA_API void lua_error (lua_State *L, const char *s) {
  469. lua_lock(L);
  470. luaD_error(L, s);
  471. lua_unlock(L);
  472. }
  473. LUA_API int lua_next (lua_State *L, int index) {
  474. StkId t;
  475. lua_lock(L);
  476. t = luaA_index(L, index);
  477. api_check(L, ttype(t) == LUA_TTABLE);
  478. index = luaH_index(L, hvalue(t), luaA_index(L, -1));
  479. index = (luaH_nexti(hvalue(t), index, L->top - 1) != -1);
  480. if (index) {
  481. api_incr_top(L);
  482. }
  483. else /* no more elements */
  484. L->top -= 1; /* remove key */
  485. lua_unlock(L);
  486. return index;
  487. }
  488. LUA_API int lua_getn (lua_State *L, int index) {
  489. StkId t;
  490. const TObject *value;
  491. int n;
  492. lua_lock(L);
  493. t = luaA_index(L, index);
  494. api_check(L, ttype(t) == LUA_TTABLE);
  495. value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
  496. if (ttype(value) == LUA_TNUMBER)
  497. n = cast(int, nvalue(value));
  498. else {
  499. Node *nd;
  500. Table *a = hvalue(t);
  501. lua_Number max = 0;
  502. int i;
  503. i = sizearray(a);
  504. while (i--) {
  505. if (ttype(&a->array[i]) != LUA_TNIL)
  506. break;
  507. }
  508. max = i+1;
  509. i = sizenode(a);
  510. nd = a->node;
  511. while (i--) {
  512. if (ttype(key(nd)) == LUA_TNUMBER &&
  513. ttype(val(nd)) != LUA_TNIL &&
  514. nvalue(key(nd)) > max)
  515. max = nvalue(key(nd));
  516. nd++;
  517. }
  518. n = cast(int, max);
  519. }
  520. lua_unlock(L);
  521. return n;
  522. }
  523. LUA_API void lua_concat (lua_State *L, int n) {
  524. lua_lock(L);
  525. api_checknelems(L, n);
  526. if (n >= 2) {
  527. luaV_strconc(L, n, L->top);
  528. L->top -= (n-1);
  529. luaC_checkGC(L);
  530. }
  531. else if (n == 0) { /* push null string */
  532. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  533. api_incr_top(L);
  534. }
  535. /* else n == 1; nothing to do */
  536. lua_unlock(L);
  537. }
  538. static Udata *pushnewudata (lua_State *L, size_t size) {
  539. Udata *u = luaS_newudata(L, size);
  540. setuvalue(L->top, u);
  541. api_incr_top(L);
  542. return uvalue(L->top-1);
  543. }
  544. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  545. Udata *u;
  546. void *p;
  547. lua_lock(L);
  548. u = pushnewudata(L, size);
  549. p = u->uv.value;
  550. lua_unlock(L);
  551. return p;
  552. }
  553. LUA_API void lua_newuserdatabox (lua_State *L, void *p) {
  554. Udata *u;
  555. lua_lock(L);
  556. u = pushnewudata(L, 0);
  557. u->uv.value = p;
  558. lua_unlock(L);
  559. }
  560. LUA_API int lua_getweakmode (lua_State *L, int index) {
  561. StkId t;
  562. int mode;
  563. lua_lock(L);
  564. t = luaA_index(L, index);
  565. api_check(L, ttype(t) == LUA_TTABLE);
  566. mode = hvalue(t)->weakmode;
  567. lua_unlock(L);
  568. return mode;
  569. }
  570. LUA_API void lua_setweakmode (lua_State *L, int mode) {
  571. lua_lock(L);
  572. api_check(L, ttype(L->top-1) == LUA_TTABLE);
  573. hvalue(L->top-1)->weakmode = cast(lu_byte, mode);
  574. lua_unlock(L);
  575. }
  576. LUA_API void lua_pushupvalues (lua_State *L) {
  577. TObject *func;
  578. int n, i;
  579. lua_lock(L);
  580. func = (L->ci->base - 1);
  581. api_check(L, iscfunction(func));
  582. n = clvalue(func)->c.nupvalues;
  583. if (LUA_MINSTACK+n > lua_stackspace(L))
  584. luaD_error(L, "stack overflow");
  585. for (i=0; i<n; i++) {
  586. setobj(L->top, &clvalue(func)->c.upvalue[i]);
  587. L->top++;
  588. }
  589. lua_unlock(L);
  590. }