lapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. ** $Id: lapi.c,v 1.222 2002/11/21 15:16:04 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "lapi.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #include "lundump.h"
  21. #include "lvm.h"
  22. const char lua_ident[] =
  23. "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  24. "$Authors: " LUA_AUTHORS " $\n"
  25. "$URL: www.lua.org $\n";
  26. #ifndef api_check
  27. #define api_check(L, o) /*{ assert(o); }*/
  28. #endif
  29. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
  30. #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
  31. static TObject *negindex (lua_State *L, int index) {
  32. if (index > LUA_REGISTRYINDEX) {
  33. api_check(L, index != 0 && -index <= L->top - L->base);
  34. return L->top+index;
  35. }
  36. else switch (index) { /* pseudo-indices */
  37. case LUA_REGISTRYINDEX: return registry(L);
  38. case LUA_GLOBALSINDEX: return gt(L);
  39. default: {
  40. TObject *func = (L->base - 1);
  41. index = LUA_GLOBALSINDEX - index;
  42. api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues);
  43. return &clvalue(func)->c.upvalue[index-1];
  44. }
  45. }
  46. }
  47. static TObject *luaA_index (lua_State *L, int index) {
  48. if (index > 0) {
  49. api_check(L, index <= L->top - L->base);
  50. return L->base + index - 1;
  51. }
  52. else
  53. return negindex(L, index);
  54. }
  55. static TObject *luaA_indexAcceptable (lua_State *L, int index) {
  56. if (index > 0) {
  57. TObject *o = L->base+(index-1);
  58. api_check(L, index <= L->stack_last - L->base);
  59. if (o >= L->top) return NULL;
  60. else return o;
  61. }
  62. else
  63. return negindex(L, index);
  64. }
  65. void luaA_pushobject (lua_State *L, const TObject *o) {
  66. setobj2s(L->top, o);
  67. incr_top(L);
  68. }
  69. LUA_API int lua_checkstack (lua_State *L, int size) {
  70. int res;
  71. lua_lock(L);
  72. if ((L->top - L->base + size) > LUA_MAXCSTACK)
  73. res = 0; /* stack overflow */
  74. else {
  75. luaD_checkstack(L, size);
  76. if (L->ci->top < L->top + size)
  77. L->ci->top = L->top + size;
  78. res = 1;
  79. }
  80. lua_unlock(L);
  81. return res;
  82. }
  83. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  84. int i;
  85. lua_lock(to);
  86. api_checknelems(from, n);
  87. from->top -= n;
  88. for (i = 0; i < n; i++) {
  89. setobj2s(to->top, from->top + i);
  90. api_incr_top(to);
  91. }
  92. lua_unlock(to);
  93. }
  94. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  95. lua_CFunction old;
  96. lua_lock(L);
  97. old = G(L)->panic;
  98. G(L)->panic = panicf;
  99. lua_unlock(L);
  100. return old;
  101. }
  102. LUA_API lua_State *lua_newthread (lua_State *L) {
  103. lua_State *L1;
  104. lua_lock(L);
  105. luaC_checkGC(L);
  106. L1 = luaE_newthread(L);
  107. setthvalue(L->top, L1);
  108. api_incr_top(L);
  109. lua_unlock(L);
  110. lua_userstateopen(L1);
  111. return L1;
  112. }
  113. /*
  114. ** basic stack manipulation
  115. */
  116. LUA_API int lua_gettop (lua_State *L) {
  117. return (L->top - L->base);
  118. }
  119. LUA_API void lua_settop (lua_State *L, int index) {
  120. lua_lock(L);
  121. if (index >= 0) {
  122. api_check(L, index <= L->stack_last - L->base);
  123. while (L->top < L->base + index)
  124. setnilvalue(L->top++);
  125. L->top = L->base + index;
  126. }
  127. else {
  128. api_check(L, -(index+1) <= (L->top - L->base));
  129. L->top += index+1; /* `subtract' index (index is negative) */
  130. }
  131. lua_unlock(L);
  132. }
  133. LUA_API void lua_remove (lua_State *L, int index) {
  134. StkId p;
  135. lua_lock(L);
  136. p = luaA_index(L, index);
  137. while (++p < L->top) setobjs2s(p-1, p);
  138. L->top--;
  139. lua_unlock(L);
  140. }
  141. LUA_API void lua_insert (lua_State *L, int index) {
  142. StkId p;
  143. StkId q;
  144. lua_lock(L);
  145. p = luaA_index(L, index);
  146. for (q = L->top; q>p; q--) setobjs2s(q, q-1);
  147. setobjs2s(p, L->top);
  148. lua_unlock(L);
  149. }
  150. LUA_API void lua_replace (lua_State *L, int index) {
  151. lua_lock(L);
  152. api_checknelems(L, 1);
  153. setobj(luaA_index(L, index), L->top - 1); /* write barrier */
  154. L->top--;
  155. lua_unlock(L);
  156. }
  157. LUA_API void lua_pushvalue (lua_State *L, int index) {
  158. lua_lock(L);
  159. setobj2s(L->top, luaA_index(L, index));
  160. api_incr_top(L);
  161. lua_unlock(L);
  162. }
  163. /*
  164. ** access functions (stack -> C)
  165. */
  166. LUA_API int lua_type (lua_State *L, int index) {
  167. StkId o = luaA_indexAcceptable(L, index);
  168. return (o == NULL) ? LUA_TNONE : ttype(o);
  169. }
  170. LUA_API const char *lua_typename (lua_State *L, int t) {
  171. UNUSED(L);
  172. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  173. }
  174. LUA_API int lua_iscfunction (lua_State *L, int index) {
  175. StkId o = luaA_indexAcceptable(L, index);
  176. return (o == NULL) ? 0 : iscfunction(o);
  177. }
  178. LUA_API int lua_isnumber (lua_State *L, int index) {
  179. TObject n;
  180. const TObject *o = luaA_indexAcceptable(L, index);
  181. return (o != NULL && tonumber(o, &n));
  182. }
  183. LUA_API int lua_isstring (lua_State *L, int index) {
  184. int t = lua_type(L, index);
  185. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  186. }
  187. LUA_API int lua_isuserdata (lua_State *L, int index) {
  188. const TObject *o = luaA_indexAcceptable(L, index);
  189. return (o != NULL && (ttisuserdata(o) || ttislightuserdata(o)));
  190. }
  191. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  192. StkId o1 = luaA_indexAcceptable(L, index1);
  193. StkId o2 = luaA_indexAcceptable(L, index2);
  194. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  195. : luaO_rawequalObj(o1, o2);
  196. }
  197. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  198. StkId o1, o2;
  199. int i;
  200. lua_lock(L); /* may call tag method */
  201. o1 = luaA_indexAcceptable(L, index1);
  202. o2 = luaA_indexAcceptable(L, index2);
  203. i = (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  204. : equalobj(L, o1, o2);
  205. lua_unlock(L);
  206. return i;
  207. }
  208. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  209. StkId o1, o2;
  210. int i;
  211. lua_lock(L); /* may call tag method */
  212. o1 = luaA_indexAcceptable(L, index1);
  213. o2 = luaA_indexAcceptable(L, index2);
  214. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  215. : luaV_lessthan(L, o1, o2);
  216. lua_unlock(L);
  217. return i;
  218. }
  219. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  220. TObject n;
  221. const TObject *o = luaA_indexAcceptable(L, index);
  222. if (o != NULL && tonumber(o, &n))
  223. return nvalue(o);
  224. else
  225. return 0;
  226. }
  227. LUA_API int lua_toboolean (lua_State *L, int index) {
  228. const TObject *o = luaA_indexAcceptable(L, index);
  229. return (o != NULL) && !l_isfalse(o);
  230. }
  231. LUA_API const char *lua_tostring (lua_State *L, int index) {
  232. StkId o = luaA_indexAcceptable(L, index);
  233. if (o == NULL)
  234. return NULL;
  235. else if (ttisstring(o))
  236. return svalue(o);
  237. else {
  238. const char *s;
  239. lua_lock(L); /* `luaV_tostring' may create a new string */
  240. s = (luaV_tostring(L, o) ? svalue(o) : NULL);
  241. luaC_checkGC(L);
  242. lua_unlock(L);
  243. return s;
  244. }
  245. }
  246. LUA_API size_t lua_strlen (lua_State *L, int index) {
  247. StkId o = luaA_indexAcceptable(L, index);
  248. if (o == NULL)
  249. return 0;
  250. else if (ttisstring(o))
  251. return tsvalue(o)->tsv.len;
  252. else {
  253. size_t l;
  254. lua_lock(L); /* `luaV_tostring' may create a new string */
  255. l = (luaV_tostring(L, o) ? tsvalue(o)->tsv.len : 0);
  256. lua_unlock(L);
  257. return l;
  258. }
  259. }
  260. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  261. StkId o = luaA_indexAcceptable(L, index);
  262. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
  263. }
  264. LUA_API void *lua_touserdata (lua_State *L, int index) {
  265. StkId o = luaA_indexAcceptable(L, index);
  266. if (o == NULL) return NULL;
  267. switch (ttype(o)) {
  268. case LUA_TUSERDATA: return (uvalue(o) + 1);
  269. case LUA_TLIGHTUSERDATA: return pvalue(o);
  270. default: return NULL;
  271. }
  272. }
  273. LUA_API lua_State *lua_tothread (lua_State *L, int index) {
  274. StkId o = luaA_indexAcceptable(L, index);
  275. return (o == NULL || !ttisthread(o)) ? NULL : thvalue(o);
  276. }
  277. LUA_API const void *lua_topointer (lua_State *L, int index) {
  278. StkId o = luaA_indexAcceptable(L, index);
  279. if (o == NULL) return NULL;
  280. else {
  281. switch (ttype(o)) {
  282. case LUA_TTABLE: return hvalue(o);
  283. case LUA_TFUNCTION: return clvalue(o);
  284. case LUA_TUSERDATA:
  285. case LUA_TLIGHTUSERDATA:
  286. return lua_touserdata(L, index);
  287. default: return NULL;
  288. }
  289. }
  290. }
  291. /*
  292. ** push functions (C -> stack)
  293. */
  294. LUA_API void lua_pushnil (lua_State *L) {
  295. lua_lock(L);
  296. setnilvalue(L->top);
  297. api_incr_top(L);
  298. lua_unlock(L);
  299. }
  300. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  301. lua_lock(L);
  302. setnvalue(L->top, n);
  303. api_incr_top(L);
  304. lua_unlock(L);
  305. }
  306. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  307. lua_lock(L);
  308. luaC_checkGC(L);
  309. setsvalue2s(L->top, luaS_newlstr(L, s, len));
  310. api_incr_top(L);
  311. lua_unlock(L);
  312. }
  313. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  314. if (s == NULL)
  315. lua_pushnil(L);
  316. else
  317. lua_pushlstring(L, s, strlen(s));
  318. }
  319. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  320. va_list argp) {
  321. const char *ret;
  322. lua_lock(L);
  323. luaC_checkGC(L);
  324. ret = luaO_pushvfstring(L, fmt, argp);
  325. lua_unlock(L);
  326. return ret;
  327. }
  328. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  329. const char *ret;
  330. va_list argp;
  331. lua_lock(L);
  332. luaC_checkGC(L);
  333. va_start(argp, fmt);
  334. ret = luaO_pushvfstring(L, fmt, argp);
  335. va_end(argp);
  336. lua_unlock(L);
  337. return ret;
  338. }
  339. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  340. Closure *cl;
  341. lua_lock(L);
  342. luaC_checkGC(L);
  343. api_checknelems(L, n);
  344. cl = luaF_newCclosure(L, n);
  345. cl->c.f = fn;
  346. L->top -= n;
  347. while (n--)
  348. setobj2n(&cl->c.upvalue[n], L->top+n);
  349. setclvalue(L->top, cl);
  350. api_incr_top(L);
  351. lua_unlock(L);
  352. }
  353. LUA_API void lua_pushboolean (lua_State *L, int b) {
  354. lua_lock(L);
  355. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  356. api_incr_top(L);
  357. lua_unlock(L);
  358. }
  359. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  360. lua_lock(L);
  361. setpvalue(L->top, p);
  362. api_incr_top(L);
  363. lua_unlock(L);
  364. }
  365. /*
  366. ** get functions (Lua -> stack)
  367. */
  368. LUA_API void lua_gettable (lua_State *L, int index) {
  369. StkId t;
  370. lua_lock(L);
  371. t = luaA_index(L, index);
  372. setobj2s(L->top - 1, luaV_gettable(L, t, L->top - 1, 0));
  373. lua_unlock(L);
  374. }
  375. LUA_API void lua_rawget (lua_State *L, int index) {
  376. StkId t;
  377. lua_lock(L);
  378. t = luaA_index(L, index);
  379. api_check(L, ttistable(t));
  380. setobj2s(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  381. lua_unlock(L);
  382. }
  383. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  384. StkId o;
  385. lua_lock(L);
  386. o = luaA_index(L, index);
  387. api_check(L, ttistable(o));
  388. setobj2s(L->top, luaH_getnum(hvalue(o), n));
  389. api_incr_top(L);
  390. lua_unlock(L);
  391. }
  392. LUA_API void lua_newtable (lua_State *L) {
  393. lua_lock(L);
  394. luaC_checkGC(L);
  395. sethvalue(L->top, luaH_new(L, 0, 0));
  396. api_incr_top(L);
  397. lua_unlock(L);
  398. }
  399. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  400. StkId obj;
  401. Table *mt;
  402. int res;
  403. lua_lock(L);
  404. obj = luaA_indexAcceptable(L, objindex);
  405. switch (ttype(obj)) {
  406. case LUA_TTABLE:
  407. mt = hvalue(obj)->metatable;
  408. break;
  409. case LUA_TUSERDATA:
  410. mt = uvalue(obj)->uv.metatable;
  411. break;
  412. default:
  413. mt = hvalue(defaultmeta(L));
  414. }
  415. if (mt == hvalue(defaultmeta(L)))
  416. res = 0;
  417. else {
  418. sethvalue(L->top, mt);
  419. api_incr_top(L);
  420. res = 1;
  421. }
  422. lua_unlock(L);
  423. return res;
  424. }
  425. LUA_API void lua_getglobals (lua_State *L, int index) {
  426. StkId o;
  427. lua_lock(L);
  428. o = luaA_index(L, index);
  429. setobj2s(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
  430. api_incr_top(L);
  431. lua_unlock(L);
  432. }
  433. /*
  434. ** set functions (stack -> Lua)
  435. */
  436. LUA_API void lua_settable (lua_State *L, int index) {
  437. StkId t;
  438. lua_lock(L);
  439. api_checknelems(L, 2);
  440. t = luaA_index(L, index);
  441. luaV_settable(L, t, L->top - 2, L->top - 1);
  442. L->top -= 2; /* pop index and value */
  443. lua_unlock(L);
  444. }
  445. LUA_API void lua_rawset (lua_State *L, int index) {
  446. StkId t;
  447. lua_lock(L);
  448. api_checknelems(L, 2);
  449. t = luaA_index(L, index);
  450. api_check(L, ttistable(t));
  451. setobj2t(luaH_set(L, hvalue(t), L->top-2), L->top-1); /* write barrier */
  452. L->top -= 2;
  453. lua_unlock(L);
  454. }
  455. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  456. StkId o;
  457. lua_lock(L);
  458. api_checknelems(L, 1);
  459. o = luaA_index(L, index);
  460. api_check(L, ttistable(o));
  461. setobj2t(luaH_setnum(L, hvalue(o), n), L->top-1); /* write barrier */
  462. L->top--;
  463. lua_unlock(L);
  464. }
  465. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  466. TObject *obj, *mt;
  467. int res = 1;
  468. lua_lock(L);
  469. api_checknelems(L, 1);
  470. obj = luaA_index(L, objindex);
  471. mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L);
  472. api_check(L, ttistable(mt));
  473. switch (ttype(obj)) {
  474. case LUA_TTABLE: {
  475. hvalue(obj)->metatable = hvalue(mt); /* write barrier */
  476. break;
  477. }
  478. case LUA_TUSERDATA: {
  479. uvalue(obj)->uv.metatable = hvalue(mt); /* write barrier */
  480. break;
  481. }
  482. default: {
  483. res = 0; /* cannot set */
  484. break;
  485. }
  486. }
  487. L->top--;
  488. lua_unlock(L);
  489. return res;
  490. }
  491. LUA_API int lua_setglobals (lua_State *L, int index) {
  492. StkId o;
  493. int res = 0;
  494. lua_lock(L);
  495. api_checknelems(L, 1);
  496. o = luaA_index(L, index);
  497. L->top--;
  498. api_check(L, ttistable(L->top));
  499. if (isLfunction(o)) {
  500. res = 1;
  501. clvalue(o)->l.g = *(L->top);
  502. }
  503. lua_unlock(L);
  504. return res;
  505. }
  506. /*
  507. ** `load' and `call' functions (run Lua code)
  508. */
  509. LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
  510. StkId func;
  511. lua_lock(L);
  512. api_checknelems(L, nargs+1);
  513. func = L->top - (nargs+1);
  514. luaD_call(L, func, nresults);
  515. lua_unlock(L);
  516. }
  517. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
  518. int status;
  519. ptrdiff_t func;
  520. lua_lock(L);
  521. func = (errfunc == 0) ? 0 : savestack(L, luaA_index(L, errfunc));
  522. status = luaD_pcall(L, nargs, nresults, func);
  523. lua_unlock(L);
  524. return status;
  525. }
  526. LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
  527. const char *chunkname) {
  528. ZIO z;
  529. int status;
  530. int c;
  531. lua_lock(L);
  532. if (!chunkname) chunkname = "?";
  533. luaZ_init(&z, reader, data, chunkname);
  534. c = luaZ_lookahead(&z);
  535. status = luaD_protectedparser(L, &z, (c == LUA_SIGNATURE[0]));
  536. lua_unlock(L);
  537. return status;
  538. }
  539. LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
  540. int status;
  541. TObject *o;
  542. lua_lock(L);
  543. api_checknelems(L, 1);
  544. o = L->top - 1;
  545. if (isLfunction(o) && clvalue(o)->l.nupvalues == 0) {
  546. luaU_dump(L, clvalue(o)->l.p, writer, data);
  547. status = 1;
  548. }
  549. else
  550. status = 0;
  551. lua_unlock(L);
  552. return status;
  553. }
  554. /*
  555. ** Garbage-collection functions
  556. */
  557. /* GC values are expressed in Kbytes: #bytes/2^10 */
  558. #define GCscalel(x) ((x)>>10)
  559. #define GCscale(x) (cast(int, GCscalel(x)))
  560. #define GCunscale(x) (cast(lu_mem, (x)<<10))
  561. LUA_API int lua_getgcthreshold (lua_State *L) {
  562. int threshold;
  563. lua_lock(L);
  564. threshold = GCscale(G(L)->GCthreshold);
  565. lua_unlock(L);
  566. return threshold;
  567. }
  568. LUA_API int lua_getgccount (lua_State *L) {
  569. int count;
  570. lua_lock(L);
  571. count = GCscale(G(L)->nblocks);
  572. lua_unlock(L);
  573. return count;
  574. }
  575. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  576. lua_lock(L);
  577. if (cast(lu_mem, newthreshold) > GCscalel(ULONG_MAX))
  578. G(L)->GCthreshold = ULONG_MAX;
  579. else
  580. G(L)->GCthreshold = GCunscale(newthreshold);
  581. luaC_checkGC(L);
  582. lua_unlock(L);
  583. }
  584. /*
  585. ** miscellaneous functions
  586. */
  587. LUA_API int lua_error (lua_State *L) {
  588. lua_lock(L);
  589. api_checknelems(L, 1);
  590. luaG_errormsg(L);
  591. lua_unlock(L);
  592. return 0; /* to avoid warnings */
  593. }
  594. LUA_API int lua_next (lua_State *L, int index) {
  595. StkId t;
  596. int more;
  597. lua_lock(L);
  598. t = luaA_index(L, index);
  599. api_check(L, ttistable(t));
  600. more = luaH_next(L, hvalue(t), L->top - 1);
  601. if (more) {
  602. api_incr_top(L);
  603. }
  604. else /* no more elements */
  605. L->top -= 1; /* remove key */
  606. lua_unlock(L);
  607. return more;
  608. }
  609. LUA_API void lua_concat (lua_State *L, int n) {
  610. lua_lock(L);
  611. luaC_checkGC(L);
  612. api_checknelems(L, n);
  613. if (n >= 2) {
  614. luaV_concat(L, n, L->top - L->base - 1);
  615. L->top -= (n-1);
  616. }
  617. else if (n == 0) { /* push empty string */
  618. setsvalue2s(L->top, luaS_newlstr(L, NULL, 0));
  619. api_incr_top(L);
  620. }
  621. /* else n == 1; nothing to do */
  622. lua_unlock(L);
  623. }
  624. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  625. Udata *u;
  626. lua_lock(L);
  627. luaC_checkGC(L);
  628. u = luaS_newudata(L, size);
  629. setuvalue(L->top, u);
  630. api_incr_top(L);
  631. lua_unlock(L);
  632. return u + 1;
  633. }
  634. LUA_API int lua_pushupvalues (lua_State *L) {
  635. Closure *func;
  636. int n, i;
  637. lua_lock(L);
  638. api_check(L, iscfunction(L->base - 1));
  639. func = clvalue(L->base - 1);
  640. n = func->c.nupvalues;
  641. luaD_checkstack(L, n + LUA_MINSTACK);
  642. for (i=0; i<n; i++) {
  643. setobj2s(L->top, &func->c.upvalue[i]);
  644. L->top++;
  645. }
  646. lua_unlock(L);
  647. return n;
  648. }