lapi.c 18 KB

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