lapi.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. ** $Id: lapi.c,v 2.22 2004/12/06 17:53: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 *index2adr (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 = index2adr(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 = index2adr(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 = index2adr(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, index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(L, index1);
  192. StkId o2 = index2adr(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 = index2adr(L, index1);
  201. o2 = index2adr(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 = index2adr(L, index1);
  212. o2 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(L, idx);
  239. return !l_isfalse(o);
  240. }
  241. LUA_API const char *lua_tostring (lua_State *L, int idx) {
  242. StkId o = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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 = index2adr(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. case LUA_GCSETPACE: {
  703. res = g->gcpace;
  704. g->gcpace = data;
  705. break;
  706. }
  707. case LUA_GCSETINCMODE: {
  708. res = g->incgc;
  709. g->incgc = data;
  710. break;
  711. }
  712. default: res = -1; /* invalid option */
  713. }
  714. lua_unlock(L);
  715. return res;
  716. }
  717. /*
  718. ** miscellaneous functions
  719. */
  720. LUA_API const char *lua_version (void) {
  721. return LUA_VERSION;
  722. }
  723. LUA_API int lua_error (lua_State *L) {
  724. lua_lock(L);
  725. api_checknelems(L, 1);
  726. luaG_errormsg(L);
  727. lua_unlock(L);
  728. return 0; /* to avoid warnings */
  729. }
  730. LUA_API int lua_next (lua_State *L, int idx) {
  731. StkId t;
  732. int more;
  733. lua_lock(L);
  734. t = index2adr(L, idx);
  735. api_check(L, ttistable(t));
  736. more = luaH_next(L, hvalue(t), L->top - 1);
  737. if (more) {
  738. api_incr_top(L);
  739. }
  740. else /* no more elements */
  741. L->top -= 1; /* remove key */
  742. lua_unlock(L);
  743. return more;
  744. }
  745. LUA_API void lua_concat (lua_State *L, int n) {
  746. lua_lock(L);
  747. luaC_checkGC(L);
  748. api_checknelems(L, n);
  749. if (n >= 2) {
  750. luaV_concat(L, n, L->top - L->base - 1);
  751. L->top -= (n-1);
  752. }
  753. else if (n == 0) { /* push empty string */
  754. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  755. api_incr_top(L);
  756. }
  757. /* else n == 1; nothing to do */
  758. lua_unlock(L);
  759. }
  760. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  761. *ud = G(L)->ud;
  762. return G(L)->realloc;
  763. }
  764. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  765. Udata *u;
  766. lua_lock(L);
  767. luaC_checkGC(L);
  768. u = luaS_newudata(L, size);
  769. setuvalue(L, L->top, u);
  770. api_incr_top(L);
  771. lua_unlock(L);
  772. return u + 1;
  773. }
  774. static const char *aux_upvalue (lua_State *L, StkId fi, int n, TValue **val) {
  775. Closure *f;
  776. if (!ttisfunction(fi)) return NULL;
  777. f = clvalue(fi);
  778. if (f->c.isC) {
  779. if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
  780. *val = &f->c.upvalue[n-1];
  781. return "";
  782. }
  783. else {
  784. Proto *p = f->l.p;
  785. if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
  786. *val = f->l.upvals[n-1]->v;
  787. return getstr(p->upvalues[n-1]);
  788. }
  789. }
  790. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  791. const char *name;
  792. TValue *val;
  793. lua_lock(L);
  794. name = aux_upvalue(L, index2adr(L, funcindex), n, &val);
  795. if (name) {
  796. setobj2s(L, L->top, val);
  797. api_incr_top(L);
  798. }
  799. lua_unlock(L);
  800. return name;
  801. }
  802. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  803. const char *name;
  804. TValue *val;
  805. StkId fi;
  806. lua_lock(L);
  807. fi = index2adr(L, funcindex);
  808. api_checknelems(L, 1);
  809. name = aux_upvalue(L, fi, n, &val);
  810. if (name) {
  811. L->top--;
  812. setobj(L, val, L->top);
  813. luaC_barrier(L, clvalue(fi), L->top);
  814. }
  815. lua_unlock(L);
  816. return name;
  817. }