lapi.c 18 KB

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