lapi.c 18 KB

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