lapi.c 17 KB

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