lapi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. ** $Id: lapi.c,v 2.19 2004/09/15 20:39:42 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <assert.h>
  7. #include <stdarg.h>
  8. #include <string.h>
  9. #define lapi_c
  10. #define LUA_CORE
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "lmem.h"
  18. #include "lobject.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. #include "lundump.h"
  24. #include "lvm.h"
  25. const char lua_ident[] =
  26. "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  27. "$Authors: " LUA_AUTHORS " $\n"
  28. "$URL: www.lua.org $\n";
  29. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
  30. #define api_checkvalidindex(L, i) api_check(L, (i) != &luaO_nilobject)
  31. #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
  32. static TValue *luaA_index (lua_State *L, int idx) {
  33. if (idx > 0) {
  34. TValue *o = L->base + (idx - 1);
  35. api_check(L, idx <= L->ci->top - L->base);
  36. if (o >= L->top) return cast(TValue *, &luaO_nilobject);
  37. else return o;
  38. }
  39. else if (idx > LUA_REGISTRYINDEX) {
  40. api_check(L, idx != 0 && -idx <= L->top - L->base);
  41. return L->top + idx;
  42. }
  43. else switch (idx) { /* pseudo-indices */
  44. case LUA_REGISTRYINDEX: return registry(L);
  45. case LUA_GLOBALSINDEX: return gt(L);
  46. default: {
  47. Closure *func = curr_func(L);
  48. idx = LUA_GLOBALSINDEX - idx;
  49. return (idx <= func->c.nupvalues)
  50. ? &func->c.upvalue[idx-1]
  51. : cast(TValue *, &luaO_nilobject);
  52. }
  53. }
  54. }
  55. void luaA_pushobject (lua_State *L, const TValue *o) {
  56. setobj2s(L, L->top, o);
  57. incr_top(L);
  58. }
  59. LUA_API int lua_checkstack (lua_State *L, int size) {
  60. int res;
  61. lua_lock(L);
  62. if ((L->top - L->base + size) > MAXCSTACK)
  63. res = 0; /* stack overflow */
  64. else {
  65. luaD_checkstack(L, size);
  66. if (L->ci->top < L->top + size)
  67. L->ci->top = L->top + size;
  68. res = 1;
  69. }
  70. lua_unlock(L);
  71. return res;
  72. }
  73. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  74. int i;
  75. if (from == to) return;
  76. lua_lock(to);
  77. api_checknelems(from, n);
  78. api_check(L, G(from) == G(to));
  79. from->top -= n;
  80. for (i = 0; i < n; i++) {
  81. setobj2s(to, 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, 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(L, 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(L, q, q-1);
  141. setobjs2s(L, 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(L, o, L->top - 1);
  151. if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
  152. luaC_barrier(L, curr_func(L), L->top - 1);
  153. L->top--;
  154. lua_unlock(L);
  155. }
  156. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  157. lua_lock(L);
  158. setobj2s(L, L->top, luaA_index(L, idx));
  159. api_incr_top(L);
  160. lua_unlock(L);
  161. }
  162. /*
  163. ** access functions (stack -> C)
  164. */
  165. LUA_API int lua_type (lua_State *L, int idx) {
  166. StkId o = luaA_index(L, idx);
  167. return (o == &luaO_nilobject) ? LUA_TNONE : ttype(o);
  168. }
  169. LUA_API const char *lua_typename (lua_State *L, int t) {
  170. UNUSED(L);
  171. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  172. }
  173. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  174. StkId o = luaA_index(L, idx);
  175. return iscfunction(o);
  176. }
  177. LUA_API int lua_isnumber (lua_State *L, int idx) {
  178. TValue n;
  179. const TValue *o = luaA_index(L, idx);
  180. return tonumber(o, &n);
  181. }
  182. LUA_API int lua_isstring (lua_State *L, int idx) {
  183. int t = lua_type(L, idx);
  184. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  185. }
  186. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  187. const TValue *o = luaA_index(L, idx);
  188. return (ttisuserdata(o) || ttislightuserdata(o));
  189. }
  190. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  191. StkId o1 = luaA_index(L, index1);
  192. StkId o2 = luaA_index(L, index2);
  193. return (o1 == &luaO_nilobject || o2 == &luaO_nilobject) ? 0
  194. : luaO_rawequalObj(o1, o2);
  195. }
  196. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  197. StkId o1, o2;
  198. int i;
  199. lua_lock(L); /* may call tag method */
  200. o1 = luaA_index(L, index1);
  201. o2 = luaA_index(L, index2);
  202. i = (o1 == &luaO_nilobject || o2 == &luaO_nilobject) ? 0
  203. : equalobj(L, o1, o2);
  204. lua_unlock(L);
  205. return i;
  206. }
  207. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  208. StkId o1, o2;
  209. int i;
  210. lua_lock(L); /* may call tag method */
  211. o1 = luaA_index(L, index1);
  212. o2 = luaA_index(L, index2);
  213. i = (o1 == &luaO_nilobject || o2 == &luaO_nilobject) ? 0
  214. : luaV_lessthan(L, o1, o2);
  215. lua_unlock(L);
  216. return i;
  217. }
  218. LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
  219. TValue n;
  220. const TValue *o = luaA_index(L, idx);
  221. if (tonumber(o, &n))
  222. return nvalue(o);
  223. else
  224. return 0;
  225. }
  226. LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
  227. TValue n;
  228. const TValue *o = luaA_index(L, idx);
  229. if (tonumber(o, &n)) {
  230. lua_Integer res;
  231. lua_number2integer(res, nvalue(o));
  232. return res;
  233. }
  234. else
  235. return 0;
  236. }
  237. LUA_API int lua_toboolean (lua_State *L, int idx) {
  238. const TValue *o = luaA_index(L, idx);
  239. return !l_isfalse(o);
  240. }
  241. LUA_API const char *lua_tostring (lua_State *L, int idx) {
  242. StkId o = luaA_index(L, idx);
  243. if (ttisstring(o))
  244. return svalue(o);
  245. else {
  246. const char *s;
  247. lua_lock(L); /* `luaV_tostring' may create a new string */
  248. s = (luaV_tostring(L, o) ? svalue(o) : NULL);
  249. luaC_checkGC(L);
  250. lua_unlock(L);
  251. return s;
  252. }
  253. }
  254. LUA_API size_t lua_objsize (lua_State *L, int idx) {
  255. StkId o = luaA_index(L, idx);
  256. if (ttisstring(o))
  257. return tsvalue(o)->len;
  258. else if (ttisuserdata(o))
  259. return uvalue(o)->len;
  260. else {
  261. size_t l;
  262. lua_lock(L); /* `luaV_tostring' may create a new string */
  263. l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
  264. lua_unlock(L);
  265. return l;
  266. }
  267. }
  268. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  269. StkId o = luaA_index(L, idx);
  270. return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
  271. }
  272. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  273. StkId o = luaA_index(L, idx);
  274. switch (ttype(o)) {
  275. case LUA_TUSERDATA: return (rawuvalue(o) + 1);
  276. case LUA_TLIGHTUSERDATA: return pvalue(o);
  277. default: return NULL;
  278. }
  279. }
  280. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  281. StkId o = luaA_index(L, idx);
  282. return (!ttisthread(o)) ? NULL : thvalue(o);
  283. }
  284. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  285. StkId o = luaA_index(L, idx);
  286. switch (ttype(o)) {
  287. case LUA_TTABLE: return hvalue(o);
  288. case LUA_TFUNCTION: return clvalue(o);
  289. case LUA_TTHREAD: return thvalue(o);
  290. case LUA_TUSERDATA:
  291. case LUA_TLIGHTUSERDATA:
  292. return lua_touserdata(L, idx);
  293. default: return NULL;
  294. }
  295. }
  296. /*
  297. ** push functions (C -> stack)
  298. */
  299. LUA_API void lua_pushnil (lua_State *L) {
  300. lua_lock(L);
  301. setnilvalue(L->top);
  302. api_incr_top(L);
  303. lua_unlock(L);
  304. }
  305. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  306. lua_lock(L);
  307. setnvalue(L->top, n);
  308. api_incr_top(L);
  309. lua_unlock(L);
  310. }
  311. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  312. lua_lock(L);
  313. setnvalue(L->top, cast(lua_Number, n));
  314. api_incr_top(L);
  315. lua_unlock(L);
  316. }
  317. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  318. lua_lock(L);
  319. luaC_checkGC(L);
  320. setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
  321. api_incr_top(L);
  322. lua_unlock(L);
  323. }
  324. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  325. if (s == NULL)
  326. lua_pushnil(L);
  327. else
  328. lua_pushlstring(L, s, strlen(s));
  329. }
  330. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  331. va_list argp) {
  332. const char *ret;
  333. lua_lock(L);
  334. luaC_checkGC(L);
  335. ret = luaO_pushvfstring(L, fmt, argp);
  336. lua_unlock(L);
  337. return ret;
  338. }
  339. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  340. const char *ret;
  341. va_list argp;
  342. lua_lock(L);
  343. luaC_checkGC(L);
  344. va_start(argp, fmt);
  345. ret = luaO_pushvfstring(L, fmt, argp);
  346. va_end(argp);
  347. lua_unlock(L);
  348. return ret;
  349. }
  350. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  351. Closure *cl;
  352. lua_lock(L);
  353. luaC_checkGC(L);
  354. api_checknelems(L, n);
  355. cl = luaF_newCclosure(L, n);
  356. cl->c.f = fn;
  357. L->top -= n;
  358. while (n--)
  359. setobj2n(L, &cl->c.upvalue[n], L->top+n);
  360. setclvalue(L, L->top, cl);
  361. lua_assert(iswhite(obj2gco(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. LUA_API int lua_pushthread (lua_State *L) {
  378. lua_lock(L);
  379. setthvalue(L, L->top, L);
  380. api_incr_top(L);
  381. lua_unlock(L);
  382. return (G(L)->mainthread == L);
  383. }
  384. /*
  385. ** get functions (Lua -> stack)
  386. */
  387. LUA_API void lua_gettable (lua_State *L, int idx) {
  388. StkId t;
  389. lua_lock(L);
  390. t = luaA_index(L, idx);
  391. api_checkvalidindex(L, t);
  392. luaV_gettable(L, t, L->top - 1, L->top - 1, NULL);
  393. lua_unlock(L);
  394. }
  395. LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  396. StkId t;
  397. TValue key;
  398. lua_lock(L);
  399. t = luaA_index(L, idx);
  400. api_checkvalidindex(L, t);
  401. setsvalue(L, &key, luaS_new(L, k));
  402. luaV_gettable(L, t, &key, L->top, NULL);
  403. api_incr_top(L);
  404. lua_unlock(L);
  405. }
  406. LUA_API void lua_rawget (lua_State *L, int idx) {
  407. StkId t;
  408. lua_lock(L);
  409. t = luaA_index(L, idx);
  410. api_check(L, ttistable(t));
  411. setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
  412. lua_unlock(L);
  413. }
  414. LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
  415. StkId o;
  416. lua_lock(L);
  417. o = luaA_index(L, idx);
  418. api_check(L, ttistable(o));
  419. setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
  420. api_incr_top(L);
  421. lua_unlock(L);
  422. }
  423. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  424. lua_lock(L);
  425. luaC_checkGC(L);
  426. sethvalue(L, L->top, luaH_new(L, narray, luaO_log2(nrec) + 1));
  427. api_incr_top(L);
  428. lua_unlock(L);
  429. }
  430. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  431. const TValue *obj;
  432. Table *mt = NULL;
  433. int res;
  434. lua_lock(L);
  435. obj = luaA_index(L, objindex);
  436. switch (ttype(obj)) {
  437. case LUA_TTABLE:
  438. mt = hvalue(obj)->metatable;
  439. break;
  440. case LUA_TUSERDATA:
  441. mt = uvalue(obj)->metatable;
  442. break;
  443. }
  444. if (mt == NULL)
  445. res = 0;
  446. else {
  447. sethvalue(L, L->top, mt);
  448. api_incr_top(L);
  449. res = 1;
  450. }
  451. lua_unlock(L);
  452. return res;
  453. }
  454. LUA_API void lua_getfenv (lua_State *L, int idx) {
  455. StkId o;
  456. lua_lock(L);
  457. o = luaA_index(L, idx);
  458. api_checkvalidindex(L, o);
  459. setobj2s(L, L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L));
  460. api_incr_top(L);
  461. lua_unlock(L);
  462. }
  463. /*
  464. ** set functions (stack -> Lua)
  465. */
  466. LUA_API void lua_settable (lua_State *L, int idx) {
  467. StkId t;
  468. lua_lock(L);
  469. api_checknelems(L, 2);
  470. t = luaA_index(L, idx);
  471. api_checkvalidindex(L, t);
  472. luaV_settable(L, t, L->top - 2, L->top - 1, NULL);
  473. L->top -= 2; /* pop index and value */
  474. lua_unlock(L);
  475. }
  476. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  477. StkId t;
  478. TValue key;
  479. lua_lock(L);
  480. api_checknelems(L, 1);
  481. t = luaA_index(L, idx);
  482. api_checkvalidindex(L, t);
  483. setsvalue(L, &key, luaS_new(L, k));
  484. luaV_settable(L, t, &key, L->top - 1, NULL);
  485. L->top--; /* pop value */
  486. lua_unlock(L);
  487. }
  488. LUA_API void lua_rawset (lua_State *L, int idx) {
  489. StkId t;
  490. lua_lock(L);
  491. api_checknelems(L, 2);
  492. t = luaA_index(L, idx);
  493. api_check(L, ttistable(t));
  494. setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
  495. luaC_barriert(L, hvalue(t), L->top-1);
  496. L->top -= 2;
  497. lua_unlock(L);
  498. }
  499. LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
  500. StkId o;
  501. lua_lock(L);
  502. api_checknelems(L, 1);
  503. o = luaA_index(L, idx);
  504. api_check(L, ttistable(o));
  505. setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
  506. luaC_barriert(L, hvalue(o), L->top-1);
  507. L->top--;
  508. lua_unlock(L);
  509. }
  510. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  511. TValue *obj;
  512. Table *mt;
  513. int res = 1;
  514. lua_lock(L);
  515. api_checknelems(L, 1);
  516. obj = luaA_index(L, objindex);
  517. api_checkvalidindex(L, obj);
  518. if (ttisnil(L->top - 1))
  519. mt = NULL;
  520. else {
  521. api_check(L, ttistable(L->top - 1));
  522. mt = hvalue(L->top - 1);
  523. }
  524. switch (ttype(obj)) {
  525. case LUA_TTABLE: {
  526. hvalue(obj)->metatable = mt;
  527. if (mt)
  528. luaC_objbarriert(L, hvalue(obj), mt);
  529. break;
  530. }
  531. case LUA_TUSERDATA: {
  532. uvalue(obj)->metatable = mt;
  533. if (mt)
  534. luaC_objbarrier(L, rawuvalue(obj), mt);
  535. break;
  536. }
  537. default: {
  538. res = 0; /* cannot set */
  539. break;
  540. }
  541. }
  542. L->top--;
  543. lua_unlock(L);
  544. return res;
  545. }
  546. LUA_API int lua_setfenv (lua_State *L, int idx) {
  547. StkId o;
  548. int res = 0;
  549. lua_lock(L);
  550. api_checknelems(L, 1);
  551. o = luaA_index(L, idx);
  552. api_checkvalidindex(L, o);
  553. api_check(L, ttistable(L->top - 1));
  554. if (isLfunction(o)) {
  555. res = 1;
  556. clvalue(o)->l.g = *(L->top - 1);
  557. luaC_objbarrier(L, clvalue(o), hvalue(L->top - 1));
  558. }
  559. L->top--;
  560. lua_unlock(L);
  561. return res;
  562. }
  563. /*
  564. ** `load' and `call' functions (run Lua code)
  565. */
  566. #define adjustresults(L,nres) \
  567. { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
  568. #define checkresults(L,na,nr) \
  569. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
  570. LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
  571. StkId func;
  572. lua_lock(L);
  573. api_checknelems(L, nargs+1);
  574. checkresults(L, nargs, nresults);
  575. func = L->top - (nargs+1);
  576. luaD_call(L, func, nresults);
  577. adjustresults(L, nresults);
  578. lua_unlock(L);
  579. }
  580. /*
  581. ** Execute a protected call.
  582. */
  583. struct CallS { /* data to `f_call' */
  584. StkId func;
  585. int nresults;
  586. };
  587. static void f_call (lua_State *L, void *ud) {
  588. struct CallS *c = cast(struct CallS *, ud);
  589. luaD_call(L, c->func, c->nresults);
  590. }
  591. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
  592. struct CallS c;
  593. int status;
  594. ptrdiff_t func;
  595. lua_lock(L);
  596. api_checknelems(L, nargs+1);
  597. checkresults(L, nargs, nresults);
  598. if (errfunc == 0)
  599. func = 0;
  600. else {
  601. StkId o = luaA_index(L, errfunc);
  602. api_checkvalidindex(L, o);
  603. func = savestack(L, o);
  604. }
  605. c.func = L->top - (nargs+1); /* function to be called */
  606. c.nresults = nresults;
  607. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  608. adjustresults(L, nresults);
  609. lua_unlock(L);
  610. return status;
  611. }
  612. /*
  613. ** Execute a protected C call.
  614. */
  615. struct CCallS { /* data to `f_Ccall' */
  616. lua_CFunction func;
  617. void *ud;
  618. };
  619. static void f_Ccall (lua_State *L, void *ud) {
  620. struct CCallS *c = cast(struct CCallS *, ud);
  621. Closure *cl;
  622. cl = luaF_newCclosure(L, 0);
  623. cl->c.f = c->func;
  624. setclvalue(L, L->top, cl); /* push function */
  625. incr_top(L);
  626. setpvalue(L->top, c->ud); /* push only argument */
  627. incr_top(L);
  628. luaD_call(L, L->top - 2, 0);
  629. }
  630. LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
  631. struct CCallS c;
  632. int status;
  633. lua_lock(L);
  634. c.func = func;
  635. c.ud = ud;
  636. status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
  637. lua_unlock(L);
  638. return status;
  639. }
  640. LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
  641. const char *chunkname) {
  642. ZIO z;
  643. int status;
  644. lua_lock(L);
  645. if (!chunkname) chunkname = "?";
  646. luaZ_init(L, &z, reader, data);
  647. status = luaD_protectedparser(L, &z, chunkname);
  648. lua_unlock(L);
  649. return status;
  650. }
  651. LUA_API int lua_dump (lua_State *L, lua_Chunkwriter writer, void *data) {
  652. int status;
  653. TValue *o;
  654. lua_lock(L);
  655. api_checknelems(L, 1);
  656. o = L->top - 1;
  657. if (isLfunction(o))
  658. status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
  659. else
  660. status = 0;
  661. lua_unlock(L);
  662. return status;
  663. }
  664. LUA_API int lua_threadstatus (lua_State *L) {
  665. return L->status;
  666. }
  667. /*
  668. ** Garbage-collection function
  669. */
  670. LUA_API int lua_gc (lua_State *L, int what, int data) {
  671. int res = 0;
  672. global_State *g;
  673. lua_lock(L);
  674. g = G(L);
  675. switch (what) {
  676. case LUA_GCSTOP: {
  677. g->GCthreshold = MAX_LUMEM;
  678. break;
  679. }
  680. case LUA_GCRESTART: {
  681. g->GCthreshold = g->totalbytes;
  682. break;
  683. }
  684. case LUA_GCCOLLECT: {
  685. luaC_fullgc(L);
  686. break;
  687. }
  688. case LUA_GCCOUNT: {
  689. /* GC values are expressed in Kbytes: #bytes/2^10 */
  690. res = cast(int, g->totalbytes >> 10);
  691. break;
  692. }
  693. case LUA_GCSTEP: {
  694. lu_mem a = (cast(lu_mem, data) << 10);
  695. if (a <= g->totalbytes)
  696. g->GCthreshold = g->totalbytes - a;
  697. else
  698. g->GCthreshold = 0;
  699. luaC_step(L);
  700. break;
  701. }
  702. default: res = -1; /* invalid option */
  703. }
  704. lua_unlock(L);
  705. return res;
  706. }
  707. /*
  708. ** miscellaneous functions
  709. */
  710. LUA_API const char *lua_version (void) {
  711. return LUA_VERSION;
  712. }
  713. LUA_API int lua_error (lua_State *L) {
  714. lua_lock(L);
  715. api_checknelems(L, 1);
  716. luaG_errormsg(L);
  717. lua_unlock(L);
  718. return 0; /* to avoid warnings */
  719. }
  720. LUA_API int lua_next (lua_State *L, int idx) {
  721. StkId t;
  722. int more;
  723. lua_lock(L);
  724. t = luaA_index(L, idx);
  725. api_check(L, ttistable(t));
  726. more = luaH_next(L, hvalue(t), L->top - 1);
  727. if (more) {
  728. api_incr_top(L);
  729. }
  730. else /* no more elements */
  731. L->top -= 1; /* remove key */
  732. lua_unlock(L);
  733. return more;
  734. }
  735. LUA_API void lua_concat (lua_State *L, int n) {
  736. lua_lock(L);
  737. luaC_checkGC(L);
  738. api_checknelems(L, n);
  739. if (n >= 2) {
  740. luaV_concat(L, n, L->top - L->base - 1);
  741. L->top -= (n-1);
  742. }
  743. else if (n == 0) { /* push empty string */
  744. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  745. api_incr_top(L);
  746. }
  747. /* else n == 1; nothing to do */
  748. lua_unlock(L);
  749. }
  750. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  751. *ud = G(L)->ud;
  752. return G(L)->realloc;
  753. }
  754. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  755. Udata *u;
  756. lua_lock(L);
  757. luaC_checkGC(L);
  758. u = luaS_newudata(L, size);
  759. setuvalue(L, L->top, u);
  760. api_incr_top(L);
  761. lua_unlock(L);
  762. return u + 1;
  763. }
  764. static const char *aux_upvalue (lua_State *L, StkId fi, int n, TValue **val) {
  765. Closure *f;
  766. if (!ttisfunction(fi)) return NULL;
  767. f = clvalue(fi);
  768. if (f->c.isC) {
  769. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  770. *val = &f->c.upvalue[n-1];
  771. return "";
  772. }
  773. else {
  774. Proto *p = f->l.p;
  775. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  776. *val = f->l.upvals[n-1]->v;
  777. return getstr(p->upvalues[n-1]);
  778. }
  779. }
  780. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  781. const char *name;
  782. TValue *val;
  783. lua_lock(L);
  784. name = aux_upvalue(L, luaA_index(L, funcindex), n, &val);
  785. if (name) {
  786. setobj2s(L, L->top, val);
  787. api_incr_top(L);
  788. }
  789. lua_unlock(L);
  790. return name;
  791. }
  792. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  793. const char *name;
  794. TValue *val;
  795. StkId fi;
  796. lua_lock(L);
  797. fi = luaA_index(L, funcindex);
  798. api_checknelems(L, 1);
  799. name = aux_upvalue(L, fi, n, &val);
  800. if (name) {
  801. L->top--;
  802. setobj(L, val, L->top);
  803. luaC_barrier(L, clvalue(fi), L->top);
  804. }
  805. lua_unlock(L);
  806. return name;
  807. }