lapi.c 18 KB

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