lapi.c 16 KB

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