lapi.c 17 KB

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