lapi.c 19 KB

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